Get Started
Advanced
Components
- Accordion
- Attachment
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Card
- Checkbox
- Code Block
- Collapsible
- Dialog
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Label
- Marker
- Message
- Number Flow
- Pagination
- Popover
- Progress
- Radio Group
- Select
- Slider
- Slot
- Sonner
- Spinner
- Switch
- Tooltip
- Separator
- Sheet
- Tabs
- Textarea
- Toggle
- Toggle Group
- Trace
AI Elements
Composites
Libraries
Utilities
"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
$ bunx --bun frakment add slot$ npx frakment add slot$ pnpm dlx frakment add slot$ yarn dlx frakment add slot
Copy and paste the following code into your project.
"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 };Update the import paths to match your project setup.
Usage
"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.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | — | A valid React element to clone; Slot renders null when it is not valid. |
className | string | — | Combines the Slot class before the child's class. |
style | React.CSSProperties | — | Merges styles, with the child's values taking precedence. |
ref | React.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.