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.

Don't add classname for `:is()` and `:where()` selectors

See original GitHub issue

The problem

I want to leverage :where selector (it is good for theming because it has 0 specificity) to style a component based on the parent attribute.

However, emotion prefix the style with the generated class name in front of :where which does not make the style works:

const Component = style('div')`
  color: black
  :where([data-color-scheme="dark"]) & {
    color: white
  }
`

turned into

<style>
// to make this style work, the `.css-9qpay1` at the beginning should be removed
.css-9qpay1:where([data-color-scheme="dark"]) .css-9qpay1{color:white;}
</style>

Here is the reproduction: https://codesandbox.io/s/emotion-forked-e9fyes?file=/index.js

Proposed solution

Should :is and :where be the exceptional case to not implicitly append to the class name?

const Component = style('div')`
  :where([data-color-scheme="dark"]) & {
    color: white
  }
  // result as ':where([data-color-scheme="dark"]) .css { color: white };
`

const Component = style('div')`
  // explicitly declare the '&'
  &:where([data-color-scheme="dark"]) {
    color: white
  }
  // result as '.css:where([data-color-scheme="dark"]) .css { color: white };
`

Alternative solutions

Is there any workaround that I might have missed?

Additional context

Hi, I am from the MUI core team. We are working on the theming API to let developers theme components based on color schemes without having to read the mode from the theme.

Our API added an attribute to the html when the color scheme changes:

// light mode
<html data-color-scheme="light">

// dark mode
<html data-color-scheme="dark">

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Aug 8, 2022

Note that this might be prone to some edge cases but it probably shouldn’t matter for real-life cases in which you are gonna use this.

1reaction
siriwatknpcommented, Aug 8, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to exclude particular class name from CSS selector
In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This ......
Read more >
not() - CSS: Cascading Style Sheets - MDN Web Docs
The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, ...
Read more >
Can I write a CSS selector selecting elements NOT having a ...
For selecting everything but a certain element (or elements). We can use the :not() CSS pseudo class. The :not() pseudo class requires a...
Read more >
:not | CSS-Tricks - CSS-Tricks
The specificity of the :not pseudo-class is the specificity of its argument. The :not() pseudo-class does not add to the selector specificity, ...
Read more >
Confusion over when and when not to use "." for selectors
I just don't have a firm grasp on when you need to use the period (or the # for ... addClass() etc. ......
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