Skip to content

Commit

Permalink
Restore infinite resize detection (chartjs#6011)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Feb 17, 2020
1 parent fb2a0b6 commit da9d329
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/platform/platform.dom.js
Expand Up @@ -155,11 +155,8 @@ function fromNativeEvent(event, chart) {

function throttled(fn, thisArg) {
let ticking = false;
let args = [];

return function(...rest) {
args = Array.prototype.slice.call(rest);

return function(...args) {
if (!ticking) {
ticking = true;
helpers.requestAnimFrame.call(window, () => {
Expand All @@ -178,11 +175,24 @@ function throttled(fn, thisArg) {
* @return {ResizeObserver}
*/
function watchForResize(element, fn) {
const resize = throttled((width, height) => {
const w = element.clientWidth;
fn(width, height);
if (w < element.clientWidth) {
// If the container size shrank during chart resize, let's assume
// scrollbar appeared. So we resize again with the scrollbar visible -
// effectively making chart smaller and the scrollbar hidden again.
// Because we are inside `throttled`, and currently `ticking`, scroll
// events are ignored during this whole 2 resize process.
// If we assumed wrong and something else happened, we are resizing
// twice in a frame (potential performance issue)
fn();
}
}, window);

const observer = new ResizeObserver(entries => {
const entry = entries[0];
helpers.requestAnimFrame.call(window, () => {
fn(entry.contentRect.width, entry.contentRect.height);
});
resize(entry.contentRect.width, entry.contentRect.height);
});
observer.observe(element);
return observer;
Expand Down

0 comments on commit da9d329

Please sign in to comment.