useActiveElement

A React Hook that returns the currently active element (via document.activeElement), optionally traversing the shadow DOM.

Demo

Click on the input, button below or change with tab to see the active element change.

Source
Active element:
null

Usage

See API for more details.

Source

Click links below to view source on GitHub.

API

const activeElement = useActiveElement()

Returns

Element or null.

Initial value is null, and will be updated to Element when component is mounted or active element is changed.