Skip to content

Commit 577651b

Browse files
committed
Handle window resize when responsive
1 parent 47e7606 commit 577651b

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

components/dash-core-components/src/fragments/Graph.react.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class PlotlyGraph extends Component {
516516
}
517517

518518
render() {
519-
const {className, id, style, loading_state, responsive} = this.props;
519+
const {className, id, style, loading_state} = this.props;
520520

521521
return (
522522
<div
@@ -532,7 +532,6 @@ class PlotlyGraph extends Component {
532532
<ResizeDetector
533533
onResize={this.graphResize}
534534
targets={[this.parentElement, this.gd]}
535-
autosize={!!responsive}
536535
/>
537536
<div ref={this.gd} style={{height: '100%', width: '100%'}} />
538537
</div>

components/dash-core-components/src/utils/ResizeDetector.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import React, {createRef, useEffect, useMemo} from 'react';
1+
import React, {createRef, useEffect, useCallback, useMemo} from 'react';
22
import PropTypes from 'prop-types';
33

4+
// Debounce 50 ms
5+
const DELAY = 50;
6+
7+
let resizeTimeout;
8+
49
const ResizeDetector = props => {
5-
const {onResize, autosize, children, targets} = props;
10+
const {onResize, children, targets} = props;
611
const ref = createRef();
712

13+
const debouncedResizeHandler = useCallback(() => {
14+
if (resizeTimeout) {
15+
clearTimeout(resizeTimeout);
16+
}
17+
resizeTimeout = setTimeout(() => {
18+
onResize(true); // Force on resize.
19+
}, DELAY);
20+
}, [onResize]);
21+
822
const observer = useMemo(
9-
() =>
10-
new ResizeObserver(() => {
11-
onResize();
12-
}),
23+
() => new ResizeObserver(debouncedResizeHandler),
1324
[onResize]
1425
);
1526

@@ -19,30 +30,15 @@ const ResizeDetector = props => {
1930
}
2031
targets.forEach(target => observer.observe(target.current));
2132
observer.observe(ref.current);
22-
const windowResizedHandled = -1;
23-
// if (autosize) {
24-
// windowResizedHandled = window.addEventListener(
25-
// 'resize',
26-
// onResize
27-
// );
28-
// }
2933
return () => {
3034
observer.disconnect();
31-
if (autosize) {
32-
window.removeEventListener(windowResizedHandled);
33-
}
3435
};
3536
}, [ref.current]);
3637

37-
return (
38-
<div ref={ref} style={{width: '100%'}}>
39-
{children}
40-
</div>
41-
);
38+
return <div ref={ref}>{children}</div>;
4239
};
4340

4441
ResizeDetector.propTypes = {
45-
autosize: PropTypes.bool,
4642
onResize: PropTypes.func,
4743
children: PropTypes.node,
4844
targets: PropTypes.any,

0 commit comments

Comments
 (0)