Skip to content
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

Attach listener not handling two-level parentNode inserting #9471

Closed
danielgindi opened this issue Jul 26, 2021 · 4 comments · Fixed by #9557
Closed

Attach listener not handling two-level parentNode inserting #9471

danielgindi opened this issue Jul 26, 2021 · 4 comments · Fixed by #9557

Comments

@danielgindi
Copy link
Contributor

danielgindi commented Jul 26, 2021

Expected Behavior

Chart should render as soon as the parent is inserted into the DOM.

Current Behavior

Canvas remains blank.

Possible Solution

Attach listener should use contains to check if the node inserted includes the chart element.
Keeping a reference for every parent node is not feasible.

function createAttachObserver(chart, type, listener) {
  const canvas = chart.canvas;
  const observer = new MutationObserver(entries => {
    entries.forEach(entry => {
      for (let i = 0; i < entry.addedNodes.length; i++) {
        const added = entry.addedNodes[i];
        if (added === canvas || added.contains(canvas)) {
          listener(entry.target);
        }
      }
    });
  });
  observer.observe(document, {childList: true, subtree: true});
  return observer;
}

Steps to Reproduce

https://codepen.io/danielgindi/pen/bGWavLX

Context

Environment

  • Chart.js version: 3.5.0
  • Browser name and version: Chrome 92
@danielgindi
Copy link
Contributor Author

@kurkle It's like the other side of #9448

@kurkle
Copy link
Member

kurkle commented Jul 26, 2021

Right, the isAttached works, but the attachment is not detected.

@danielgindi
Copy link
Contributor Author

@kurkle Please note, that even when applying this fix, there's an odd new behavior - when the element is added to the dom, a vertical bar chart animates like from a 0 on 0 size, not from fullWidth on 0 size.

@snowsunny
Copy link

snowsunny commented Aug 12, 2021

I had same issue, and I fixed by same the solution. It working for my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants