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.

Custom SVG slots for icons are not working as expected with tailwind

See original GitHub issue

When using Media Chrome with tailwindcss on the page I noticed some unexpected behavior when using custom icons

Here’s a <media-play-button> that uses a slot="play" and a slot="pause"

<media-play-button>
  <svg slot="play" class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd"></path></svg>
  <svg slot="pause" class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zM7 8a1 1 0 012 0v4a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v4a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd"></path></svg>
</media-play-button>

What I noticed is that both the play button and pause button were visible at the same time. Upon some digging I found that both the play and pause SVGs were getting display: block.

CSS in the light DOM (tailwind reset) has a rule where it sets svg { display: block; } – this isn’t tailwind specific, probably a lot of CSS reset code does this or something similar.

pause-showing_2022-06-02_09-50-27

Because of specificity, the CSS from light DOM that set svg { display: block; } was over-riding the { display: none } set on the slots by the shadow DOM

What’s the best way to work-around this?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
dylanjhacommented, Jul 13, 2022

I think we’re pretty aligned on:

1reaction
meyerwebcommented, Jul 12, 2022

I didn’t come up with anything new and fancy, but adding this to the web component’s CSS seems to work for me:

media-play-button[aria-label="pause"] svg[slot="play"],
media-play-button[aria-label="play"] svg[slot="pause"] {
   display: none;
}

The higher specificity of the rule overcomes Tailwind’s reset. Would you be able to ship this as part of your default styles, and trust users to not override or otherwise mess it up?

I also kinda disagree with TW’s application of block to those elements, and would happily just delete it, but if it’s an automated dependency I can see not wanting to mess around with its code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js Slots with Tailwind CSS and Rails - YouTube
Check out https://gorails.com for Pro episodes and more!
Read more >
Indeterminate state for checkboxes · Issue #27 - GitHub
I'm writing here so I mostly don't forget, but it seems that .form-checkbox removes native stylings but doesn't add one for the indeterminate ......
Read more >
Building React + Vue support for Tailwind UI
We wanted the APIs to work well with a class-based styling solution like Tailwind. A lot of the other tools out there expected...
Read more >
Tailwind Utility Classes as an antipattern - Joaquin Marti
Non -supported new CSS features and custom styles. A utility classes framework abstracts the styles from the developer. We do not write CSS...
Read more >
Dialog Component - VueTailwind
VueJs Dialog component with configurable classes and infinite variants. Use it for alerts, confirmation, or prompt dialogs.
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