<slot>

Draft

Definition

The <slot> HTML element is used in conjunction with the Shadow DOM to define a placeholder within a web component where external content can be inserted. It allows the component author to create customizable and flexible components that can receive content from the component’s consumer.

Here’s an example of how to use the <slot> element:

<!-- Component definition -->
<template id="custom-button-template">
  <button>
    <slot></slot>
  </button>
</template>

<!-- Component usage -->
<custom-button> Click me! </custom-button>

In this example, a custom button component is defined using the <template> element. Within the template, a <button> element is used, and the <slot> element is placed inside it. The <slot> element acts as a placeholder for content that will be inserted when using the component.

When using the custom button component, the desired content, in this case, the text “Click me!”, is provided between the opening and closing tags of the component. This content will be dynamically inserted into the <slot> element within the template, replacing the <slot> placeholder.

By using the <slot> element, web component authors can create components that allow users to customize the content without modifying the component’s internal structure. Users can provide different content, such as text, HTML elements, or even other web components, to be displayed within the component.

The <slot> element can also have a name attribute, allowing the component author to define multiple named slots within the component template. This provides more flexibility and control over where specific content should be placed within the component.