Replies: 4 comments 8 replies
-
Emotion allows you to add arbitrary CSS to the page, so you should be able to use the Example: https://codesandbox.io/s/emotion-specificity-hack-78jl77?file=/src/App.tsx |
Beta Was this translation helpful? Give feedback.
-
You could explore writing a Stylis plugin for this or try to hack around using a custom |
Beta Was this translation helpful? Give feedback.
-
Well I sort of found a way, it's not exactly what I want but it appears to work. If someone else needs to solve the same issue the following is what I've done. import createCache, {
StylisElement,
StylisPluginCallback,
} from '@emotion/cache';
import { prefixer, serialize } from 'stylis';
import { CacheProvider } from '@emotion/react';
import { map } from 'lodash';
const plugin = (
element: StylisElement,
index: number,
children: StylisElement[],
callback: StylisPluginCallback,
) => {
let modified = false;
const props = map(element.props, (prop) => {
if (prop.includes(':not(#\\20)')) {
return prop;
} else {
modified = true;
return `:not(#\\20)${prop}`;
}
});
if (element.type === 'rule' && modified) {
serialize([{ ...element, props }], callback);
}
};
const myCache = createCache({
key: 'temp-frontend',
stylisPlugins: [plugin, prefixer],
});
const store = buildStore();
<CacheProvider value={myCache}><ReactApp/></CacheProvider> I wish stylis had better documentation on how these plugins suppose to work. Maybe someone else knows better and can help because the above snippet does not modify the rule name, I could not find a way to do that, it just inserts the clone of the rule with a modified rule name. Not exactly desired behaviour but it works for now. |
Beta Was this translation helpful? Give feedback.
-
@Andarist I have a related problem, would be very nice to get your feedback!
"h1": {
marginTop: "1em",
marginBottom: "1rem",
}, and they, unfortunately, do override For example .xxx {
marginTop: "2rem"
} And .xxx h1 {
marginTop: "1em",
marginBottom: "1rem",
}
":where(&) h1": {
marginTop: "1em",
marginBottom: "1rem",
},
❗ Unfortunately, Emotion does not recognize .xxx:where(.xxx) h1 {
...
} instead of: :where(.xxx) h1 {
...
} Which would have a correct, lower specificity than styles from To me it looks like a bug and I believe Emotion should recognize E.g. body :where(.xxx) h1 {
...
} but it's, again, an increased specificity. What do you think? |
Beta Was this translation helpful? Give feedback.
-
I'm migration an old application to react, using mui as default components, since v5 mui started to use emotion which brings me here. This legacy application has a lot of styles that cannot be removed and are messing up material ui components, I need emotion generated css to have higher specificity. This is something that I used to be able to do quite easily with JSS plugins, all that you need to do is prefix all rules with
:not(#\\20)
where#\\20
is just a char DC4 (device control 4), this can be anything I just chose#\\20
because no one would normally have an id with this name, e.g.example 1
the number of
:not(#\\20)
defines the specificity, with this approach you can have multiple layers of specificityexample 2
will always take precendence over the example 1. This approach to specificity increase is described here and if you would like to see how I did this in JSS you can find source code for it here and here (no documentation for it, sorry).
Since material ui moved to emotion I now have a need to find a way to replicate this functionality in emotion, perhaps this is something that is already possible but I can't find any documentation for it, so I would like to propose 2 solutions.
Solution 1
Add an ability to add plugins that allow css rule names to be modified. A highly flexible solution but probably too difficult to use.
Solution 2
Add an ability to specify the level of specificity emotion should use. A react example could be something like
With this users would be able to choose the specificity that suits them, this is less flexible approach but very easy to use.
Beta Was this translation helpful? Give feedback.
All reactions