• Custom React hook that retrieves the offset size (width and height) of a referenced element.

    This hook listens for resize events on the specified element and updates the offsetWidth and offsetHeight states whenever the size of the element changes. The offset size includes padding, scrollbars, and borders, but excludes margins.

    Example

    const MyComponent = () => {
    const ref = useRef<HTMLDivElement>(null);
    const { offsetWidth, offsetHeight } = useOffsetSize(ref);

    return (
    <div ref={ref}>
    <p>Offset Width: {offsetWidth}px</p>
    <p>Offset Height: {offsetHeight}px</p>
    </div>
    );
    };

    Parameters

    • ref: THookEventElement<HTMLElement>

      A reference to the element whose offset size is to be measured.

    Returns {
        offsetHeight: number;
        offsetWidth: number;
    }

    • offsetHeight: number
    • offsetWidth: number