pota/use/resizepota/use/resize exposes document-level and element-level resize
behind one use* / on* Emitter pattern. Document size tracks the
window resize event; element size is backed by a ResizeObserver.
Each pair gives you a reactive accessor (use*) and a plain callback
registration (on*), and the module also ships two use:ref helpers
— the resize(handler) factory for per-element callbacks and
ensureInBounds, a ref used directly to
clamp a floating element to the viewport.
The module's own resize export is documented inline below.
resize(handler) — concise use:ref element-resize factory
(documented below)documentSize() — { width, height }
viewport snapshotonDocumentSize(fn) — callback on
viewport resizeuseDocumentSize() — reactive
viewport size accessoruseElementSize(node) — reactive
element size accessoronElementSize(node, fn) — callback on
element resizeensureInBounds — ref: clamp an
element to the viewportresize(handler) returns a use:ref factory.
| Argument | Type | Description |
|---|---|---|
handler |
(entry: ResizeObserverEntry) => void |
Called with the latest entry whenever the element resizes. |
Returns: a ref function (node: Element) => void for use:ref.
Multiple subscribers on the same node share a single ResizeObserver.
The most concise form: attach with use:ref and the handler receives
each ResizeObserverEntry. Drag the box corner to see the reported
size update.
import { render, signal } from 'pota'
import { resize } from 'pota/use/resize'
function App() {
const size = signal({ width: 0, height: 0 })
return (
<div
use:ref={resize(entry => {
size.write({
width: Math.round(entry.contentRect.width),
height: Math.round(entry.contentRect.height),
})
})}
style={{
resize: 'both',
overflow: 'auto',
width: '300px',
height: '200px',
padding: '1rem',
border: '1px solid #aaa',
}}
>
drag the bottom-right corner to resize. current:{' '}
<mark>
{() => size.read().width}×{() => size.read().height}
</mark>
</div>
)
}
render(App)