How can we scope the global styles under one element? #841
Replies: 2 comments
-
Perhaps the new css Something like: import styled from 'styled-components';
import { globalStyles } from 'twin.macro';
const GlobalStylesScope = styled.div({ '@scope (#my-element)': globalStyles }); |
Beta Was this translation helpful? Give feedback.
-
@ben-rogerson Thanks for the help, I think you meant I did find an alternate solution to this which is similar to the idea I was hoping worked. Turns out, styled-components exposes a There is a stylis plugin - stylis-plugin-extra-scope, that boosts the specificity of styles generated by adding additional scopes in front of the class names. A combination of these worked exactly how I wanted! import styled, { StyleSheetManager } from 'styled-components';
import { globalStyles } from 'twin.macro';
import extraScopePlugin from 'stylis-plugin-extra-scope';
const GlobalStylesScope = styled.div(globalStyles);
function App() {
return (
<>
<GlobalStylesScope id="my-element">
<StyleSheetManager stylisPlugins={[extraScopePlugin("#my-element")]}>
<button tw="bg-black">Black button</button>
</StyleSheetManager>
</GlobalStylesScope>
<button>Default button</button>
</>
);
} The only thing left to solve was that the |
Beta Was this translation helpful? Give feedback.
-
I'm trying to use twin.macro in a project which has polluted global styles, and I want neither it affecting components built with twin.macro, nor twin.macro's global styles affecting them.
A solution I could come up for this was scoping the global styles from twin.macro under one element, for example:
The problem I'm facing here is that the styles generated using tailwind classes (via twin.macro) is on an outer scope, and thus the global styles have higher specificity, resulting in resets from global styles taking higher priority than the utilities themselves.
The only approach I could think of to solve this would be if twin.macro could generate styles with some scope applied. Example:
This could work as ids have more specificity than classnames. Open to any ideas / other approaches that could solve this 🙂
Beta Was this translation helpful? Give feedback.
All reactions