Skip to content

useFocusTrap

useFocusTrap(__namedParameters): (event) => void

Defined in: src/hooks/useFocusTrap/useFocusTrap.ts:41

Trap focus within a container element. Tab and Shift+Tab cycle through the focusable descendants without escaping the container.

Returns a keyboard event handler to attach to the container’s onKeyDown.

Parameters

__namedParameters

UseFocusTrapOptions

Returns

(event): void

Parameters

event

KeyboardEvent

Returns

void

Example

const ref = useRef<HTMLDivElement>(null);
const handleKeyDown = useFocusTrap({ containerRef: ref, enabled: isOpen });
return <div ref={ref} onKeyDown={handleKeyDown}>...</div>;