Failed to override styles when selectors are grouped #495
-
Hi Griffel developers, I ran into an issue when trying to override CSS styles with mergeStyles. After some investigation I think it might relate to selctor matching issue. The original style is defined with grouped selectors ".active,:hover":
Then tried to override the behavior of ":hover":
This is how the styles are used:
It is expected to override the previous definition by the later one. But it doesn't work. Then I tried other ways to override the styles. It turns out override only works when the exact same grouped selectors are used. Even one whitespace between the selectors can fail it. For example:
You can see the demo in the sandbox |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @Yixian15. Thanks for bringing this up 👍 Indeed Griffel is "picky" about selectors (or keys on an object). makeStyles({
root: {
":hover": { color: "red" },
":hover,:active": { color: "blue" },
},
}); That's not limited to this case, as it's true for any defined selector... To avoid the problem, in the case that you mentioned it should be declared like: const sharedStyles = { color: "red" }
makeStyles({
root: {
":hover": sharedStyles,
":active": sharedStyles
},
}); I am not sure that anything can be done there to make it more predictable as this problem is a part of design... We can emit a warning in dev environment and add a ESLint rule to avoid such selectors as the change in transform logic does not seem to be something easy... WDYT? |
Beta Was this translation helpful? Give feedback.
You are correct. There is no predictable way to override it. I created #499 to introduce a lint rule.
Now you can do a nasty hack to increase specificity:
https://stackblitz.com/edit/stackblitz-starters-2xtajv