Skip to main content
cngx-src documentation

createResizeSignal

Functioncommon/layout/observersv0.1.0

projects/common/layout/observers/resize-signal.ts

Description#

Creates a Signal<ResizeObserverEntry | null> that carries the most recent observation of element, null until the first one arrives.

Disconnects when destroyRef is destroyed. On a host without ResizeObserver (SSR, jsdom) the signal stays null and no observer is created, so it never throws off the browser.

This is the shared kernel behind CngxResizeObserver and createContainerSize; consumers that hand-roll a ResizeObserver should route through it instead.

const entry = createResizeSignal(
  host,
  'border-box',
  inject(DestroyRef),
  inject(DOCUMENT).defaultView,
);
const width = computed(() => entry()?.borderBoxSize[0]?.inlineSize ?? 0);

Signature#

createResizeSignal(element: Element, box: ResizeObserverBoxOptions, destroyRef: DestroyRef, host)

Parameters#

@paramelementElement

The element to observe.

@paramboxResizeObserverBoxOptions

Which box model to report.

@paramdestroyRefDestroyRef

Scope whose destruction disconnects the observer.

@paramhost

The window-like object to read ResizeObserver from.

Returns#

Signal

A readonly signal carrying the latest entry.