Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS selector referencing later-defined styled components not working #270

Open
jantimon opened this issue Feb 7, 2025 · 0 comments
Open
Labels
bug Something isn't working

Comments

@jantimon
Copy link
Collaborator

jantimon commented Feb 7, 2025

I discovered an issue with next-yak where using a styled component as a selector inside another styled component fails if the referenced component is declared after the usage, even when that usage is inside an arrow function.

This pattern should work since the arrow function is only evaluated at runtime after all components are defined:

import { styled } from "next-yak";

const CompA = styled.div<{ $active?: boolean }>`
  ${({ $active }) =>
    $active && css`
    ${CompB} {
      color: red;
    }`}
`;

const CompB = styled.div``;

However, this produces the following error:

Error:   × Unsupported "CompB" template literal in css expression - only css`` mixins are allowed
   ╭─[/example.tsx:5:1]
 2 │ const CompA = styled.div<{ $active?: boolean }>`
 3 │   ${({ $active }) =>
 4 │     $active && css`
 5 │     ${CompB} {
   ·       ─────
 6 │       color: red;
 7 │     }`}
 8 │ `;
   ╰────

The error seems to occur because the SWC plugin is checking for component references at compile time rather than deferring to runtime evaluation. Since JavaScript/TypeScript allow referencing variables before their declaration when they're inside functions that run later, this should be valid code

Environment:

  • next-yak: 4.1.0
  • yak-swc: 4.1.0
  • Node: 22.13.1
@jantimon jantimon added the bug Something isn't working label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant