Original fishing-dog mascot for jaspr_hooks jaspr_hooks

useActiveElement

Track the browser document's active element.

useActiveElement returns the latest document.activeElement and rebuilds after focus transitions.

Signature

web.Element? useActiveElement();

Lifecycle

final active = useActiveElement();
final tag = active?.tagName.toLowerCase();
Interactive useActiveElement demo
Active element: none

One document-level subscription is owned by the hook and removed on unmount.

Server rendering

The result is null during SSR and the first hydration build.

Common mistakes

Do not retain the returned element after a rebuild. Shadow DOM focus may surface the host rather than a deeply focused descendant.

Use useFocus for one element and useFocusWithin for a subtree.