Ask toddle AI

๐Ÿ 

Docs home

๐Ÿ‘ถ

Getting started

๐ŸŽ“

1st week in toddle

๐Ÿ› ๏ธ

The editor

๐Ÿงฑ

Elements

๐Ÿ’…

Styling and layout

๐Ÿ“ฆ๏ธ

Components

๐Ÿ“„

Pages and navigation

โ˜๏ธ

Working with APIs

๐Ÿงฎ

Formulas

๐Ÿ””

Events and actions

๐Ÿ“ฆ

Packages

๐Ÿค–

Custom code

๐Ÿ‘ฏโ€โ™‚๏ธ

Integrations

๐Ÿฟ

Tutorials

๐Ÿงฐ

Other

Slots

The <slot> element is a fundamental part of toddle's Component specification. As many other core concepts in toddle, slots build upon native web standards: Read more on the underlying Web Components Slots specification.

Tip: When exporting a component as a web-component, you can use your <slot> as you would any other web-component. You can insert anything, for example a React component into a slot on your toddle web component just by defining attribute `slot="my-slot"`!

Adding a slot

This is how you add a slot to toddle.

You can add a slot to your element tree by using the element catalog . Slots can be added to all components, but not pages.

Naming

Named Slot : A <slot> with a "name" attribute allows selective insertion of content. You will have to select which slot an element should be assigned to by picking it from the attributes tab.

Default Slot : A slot without a name will catch all elements that do not have a specific name selected in their attributes.

Reusability and encapsulation

This is how your element tree may look after adding some slots.

Components are entirely encapsulated. This is great as you are guaranteed that your component always behaves and looks the same. However, sometimes you may want certain parts of your component to be modified based on it's use-case. The <slot> element acts as a placeholder inside a component's element tree for content that is defined outside of the component. This can make for truly dynamic components. For example, in toddle โœจ we use slots for our dialog component (see image above)

Certain parts of the dialog are fixed, such as the backdrop, the dialog container, and the buttons, but you can insert anything you like into the content area.

Placeholder content

You may have noticed that a <slot> element may contain elements itself. These will be shown by default, as a type of placeholder. It is only when an element with a corresponding `slot` attribute with the slot's name is given, that the content is replaced.

Use cases

Slots should be used sparingly, but are extremely powerful when used well. They play a crucial role in creating reusable and encapsulated components. Slots enable you to move almost anything to their components and enable an incredible level of composition to your components.

In toddle (toddle is built in toddle) we use slots for a multitude of cases. Any time we need to add a right-click menu, we use our own `context-menu` component with two slots (" Content ", & " Default "). We can then add whatever content we like in the `Default` slot, and whenever it is clicked, `Content` is shown in a context menu. We do the same for `toddle-tooltip`, `dialog`, and many others.

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code โ€” but accessible to your entire team!

Try toddle โ€” it's free!
Server-side rendered Installable PWAs Connect to any database As performant as code toddle is built in toddle undefined undefined undefined undefined undefined
undefined Style tokens Export as web components No feature limits Connec to any API Extend with code undefined undefined undefined undefined

ยฉ Copyright 2024 toddle. All rights reserved.