Knit UI
GitHub

Playground

Loading playground…

true
Playground
<FocusTrap
  active
  refProp="ref"
/>

Examples

Active

Active trap — Tab key stays within the wrapped region until active is set to false.

Loading example…

ActiveSourceStorybook

Inactive

Inactive trap — active={false} disables trapping; Tab moves freely through the page.

Loading example…

InactiveSourceStorybook

With Initial Focus

FocusTrap.InitialFocus — the hidden marker receives focus first; useful in modals and dialogs to prevent focus jumping to the first interactive element unexpectedly.

Loading example…

With Initial FocusSourceStorybook

Controlled Toggle

Controlled toggle — a button outside the trap enables/disables it at runtime, demonstrating the active prop change.

Loading example…

Controlled ToggleSourceStorybook

Modal Like

Modal-like overlay — demonstrates the canonical use-case: a dialog that traps focus while open and restores it on close.

Loading example…

Modal LikeSourceStorybook

Props

PropTypeDefaultDescription
children requiredReactElement<FocusTrapChildProps, string | JSXElementConstructor<any>>Single element child to trap focus within. Must accept a ref.
activeboolean | undefinedtrueWhen `false`, focus trapping is disabled and Tab behaves normally.
innerRefRef<TamaguiElement> | undefinedRef combined with the internal focus-trap ref.
refPropstring | undefined'ref'Prop name used to forward the internal ref into the child element.