Skip to content

Commit

Permalink
feat(shadcn): add --base-color flag (#6864)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadcn authored Mar 5, 2025
1 parent fc0e8c0 commit 19665ad
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-years-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": minor
---

add --base-color flag
37 changes: 31 additions & 6 deletions packages/shadcn/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { promises as fs } from "fs"
import path from "path"
import { preFlightInit } from "@/src/preflights/preflight-init"
import { getRegistryBaseColors, getRegistryStyles } from "@/src/registry/api"
import {
BASE_COLORS,
getRegistryBaseColors,
getRegistryStyles,
} from "@/src/registry/api"
import { addComponents } from "@/src/utils/add-components"
import { TEMPLATES, createProject } from "@/src/utils/create-project"
import * as ERRORS from "@/src/utils/errors"
Expand Down Expand Up @@ -53,6 +57,23 @@ export const initOptionsSchema = z.object({
message: "Invalid template. Please use 'next' or 'next-monorepo'.",
}
),
baseColor: z
.string()
.optional()
.refine(
(val) => {
if (val) {
return BASE_COLORS.find((color) => color.name === val)
}

return true
},
{
message: `Invalid base color. Please use '${BASE_COLORS.map(
(color) => color.name
).join("', '")}'`,
}
),
})

export const init = new Command()
Expand All @@ -64,8 +85,12 @@ export const init = new Command()
)
.option(
"-t, --template <template>",
"the template to use. (next, next-monorepo)",
""
"the template to use. (next, next-monorepo)"
)
.option(
"-b, --base-color <base-color>",
"the base color to use. (neutral, gray, zinc, stone, slate)",
undefined
)
.option("-y, --yes", "skip confirmation prompt.", true)
.option("-d, --defaults,", "use default configuration.", false)
Expand Down Expand Up @@ -311,7 +336,7 @@ async function promptForMinimalConfig(
opts: z.infer<typeof initOptionsSchema>
) {
let style = defaultConfig.style
let baseColor = defaultConfig.tailwind.baseColor
let baseColor = opts.baseColor
let cssVariables = defaultConfig.tailwind.cssVariables

if (!opts.defaults) {
Expand All @@ -334,7 +359,7 @@ async function promptForMinimalConfig(
initial: 0,
},
{
type: "select",
type: opts.baseColor ? null : "select",
name: "tailwindBaseColor",
message: `Which color would you like to use as the ${highlighter.info(
"base color"
Expand All @@ -347,7 +372,7 @@ async function promptForMinimalConfig(
])

style = options.style ?? "new-york"
baseColor = options.tailwindBaseColor
baseColor = options.tailwindBaseColor ?? baseColor
cssVariables = opts.cssVariables
}

Expand Down
46 changes: 24 additions & 22 deletions packages/shadcn/src/registry/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ const agent = process.env.https_proxy

const registryCache = new Map<string, Promise<any>>()

export const BASE_COLORS = [
{
name: "neutral",
label: "Neutral",
},
{
name: "gray",
label: "Gray",
},
{
name: "zinc",
label: "Zinc",
},
{
name: "stone",
label: "Stone",
},
{
name: "slate",
label: "Slate",
},
] as const

export async function getRegistryIndex() {
try {
const [result] = await fetchRegistry(["index.json"])
Expand Down Expand Up @@ -75,28 +98,7 @@ export async function getRegistryItem(name: string, style: string) {
}

export async function getRegistryBaseColors() {
return [
{
name: "neutral",
label: "Neutral",
},
{
name: "gray",
label: "Gray",
},
{
name: "zinc",
label: "Zinc",
},
{
name: "stone",
label: "Stone",
},
{
name: "slate",
label: "Slate",
},
]
return BASE_COLORS
}

export async function getRegistryBaseColor(baseColor: string) {
Expand Down

0 comments on commit 19665ad

Please sign in to comment.