For the complete documentation index, see llms.txt. Markdown variants are available by appending .md to any URL or sending an Accept: text/markdown header. An agent skill is available at /.well-known/agent-skills/site-skill.md.
FrakmentFrakment

Slot

Merges attributes onto a single child without adding a DOM wrapper.

Web
Frakment
slot.tsx
"use client";

import * as React from "react";

export interface SlotProps extends React.HTMLAttributes<HTMLElement> {
  children?: React.ReactNode;
  ref?: React.Ref<HTMLElement>;
}

function setRef(
  ref: React.Ref<HTMLElement> | undefined,
  node: HTMLElement | null
) {
  if (!ref) {
    return;
  }
  if (typeof ref === "function") {
    ref(node);
  } else {
    (ref as React.MutableRefObject<HTMLElement | null>).current = node;
  }
}

function mergeRefs(...refs: Array<React.Ref<HTMLElement> | undefined>) {
  return (node: HTMLElement | null) => {
    for (const ref of refs) {
      setRef(ref, node);
    }
  };
}

function Slot({ children, ref, ...slotProps }: SlotProps) {
  if (!React.isValidElement(children)) {
    return null;
  }
  const childProps = children.props as SlotProps;
  const merged: Record<string, unknown> = { ...slotProps, ...childProps };
  for (const key of Object.keys(slotProps)) {
    const slotValue = (slotProps as Record<string, unknown>)[key];
    const childValue = (childProps as Record<string, unknown>)[key];
    if (typeof slotValue === "function" && typeof childValue === "function") {
      merged[key] = (...args: unknown[]) => {
        (childValue as (...a: unknown[]) => unknown)(...args);
        (slotValue as (...a: unknown[]) => unknown)(...args);
      };
    }
  }
  merged.className =
    [slotProps.className, childProps.className].filter(Boolean).join(" ") ||
    undefined;
  merged.style = { ...slotProps.style, ...childProps.style };
  merged.ref = mergeRefs(ref, childProps.ref);
  return React.cloneElement(
    children as React.ReactElement<Record<string, unknown>>,
    merged
  );
}

export { Slot };

Slot clones one child element and merges its attributes onto that element, allowing a wrapper to add styles and behavior without adding another DOM node.

Installation

Usage

slot-demo.tsx
"use client";

import { Slot } from "@/components/ui/slot";

export function SlotDemo() {
  return (
    <Slot className="bg-primary text-primary-foreground inline-flex h-9 items-center rounded-md px-4 text-sm font-medium">
      <a href="/docs">Open documentation</a>
    </Slot>
  );
}

Pass one element as children; Slot applies its attributes to that element.

tsx
import { Slot } from "@/components/ui/slot";
 
<Slot className="rounded-md bg-primary px-4 py-2 text-primary-foreground">
  <a href="/docs/getting-started">Open documentation</a>
</Slot>

API

Slot

Slot accepts standard React.HTMLAttributes<HTMLElement> and a single React element as children.

PropTypeDefaultDescription
childrenReact.ReactNode—A valid React element to clone; Slot renders null when it is not valid.
classNamestring—Combines the Slot class before the child's class.
styleReact.CSSProperties—Merges styles, with the child's values taking precedence.
refReact.Ref<HTMLElement>—Merges the Slot ref with the child's ref.

Other child props override matching Slot props. When both elements provide the same event handler, the child's handler runs before the Slot handler.