1
0
Fork 0
react-playground/node_modules/@mui/x-internals/esm/useResizeObserver/useResizeObserver.js
Techognito fc0f69dacb Added Statistics calculation
Statistics now show calculated values
2025-09-04 17:30:00 +02:00

36 lines
No EOL
1.1 KiB
JavaScript

import * as React from 'react';
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
const isDevEnvironment = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
const noop = () => {};
export function useResizeObserver(ref, fn, enabled) {
const fnRef = React.useRef(null);
fnRef.current = fn;
useEnhancedEffect(() => {
if (enabled === false || typeof ResizeObserver === 'undefined') {
return noop;
}
let frameID = 0;
const target = ref.current;
const observer = new ResizeObserver(entries => {
// See https://github.com/mui/mui-x/issues/8733
// In dev, we avoid the React warning by moving the task to the next frame.
// In prod, we want the task to run in the same frame as to avoid tear.
if (isDevEnvironment) {
frameID = requestAnimationFrame(() => {
fnRef.current(entries);
});
} else {
fnRef.current(entries);
}
});
if (target) {
observer.observe(target);
}
return () => {
if (frameID) {
cancelAnimationFrame(frameID);
}
observer.disconnect();
};
}, [ref, enabled]);
}