Skip to content

Commit 705b184

Browse files
authored
Merge pull request #3972 from plotly/fix-3866
responsive handler: do not resize if gd is hidden
2 parents 2885f23 + 913bb68 commit 705b184

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/lib/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -1171,3 +1171,8 @@ lib.formatPercent = function(ratio, n) {
11711171
}
11721172
return str;
11731173
};
1174+
1175+
lib.isHidden = function(gd) {
1176+
var display = window.getComputedStyle(gd).display;
1177+
return !display || display === 'none';
1178+
};

src/plot_api/plot_api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function plot(gd, data, layout, config) {
182182
if(gd._context.responsive) {
183183
if(!gd._responsiveChartHandler) {
184184
// Keep a reference to the resize handler to purge it down the road
185-
gd._responsiveChartHandler = function() { Plots.resize(gd); };
185+
gd._responsiveChartHandler = function() { if(!Lib.isHidden(gd)) Plots.resize(gd); };
186186

187187
// Listen to window resize
188188
window.addEventListener('resize', gd._responsiveChartHandler);

src/plots/plots.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,15 @@ plots.resize = function(gd) {
7575
gd = Lib.getGraphDiv(gd);
7676

7777
return new Promise(function(resolve, reject) {
78-
function isHidden(gd) {
79-
var display = window.getComputedStyle(gd).display;
80-
return !display || display === 'none';
81-
}
82-
83-
if(!gd || isHidden(gd)) {
78+
if(!gd || Lib.isHidden(gd)) {
8479
reject(new Error('Resize must be passed a displayed plot div element.'));
8580
}
8681

8782
if(gd._redrawTimer) clearTimeout(gd._redrawTimer);
8883

8984
gd._redrawTimer = setTimeout(function() {
9085
// return if there is nothing to resize or is hidden
91-
if(!gd.layout || (gd.layout.width && gd.layout.height) || isHidden(gd)) {
86+
if(!gd.layout || (gd.layout.width && gd.layout.height) || Lib.isHidden(gd)) {
9287
resolve(gd);
9388
return;
9489
}

test/jasmine/tests/config_test.js

+17
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,23 @@ describe('config argument', function() {
771771
.catch(failTest)
772772
.then(done);
773773
});
774+
775+
it('should not resize if gd is hidden', function(done) {
776+
spyOn(Plotly.Plots, 'resize').and.callThrough();
777+
778+
fillParent(1, 1);
779+
Plotly.plot(gd, data, {}, {responsive: true})
780+
.then(function() {
781+
gd.style.display = 'none';
782+
viewport.set(width / 2, height / 2);
783+
})
784+
.then(delay(RESIZE_DELAY))
785+
.then(function() {
786+
expect(Plotly.Plots.resize.calls.count()).toBe(0);
787+
})
788+
.catch(failTest)
789+
.then(done);
790+
});
774791
});
775792
});
776793

0 commit comments

Comments
 (0)