Skip to content

useSize ​

Hook that observes and returns the width and height of element

elements
low
test coverage
Last changed: 34 minutes ago

Installation ​

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

Usage ​

typescript
const { value, observer } = useSize(ref);
// or
const { ref, value, observer } = useSize();

Demo ​

Api ​

Parameters

NameTypeDefaultNote
targetHookTarget-The target element to observe

Returns

UseSizeReturn

Returns

{ ref: StateRef<Target> } & UseSizeReturn

Type declaration ​

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

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

export interface UseSizeValue {
  /** The element's height */
  height: number;
  /** The element's width */
  width: number;
}

export interface UseSizeReturn {
  /** The resize observer instance */
  observer?: ResizeObserver;
  /** The current size value */
  value: UseSizeValue;
}

export interface UseSize {
  (target: HookTarget): UseSizeReturn;

  <Target extends Element>(
    target?: never
  ): {
    ref: StateRef<Target>;
  } & UseSizeReturn;
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
A
Alexander Lee
Alexander Lee
B
babin
babin
V
VLADISLAW9
VLADISLAW9

Released under the MIT License.