# Slots

> defineSlots declares a component's slots, with fallbacks, scoped props, a presence test and forwarding. A parent fills them with children and a slot object.

Slots let a component leave holes for its parent to fill: a panel's body, a card's header, the way a list renders each row. The component decides where each slot goes and what shows when the parent leaves it empty, and the parent decides what goes in. Each framework has its own way to pass content into a component, and Unframework writes each one: children and render props on React, Solid and Qwik, `<slot>` on Vue and Astro, snippets on Svelte and `ng-content` with templates on Angular.

## Declaring slots

A component declares its slots with `defineSlots`, bound to a `const`, and renders each one as a child:

<!-- prettier-ignore -->
```tsx [Notice.uf.tsx]
import { defineSlots } from "@uxfront/unframework";
import type { Element } from "@uxfront/unframework";

export default function Notice({ label }: { label: string }) {
  const slots = defineSlots<{ default?(): Element; action?(): Element }>();
  return (
    <div role="note" aria-label={label} class="notice">
      <p>{slots.default?.() ?? <em>Nothing to report.</em>}</p>
      {slots.action?.() ?? <span class="muted">No action</span>}
    </div>
  );
}
```

Each member of the type is optional, since a parent may leave any slot empty, and a function type that returns `Element`. The default slot is named `default`, and takes what the parent writes as children. Every other slot is named. React, Solid, Qwik and Astro pass slots as props, so a slot is named apart from every prop and event ([UF2029](/docs/reference/diagnostics#UF2029)).

`{slots.default?.()}` renders a slot, and what follows `??` is its fallback, which renders when the parent passes nothing for that slot. Here a parent fills `Notice`'s default slot and its `action` slot, and leaves both empty in a second `Notice`:

<!-- prettier-ignore -->
```tsx [Board.uf.tsx]
import Notice from "./Notice.uf.tsx";

export default function Board({ build }: { build: string }) {
  return (
    <div>
      <Notice label="Build">
        {{
          default: () => <>Build {build} failed.</>,
          action: () => <a href="#logs">See the logs</a>,
        }}
      </Notice>
      <Notice label="Deploy" />
    </div>
  );
}
```

::framework-switcher

#react

<!-- prettier-ignore -->
```tsx [Notice.tsx]
import type { CSSProperties, ReactNode } from "react";

export interface NoticeSlots {
  children?: ReactNode;
  action?: ReactNode;
}

export interface NoticeAttributes {
  className?: string;
  style?: CSSProperties;
}

export default function Notice({
  label,
  children,
  action,
  className,
  style,
  ...attributes
}: { label: string } & NoticeSlots & NoticeAttributes) {
  return (
    <div
      role="note"
      aria-label={label}
      className={cx("notice", className)}
      style={style}
      {...scopeAttributes(attributes)}
    >
      <p>{children ?? <em>Nothing to report.</em>}</p>
      {action ?? <span className="muted">No action</span>}
    </div>
  );
}

/** Joins class names, and the keys of an object's truthy entries, into one `className`. */
function cx(...parts: unknown[]): string {
  const names: string[] = [];
  for (const part of parts) {
    if (typeof part === "string") {
      if (part) names.push(part);
    } else if (part && typeof part === "object") {
      for (const [key, on] of Object.entries(part)) {
        if (on) names.push(key);
      }
    }
  }
  return names.join(" ");
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

<!-- prettier-ignore -->
```tsx [Board.tsx]
import Notice from "./Notice";
import type { CSSProperties } from "react";

export interface BoardAttributes {
  className?: string;
  style?: CSSProperties;
}

export default function Board({
  build,
  className,
  style,
  ...attributes
}: { build: string } & BoardAttributes) {
  return (
    <div className={className} style={style} {...scopeAttributes(attributes)}>
      <Notice label="Build" action={<a href="#logs">See the logs</a>}>
        Build {build} failed.
      </Notice>
      <Notice label="Deploy" />
    </div>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

#vue

<!-- prettier-ignore -->
```vue [Notice.vue]
<script setup lang="ts">
const { label } = defineProps<{ label: string }>();
defineSlots<{ default?(): unknown; action?(): unknown }>();
</script>

<template>
  <div role="note" :aria-label="label" class="notice">
    <p>
      <slot>
        <em>Nothing to report.</em>
      </slot>
    </p>
    <slot name="action">
      <span class="muted">No action</span>
    </slot>
  </div>
</template>
```

<!-- prettier-ignore -->
```vue [Board.vue]
<script setup lang="ts">
import Notice from "./Notice.vue";

const { build } = defineProps<{ build: string }>();
</script>

<template>
  <div>
    <Notice label="Build">
      <template #default>Build {{ build }} failed.</template>
      <template #action>
        <a href="#logs">See the logs</a>
      </template>
    </Notice>
    <Notice label="Deploy" />
  </div>
</template>
```

#svelte

<!-- prettier-ignore -->
```svelte [Notice.svelte]
<svelte:options runes={true} preserveWhitespace={false} />

<script lang="ts">
  import type { Snippet } from "svelte";
  import type { ClassValue } from "svelte/elements";

  type Props = {
    label: string;
    children?: Snippet;
    action?: Snippet;
    class?: ClassValue;
    style?: string;
  };

  let { label, children, action, class: className, style, ...attributes }: Props = $props();

  /** The scope attributes of the styled parents that render this component, for its root. */
  function scopeAttributes(props: object): Record<string, ""> {
    const scope: Record<string, ""> = {};
    for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
    return scope;
  }
</script>

<div
  role="note"
  aria-label={label}
  class={["notice", className]}
  {style}
  {...scopeAttributes(attributes)}
>
  <p>
    {#if children}{@render children()}{:else}<em>Nothing to report.</em>{/if}
  </p
  >{#if action}{@render action()}{:else}<span class="muted">No action</span>{/if}
</div>
```

<!-- prettier-ignore -->
```svelte [Board.svelte]
<svelte:options runes={true} preserveWhitespace={false} />

<script lang="ts">
  import Notice from "./Notice.svelte";
  import type { ClassValue } from "svelte/elements";

  type Props = { build: string; class?: ClassValue; style?: string };

  let { build, class: className, style, ...attributes }: Props = $props();

  /** The scope attributes of the styled parents that render this component, for its root. */
  function scopeAttributes(props: object): Record<string, ""> {
    const scope: Record<string, ""> = {};
    for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
    return scope;
  }
</script>

<div class={className} {style} {...scopeAttributes(attributes)}>
  <Notice
    label="Build"
  >Build {build} failed.{#snippet action()}<a href="#logs">See the logs</a>{/snippet}</Notice
  ><Notice label="Deploy" />
</div>
```

#angular

<!-- prettier-ignore -->
```ts [notice.ts]
import { NgTemplateOutlet } from "@angular/common";
import {
  Component,
  Directive,
  ElementRef,
  type OnInit,
  Renderer2,
  TemplateRef,
  contentChild,
  inject,
  input,
} from "@angular/core";

/** A template of `Notice`'s `action` slot, which takes no `let-` variable (ADR-0056). */
@Directive({ selector: "ng-template[ufNoticeAction]" })
export class NoticeAction {
  static ngTemplateContextGuard(
    _directive: NoticeAction,
    _context: unknown,
  ): _context is Record<never, never> {
    return true;
  }
}

/** The scope attributes taken off each host, for the roots its component renders later. */
const scopes = new WeakMap<Element, string[]>();

/**
 * Gives a root node of its component the scope attributes the component's styled parents put on
 * its host (ADR-0063), and takes them off the host, which keeps only `display: contents`.
 */
@Directive({ selector: "[ufScope]" })
export class ScopeAttributes implements OnInit {
  readonly ufScope = input.required<HTMLElement>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);

  ngOnInit(): void {
    const host = this.ufScope();
    let names = scopes.get(host);
    if (!names) {
      names = host.getAttributeNames().filter((name) => name.startsWith("data-uf-"));
      for (const name of names) this.renderer.removeAttribute(host, name);
      scopes.set(host, names);
    }
    for (const name of names) this.renderer.setAttribute(this.element.nativeElement, name, "");
  }
}

@Component({
  selector: "uf-notice",
  imports: [NgTemplateOutlet, ScopeAttributes],
  host: { "[attr.class]": "null", "[attr.style]": "'display: contents;'" },
  preserveWhitespaces: false,
  template: `
    @let label = this.label();
    <div
      role="note"
      [attr.aria-label]="label"
      class="notice"
      [class]="this.class()"
      [style]="this.style()"
      [ufScope]="this.host"
    >
      <p>
        <ng-content>
          <em>Nothing to report.</em>
        </ng-content>
      </p>
      @if (this.actionTemplate(); as template) {
        <ng-container [ngTemplateOutlet]="template" />
      } @else {
        <span class="muted">No action</span>
      }
    </div>
    <ng-content select="[ufNoticeAction]" />
  `,
})
export default class Notice {
  readonly label = input.required<string>();
  readonly class = input<string>();
  readonly style = input<Record<string, string | null | undefined>>();
  protected readonly actionTemplate = contentChild(NoticeAction, { read: TemplateRef });
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

<!-- prettier-ignore -->
```ts [board.ts]
import Notice, { NoticeAction } from "./notice";
import {
  Component,
  Directive,
  ElementRef,
  type OnInit,
  Renderer2,
  inject,
  input,
} from "@angular/core";

/** The scope attributes taken off each host, for the roots its component renders later. */
const scopes = new WeakMap<Element, string[]>();

/**
 * Gives a root node of its component the scope attributes the component's styled parents put on
 * its host (ADR-0063), and takes them off the host, which keeps only `display: contents`.
 */
@Directive({ selector: "[ufScope]" })
export class ScopeAttributes implements OnInit {
  readonly ufScope = input.required<HTMLElement>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);

  ngOnInit(): void {
    const host = this.ufScope();
    let names = scopes.get(host);
    if (!names) {
      names = host.getAttributeNames().filter((name) => name.startsWith("data-uf-"));
      for (const name of names) this.renderer.removeAttribute(host, name);
      scopes.set(host, names);
    }
    for (const name of names) this.renderer.setAttribute(this.element.nativeElement, name, "");
  }
}

@Component({
  selector: "uf-board",
  imports: [Notice, NoticeAction, ScopeAttributes],
  host: { "[attr.class]": "null", "[attr.style]": "'display: contents;'" },
  preserveWhitespaces: false,
  template: `
    @let build = this.build();
    <div [class]="this.class()" [style]="this.style()" [ufScope]="this.host">
      <uf-notice
        [label]="'Build'"
      >Build {{ build }} failed.<ng-template
        ufNoticeAction
      ><a href="#logs">See the logs</a></ng-template></uf-notice>
      <uf-notice [label]="'Deploy'" />
    </div>
  `,
})
export default class Board {
  readonly build = input.required<string>();
  readonly class = input<string>();
  readonly style = input<Record<string, string | null | undefined>>();
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

#solid

<!-- prettier-ignore -->
```tsx [Notice.tsx]
import { type JSX, children } from "solid-js";

export interface NoticeSlots {
  children?: JSX.Element;
  action?: JSX.Element;
}

export default function Notice(
  props: { label: string } & NoticeSlots & { class?: string; style?: JSX.CSSProperties },
) {
  const content = children(() => props.children);
  return (
    <div
      role="note"
      aria-label={props.label}
      class={props.class ? `notice ${props.class}` : "notice"}
      style={props.style}
      {...scopeAttributes(props)}
    >
      <p>{content() ?? <em>Nothing to report.</em>}</p>
      {props.action ?? <span class="muted">No action</span>}
    </div>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
```

<!-- prettier-ignore -->
```tsx [Board.tsx]
import Notice from "./Notice";
import type { JSX } from "solid-js";

export default function Board(
  props: { build: string } & { class?: string; style?: JSX.CSSProperties },
) {
  return (
    <div class={props.class} style={props.style} {...scopeAttributes(props)}>
      <Notice label="Build" action={<a href="#logs">See the logs</a>}>
        Build {props.build} failed.
      </Notice>
      <Notice label="Deploy" />
    </div>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
```

#qwik

<!-- prettier-ignore -->
```tsx [Notice.tsx]
import {
  type CSSProperties,
  type ClassList,
  type JSXOutput,
  type QRL,
  Slot,
  component$,
} from "@qwik.dev/core";

export interface NoticeSlots {
  action$?: QRL<() => JSXOutput>;
}

export default component$<
  { label: string } & NoticeSlots & { class?: ClassList; style?: CSSProperties }
>(({ label, action$: actionSlot$, class: className, style, ...attributes }) => {
  return (
    <div
      role="note"
      aria-label={label}
      class={["notice", className]}
      {...scopeAttributes(attributes)}
      style={style}
    >
      <p>
        <Slot>
          <em>Nothing to report.</em>
        </Slot>
      </p>
      {actionSlot$?.() ?? <span class="muted">No action</span>}
    </div>
  );
});

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

<!-- prettier-ignore -->
```tsx [Board.tsx]
import Notice from "./Notice";
import { type CSSProperties, type ClassList, component$ } from "@qwik.dev/core";

export default component$<{ build: string } & { class?: ClassList; style?: CSSProperties }>(
  ({ build, class: className, style, ...attributes }) => {
    return (
      <div {...scopeAttributes(attributes)} class={className} style={style}>
        <Notice label="Build" action$={() => <a href="#logs">See the logs</a>}>
          Build {build} failed.
        </Notice>
        <Notice label="Deploy" />
      </div>
    );
  },
);

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

#astro

<!-- prettier-ignore -->
```astro [Notice.astro]
---
interface Props {
  label: string;
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const { label, class: className, style } = Astro.props;

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
---

<div
  role="note"
  aria-label={label}
  class:list={["notice", className]}
  style={style}
  {...scopeAttributes(Astro.props)}
>
  <p>
    <slot>
      <em>Nothing to report.</em>
    </slot>
  </p>
  <slot name="action">
    <span class="muted">No action</span>
  </slot>
</div>
```

<!-- prettier-ignore -->
```astro [Board.astro]
---
import Notice from "./Notice.astro";

interface Props {
  build: string;
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const { build, class: className, style } = Astro.props;

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
---

<div class:list={[className]} style={style} {...scopeAttributes(Astro.props)}>
  <Notice
    label="Build"
  >Build {build} failed.<Fragment slot="action"><a href="#logs">See the logs</a></Fragment></Notice>
  <Notice label="Deploy" />
</div>
```

::

- React and Solid take the default slot as `children` and a named one as a prop of its name. Solid resolves the children once, with `children()`.
- Vue writes `<slot>` and `<slot name="action">`, with the fallback inside, and the parent fills them with `<template #action>`.
- Svelte takes each slot as a snippet prop, `children` for the default one, and renders it with `{@render}` under `{#if}` for the fallback. The parent writes `{#snippet action()}`.
- Angular projects the default slot with `<ng-content>`, its fallback inside. Each named slot is a directive that the child's file exports, `ng-template[ufNoticeAction]`: the child reads the parent's template with `contentChild` and renders it with `ngTemplateOutlet` under `@if`, and the parent writes `<ng-template ufNoticeAction>` and lists the directive in `imports` (`named-slot`, emulated).
- Qwik projects the default slot with `<Slot>`, and passes a named one as a QRL render prop, `action$`, because its `<Slot>` takes no props and has no way to tell whether it was filled (`named-slot`, emulated).
- Astro writes `<slot>` and `<slot name="action">`, and the parent fills a named one with `<Fragment slot="action">`.

Fallback renders when the parent passes no slot. A fill that renders nothing is outside the contract: Vue shows the fallback for it, and the other targets render nothing. Leave a fill out where it would be empty.

## Filling slots

A parent fills the default slot with its children. To fill named slots, it passes a slot object as the component's only child, with each slot as an arrow function that returns JSX, `default` included:

<!-- prettier-ignore -->
```tsx [Post.uf.tsx]
import Card from "./Card.uf.tsx";

export default function Post({ author }: { author: string }) {
  return (
    <Card>
      {{
        header: () => <h2>Release notes</h2>,
        default: () => <p>Slots arrive in M3.</p>,
        footer: () => <small>By {author}</small>,
      }}
    </Card>
  );
}
```

A member of the slot object that isn't an arrow function returning JSX, or a parent's own slot passed on, is [UF3040](/docs/reference/diagnostics#UF3040). A key the child doesn't declare, or children for a child with no default slot, is [UF3038](/docs/reference/diagnostics#UF3038), with a fix to the closest slot's name.

A fill can't have a root element that belongs only inside a given parent, such as a `<summary>`, a `<figcaption>`, a `<dt>` or a `<dd>` ([UF3003](/docs/reference/diagnostics#UF3003)). The compiler doesn't record where a child places each slot, so it can't prove that the fill lands in a `<details>`, a `<figure>` or a `<dl>`. Write that parent element in the fill too, or let the child write the element and pass it only the content.

## Scoped slots

A scoped slot hands its fill some values, so the parent decides how to render what the child holds. Declare the slot with a parameter, and pass its props where you render it:

<!-- prettier-ignore -->
```tsx [List.uf.tsx]
import { defineSlots } from "@uxfront/unframework";
import type { Element } from "@uxfront/unframework";

export default function List({ items, label }: { items: string[]; label: string }) {
  const slots = defineSlots<{ item?(props: { item: string; index: number }): Element }>();
  return (
    <ul aria-label={label}>
      {items.map((item, index) => (
        <li key={item}>{slots.item?.({ item, index })}</li>
      ))}
    </ul>
  );
}
```

<!-- prettier-ignore -->
```tsx [Ranking.uf.tsx]
import List from "./List.uf.tsx";

export default function Ranking({ names }: { names: string[] }) {
  return (
    <div>
      <List items={names} label="Podium">
        {{
          item: ({ item, index }) => (
            <span>
              {index + 1}. <b>{item}</b>
            </span>
          ),
        }}
      </List>
      <List items={names} label="Initials">
        {{ item: (entry) => <i>{entry.item.slice(0, 1)}</i> }}
      </List>
    </div>
  );
}
```

A fill takes the slot's props as its one parameter, destructured, `({ item, index })`, or by a name, `(entry)`. The fill renders once for every item the child renders it for:

::framework-switcher

#react

<!-- prettier-ignore -->
```tsx [List.tsx]
import type { CSSProperties, ReactNode } from "react";

export interface ListSlots {
  renderItem?: (props: { item: string; index: number }) => ReactNode;
}

export interface ListAttributes {
  className?: string;
  style?: CSSProperties;
}

export default function List({
  items,
  label,
  renderItem,
  className,
  style,
  ...attributes
}: { items: string[]; label: string } & ListSlots & ListAttributes) {
  return (
    <ul aria-label={label} className={className} style={style} {...scopeAttributes(attributes)}>
      {items.map((item, index) => (
        <li key={item}>{renderItem?.({ item, index })}</li>
      ))}
    </ul>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

<!-- prettier-ignore -->
```tsx [Ranking.tsx]
import List from "./List";
import type { CSSProperties } from "react";

export interface RankingAttributes {
  className?: string;
  style?: CSSProperties;
}

export default function Ranking({
  names,
  className,
  style,
  ...attributes
}: { names: string[] } & RankingAttributes) {
  return (
    <div className={className} style={style} {...scopeAttributes(attributes)}>
      <List
        items={names}
        label="Podium"
        renderItem={({ item, index }) => (
          <span>
            {index + 1}. <b>{item}</b>
          </span>
        )}
      />
      <List
        items={names}
        label="Initials"
        renderItem={(entry) => <i>{entry.item.slice(0, 1)}</i>}
      />
    </div>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

#vue

<!-- prettier-ignore -->
```vue [List.vue]
<script setup lang="ts">
const { items, label } = defineProps<{ items: string[]; label: string }>();
defineSlots<{ item?(props: { item: string; index: number }): unknown }>();
</script>

<template>
  <ul :aria-label="label">
    <li v-for="(item, index) in items" :key="item">
      <slot name="item" :item="item" :index="index" />
    </li>
  </ul>
</template>
```

<!-- prettier-ignore -->
```vue [Ranking.vue]
<script setup lang="ts">
import List from "./List.vue";

const { names } = defineProps<{ names: string[] }>();
</script>

<template>
  <div>
    <List :items="names" label="Podium">
      <template #item="{ item, index }">
        <span>{{ index + 1 }}. <b>{{ item }}</b></span>
      </template>
    </List>
    <List :items="names" label="Initials">
      <template #item="entry">
        <i>{{ entry.item.slice(0, 1) }}</i>
      </template>
    </List>
  </div>
</template>
```

#svelte

<!-- prettier-ignore -->
```svelte [List.svelte]
<svelte:options runes={true} preserveWhitespace={false} />

<script lang="ts">
  import type { Snippet } from "svelte";
  import type { ClassValue } from "svelte/elements";

  type Props = {
    items: string[];
    label: string;
    item?: Snippet<[{ item: string; index: number }]>;
    class?: ClassValue;
    style?: string;
  };

  let { items, label, item: itemSnippet, class: className, style, ...attributes }: Props = $props();

  /** The scope attributes of the styled parents that render this component, for its root. */
  function scopeAttributes(props: object): Record<string, ""> {
    const scope: Record<string, ""> = {};
    for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
    return scope;
  }
</script>

<ul aria-label={label} class={className} {style} {...scopeAttributes(attributes)}>
  {#each items as item, index (item)}
    <li>{@render itemSnippet?.({ item, index })}</li>
  {/each}
</ul>
```

<!-- prettier-ignore -->
```svelte [Ranking.svelte]
<svelte:options runes={true} preserveWhitespace={false} />

<script lang="ts">
  import List from "./List.svelte";
  import type { ClassValue } from "svelte/elements";

  type Props = { names: string[]; class?: ClassValue; style?: string };

  let { names, class: className, style, ...attributes }: Props = $props();

  /** The scope attributes of the styled parents that render this component, for its root. */
  function scopeAttributes(props: object): Record<string, ""> {
    const scope: Record<string, ""> = {};
    for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
    return scope;
  }
</script>

<div class={className} {style} {...scopeAttributes(attributes)}>
  <List items={names} label="Podium">
    {#snippet item({ item, index })}
      <span>{index + 1}. <b>{item}</b></span>
    {/snippet}
  </List
  ><List items={names} label="Initials">
    {#snippet item(entry)}
      <i>{entry.item.slice(0, 1)}</i>
    {/snippet}
  </List>
</div>
```

#angular

<!-- prettier-ignore -->
```ts [list.ts]
import { NgTemplateOutlet } from "@angular/common";
import {
  Component,
  Directive,
  ElementRef,
  type OnInit,
  Renderer2,
  TemplateRef,
  contentChild,
  inject,
  input,
} from "@angular/core";

/** What a template of `List`'s `item` slot reads: the slot's props. */
export type ListItemContext = { item: string; index: number };

/** A template of `List`'s `item` slot, its `let-` variables typed (ADR-0056). */
@Directive({ selector: "ng-template[ufListItem]" })
export class ListItem {
  static ngTemplateContextGuard(
    _directive: ListItem,
    _context: unknown,
  ): _context is ListItemContext & { $implicit: ListItemContext } {
    return true;
  }
}

/** The scope attributes taken off each host, for the roots its component renders later. */
const scopes = new WeakMap<Element, string[]>();

/**
 * Gives a root node of its component the scope attributes the component's styled parents put on
 * its host (ADR-0063), and takes them off the host, which keeps only `display: contents`.
 */
@Directive({ selector: "[ufScope]" })
export class ScopeAttributes implements OnInit {
  readonly ufScope = input.required<HTMLElement>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);

  ngOnInit(): void {
    const host = this.ufScope();
    let names = scopes.get(host);
    if (!names) {
      names = host.getAttributeNames().filter((name) => name.startsWith("data-uf-"));
      for (const name of names) this.renderer.removeAttribute(host, name);
      scopes.set(host, names);
    }
    for (const name of names) this.renderer.setAttribute(this.element.nativeElement, name, "");
  }
}

@Component({
  selector: "uf-list",
  imports: [NgTemplateOutlet, ScopeAttributes],
  host: { "[attr.class]": "null", "[attr.style]": "'display: contents;'" },
  preserveWhitespaces: false,
  template: `
    @let items = this.items();
    @let label = this.label();
    <ul
      [attr.aria-label]="label"
      [class]="this.class()"
      [style]="this.style()"
      [ufScope]="this.host"
    >
      @for (item of items; track item; let index = $index) {
        <li>
          @if (this.itemTemplate(); as template) {
            <ng-container
              [ngTemplateOutlet]="template"
              [ngTemplateOutletContext]="{ $implicit: { item: item, index: index }, item: item, index: index }"
            />
          }
        </li>
      }
    </ul>
    <ng-content select="[ufListItem]" />
  `,
})
export default class List {
  readonly items = input.required<string[]>();
  readonly label = input.required<string>();
  readonly class = input<string>();
  readonly style = input<Record<string, string | null | undefined>>();
  protected readonly itemTemplate = contentChild(ListItem, { read: TemplateRef });
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

<!-- prettier-ignore -->
```ts [ranking.ts]
import List, { ListItem } from "./list";
import {
  Component,
  Directive,
  ElementRef,
  type OnInit,
  Renderer2,
  inject,
  input,
} from "@angular/core";

/** The scope attributes taken off each host, for the roots its component renders later. */
const scopes = new WeakMap<Element, string[]>();

/**
 * Gives a root node of its component the scope attributes the component's styled parents put on
 * its host (ADR-0063), and takes them off the host, which keeps only `display: contents`.
 */
@Directive({ selector: "[ufScope]" })
export class ScopeAttributes implements OnInit {
  readonly ufScope = input.required<HTMLElement>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);

  ngOnInit(): void {
    const host = this.ufScope();
    let names = scopes.get(host);
    if (!names) {
      names = host.getAttributeNames().filter((name) => name.startsWith("data-uf-"));
      for (const name of names) this.renderer.removeAttribute(host, name);
      scopes.set(host, names);
    }
    for (const name of names) this.renderer.setAttribute(this.element.nativeElement, name, "");
  }
}

@Component({
  selector: "uf-ranking",
  imports: [List, ListItem, ScopeAttributes],
  host: { "[attr.class]": "null", "[attr.style]": "'display: contents;'" },
  preserveWhitespaces: false,
  template: `
    @let names = this.names();
    <div [class]="this.class()" [style]="this.style()" [ufScope]="this.host">
      <uf-list [items]="names" [label]="'Podium'">
        <ng-template ufListItem let-item="item" let-index="index">
          <span>{{ index + 1 }}. <b>{{ item }}</b></span>
        </ng-template>
      </uf-list>
      <uf-list [items]="names" [label]="'Initials'">
        <ng-template ufListItem let-entry>
          <i>{{ entry.item.slice(0, 1) }}</i>
        </ng-template>
      </uf-list>
    </div>
  `,
})
export default class Ranking {
  readonly names = input.required<string[]>();
  readonly class = input<string>();
  readonly style = input<Record<string, string | null | undefined>>();
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

#solid

<!-- prettier-ignore -->
```tsx [List.tsx]
import { For, type JSX, createMemo, untrack } from "solid-js";

export interface ListSlots {
  item?: (props: { item: string; index: number }) => JSX.Element;
}

export default function List(
  props: { items: string[]; label: string } & ListSlots & {
      class?: string;
      style?: JSX.CSSProperties;
    },
) {
  return (
    <ul
      aria-label={props.label}
      class={props.class}
      style={props.style}
      {...scopeAttributes(props)}
    >
      <KeyedFor each={props.items} by={(item) => item}>
        {(item) => <li>{props.item?.({ item: item.value, index: item.index })}</li>}
      </KeyedFor>
    </ul>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}

/** A list whose rows follow their keys, so a row keeps its element while the list reorders and while its item is replaced. */
function KeyedFor<T>(props: {
  each: readonly T[];
  by: (item: T, index: number) => unknown;
  children: (row: { readonly value: T; readonly index: number }) => JSX.Element;
}): JSX.Element {
  const items = createMemo(() => {
    const rows = new Map<unknown, T>();
    props.each.forEach((item, index) => {
      const key = props.by(item, index);
      rows.set(rows.has(key) ? {} : key, item);
    });
    return rows;
  });
  return (
    <For each={[...items().keys()]}>
      {(key, index) => {
        const item = createMemo(() => items().get(key) as T);
        return untrack(() =>
          props.children({
            get value() {
              return item();
            },
            get index() {
              return index();
            },
          }),
        );
      }}
    </For>
  );
}
```

<!-- prettier-ignore -->
```tsx [Ranking.tsx]
import List from "./List";
import type { JSX } from "solid-js";

export default function Ranking(
  props: { names: string[] } & { class?: string; style?: JSX.CSSProperties },
) {
  return (
    <div class={props.class} style={props.style} {...scopeAttributes(props)}>
      <List
        items={props.names}
        label="Podium"
        item={({ item, index }) => (
          <span>
            {index + 1}. <b>{item}</b>
          </span>
        )}
      />
      <List
        items={props.names}
        label="Initials"
        item={(entry) => <i>{entry.item.slice(0, 1)}</i>}
      />
    </div>
  );
}

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
```

#qwik

<!-- prettier-ignore -->
```tsx [List.tsx]
import {
  type CSSProperties,
  type ClassList,
  type JSXOutput,
  type QRL,
  component$,
} from "@qwik.dev/core";

export interface ListSlots {
  item$?: QRL<(props: { item: string; index: number }) => JSXOutput>;
}

export default component$<
  { items: string[]; label: string } & ListSlots & { class?: ClassList; style?: CSSProperties }
>(({ items, label, item$, class: className, style, ...attributes }) => {
  return (
    <ul aria-label={label} {...scopeAttributes(attributes)} class={className} style={style}>
      {items.map((item, index) => (
        <li key={item}>{item$?.({ item, index })}</li>
      ))}
    </ul>
  );
});

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

<!-- prettier-ignore -->
```tsx [Ranking.tsx]
import List from "./List";
import { type CSSProperties, type ClassList, component$ } from "@qwik.dev/core";

export default component$<{ names: string[] } & { class?: ClassList; style?: CSSProperties }>(
  ({ names, class: className, style, ...attributes }) => {
    return (
      <div {...scopeAttributes(attributes)} class={className} style={style}>
        <List
          items={names}
          label="Podium"
          item$={({ item, index }) => (
            <span>
              {index + 1}. <b>{item}</b>
            </span>
          )}
        />
        <List items={names} label="Initials" item$={(entry) => <i>{entry.item.slice(0, 1)}</i>} />
      </div>
    );
  },
);

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const key of Object.keys(props)) {
    if (key.startsWith("data-uf-")) scope[key] = "";
  }
  return scope;
}
```

#astro

<!-- prettier-ignore -->
```astro [List.astro]
---
interface Props {
  items: string[];
  label: string;
  item?: (props: { item: string; index: number }) => unknown;
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const { items, label, item: item_1, class: className, style } = Astro.props;

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
---

<ul aria-label={label} class:list={[className]} style={style} {...scopeAttributes(Astro.props)}>
  {items.map((item, index) => (
    <li>{item_1?.({ item, index })}</li>
  ))}
</ul>
```

<!-- prettier-ignore -->
```astro [Ranking.astro]
---
import List from "./List.astro";

interface Props {
  names: string[];
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const { names, class: className, style } = Astro.props;

/** The scope attributes of the styled parents that render this component, for its root. */
function scopeAttributes(props: object): Record<string, ""> {
  const scope: Record<string, ""> = {};
  for (const name of Object.keys(props)) if (name.startsWith("data-uf-")) scope[name] = "";
  return scope;
}
---

<div class:list={[className]} style={style} {...scopeAttributes(Astro.props)}>
  <List
    items={names}
    label="Podium"
    item={({ item, index }) => <span>{index + 1}. <b>{item}</b></span>}
  />
  <List items={names} label="Initials" item={(entry) => <i>{entry.item.slice(0, 1)}</i>} />
</div>
```

::

- React passes a scoped slot as a render prop named `render` and the slot's name, `renderItem`, because oxlint's React rules reject a render prop named like the slot. Solid and Astro keep the slot's name, `item`, and Qwik writes `item$`, a QRL ([`scoped-slot`](/docs/reference/targets#capabilities), emulated on Angular, Astro and Qwik).
- Vue binds the props on the `<slot>`, and Svelte types its snippet, `Snippet<[{ item: string; index: number }]>`.
- Angular types the template's `let-` variables with a static `ngTemplateContextGuard` on the slot's directive, so a consumer that reads a prop the slot doesn't have fails to compile.

The default slot may be scoped too, filled through the slot object: `{{ default: ({ item }) => <b>{item}!</b> }}`. React names its render prop `renderDefault`. A slot's props may have any names, `name` and `data-id` included, though a template language could read them as the slot's own name or as an attribute: Vue binds them as one object, `v-bind="{ name: person.name, 'data-id': person.id, index }"`, and the other targets pass an object too.

A slot rendered inside a `.map` renders its fill once for each item, scoped or not, with one exception: Qwik projects a default slot once, so a default slot rendered in a list, or twice, shows its content once or not at all there, and no diagnostic reports it yet. A named slot, a QRL render prop on Qwik, renders for every item:

<!-- prettier-ignore -->
```tsx [Steps.uf.tsx]
{steps.map((step) => (
  <li key={step}>
    {slots.marker?.()}
    {step}
  </li>
))}
```

## Testing for a slot

`slots.title` as a condition tests whether the parent filled the slot, so a component can leave out a wrapper that would be empty:

<!-- prettier-ignore -->
```tsx [Card.uf.tsx]
<section class="card" aria-label={label}>
  {slots.title ? <header class="title">{slots.title?.()}</header> : null}
  {slots.default ? <div class="body">{slots.default?.()}</div> : <p>Empty card</p>}
</section>
```

React tests `title != null`, Vue `slots.title`, Svelte `{#if title}`, Angular its content query, `this.titleTemplate()`, Solid `<Show when={title()}>`, Qwik `title$` and Astro `Astro.slots.has("title")`.

Testing the default slot is unsupported on Angular and Qwik ([`default-slot-presence`](/docs/reference/targets#capabilities), an error): Angular has no way to tell whether `<ng-content>` received content, and Qwik's `<Slot>` has none either. A named slot works on every target, so test a named slot where you need to know.

A slot is rendered, tested or forwarded, and nothing else: storing it, calling it in setup code or passing `slots` itself is [UF3041](/docs/reference/diagnostics#UF3041).

## Forwarding slots

A component may pass its own slots on to a child, in the child's slot object:

<!-- prettier-ignore -->
```tsx [Box.uf.tsx]
import { defineSlots } from "@uxfront/unframework";
import type { Element } from "@uxfront/unframework";

export default function Box() {
  const slots = defineSlots<{ title?(): Element; default?(): Element }>();
  return (
    <div class="box">
      <h3>{slots.title?.() ?? "Untitled"}</h3>
      {slots.default?.() ?? <p>No content</p>}
    </div>
  );
}
```

<!-- prettier-ignore -->
```tsx [Frame.uf.tsx]
import { defineSlots } from "@uxfront/unframework";
import type { Element } from "@uxfront/unframework";

import Box from "./Box.uf.tsx";

export default function Frame({ label }: { label: string }) {
  const slots = defineSlots<{ title?(): Element; default?(): Element }>();
  return (
    <section class="frame" aria-label={label}>
      <Box>{{ title: slots.title, default: slots.default }}</Box>
    </section>
  );
}
```

Forwarding keeps whether the slot was filled: where the outer parent fills nothing, the inner child shows its own fallback, "Untitled" here. React, Solid and Svelte pass the slot props on, and Qwik its `title$`. Vue fills the child's slot with its own `<slot>` under `v-if="slots.title"`. Angular declares the child's template again under `@if`, around its own (`slot-forwarding`, emulated). Astro writes `<slot name="title" slot="title" />`, under a test of which slots it received.

Forwarding the default slot keeps its presence too, so it is unsupported on Angular and Qwik, as testing it is: `Frame` above is an error on both. Forward named slots where those targets matter.

## Not yet

- A fill whose root is a `<summary>`, a `<figcaption>`, a `<dt>`, a `<dd>` or another element tied to its parent stays [UF3003](/docs/reference/diagnostics#UF3003), because a component's API doesn't record where it places each slot yet. Recording that is a separate issue.
- Testing or forwarding the default slot is unsupported on Angular and Qwik.
- Qwik projects a default slot once: rendered in a list or twice, its content shows once or not at all, with no diagnostic yet. Use a named slot there.

The decisions behind this page are [ADR-0054](https://github.com/uxfront-com/unframework/blob/main/docs/adrs/0054-composition-on-every-target.md), [ADR-0055](https://github.com/uxfront-com/unframework/blob/main/docs/adrs/0055-composition-ir-capabilities-and-codes.md) and [ADR-0056](https://github.com/uxfront-com/unframework/blob/main/docs/adrs/0056-angular-hosts-in-composition.md).
