Skip to content

useActiveElement

Hook that returns the active element

elements
low
test coverage
Last changed: 24 days ago

Installation

Library
CLI
Manual
typescript
import { useActiveElement } from '@siberiacancode/reactuse';

Usage

typescript
const activeElement = useActiveElement(ref);
// or
const { ref, value } = useActiveElement();
## Demo

Api

Parameters

NameTypeDefaultNote
target?HookTargetwindowThe target element to observe active element changes

Returns

ActiveElement | null

Returns

{ ref: StateRef<Element>; activeElement: ActiveElement | null }

Type declaration

typescript
import type { HookTarget } from '@/utils/helpers';

import type { StateRef } from '../useRefState/useRefState';

export interface UseActiveElementReturn<ActiveElement extends HTMLElement = HTMLElement> {
  value: ActiveElement | null;
}

export interface UseActiveElement {
  (): UseActiveElementReturn;

  <Target extends Element, ActiveElement extends HTMLElement = HTMLElement>(
    target?: never
  ): {
    ref: StateRef<Target>;
  } & UseActiveElementReturn<ActiveElement>;

  <ActiveElement extends HTMLElement = HTMLElement>(
    target: HookTarget
  ): UseActiveElementReturn<ActiveElement>;
}

Source

SourceDemo

Contributors

D
debabin
debabin
B
babin
babin

Released under the MIT License.