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

fix(shadcn): default next styles are not completely cleared #4922

Merged
merged 3 commits into from
Sep 23, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/hip-ads-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": patch
---

remove next.js default vars
43 changes: 41 additions & 2 deletions packages/shadcn/src/utils/updaters/update-css-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,27 @@ function updateCssVarsPlugin(
}
}

function removeConflictVars(root: Rule | Root) {
const rootRule = root.nodes.find(
(node): node is Rule => node.type === "rule" && node.selector === ":root"
)

if (rootRule) {
const propsToRemove = ["--background", "--foreground"]

rootRule.nodes
.filter(
(node): node is postcss.Declaration =>
node.type === "decl" && propsToRemove.includes(node.prop)
)
.forEach((node) => node.remove())

if (rootRule.nodes.length === 0) {
rootRule.remove()
}
}
}

function cleanupDefaultNextStylesPlugin() {
return {
postcssPlugin: "cleanup-default-next-styles",
Expand All @@ -197,7 +218,9 @@ function cleanupDefaultNextStylesPlugin() {
(node): node is postcss.Declaration =>
node.type === "decl" &&
node.prop === "color" &&
node.value === "rgb(var(--foreground-rgb))"
["rgb(var(--foreground-rgb))", "var(--foreground)"].includes(
node.value
)
)
?.remove()

Expand All @@ -208,7 +231,8 @@ function cleanupDefaultNextStylesPlugin() {
node.type === "decl" &&
node.prop === "background" &&
// This is only going to run on create project, so all good.
node.value.startsWith("linear-gradient")
(node.value.startsWith("linear-gradient") ||
node.value === "var(--background)")
)
})
?.remove()
Expand All @@ -218,6 +242,21 @@ function cleanupDefaultNextStylesPlugin() {
bodyRule.remove()
}
}

removeConflictVars(root)

const darkRootRule = root.nodes.find(
(node): node is Rule =>
node.type === "atrule" &&
node.params === "(prefers-color-scheme: dark)"
)

if (darkRootRule) {
removeConflictVars(darkRootRule)
if (darkRootRule.nodes.length === 0) {
darkRootRule.remove()
}
}
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
Expand Down
Loading