FocusTrap API

API reference for the FocusTrap component: its props with their token values and defaults, the styled-system prop groups, and forwarded HTML attributes.

Demos

For usage examples, visit the component demo page: FocusTrap

Import

import { FocusTrap } from '@soroush.tech/design-system/FocusTrap'

Props

PropTypeDefaultDescription
childrenReactNode-Content whose focusable descendants are trapped.
isEnabledbooleantrueWhen false, focus is neither moved nor trapped (e.g. a non-top modal).
shouldAutoFocusbooleantrueMove focus into the trap when it activates.
shouldTrapFocusbooleantrueKeep Tab / Shift+Tab cycling within the trap.
shouldEnforceFocusbooleantruePull focus back inside whenever it escapes the trap (e.g. a click outside).
shouldRestoreFocusbooleantrueRestore focus to the previously focused element when the trap deactivates.

Example

<FocusTrap isEnabled={isOpen}>
  <div role="dialog" aria-modal="true">
    <button>Confirm</button>
    <button>Cancel</button>
  </div>
</FocusTrap>

Source code

If this page does not answer your question, have a look at the implementation of the component for more detail.