Skip to content

Commit

Permalink
better css class names (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon authored Jan 22, 2024
1 parent 658b730 commit 5de68d6
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 81 deletions.
118 changes: 59 additions & 59 deletions packages/next-yak/loaders/__tests__/cssloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
".yak_0 {
font-size: 2rem;
font-weight: bold;
color: red;
Expand Down Expand Up @@ -77,15 +77,15 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
".yak_0 {
font-size: 2rem;
font-weight: bold;
color: red;
&:where(._yak_1) {
&:where(.yak_1) {
color: orange;
}
&:where(._yak_2) {
&:where(.yak_2) {
color: teal;
}
&:hover {
Expand Down Expand Up @@ -113,9 +113,9 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
".yak_0 {
/* comment */
&:where(._yak_1) {
&:where(.yak_1) {
color: blue;
}
}"
Expand All @@ -138,12 +138,12 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
&:hover {
color: var(--🦬18fi82j0);
}
}"
`);
".yak_0 {
&:hover {
color: var(--🦬18fi82j0);
}
}"
`);
});

it("should support attrs on intrinsic elements", async () => {
Expand Down Expand Up @@ -211,14 +211,14 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
transition: color var(--🦬18fi82j0) var(--🦬18fi82j1);
display: block;
&:where(._yak_1) {
color: orange
}
}"
`);
".yak_0 {
transition: color var(--🦬18fi82j0) var(--🦬18fi82j1);
display: block;
&:where(.yak_1) {
color: orange
}
}"
`);
});

it("should replace breakpoint references with actual media queries", async () => {
Expand All @@ -241,18 +241,18 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
color: blue;
@media (min-width: 640px) {
color: red;
}
transition: color var(--🦬18fi82j0) var(--🦬18fi82j1);
display: block;
&:where(._yak_1) {
color: orange
".yak_0 {
color: blue;
@media (min-width: 640px) {
color: red;
}
}"
`);
transition: color var(--🦬18fi82j0) var(--🦬18fi82j1);
display: block;
&:where(.yak_1) {
color: orange
}
}"
`);
});

it("should replace breakpoint references with actual media queries from single quote imports", async () => {
Expand All @@ -275,18 +275,18 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
color: blue;
@media (min-width: 640px) {
color: red;
}
transition: color var(--🦬18fi82j0) var(--🦬18fi82j1);
display: block;
&:where(._yak_1) {
color: orange
".yak_0 {
color: blue;
@media (min-width: 640px) {
color: red;
}
}"
`);
transition: color var(--🦬18fi82j0) var(--🦬18fi82j1);
display: block;
&:where(.yak_1) {
color: orange
}
}"
`);
});

it("should prevent double escaped chars", async () => {
Expand All @@ -311,16 +311,16 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"._yak_0 {
:before {
content: \\"\\\\2022\\";
}
:after {
content: \\"\\\\2022\\";
}
content: \\"\\\\\\\\\\"
}"
`);
".yak_0 {
:before {
content: \\"\\\\2022\\";
}
:after {
content: \\"\\\\2022\\";
}
content: \\"\\\\\\\\\\"
}"
`);
});

it("should convert keyframes", async () => {
Expand Down Expand Up @@ -481,16 +481,16 @@ const Component2 = styled.div\`
".Component {
background-color: red;
color: white;
&:where(._yak_0) {
&:where(.active_0) {
background-color: blue;
}
border: 1px solid black;
&:focus {
background-color: green;
&:where(._yak_1) {
&:where(.active_1) {
background-color: blue;
&:where(._yak_2) {
&:where(.active_2) {
background-color: brown;
}
}
Expand Down Expand Up @@ -535,17 +535,17 @@ const Component = styled.div\`
".Component {
background-color: red;
color: white;
&:where(._yak_0) {
&:where(.active_0) {
background-color: blue;
}
&:where(._yak_1) {
&:where(.not_active_1) {
background-color: var(--🦬18fi82j0);
}
border: 1px solid black;
&:where(._yak_2) {
&:where(.active_2) {
color: orange;
}
&:where(._yak_3) {
&:where(.not_active_3) {
transition: color var(--🦬18fi82j1) var(--🦬18fi82j2);
}
}"
Expand Down
83 changes: 83 additions & 0 deletions packages/next-yak/loaders/__tests__/getCssName.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { parse, traverse } from "@babel/core";
import { NodePath } from "@babel/core";
import getCssName from "../lib/getCssName.cjs";
import type { TaggedTemplateExpression } from "@babel/types";
import { describe, it, expect } from "vitest";

function extractConditionsWithBabel(code: string) {
let result: string = "";
const ast = parse(code);
if (!ast) {
throw new Error("Could not parse code");
}
traverse(ast, {
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>) {
if (path.node.tag.type === "Identifier" && path.node.tag.name === "css") {
result = getCssName(path);
}
},
});
return result;
}

describe("getCssName", () => {
it("should guess the css name from the condition of a logical expression", () => {
const code = `({$active}) => $active && css\`\``;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("active");
});

it("should guess the css name from the condition of a ternary expression", () => {
const code = `({$active}) => $active ? css\`\` : null`;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("active");
});

it("should guess the css name from the condition of a logical expression with negation", () => {
const code = `({$active}) => !$active && css\`\``;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("not_active");
});

it("should guess the css name from the condition of a ternary expression for the else case", () => {
const code = `({$active}) => $active ? null : css\`\``;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("not_active");
});

it("should guess the css name from the condition of a logical expression with multiple conditions", () => {
const code = `({$active, $visible}) => $active && $visible && css\`\``;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("active_and_visible");
});

it("should guess the css name from the condition of a ternary expression with multiple conditions", () => {
const code = `({$active, $visible}) => $active ? ($visible ? css\`\` : null) : null`;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("active_and_visible");
});

it("should guess the css name from the condition of a logical expression with negation and multiple conditions", () => {
const code = `({$active, $visible}) => !$active && $visible && css\`\``;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("not_active_and_visible");
});

it("should guess the css name from the condition of a ternary expression with negation and multiple conditions", () => {
const code = `({$active, $visible}) => !$active ? ($visible ? css\`\`: null) : null`;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("not_active_and_visible");
});

it("should guess the css name from the condition of a logical expression with negation and multiple conditions for the else case", () => {
const code = `({$active, $visible}) => $active && !$visible && css\`\``;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("active_and_not_visible");
});

it("should guess the css name from the condition of a ternary expression with negation and multiple conditions for the else case", () => {
const code = `({$active, $visible}) => $active ? ($visible ? null : css\`\`) : null`;
const cssName = extractConditionsWithBabel(code);
expect(cssName).toBe("active_and_not_visible");
});
});
38 changes: 19 additions & 19 deletions packages/next-yak/loaders/__tests__/tsloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const Main = () => <h1 className={headline({}).className}>Hello World</h1
import { css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
type x = number;
const headline = css(__styleYak._yak_0);
const headline = css(__styleYak.yak_0);
export const Main = () => <h1 className={headline({}).className}>Hello World</h1>;"
`);
});
Expand Down Expand Up @@ -91,7 +91,7 @@ const headline = css\`
import { css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
const x = Math.random();
const headline = css(__styleYak._yak_0, x > 0.5 && css(__styleYak._yak_1));"
const headline = css(__styleYak.yak_0, x > 0.5 && css(__styleYak.yak_1));"
`);
});

Expand Down Expand Up @@ -125,7 +125,7 @@ const FancyButton = styled(Button)\`
import { styled, css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
const x = Math.random();
const Button = styled.button(__styleYak.Button, x > 0.5 && css(__styleYak._yak_0));
const Button = styled.button(__styleYak.Button, x > 0.5 && css(__styleYak.yak_0));
const FancyButton = styled(Button)(__styleYak.FancyButton);"
`);
});
Expand Down Expand Up @@ -162,7 +162,7 @@ const FancyButton = styled(Button)\`
const x = Math.random();
const Button = styled.button(__styleYak.Button, ({
theme
}) => theme.mode === \\"dark\\" && css(__styleYak._yak_0));
}) => theme.mode === \\"dark\\" && css(__styleYak.yak_0));
const FancyButton = styled(Button)(__styleYak.FancyButton);"
`);
});
Expand Down Expand Up @@ -240,7 +240,7 @@ const headline = css\`
import { css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
import { easing } from \\"styleguide\\";
const headline = css(__styleYak._yak_0, css(__styleYak._yak_1), css(__styleYak._yak_2), {
const headline = css(__styleYak.yak_0, css(__styleYak.yak_1), css(__styleYak.yak_2), {
\\"style\\": {
\\"--\\\\uD83E\\\\uDDAC18fi82j0\\": ({
i
Expand Down Expand Up @@ -477,12 +477,12 @@ const headline = css\`
`
)
).toMatchInlineSnapshot(`
"import styles from \\"./page.module.css\\";
import { css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
const x = Math.random();
const headline = css(__styleYak._yak_0, x > 0.5 && css(__styleYak._yak_1));"
`);
"import styles from \\"./page.module.css\\";
import { css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
const x = Math.random();
const headline = css(__styleYak.yak_0, x > 0.5 && css(__styleYak.yak_1));"
`);
});

it("should show error when a dynamic selector is used after a comma", async () => {
Expand Down Expand Up @@ -552,13 +552,13 @@ const Button = styled.button\`
`
)
).toMatchInlineSnapshot(`
"import styles from \\"./page.module.css\\";
import { styled, css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
const Icon = styled.svg(__styleYak.Icon);
const Button = styled.button(__styleYak.Button, ({
$primary
}) => $primary && css(__styleYak._yak_0));"
`);
"import styles from \\"./page.module.css\\";
import { styled, css } from \\"next-yak\\";
import __styleYak from \\"./page.yak.module.css!=!./page?./page.yak.module.css\\";
const Icon = styled.svg(__styleYak.Icon);
const Button = styled.button(__styleYak.Button, ({
$primary
}) => $primary && css(__styleYak.primary_0));"
`);
});
});
Loading

0 comments on commit 5de68d6

Please sign in to comment.