Components
Components are a way to encapsulate a piece of UI that can be reused throughout your application. In this example, we'll create a Card
component and use it multiple times on a page, each time using slots to fill in content.
- The
@t.component()
decorator registers the class as a component for use elsewhere. - All components should subclass the
puepy.Component
class. - The
props
attribute is a list of properties that can be passed to the component. - Classes can be defined programmatically in Python. Class names are automatically generated for each instance, so they're scoped like Python instances.
default_classes
is a list of CSS classes that will be applied to the component by default.- The
insert_slot
method is used to insert content into a named slot. In this case, we are inserting content into thecard-header
slot. - Unnamed, or default slots, can be filled by calling
insert_slot
without a name. trigger_event
is used to trigger a custom event. Notice the detail dictionary. This pattern matches the JavaScriptCustomEvent
API.
- The
card
component is used with thetype
prop set to"success"
. - The
my-custom-event
event is bound to theself.handle_custom_event
method. - The content for the
card-header
slot, as defined in theCard
component, is populated with "Success!". - The default slot is populated with "Your operation worked". Default slots are not named.
- The
handle_custom_event
method is called when themy-custom-event
event is triggered.
Slots
Slots are a way to pass content into a component. A component can define one or more slots, and the calling code can fill in the slots with content. In the example above, the Card
component defines two slots: card-header
and the default slot. The calling code fills in the slots by calling card.slot("card-header")
and card.slot()
.
Consuming Slots
When consuming components with slots, to populate a slot, you do not call t.slot
. You
call .slot
directly on the component instance provided by the context manager:
- Notice
card.slot
is called, nott.slot
orself.slot
.
More information on components
For more information on components in PuePy, see the Component Developer Guide.