securityos/hooks/useResizeObserver.ts

29 lines
728 B
TypeScript
Raw Permalink Normal View History

2024-09-06 15:32:35 +00:00
import { useEffect, useState } from "react";
const useResizeObserver = (
componentWindow?: HTMLElement | null,
callback?: ResizeObserverCallback
): void => {
const [resizeObserver, setResizeObserver] = useState<ResizeObserver>();
useEffect(() => {
if (callback) {
setResizeObserver(new ResizeObserver(callback));
}
}, [callback]);
useEffect(() => {
if (componentWindow instanceof HTMLElement) {
resizeObserver?.observe(componentWindow);
}
return () => {
if (componentWindow instanceof HTMLElement) {
resizeObserver?.unobserve(componentWindow);
}
};
}, [componentWindow, resizeObserver]);
};
export default useResizeObserver;