mirror of
https://github.com/block/buzz.git
synced 2026-11-09 02:41:57 +00:00
Signed-off-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
32 lines
836 B
TypeScript
32 lines
836 B
TypeScript
/**
|
|
* Reads the block size from a ResizeObserver entry, preferring border-box size
|
|
* when available.
|
|
*/
|
|
export function readElementBlockSize(entry: ResizeObserverEntry): number {
|
|
return entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;
|
|
}
|
|
|
|
/**
|
|
* Invokes `onSize` immediately and whenever the observed element's block size
|
|
* changes. Returns a cleanup function that disconnects the observer.
|
|
*/
|
|
export function observeElementBlockSize(
|
|
element: HTMLElement,
|
|
onSize: (size: number) => void,
|
|
): () => void {
|
|
onSize(element.getBoundingClientRect().height);
|
|
|
|
if (typeof ResizeObserver === "undefined") {
|
|
return () => {};
|
|
}
|
|
|
|
const observer = new ResizeObserver(([entry]) => {
|
|
onSize(readElementBlockSize(entry));
|
|
});
|
|
observer.observe(element);
|
|
|
|
return () => {
|
|
observer.disconnect();
|
|
};
|
|
}
|