Hooks

useFocusManagement

Intended for modal-like surfaces together with scroll locking from the same hook.

Import

import { useFocusManagement } from "@zentauri-ui/zentauri-components/lib/useFocusManagement";

Demo source

Full client component used for this preview (same JSX as below). Adjust paths if you copy outside this app.

"use client";

import { useFocusManagement } from "@zentauri-ui/zentauri-components/lib/useFocusManagement";
import { Button } from "@zentauri-ui/zentauri-components/ui/buttons";
import { HookDemoPanel } from "@/components/preview/hooks/hook-demo-panel";
import { useRef, useState } from "react";

export default function HookDemo() {
  const [open, setOpen] = useState(false);
  const contentRef = useRef<HTMLDivElement>(null);
  useFocusManagement({ open, setOpen, contentRef });

  return (
    <HookDemoPanel title="Interactive demo">
      <p className="mb-4 text-sm text-slate-400">
        Opens a focus-managed region with Escape to close. Tab should stay inside
        while open.
      </p>
      <Button type="button" onClick={() => setOpen(true)}>
        Open dialog
      </Button>
      {open ? (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 p-4" role="presentation">
          <div
            ref={contentRef}
            role="dialog"
            aria-modal="true"
            aria-label="Focus trap demo"
            tabIndex={-1}
            className="max-w-md rounded-2xl border border-white/10 bg-slate-900 p-6 shadow-2xl outline-none"
          >
            <p className="mb-4 text-sm text-slate-300">
              Try Tab through these controls, then Escape.
            </p>
            <div className="flex flex-col gap-3">
              <Button type="button">First action</Button>
              <Button type="button" appearance="outline">
                Second action
              </Button>
              <Button type="button" appearance="outline" onClick={() => setOpen(false)}>
                Close
              </Button>
            </div>
          </div>
        </div>
      ) : null}
    </HookDemoPanel>
  );
}

Interactive demo

Opens a focus-managed region with Escape to close. Tab should stay inside while open.

What it does

useFocusManagement ships as a small client module you can import from @zentauri-ui/zentauri-components/lib/useFocusManagement. Use it when the behavior matches your integration without copying utility code.

API notes

Read the interactive demo on this page for the parameters used in typical flows. Refer to TypeScript types exported next to the hook for exhaustive options.

Common use cases

  • Reuse the same hook in client components across Next.js App Router routes.
  • Keep server components free of browser APIs; colocate interactive demos in client files.
  • Compose hooks with Zentauri UI primitives for overlays, forms, and data views.
  • Align documentation with published @zentauri-ui/zentauri-components/lib/* paths.

Accessibility

Hooks manipulate behavior, not roles. Pair them with appropriate elements, ARIA attributes, and keyboard handlers so assistive technologies receive a coherent experience.

Next.js integration notes

Keep interactive subtrees in client components. When you only need static copy, leave it in the server layout and pass children into a client boundary that calls the hook.

FAQ

Can I import this hook from a server component?

No. These hooks rely on browser APIs or React client lifecycle. Import them from a file marked with the "use client" directive or from a client child component.

Does the preview site bundle the same code as npm?

The component library app depends on the workspace package. Published builds resolve the same export paths under @zentauri-ui/zentauri-components.

Where should I validate accessibility for hook-driven UI?

Hooks do not replace semantics. Test focus order, labels, and keyboard flows in your real layout with assistive technologies you support.