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

Render samples with shadowDOM to prevent style clashes. #39

Closed
wants to merge 6 commits into from

Conversation

kaspth
Copy link
Collaborator

@kaspth kaspth commented Feb 25, 2023

This renders our samples with shadowDOM on a custom element, so we can embed app styles in those elements and not have them leak into Showcase.

This renders our samples with shadowDOM on a custom element, so we can
embed app styles in those elements and not have them leak into Showcase.
@kaspth

This comment was marked as resolved.

@kaspth
Copy link
Collaborator Author

kaspth commented Feb 25, 2023

Ok, it looks like if I rendered the element like this:

<showcase-isolated-context>
  <template data-shadowroot>
    <%= render "showcase/engine/stylesheets" %>
    <%= sample.preview %>
  </template>
</showcase-isolated-context>

The template element wouldn't, for some reason, be in the element's DOM to be able to be added to its shadowDOM. This does fit with the MDN documentation that I've been working off of, even though they don't explicitly tell you the template needs to be in the DOM before the element, all their examples do that.

Since we've already isolated the template tags in a container, we can use parentNode to find the element once we're connected. Not the prettiest, and it looks like all this could go away with declarative shadow DOMs (e.g. shadowroot="open" on the sample section even).

@kaspth
Copy link
Collaborator Author

kaspth commented Feb 25, 2023

@KonnorRogers thanks for the advice, I went with requestAnimationFrame instead of setTimeout, although I think they're roughly the same.

@kaspth
Copy link
Collaborator Author

kaspth commented Feb 25, 2023

Looks like shadowDOM also prevents us from logging any dispatched JavaScript events in the showcase-sample element, hm. Fun times!

within :element, data: {controller: "welcome"} do
assert_element text: "Somebody", data: {welcome_target: "greeter"}
within :section, "Samples" do |section|
section.find_css "[data-shadowroot]" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capybara will strip out all template element content from the moment it parses the HTML string. We might need to build a Nokogiri tree from response.body (for Integration Tests) or rendered (for View Test). From there, we'd traverse until the template and pass it's contents to Capybara.string so that we could make assertions on it directly.

@kaspth
Copy link
Collaborator Author

kaspth commented Mar 13, 2023

Gonna call this done for now, since it seems like ShadowDOM is tough for our use case of having user supplied HTML — and I'll have a hard time maintaining. We'll see how it goes with style clashes.

@kaspth kaspth closed this Mar 13, 2023
@kaspth kaspth deleted the shadowdom-samples branch March 13, 2023 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants