blakeblackshear.frigate/web/src/hooks/resize-observer.ts
Josh Hawkins ed99be0856
Live camera aspect ratio fixes (#10266)
* dynamically manage aspect ratio

* full size

* always use camera aspect ratio for mobile

* no need for different handling for pano cams

* don't set aspect ratio on fullscreen
2024-03-05 13:19:56 -07:00

48 lines
1.2 KiB
TypeScript

import { MutableRefObject, useEffect, useMemo, useState } from "react";
type RefType = MutableRefObject<Element | null> | Window;
export function useResizeObserver(...refs: RefType[]) {
const [dimensions, setDimensions] = useState<
{ width: number; height: number; x: number; y: number }[]
>(
new Array(refs.length).fill({
width: 0,
height: 0,
x: -Infinity,
y: -Infinity,
}),
);
const resizeObserver = useMemo(
() =>
new ResizeObserver((entries) => {
window.requestAnimationFrame(() => {
setDimensions(entries.map((entry) => entry.contentRect));
});
}),
[],
);
useEffect(() => {
refs.forEach((ref) => {
if (ref instanceof Window) {
resizeObserver.observe(document.body);
} else if (ref.current) {
resizeObserver.observe(ref.current);
}
});
return () => {
refs.forEach((ref) => {
if (ref instanceof Window) {
resizeObserver.unobserve(document.body);
} else if (ref.current) {
resizeObserver.unobserve(ref.current);
}
});
};
}, [refs, resizeObserver]);
return dimensions;
}