Overview

onResize attaches a ResizeObserver to the host element (or an optional target) and disconnects on unmount.

onScroll listens on a target (default window). onWindowScroll uses capture so nested scroll containers are visible. onWindowResize is the window resize event.

Resize & scroll APIs

API
Target
Notes
onResize(fn, target?) Host / element ResizeObserver; default target is the component host.
onWindowResize(fn) window Viewport resize — not element size.
onScroll(fn, target?) window / element Passive scroll listener; optional target.
onWindowScroll(fn) window (capture) Capture required because scroll does not bubble.

onResize — element size tracker

onResize attaches a ResizeObserver to the component's root element. The observer is automatically disconnected when the component unmounts. Try resizing the preview pane to see the values update.

import { html, atom, onResize, renderApp } from 'mates';

const ResizeTracker = () => {
const size = atom({ w: 0, h: 0 });

// Tracks the host element size via ResizeObserver; auto-disconnects on unmount
onResize((entry) => {
const { width, height } = entry.contentRect;
size.set({ w: Math.round(width), h: Math.round(height) });
});

return () => html`
<div class="m-col m-gap">
<h2 class="m-0">Resize the preview pane</h2>
<div class="m-grid m-grid-cols-3 m-gap">
<div class="card card-body center">
<span class="overline">Width</span>
<span class="card-title">${size().w}px</span>
</div>
<div class="card card-body center">
<span class="overline">Height</span>
<span class="card-title">${size().h}px</span>
</div>
</div>
</div>
`;
};
renderApp(ResizeTracker, document.getElementById('app'));