Skip to content

Commit

Permalink
Fix nested css prop (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mad-Kat authored Dec 21, 2024
1 parent 11261b7 commit 154da2f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-bulldogs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"next-yak": patch
"yak-swc": patch
---

fix css prop class name access in nested jsx
16 changes: 16 additions & 0 deletions packages/example/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ export default function Home() {
>
Conditional CSS Prop works if this is green
</p>
<p
css={css`
color: violet;
`}
>
Nested CSS Prop works
<span
css={css`
color: green;
`}
>
{" "}
if this is green{" "}
</span>
and this is violet
</p>
<Inputs />
</main>
</YakThemeProvider>
Expand Down
16 changes: 8 additions & 8 deletions packages/yak-swc/yak_swc/src/yak_transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub struct TransformCssMixin {
export_name: Option<ScopedVariableReference>,
is_exported: bool,
is_within_jsx_attribute: bool,
generated_class_name: Option<String>,
}

impl TransformCssMixin {
Expand All @@ -157,6 +158,7 @@ impl TransformCssMixin {
export_name: None,
is_exported,
is_within_jsx_attribute,
generated_class_name: None,
}
}
}
Expand All @@ -170,14 +172,14 @@ impl YakTransform for TransformCssMixin {
) -> ParserState {
self.export_name = Some(declaration_name.clone());
let mut parser_state = ParserState::new();
let generated_class_name =
naming_convention.generate_unique_name_for_variable(declaration_name);
// TODO: Remove the unused scope once nested mixins work again
parser_state.current_scopes = vec![CssScope {
name: format!(
".{}",
naming_convention.generate_unique_name_for_variable(declaration_name)
),
name: format!(".{}", generated_class_name),
scope_type: ScopeType::AtRule,
}];
self.generated_class_name = Some(generated_class_name);
parser_state
}

Expand Down Expand Up @@ -233,9 +235,7 @@ impl YakTransform for TransformCssMixin {
Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: Box::new(Expr::Ident(css_module_identifier.clone())),
prop: create_member_prop_from_string(
self.export_name.clone().unwrap().to_readable_string(),
),
prop: create_member_prop_from_string(self.generated_class_name.clone().unwrap()),
})
.into(),
);
Expand Down Expand Up @@ -266,7 +266,7 @@ impl YakTransform for TransformCssMixin {

fn get_css_reference_name(&self) -> Option<String> {
if self.is_within_jsx_attribute {
return Some(self.export_name.as_ref().unwrap().to_readable_string());
return Some(self.generated_class_name.clone().unwrap());
}
None
}
Expand Down
22 changes: 22 additions & 0 deletions packages/yak-swc/yak_swc/tests/fixture/css-prop-nested/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { css } from "next-yak";

<div
css={css`
color: red;
`}
>
<p
css={css`
color: blue;
`}
>
<span
css={css`
color: green;
}`}
>
hello
</span>
world
</p>
</div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { css, __yak_mergeCssProp } from "next-yak/internal";
import __styleYak from "./input.yak.module.css!=!./input?./input.yak.module.css";
<div {.../*YAK Extracted CSS:
.yak {
color: red;
}
*/ /*#__PURE__*/ css(__styleYak.yak)({})}>
<p {.../*YAK Extracted CSS:
.yak-01 {
color: blue;
}
*/ /*#__PURE__*/ css(__styleYak["yak-01"])({})}>
<span {.../*YAK Extracted CSS:
.yak-02 {
color: green;
}
*/ /*#__PURE__*/ css(__styleYak["yak-02"])({})}>
hello
</span>
world
</p>
</div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { css, __yak_mergeCssProp } from "next-yak/internal";
import __styleYak from "./input.yak.module.css!=!./input?./input.yak.module.css";
<div {.../*YAK Extracted CSS:
.yak {
color: red;
}
*/ /*#__PURE__*/ css(__styleYak.yak)({})}>
<p {.../*YAK Extracted CSS:
.yak1 {
color: blue;
}
*/ /*#__PURE__*/ css(__styleYak.yak1)({})}>
<span {.../*YAK Extracted CSS:
.yak2 {
color: green;
}
*/ /*#__PURE__*/ css(__styleYak.yak2)({})}>
hello
</span>
world
</p>
</div>;

0 comments on commit 154da2f

Please sign in to comment.