Skip to content

responsive handler: do not resize if gd is hidden #3972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,8 @@ lib.formatPercent = function(ratio, n) {
}
return str;
};

lib.isHidden = function(gd) {
var display = window.getComputedStyle(gd).display;
return !display || display === 'none';
};
2 changes: 1 addition & 1 deletion src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function plot(gd, data, layout, config) {
if(gd._context.responsive) {
if(!gd._responsiveChartHandler) {
// Keep a reference to the resize handler to purge it down the road
gd._responsiveChartHandler = function() { Plots.resize(gd); };
gd._responsiveChartHandler = function() { if(!Lib.isHidden(gd)) Plots.resize(gd); };

// Listen to window resize
window.addEventListener('resize', gd._responsiveChartHandler);
Expand Down
9 changes: 2 additions & 7 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,15 @@ plots.resize = function(gd) {
gd = Lib.getGraphDiv(gd);

return new Promise(function(resolve, reject) {
function isHidden(gd) {
var display = window.getComputedStyle(gd).display;
return !display || display === 'none';
}

if(!gd || isHidden(gd)) {
if(!gd || Lib.isHidden(gd)) {
reject(new Error('Resize must be passed a displayed plot div element.'));
}

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

gd._redrawTimer = setTimeout(function() {
// return if there is nothing to resize or is hidden
if(!gd.layout || (gd.layout.width && gd.layout.height) || isHidden(gd)) {
if(!gd.layout || (gd.layout.width && gd.layout.height) || Lib.isHidden(gd)) {
resolve(gd);
return;
}
Expand Down
17 changes: 17 additions & 0 deletions test/jasmine/tests/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,23 @@ describe('config argument', function() {
.catch(failTest)
.then(done);
});

it('should not resize if gd is hidden', function(done) {
spyOn(Plotly.Plots, 'resize').and.callThrough();

fillParent(1, 1);
Plotly.plot(gd, data, {}, {responsive: true})
.then(function() {
gd.style.display = 'none';
viewport.set(width / 2, height / 2);
})
.then(delay(RESIZE_DELAY))
.then(function() {
expect(Plotly.Plots.resize.calls.count()).toBe(0);
})
.catch(failTest)
.then(done);
});
});
});

Expand Down