Shadcn Hooks

useActiveElement

A hook to track the currently focused element in the document

Loading...

Installation

npx shadcn@latest add @shadcnhooks/use-active-element

API

export interface UseActiveElementOptions {
  /**
   * Search active element deeply inside shadow dom.
   * @default true
   */
  deep?: boolean
  /**
   * Track active element when it's removed from the DOM
   * Using a MutationObserver under the hood
   * @default false
   */
  triggerOnRemoval?: boolean
}

export function useActiveElement<T extends Element = HTMLElement>(
  options?: UseActiveElementOptions,
): T | null

Credits

Last updated on

On this page