createComponentId

v0.10.4

·View as markdown·

Description

Call createComponentId() to create an id and pass it as componentId to the Root you want to identify. Pass the same id as forComponent to a descendant that should use that Root. For example, a <Sheet.Trigger> inside two nested sheets uses the nearest <Sheet.Root> by default. Pass the outer Root's id to target it instead.

Where the target Root must be

The Root with the matching componentId must be an ancestor of the component using forComponent in the React tree. This applies to a Trigger, Outlet or View, a <Sheet.Root> joining a SheetStack, and an <AutoFocusTarget.Root> targeting a specific sheet. A sibling of the target Root, or a component in a separate React root, can't target it this way. A React portal changes where the element appears in the DOM and preserves its React ancestry.

<Island.Root> can use a Sheet or SheetStack id from outside the target Root's React subtree.

Notes
  • To avoid any HMR issue createComponentId must always be called outside of component functions or classes, inside of a file dedicated to component ids and/or React contexts.

Example

This trigger opens the outer sheet because it uses loginSheetId and is inside that Root's React subtree.

TypeScript

import { Sheet, createComponentId } from "@silk-hq/components";

const loginSheetId = createComponentId();

<Sheet.Root componentId={loginSheetId}>
  ...
  <Sheet.Root>
    ...
    <Sheet.Trigger forComponent={loginSheetId}>Open login sheet</Sheet.Trigger>
    ...
  </Sheet.Root>
  ...
</Sheet.Root>;
createComponentId
All Examples