Skip to content

Commit 47e7606

Browse files
committed
Handle window resize when responsive
1 parent 54f5cd5 commit 47e7606

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

components/dash-core-components/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"no-extra-bind": ["error"],
6464
"no-extra-boolean-cast": ["error"],
6565
"no-inline-comments": ["off"],
66-
"no-implicit-coercion": ["error"],
66+
"no-implicit-coercion": ["off"],
6767
"no-implied-eval": ["error"],
6868
"no-inner-declarations": ["off"],
6969
"no-invalid-this": ["error"],

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class PlotlyGraph extends Component {
148148
this._prevGd = null;
149149
this._queue = Promise.resolve();
150150

151+
this.parentElement = React.createRef();
152+
151153
this.bindEvents = this.bindEvents.bind(this);
152154
this.getConfig = this.getConfig.bind(this);
153155
this.getConfigOverride = this.getConfigOverride.bind(this);
@@ -514,7 +516,7 @@ class PlotlyGraph extends Component {
514516
}
515517

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

519521
return (
520522
<div
@@ -525,8 +527,13 @@ class PlotlyGraph extends Component {
525527
}
526528
className={className}
527529
style={style}
530+
ref={this.parentElement}
528531
>
529-
<ResizeDetector onResize={this.graphResize} />
532+
<ResizeDetector
533+
onResize={this.graphResize}
534+
targets={[this.parentElement, this.gd]}
535+
autosize={!!responsive}
536+
/>
530537
<div ref={this.gd} style={{height: '100%', width: '100%'}} />
531538
</div>
532539
);

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, {createRef, useEffect, useMemo} from 'react';
22
import PropTypes from 'prop-types';
33

44
const ResizeDetector = props => {
5-
const {onResize, children} = props;
5+
const {onResize, autosize, children, targets} = props;
66
const ref = createRef();
77

88
const observer = useMemo(
@@ -17,18 +17,35 @@ const ResizeDetector = props => {
1717
if (!ref.current) {
1818
return () => {};
1919
}
20+
targets.forEach(target => observer.observe(target.current));
2021
observer.observe(ref.current);
22+
const windowResizedHandled = -1;
23+
// if (autosize) {
24+
// windowResizedHandled = window.addEventListener(
25+
// 'resize',
26+
// onResize
27+
// );
28+
// }
2129
return () => {
2230
observer.disconnect();
31+
if (autosize) {
32+
window.removeEventListener(windowResizedHandled);
33+
}
2334
};
2435
}, [ref.current]);
2536

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

2944
ResizeDetector.propTypes = {
45+
autosize: PropTypes.bool,
3046
onResize: PropTypes.func,
3147
children: PropTypes.node,
48+
targets: PropTypes.any,
3249
};
3350

3451
export default ResizeDetector;

0 commit comments

Comments
 (0)