jantimon / next-yak

a streamlined CSS-in-JS solution tailor-made for Next.js, seamlessly combining the expressive power of styled-components syntax with efficient build-time extraction and minimal runtime footprint, ensuring optimal performance and easy integration with existing atomic CSS frameworks like Tailwind CSS
https://yak.js.org
118 stars 4 forks source link

automatically inject `:global` #53

Open jantimon opened 9 months ago

jantimon commented 9 months ago

next-yak automatically uses css modules behind the scenes and in nextjs css modules are local by default. this means that selectors e.g. html.dark-mode will be scoped automatically to the component

for example

const StyledDiv = styled.div`
  background-color: #f0f0f0;
  html.dark-mode & {
    background-color: #000;
  }
`;

becomes

.StyledDiv-be2f45 {
  background-color: #f0f0f0;
  html.dark-mode-a4e345 & {
    background-color: #000;
  }
}

so although the user wrote html.dark-mode it would NOT match.

as this is unintuitive for most users and therefore next-yak should automatically inject :global

jantimon commented 7 months ago

I guess there are multiple cases:

  1. String - (should be global)
const Bar = styled.div`.Foo { ... }`
  1. Same file component - (should be local)
const Foo = styled.div``;
const Bar = styled.div`${Foo} { ... }`
  1. Constant - (should be global)
const darkMode = ".darkMode";
const Bar = styled.div`${darkMode} { ... }`