feature/IO-3499-React-19: Move react-grid-gallery to a vendor directory internally, will be removed shortly but for now we keep it
This commit is contained in:
37
client/src/vendor/react-grid-gallery/useContainerWidth.js
vendored
Normal file
37
client/src/vendor/react-grid-gallery/useContainerWidth.js
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
|
||||
export function useContainerWidth(defaultContainerWidth) {
|
||||
const ref = useRef(null);
|
||||
const observerRef = useRef();
|
||||
|
||||
const [containerWidth, setContainerWidth] = useState(defaultContainerWidth);
|
||||
|
||||
const containerRef = useCallback((node) => {
|
||||
observerRef.current?.disconnect();
|
||||
observerRef.current = undefined;
|
||||
|
||||
ref.current = node;
|
||||
|
||||
const updateWidth = () => {
|
||||
if (!ref.current) {
|
||||
return;
|
||||
}
|
||||
let width = ref.current.clientWidth;
|
||||
try {
|
||||
width = ref.current.getBoundingClientRect().width;
|
||||
} catch {
|
||||
//
|
||||
}
|
||||
setContainerWidth(Math.floor(width));
|
||||
};
|
||||
|
||||
updateWidth();
|
||||
|
||||
if (node && typeof ResizeObserver !== "undefined") {
|
||||
observerRef.current = new ResizeObserver(updateWidth);
|
||||
observerRef.current.observe(node);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { containerRef, containerWidth };
|
||||
}
|
||||
Reference in New Issue
Block a user