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

Turbo does not work with Declarative Shadow DOM #1292

Open
KonnorRogers opened this issue Jul 29, 2024 · 1 comment
Open

Turbo does not work with Declarative Shadow DOM #1292

KonnorRogers opened this issue Jul 29, 2024 · 1 comment

Comments

@KonnorRogers
Copy link
Contributor

Turbo appears to leave template tags in place on navigation breaking components that use Declarative Shadow DOM.

2024-07-29.12-35-13.mp4

Reproduction: https://github.com/KonnorRogers/turbo-dsd-reproduction

@KonnorRogers
Copy link
Contributor Author

This seems to work as a sort of "polyfill"

function fixDSD (e) {
  const newElement = e.detail.newBody || e.detail.newFrame || e.detail.newStream
  if (!newElement) { return }

  // https://developer.chrome.com/docs/css-ui/declarative-shadow-dom#polyfill
  ;(function attachShadowRoots(root) {
    root.querySelectorAll("template[shadowrootmode]").forEach(template => {
      const mode = template.getAttribute("shadowrootmode");
      const shadowRoot = template.parentNode.attachShadow({ mode });
      shadowRoot.appendChild(template.content);
      template.remove();
      attachShadowRoots(shadowRoot);
    });
  })(newElement);
}

;["turbo:before-render", "turbo:before-stream-render", "turbo:before-frame-render"].forEach((eventName) => {
  document.addEventListener(eventName, fixDSD)
})

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

No branches or pull requests

1 participant