use-active-element
Tracks the currently focused element (document.activeElement)
Source
Package
Usage
use-active-element returns the element that currently has focus
(document.activeElement).
The value updates every time focus moves to another element:
Focused element: null
Definition
Example: ignore shortcuts while typing
In the example below, / focuses the search field only when no input is focused:
Press / to focus the search field
Shadow DOM
When focus is inside a web component, the hook returns the element focused inside the shadow root.
Set deep: false to get the host element instead:
Trigger on removal
By default, the value is updated when the focused element is removed from the DOM.
Set triggerOnRemoval: false to disable this:
TypeScript
Pass a type argument to narrow the type of the returned element:
UseActiveElementOptions type is exported from the @mantine/hooks package, you can import it
in your application: