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

feat(shadcn): handle @apply directive in css prop #7104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/shadcn/src/utils/updaters/update-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,24 @@ function processRule(parent: Root | AtRule, selector: string, properties: any) {

if (typeof properties === "object") {
for (const [prop, value] of Object.entries(properties)) {
if (typeof value === "string") {
// Handle @apply
if (prop === "@apply" && typeof value === "string") {
const applyAtRule = postcss.atRule({
name: "apply",
params: value,
raws: { semicolon: true, before: "\n " },
})

// Replace existing apply or add new one
const existingApply = rule.nodes?.find(
(node): node is AtRule =>
node.type === "atrule" && node.name === "apply"
)

existingApply
? existingApply.replaceWith(applyAtRule)
: rule.append(applyAtRule)
} else if (typeof value === "string") {
const decl = postcss.decl({
prop,
value: value,
Expand Down Expand Up @@ -313,6 +330,18 @@ function processRule(parent: Root | AtRule, selector: string, properties: any) {
const clone = node.clone()
clone.raws.before = "\n "
rule?.append(clone)
} else if (node.type === "atrule" && node.name === "apply") {
// Handle @apply in string, replace existing one if present
const clone = node.clone()
clone.raws.before = "\n "

const existingApply = rule.nodes?.find(
(node): node is AtRule =>
node.type === "atrule" && node.name === "apply"
)
existingApply
? existingApply.replaceWith(clone)
: rule?.append(clone)
}
})
}
Expand Down
79 changes: 79 additions & 0 deletions packages/shadcn/test/utils/updaters/update-css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ describe("transformCss", () => {
`)
})

test("should add styles with @apply", async () => {
const input = `@tailwind base;
@tailwind components;
@tailwind utilities;`

const result = await transformCss(input, {
"@layer components": {
".card": {
"@apply": "rounded-lg bg-white p-6 shadow-xl",
},
},
})

expect(result).toMatchInlineSnapshot(`
"@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
.card {
@apply rounded-lg bg-white p-6 shadow-xl;
}
}"
`)
})

test("should update existing rules", async () => {
const input = `@import "tailwindcss";

Expand Down Expand Up @@ -159,6 +185,34 @@ describe("transformCss", () => {
`)
})

test("should update existing @apply", async () => {
const input = `@import "tailwindcss";

@layer components {
.card {
@apply bg-white p-4;
}
}`

const result = await transformCss(input, {
"@layer components": {
".card": {
"@apply": "rounded-lg bg-background p-6 shadow-xl",
},
},
})

expect(result).toMatchInlineSnapshot(`
"@import "tailwindcss";

@layer components {
.card {
@apply rounded-lg bg-background p-6 shadow-xl;
}
}"
`)
})

test("should add multiple rules and types", async () => {
const input = `@tailwind base;
@tailwind components;
Expand Down Expand Up @@ -268,6 +322,31 @@ describe("transformCss", () => {
`)
})

test("should handle direct string content with @apply", async () => {
const input = `@tailwind base;
@tailwind components;
@tailwind utilities;`

const result = await transformCss(input, {
"@layer base": {
body: "@apply bg-background font-sans text-foreground; line-height: 1.5;",
},
})

expect(result).toMatchInlineSnapshot(`
"@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
body {
@apply bg-background font-sans text-foreground;
line-height: 1.5;
}
}"
`)
})

test("should handle nested at-rules", async () => {
const input = `@tailwind base;
@tailwind components;
Expand Down