MCPcopy
hub / github.com/plotly/dash / ResizeDetector

Function ResizeDetector

components/dash-core-components/src/utils/ResizeDetector.js:7–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5const DELAY = 50;
6
7const ResizeDetector = props => {
8 const {onResize, children, targets} = props;
9 const ref = createRef();
10 let resizeTimeout;
11
12 const debouncedResizeHandler = useCallback(() => {
13 if (resizeTimeout) {
14 clearTimeout(resizeTimeout);
15 }
16 resizeTimeout = setTimeout(() => {
17 onResize(true); // Force on resize.
18 }, DELAY);
19 }, [onResize]);
20
21 const observer = useMemo(
22 () => new ResizeObserver(debouncedResizeHandler),
23 [onResize]
24 );
25
26 useEffect(() => {
27 if (!ref.current) {
28 return () => {};
29 }
30 targets.forEach(target => observer.observe(target.current));
31 observer.observe(ref.current);
32 return () => {
33 observer.disconnect();
34 };
35 }, [ref.current]);
36
37 return <div ref={ref}>{children}</div>;
38};
39
40ResizeDetector.propTypes = {
41 onResize: PropTypes.func,

Callers

nothing calls this directly

Calls 5

createRefFunction · 0.90
useCallbackFunction · 0.90
useMemoFunction · 0.90
useEffectFunction · 0.90
disconnectMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…