syncova-policies/components/ui/use-dialog-focus.ts
Weapie 265ca8ac38
All checks were successful
Container-Image bauen und veröffentlichen / build-and-push (push) Successful in 5m15s
Improve accessibility and access controls; verify production and database recovery
2026-09-14 11:44:41 +02:00

22 lines
774 B
TypeScript

"use client"
import { useRef } from "react"
export function useDialogFocus() {
const target = useRef<HTMLElement | null>(null)
return {
onOpenAutoFocus() {
const active = document.activeElement
const menuTrigger = document.querySelector<HTMLElement>('[data-slot="dropdown-menu-trigger"][data-state="open"]')
target.current = menuTrigger ?? (active instanceof HTMLElement ? active : null)
},
onCloseAutoFocus(event: Event) {
event.preventDefault()
requestAnimationFrame(() => {
const fallback = document.querySelector<HTMLElement>('[role="dialog"] button, main')
const next = target.current?.isConnected && target.current !== document.body ? target.current : fallback
next?.focus()
})
},
}
}