Skip to main content
FocusTrap
@coinbase/cds-web@8.38.0
FocusTrap is a component that traps focus within its children.
Import
import { FocusTrap } from '@coinbase/cds-web/overlays/FocusTrap'
SourceView source code
Related components
View as Markdown
Before using FocusTrap

<FocusTrap> is intended to prevent keyboard users from interacting with elements on the page that a mouse user cannot interact with either. An example of this is <Modal> where the user cannot click the items behind the modal. If you want to shift focus based on UI events, consider using the .focus() method instead.

Accessibility

It is required that when using a <FocusTrap> there is logic using only key presses to escape the focus trap. Otherwise, keyboard users will be blocked after they enter a <FocusTrap>.

Basic example

Note

All the examples have controls to enable / disable the focus trap so that page keyboard navigation isn't blocked.

When enabled, only the children of the <FocusTrap> will be able to receive focus. Otherwise, standard DOM focus order follows.

Loading...

Include trigger in FocusTrap

The includeTriggerInFocusTrap prop includes the triggering element causing the <FocusTrap> to render as part of the focus order.

Loading...

Restore focus on unmount

The restoreFocusOnUnmount prop returns focus to the triggering element when the <FocusTrap> is unmounted.

Loading...

Is this page useful?

Coinbase Design is an open-source, adaptable system of guidelines, components, and tools that aid the best practices of user interface design for crypto products.