Shadcn Hooks

useElementSize

A hook to track an element's width and height

Loading...

Installation

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

API

export interface ElementSize {
  width: number
  height: number
}

export interface UseElementSizeOptions {
  /**
   * ResizeObserver box model to use for measuring.
   *
   * @default 'content-box'
   */
  box?: ResizeObserverBoxOptions
}

/**
 * A hook to track an element's width and height.
 *
 * @param target - The target element to observe
 * @param initialSize - Initial width and height
 * @param options - Measurement options
 * @returns Current element size
 */
export function useElementSize(
  target: BasicTarget<Element>,
  initialSize?: ElementSize,
  options?: UseElementSizeOptions,
): ElementSize

Credits

Last updated on

On this page