apps/mantine.dev/src/pages/hooks/use-focus-trap.mdx
import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useFocusTrap);
The use-focus-trap hook traps focus at the given node, for example in a modal, drawer, or menu.
The node must include at least one focusable element. When the node unmounts, the focus trap is automatically released.
import { useFocusTrap } from '@mantine/hooks';
function Demo() {
const focusTrapRef = useFocusTrap();
return (
<div ref={focusTrapRef}>
<input />
</div>
);
}
The hook accepts focus trap active state as a single argument:
import { useFocusTrap } from '@mantine/hooks';
useFocusTrap(); // -> focus trap inactive
useFocusTrap(true); // -> focus trap active
useFocusTrap(false); // -> focus trap disabled
The hook returns ref that should be passed to the element:
import { Paper } from '@mantine/core';
import { useFocusTrap } from '@mantine/hooks';
function Demo() {
const focusTrapRef = useFocusTrap();
return (
<>
<div ref={focusTrapRef} />
<Paper ref={focusTrapRef} />
</>
);
}
To combine use-focus-trap with other ref-based hooks, use the use-merged-ref hook:
import { useRef } from 'react';
import {
useClickOutside,
useFocusTrap,
useMergedRef,
} from '@mantine/hooks';
function Demo() {
const myRef = useRef();
const useClickOutsideRef = useClickOutside(() => {});
const focusTrapRef = useFocusTrap();
const mergedRef = useMergedRef(
myRef,
useClickOutsideRef,
focusTrapRef
);
return <div ref={mergedRef} />;
}
By default, the focus trap will move focus to the first interactive element.
To specify the element that should receive initial focus, add the data-autofocus attribute:
import { useFocusTrap } from '@mantine/hooks';
function Demo() {
const focusTrapRef = useFocusTrap();
return (
<div ref={focusTrapRef}>
<input />
<input data-autofocus />
<input />
</div>
);
}
function useFocusTrap(active?: boolean): React.RefCallback<HTMLElement | null>