mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
More DRY code. Also move non-hooks to separate directories. BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint` BREAKING CHANGE: `comps` directory renamed to `component`
51 lines
972 B
TypeScript
51 lines
972 B
TypeScript
import { RefObject, useEffect, useRef, useState } from 'react';
|
|
import ResizeObserver from 'resize-observer-polyfill';
|
|
|
|
export interface ContentRect {
|
|
width: number;
|
|
height: number;
|
|
top: number;
|
|
right: number;
|
|
left: number;
|
|
bottom: number;
|
|
}
|
|
|
|
const useMeasureDirty = (ref: RefObject<HTMLElement>): ContentRect => {
|
|
const frame = useRef(0);
|
|
const [rect, set] = useState({
|
|
width: 0,
|
|
height: 0,
|
|
top: 0,
|
|
left: 0,
|
|
bottom: 0,
|
|
right: 0,
|
|
});
|
|
|
|
const [observer] = useState(
|
|
() =>
|
|
new ResizeObserver((entries) => {
|
|
const entry = entries[0];
|
|
|
|
if (entry) {
|
|
cancelAnimationFrame(frame.current);
|
|
|
|
frame.current = requestAnimationFrame(() => {
|
|
set(entry.contentRect);
|
|
});
|
|
}
|
|
})
|
|
);
|
|
|
|
useEffect(() => {
|
|
observer.disconnect();
|
|
|
|
if (ref.current) {
|
|
observer.observe(ref.current);
|
|
}
|
|
}, [ref]);
|
|
|
|
return rect;
|
|
};
|
|
|
|
export default useMeasureDirty;
|