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

Use :is to make important selector option insensitive to DOM order #10835

Merged
merged 7 commits into from
Mar 21, 2023

Conversation

adamwathan
Copy link
Member

This PR changes the implementation of the important option's selector strategy to wrap the underlying selector with :is(...) to make it insensitive to DOM order. This is especially useful when using the "class" dark mode strategy, or using the rtl and ltr modifiers because it allows you to add the .dark class or dir="rtl" attributes to the <html> element instead of to some descendant of the element where you've added your important selector.

For example, given this configuration:

module.exports = {
  // ...
  important: '#app',
  darkMode: 'class',
}

...Tailwind would historically generate classes like this:

#app .dark .dark\:bg-black {
  background-color: #000;
}

That means that this DOM structure wouldn't work:

<html class="dark">
<!-- ... -->
<body>
  <div id="app">
    <div class="dark:bg-black">
      <!-- ... -->
    </div>
  </div>
</body>
</html>

With the changes in this PR, Tailwind will generate this CSS instead:

#app :is(.dark .dark\:bg-black) {
  background-color: #000;
}

...which means the above DOM structure will work, which is more intuitive behavior.

The :is(...) pseudo-class has good browser support, with the most recent adopter being Safari in v14, which is almost three years old and two major versions in the past.

@jonasdev
Copy link

Hi @adamwathan
Can I somehow prevent using :is() but keep the latest tailwind version?
We are using important: #app just like your example, but is used in a WebView running on a Chromiun 87.0.
After this release, it will break our styling. Can I somehow support legacy browsers and still use Tailwind 3.4.1?

image

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.

3 participants