Slot and Slottable
Slot clones a child element and merges slot props into it. It is useful for asChild composition where a wrapper component should pass behavior to the actual child element.
import { Slot } from '@ilokesto/utilinent';
function Button({ asChild, ...props }: React.ComponentProps<'button'> & { asChild?: boolean }) {
const Component = asChild ? Slot : 'button';
return <Component className="button" {...props} />;
}Prop merging
- Event handlers are chained: child handler runs first, then slot handler.
styleobjects are merged, with slot styles taking precedence.classNamestrings are joined.- Refs are composed when possible.
Children contract
Slot expects exactly one element child. Other inputs have defined results and warn in development so mistakes surface early; production builds stay silent.
| Children | Result |
|---|---|
| Single element | Props merge onto the child. |
| Multiple children | Only the first element receives the merged props; remaining children are dropped. Warns in development. |
| Text or number child | Renders null. Warns in development. |
null, undefined, or false | Renders null without a warning, so conditional children such as {open && <button />} stay valid. |
Dropping the remaining children is intentional: prop merging is defined for a single element, and Slot must keep returning that one element instead of introducing a wrapper. Render all children explicitly if you need them.
Slottable
Use Slottable when only one child inside a larger set should receive the merged props.
import { Slot, Slottable } from '@ilokesto/utilinent';
<Slot className="trigger">
<span>Prefix</span>
<Slottable><a href="/docs">Docs</a></Slottable>
</Slot>Slottable follows a sibling-preservation contract: the element inside Slottable receives the merged props while its siblings render unchanged. When the Slottable child is not a single valid element, the slottable position renders nothing, its siblings are still rendered, and development warns.
Ref behavior
The forwarded ref and the child's own ref are composed into a single callback ref. The composed callback keeps its identity while both inputs keep theirs, so rerenders with stable refs do not detach and re-attach refs or re-run cleanup. When either ref changes, the previous ref detaches exactly once and the new ref attaches once. Callback refs returning a cleanup function (React 19) are supported, and the cleanup runs exactly once on unmount.
Keep ref identities stable — for example with useRef or a memoized callback — so ref callbacks do not re-run on every render.