apps/mantine.dev/src/pages/core/focus-trap.mdx
import { FocusTrapDemos, ModalDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.FocusTrap);
FocusTrap is a component implementation of the use-focus-trap hook. It is used in all Mantine components that require focus trap (Modal, DatePicker, Popover, etc.).
<Demo data={FocusTrapDemos.usage} />To define the element that will receive initial focus, set the data-autofocus attribute:
FocusTrap.InitialFocus is a special component that adds a visually hidden
element which will receive the focus when the focus trap is activated.
Once FocusTrap.InitialFocus loses focus, it is removed from the tab order.
For example, it is useful if you do not want to focus any elements inside the Modal when it is opened:
<Demo data={ModalDemos.initialFocusTrap} />active prop is trueactive prop changes from false to true, the first element with the data-autofocus attribute is focuseddata-autofocus attribute, then the first element that supports keyboard interaction is focusedref, then the focus trap will not workFocusTrap child is focused