Files
wehub-resource-sync 2114b14ee0
Sync main into demo / sync (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:26 +08:00

28 lines
712 B
TypeScript

import { useEffect, useState, type RefObject } from 'react';
export function useElementHeight(ref: RefObject<HTMLElement | null>, initial = 0): number {
const [height, setHeight] = useState(initial);
useEffect(() => {
const el = ref.current;
if (!el) return;
const update = () => {
setHeight(el.getBoundingClientRect().height);
};
update();
if (typeof ResizeObserver === 'undefined') {
window.addEventListener('resize', update);
return () => window.removeEventListener('resize', update);
}
const resizeObserver = new ResizeObserver(update);
resizeObserver.observe(el);
return () => resizeObserver.disconnect();
}, [ref]);
return height;
}