usePreventAutoFocus
function usePreventAutoFocus<E>(): object;Defined in: use-prevent-auto-focus.ts:28
Hook that prevents auto-focus behavior while still maintaining focus management. Useful for modal dialogs (e.g. Radix Dialog) where you want to control the initial focus.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
E extends HTMLElement |
HTMLDivElement |
Element type, defaults to HTMLDivElement |
Returns
Section titled “Returns”object
Object containing:
ref— React ref to attach to the elementonOpenAutoFocus— Event handler to prevent default focus behaviortabIndex—-1to make the element programmatically focusable
| Name | Type | Defined in |
|---|---|---|
onOpenAutoFocus() |
(event) => void |
use-prevent-auto-focus.ts:36 |
ref |
RefObject<E | null> |
use-prevent-auto-focus.ts:36 |
tabIndex |
-1 |
use-prevent-auto-focus.ts:36 |
Example
Section titled “Example”import { usePreventAutoFocus } from "@xtandard/lib/react";
function MyDialog() { const preventAutoFocus = usePreventAutoFocus();
return ( <DialogContent {...preventAutoFocus}> <p>Dialog content here</p> </DialogContent> );}