useElementSize
Tracks the width and height of a DOM element using the ResizeObserver API. Returns reactive Observable<number> values that update whenever the element resizes. SVG elements use getBoundingClientRect() as a fallback. Supports all three box models.
Resize the textarea to see width & height update.
import { const useRef$: { <T = any>(initialValue: null): Ref$<T | null>; <T = any>(initialValue: T): Ref$<T>; <T = any>(): Ref$<T | null>;}
useRef$ } from "@usels/core";import { const useElementSize: (target: MaybeEventTarget, initialSize?: { width: number; height: number;}, options?: DeepMaybeObservable<UseElementSizeOptions>) => UseElementSizeReturn
useElementSize } from "@usels/web";
function function Component(): JSX.Element
Component() { const const el$: Ref$<HTMLDivElement | null>
el$ = useRef$<HTMLDivElement>(): Ref$<HTMLDivElement | null> (+2 overloads)
Core (framework-agnostic) version of useRef$.
Creates an observable element ref with opaque wrapping.
- non-null value →
Ref$<T>: current, get(), peek() return T
- null / no arg →
Ref$<T | null>: current, get(), peek() return T | null
Nullability is expressed via the type parameter, mirroring T | null at the call site.
useRef$<interface HTMLDivElement
HTMLDivElement>(); const { const width$: any
width$, const height$: any
height$ } = function useElementSize(target: MaybeEventTarget, initialSize?: { width: number; height: number;}, options?: DeepMaybeObservable<UseElementSizeOptions>): UseElementSizeReturn
Framework-agnostic reactive element size tracker.
Tracks the width and height of a DOM element using ResizeObserver.
SVG elements use getBoundingClientRect() as fallback.
useElementSize(const el$: Ref$<HTMLDivElement | null>
el$);
return ( <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div RefAttributes<HTMLDivElement>.ref?: Ref<HTMLDivElement> | undefined
Allows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
ref={const el$: Ref$<HTMLDivElement | null>
el$}> {const width$: any
width$.any
get()} × {const height$: any
height$.any
get()} </JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> );}import { createRef$ } from "@usels/core";import { createElementSize } from "@usels/web";
function Component() { "use scope" const el$ = createRef$<HTMLDivElement>(); const { width$, height$ } = createElementSize(el$);
return ( <div ref={el$}> {width$.get()} × {height$.get()} </div> );}Custom initial size
Section titled “Custom initial size”useElementSize(el$, { width: 320, height: 240 });createElementSize(el$, { width: 320, height: 240 });With border-box
Section titled “With border-box”useElementSize(el$, undefined, { box: "border-box" });createElementSize(el$, undefined, { box: "border-box" });Stopping observation manually
Section titled “Stopping observation manually”const { width$, height$, stop } = useElementSize(el$);
stop();const { width$, height$, stop } = createElementSize(el$);
stop();Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
target | MaybeEventTarget | - Element to observe (Ref$, Observable element, Document, Window, or null). |
initialSize | { width: number; height: number; } | undefined (optional) | - Initial size values. Default { width: 0, height: 0 }. |
options | UseElementSizeOptions (optional) | - Optional box model option. |
UseElementSizeOptions
Section titled “UseElementSizeOptions”Returns
Section titled “Returns”UseElementSizeReturn
| Name | Type | Description |
|---|---|---|
width$ | ObservablePrimitive<number> | - |
height$ | ObservablePrimitive<number> | - |
stop | () => void | - |