Websites v4 docs v12.7.0
    Preparing search index...

    Function useElementSize

    • A React hook that tracks the width and height of a DOM element using ResizeObserver.

      Unlike useWindowDimensions, this reflects the element's own size, so it matches container queries and works inside the admin page builder preview.

      Type Parameters

      • T extends HTMLElement

        The type of HTML element being observed, must extend HTMLElement

      Parameters

      • target: string | RefObject<T | null>

        A React ref object pointing to the element to track, or a class name string

      • options: UseElementSizeOptions = {}

        Optional configuration object

        • defaultWidth

          Initial width to use before the element is measured (defaults to 0)

        • defaultHeight

          Initial height to use before the element is measured (defaults to 0)

      Returns ElementSize

      The current width and height of the element in pixels, or the defaults if the element is not yet mounted

      const navRef = useRef<HTMLDivElement>(null);
      const { height: navHeight } = useElementSize(navRef, { defaultHeight: 230 });

      // or
      const { width } = useElementSize('js-my-class');