Skip to content

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 Parameter Default type Description
E extends HTMLElement HTMLDivElement Element type, defaults to HTMLDivElement

object

Object containing:

  • ref — React ref to attach to the element
  • onOpenAutoFocus — Event handler to prevent default focus behavior
  • tabIndex — -1 to 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
import { usePreventAutoFocus } from "@xtandard/lib/react";
function MyDialog() {
const preventAutoFocus = usePreventAutoFocus();
return (
<DialogContent {...preventAutoFocus}>
<p>Dialog content here</p>
</DialogContent>
);
}