jantimon / next-yak

🦀 Zero-runtime CSS-in-JS powered by Rust. Write styled-components syntax, get build-time CSS extraction and full RSC compatibility
https://yak.js.org
139 stars 4 forks source link

negative numbers in CSS styles are not statically extracted #191

Closed jantimon closed 1 month ago

jantimon commented 1 month ago

When using negative numbers in styled components next-yak fails to statically extract them..
So these numbers get converted to CSS variables and evaluated at runtime, even though they are constant values. This creates unnecessary overhead since these values could be inlined directly into the CSS

const Button = styled.button`
  margin-top: ${-1}px;  // Gets converted to CSS variable
`;

Gets compiled to something like:

const Button = styled.button(
  "Button_xyz", 
  { style: { "--marginTop": -1 } }
);

With CSS output:

.Button_xyz {
  margin-top: var(--marginTop);
}

Since the negative number is a constant value, it should be inlined directly into the CSS:

.Button_xyz {
  margin-top: -1px;
}