Skip to content

Commit 9b8b98d

Browse files
add resizeHandler prop
1 parent 05947dc commit 9b8b98d

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/factory.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default function plotComponentFactory(Plotly) {
5555
super(props);
5656

5757
this.p = Promise.resolve();
58-
this.resizeHandler = null;
58+
this.fitHandler = null;
5959
this.handlers = {};
6060

6161
this.syncWindowResize = this.syncWindowResize.bind(this);
@@ -134,9 +134,9 @@ export default function plotComponentFactory(Plotly) {
134134
if (this.props.onPurge) {
135135
this.props.onPurge(this.el);
136136
}
137-
if (this.resizeHandler && isBrowser) {
137+
if (this.fitHandler && isBrowser) {
138138
window.removeEventListener('resize', this.handleResize);
139-
this.resizeHandler = null;
139+
this.fitHandler = null;
140140
}
141141

142142
this.removeUpdateEvents();
@@ -175,14 +175,24 @@ export default function plotComponentFactory(Plotly) {
175175
props = props || this.props;
176176
if (!isBrowser) return;
177177

178-
if (props.fit && !this.resizeHandler) {
179-
this.resizeHandler = () => {
178+
if (props.fit && !this.fitHandler) {
179+
this.fitHandler = () => {
180180
return Plotly.relayout(this.el, this.getSize());
181181
};
182-
window.addEventListener('resize', this.resizeHandler);
182+
window.addEventListener('resize', this.fitHandler);
183+
184+
if (invoke) return this.fitHandler();
185+
} else if (!props.fit && this.fitHandler) {
186+
window.removeEventListener('resize', this.fitHandler);
187+
this.fitHandler = null;
188+
}
183189

184-
if (invoke) return this.resizeHandler();
185-
} else if (!props.fit && this.resizeHandler) {
190+
if (props.useResizeHandler && !this.resizeHandler) {
191+
this.resizeHandler = () => {
192+
return Plotly.Plots.resize(this.el);
193+
};
194+
window.addEventListener('resize', this.resizeHandler);
195+
} else if (!props.useResizeHandler && this.resizeHandler) {
186196
window.removeEventListener('resize', this.resizeHandler);
187197
this.resizeHandler = null;
188198
}
@@ -269,6 +279,7 @@ export default function plotComponentFactory(Plotly) {
269279
onUpdate: PropTypes.func,
270280
debug: PropTypes.bool,
271281
style: PropTypes.object,
282+
useResizeHandler: PropTypes.bool,
272283
};
273284

274285
for (let i = 0; i < eventNames.length; i++) {
@@ -278,6 +289,7 @@ export default function plotComponentFactory(Plotly) {
278289
PlotlyComponent.defaultProps = {
279290
debug: false,
280291
fit: false,
292+
useResizeHandler: false,
281293
data: [],
282294
style: {position: 'relative', display: 'inline-block'},
283295
};

0 commit comments

Comments
 (0)