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.Componentclass. - The 
propsattribute 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_classesis a list of CSS classes that will be applied to the component by default.- The 
insert_slotmethod is used to insert content into a named slot. In this case, we are inserting content into thecard-headerslot. - Unnamed, or default slots, can be filled by calling 
insert_slotwithout a name. trigger_eventis used to trigger a custom event. Notice the detail dictionary. This pattern matches the JavaScriptCustomEventAPI.
- The 
cardcomponent is used with thetypeprop set to"success". - The 
my-custom-eventevent is bound to theself.handle_custom_eventmethod. - The content for the 
card-headerslot, as defined in theCardcomponent, is populated with "Success!". - The default slot is populated with "Your operation worked". Default slots are not named.
 - The 
handle_custom_eventmethod is called when themy-custom-eventevent 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.slotis called, nott.slotorself.slot. 
More information on components
For more information on components in PuePy, see the Component Developer Guide.