Slot

Struct Slot 

Source
pub struct Slot;
Expand description

The <slot> element - defines a placeholder in a web component’s shadow DOM.

§Purpose

The <slot> element is part of the Web Components technology suite. It creates a placeholder inside a web component that users can fill with their own markup. Slots enable flexible, reusable components where content can be projected from the light DOM into the shadow DOM.

§Content Categories

  • Flow Content
  • Phrasing Content

§Permitted Content Model

  • Transparent content (fallback content when slot is not filled)

§Common Use Cases

  • Creating reusable web components
  • Defining customizable areas in shadow DOM templates
  • Building flexible UI component libraries
  • Implementing compound components with multiple insertion points
  • Providing default fallback content for empty slots

§Key Attributes

  • name: Named slot identifier (unnamed slots are default slots)

§Example

<!-- Custom element template (in shadow DOM) -->
<template id="card-template">
  <style>
    .card { border: 1px solid #ccc; padding: 1rem; }
    .card-header { font-weight: bold; }
  </style>
  <div class="card">
    <div class="card-header">
      <slot name="header">Default Header</slot>
    </div>
    <div class="card-body">
      <slot>Default content</slot>
    </div>
    <div class="card-footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<!-- Usage of the custom element -->
<my-card>
  <span slot="header">Custom Header</span>
  <p>This is the main content that goes into the default slot.</p>
  <small slot="footer">Footer text</small>
</my-card>

<!-- Named slots with fallback -->
<custom-dialog>
  <h2 slot="title">Confirmation</h2>
  <p>Are you sure you want to proceed?</p>
  <div slot="actions">
    <button>Cancel</button>
    <button>OK</button>
  </div>
</custom-dialog>

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Slot

Source§

const TAG: &'static str = "slot"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl<T: PhrasingContent> CanContain<T> for Slot

Source§

impl FlowContent for Slot

Source§

impl PhrasingContent for Slot

Auto Trait Implementations§

§

impl Freeze for Slot

§

impl RefUnwindSafe for Slot

§

impl Send for Slot

§

impl Sync for Slot

§

impl Unpin for Slot

§

impl UnwindSafe for Slot

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.