Skip to content

useActiveElement ​

Hook that returns the active element

elements
test coverage
Last changed: 2 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 UseActiveElement {
  (): HTMLElement | null;

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

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

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
B
babin
babin

Released under the MIT License.