Skip to main content

useElementBounding

React Element Hook that tracks bounding box of an HTML element

Usage

Live Editor
function Demo() {
  const ref = useRef<HTMLTextAreaElement>(null);
  const rect = useElementBounding(ref);
  return (
    <div>
      <p> Resize the box to see changes</p>
      <textarea ref={ref} readOnly value={JSON.stringify(rect, null, 2)} />
    </div>
  );
};

Result
Loading...

API

useElementBounding

Returns

UseElementBoundingReturn

Arguments

ArgumentDescriptionTypeDefaultValue
targettarget elementBasicTarget<Element> (Required)-
optionsoptional paramsUseElementBoundingOptions | undefined-

UseElementBoundingOptions

PropertyDescriptionTypeDefaultValue
resetReset values to 0 on component unmountedbooleantrue
windowResizeListen to window resize eventbooleantrue
windowScrollListen to window scroll eventbooleantrue
immediateImmediately call update on component mountedboolean-

UseElementBoundingReturn

PropertyDescriptionTypeDefaultValue
heightHeight of the elementnumber (Required)-
bottomBottom position of the elementnumber (Required)-
leftLeft position of the elementnumber (Required)-
rightRight position of the elementnumber (Required)-
topTop position of the elementnumber (Required)-
widthWidth of the elementnumber (Required)-
xX position of the elementnumber (Required)-
yY position of the elementnumber (Required)-
updateManual update() => void (Required)-

BasicTarget

export type BasicTarget<T extends TargetType = Element> = (() => TargetValue<T>) | TargetValue<T> | MutableRefObject<TargetValue<T>>;

TargetValue

type TargetValue<T> = T | undefined | null;

TargetType

type TargetType = HTMLElement | Element | Window | Document | EventTarget;