Usage
FocusTrap is a component implementation of use-focus-trap hook, it is used in all Mantine components that require focus trap (Modal, DatePicker, Popover, etc.).
Initial focus
To define the element that will receive initial focus set data-autofocus
attribute:
Focus trapping logic
- Focus is trapped within child node if
active
prop istrue
- When FocusTrap component is mounted or when
active
prop changes fromfalse
totrue
first element withdata-autofocus
attribute is focused - If there are no elements with
data-autofocus
attribute, then the first element that supports keyboard interaction is focused - If the target element does not have focusable elements or does not support
ref
, then the focus trap will not work - Trap stops working when element outside of the
FocusTrap
child is focused