question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

id attribute on style rendered into shadow dom should be replaced with data-id

See original GitHub issue

Describe the problem

When we build a svelte component as stand-alone custom element, its style is rendered into the shadow root of the custom element like <style id="svelte-mtnic9">.

Now lets say the compiled and minified stand-alone svelte custom element is called <my-super-input> and we import it into a vanilla or react project.

The problem is, that we want to use <my-super-input> in a vanilla or react website multiple times in different places.

That works, BUT since it is not allowed that multiple elements have the same id attribute, the dev tools will complain about all the <style id="svelte-mtnic9"> in each instance of <my-super-input>.

This can potentially break unit tests, customer acceptance and is generally against the rule that id attributes must be unique.

This originated from a discussion I had here with @ivanhofer

Describe the proposed solution

The solution is fortunately super easy. Instead of using the id attribute, please use the data-id attribute when rendering the style element into the shadow root of the custom element.

Alternatives considered

Alternatively you can use basically any attribute other than id. But data-id seems the tidiest.

Importance

i cannot use svelte without it

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Conduitrycommented, Sep 29, 2021

I’m going to close this. There are good reasons to use id, as mentioned above. The compiler shouldn’t produce worse code in order to try to appease overzealous validation checks in other tools.

0reactions
ivanhofercommented, Sep 12, 2021

@BerndWessels just because everyone does it like this, does not mean that it is correct 😉 You probably should create an issue in Chrome and other Browsers (or for the tool they use to check a11y). Maybe they don’t know about the issue and are wiling to fix it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Encapsulating Style and Structure with Shadow DOM
In the demo above, changing the template attribute on our <one-dialog> element will alter which design is being used when the element is ......
Read more >
Jest Test - element.shadowRoot.querySelector() not retrieving ...
It isn't recommended to use querySelector() with the id attribute in LWC (see docs), as the id "may be transformed into globally unique...
Read more >
HTML Template Shadow DOM not rendering within ...
Figured out the issue: Declarative Shadow DOM is processed and attached when the document is loaded. It will not work for post-load script ......
Read more >
5. Working with the Shadow DOM - Modern JavaScript [Book]
For example, a shadow DOM subtree can contain IDs and styles that overlap with IDs and styles ... However, only the last shadow...
Read more >
Read Web Components | Leanpub
LitElement uses lit-html to render into the element's Shadow DOM and adds API to ... However, when you change one of the observed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found