Skip to content

Commit 19665ad

Browse files
authored
feat(shadcn): add --base-color flag (#6864)
1 parent fc0e8c0 commit 19665ad

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

.changeset/fluffy-years-knock.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shadcn": minor
3+
---
4+
5+
add --base-color flag

packages/shadcn/src/commands/init.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { promises as fs } from "fs"
22
import path from "path"
33
import { preFlightInit } from "@/src/preflights/preflight-init"
4-
import { getRegistryBaseColors, getRegistryStyles } from "@/src/registry/api"
4+
import {
5+
BASE_COLORS,
6+
getRegistryBaseColors,
7+
getRegistryStyles,
8+
} from "@/src/registry/api"
59
import { addComponents } from "@/src/utils/add-components"
610
import { TEMPLATES, createProject } from "@/src/utils/create-project"
711
import * as ERRORS from "@/src/utils/errors"
@@ -53,6 +57,23 @@ export const initOptionsSchema = z.object({
5357
message: "Invalid template. Please use 'next' or 'next-monorepo'.",
5458
}
5559
),
60+
baseColor: z
61+
.string()
62+
.optional()
63+
.refine(
64+
(val) => {
65+
if (val) {
66+
return BASE_COLORS.find((color) => color.name === val)
67+
}
68+
69+
return true
70+
},
71+
{
72+
message: `Invalid base color. Please use '${BASE_COLORS.map(
73+
(color) => color.name
74+
).join("', '")}'`,
75+
}
76+
),
5677
})
5778

5879
export const init = new Command()
@@ -64,8 +85,12 @@ export const init = new Command()
6485
)
6586
.option(
6687
"-t, --template <template>",
67-
"the template to use. (next, next-monorepo)",
68-
""
88+
"the template to use. (next, next-monorepo)"
89+
)
90+
.option(
91+
"-b, --base-color <base-color>",
92+
"the base color to use. (neutral, gray, zinc, stone, slate)",
93+
undefined
6994
)
7095
.option("-y, --yes", "skip confirmation prompt.", true)
7196
.option("-d, --defaults,", "use default configuration.", false)
@@ -311,7 +336,7 @@ async function promptForMinimalConfig(
311336
opts: z.infer<typeof initOptionsSchema>
312337
) {
313338
let style = defaultConfig.style
314-
let baseColor = defaultConfig.tailwind.baseColor
339+
let baseColor = opts.baseColor
315340
let cssVariables = defaultConfig.tailwind.cssVariables
316341

317342
if (!opts.defaults) {
@@ -334,7 +359,7 @@ async function promptForMinimalConfig(
334359
initial: 0,
335360
},
336361
{
337-
type: "select",
362+
type: opts.baseColor ? null : "select",
338363
name: "tailwindBaseColor",
339364
message: `Which color would you like to use as the ${highlighter.info(
340365
"base color"
@@ -347,7 +372,7 @@ async function promptForMinimalConfig(
347372
])
348373

349374
style = options.style ?? "new-york"
350-
baseColor = options.tailwindBaseColor
375+
baseColor = options.tailwindBaseColor ?? baseColor
351376
cssVariables = opts.cssVariables
352377
}
353378

packages/shadcn/src/registry/api.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ const agent = process.env.https_proxy
2727

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

30+
export const BASE_COLORS = [
31+
{
32+
name: "neutral",
33+
label: "Neutral",
34+
},
35+
{
36+
name: "gray",
37+
label: "Gray",
38+
},
39+
{
40+
name: "zinc",
41+
label: "Zinc",
42+
},
43+
{
44+
name: "stone",
45+
label: "Stone",
46+
},
47+
{
48+
name: "slate",
49+
label: "Slate",
50+
},
51+
] as const
52+
3053
export async function getRegistryIndex() {
3154
try {
3255
const [result] = await fetchRegistry(["index.json"])
@@ -75,28 +98,7 @@ export async function getRegistryItem(name: string, style: string) {
7598
}
7699

77100
export async function getRegistryBaseColors() {
78-
return [
79-
{
80-
name: "neutral",
81-
label: "Neutral",
82-
},
83-
{
84-
name: "gray",
85-
label: "Gray",
86-
},
87-
{
88-
name: "zinc",
89-
label: "Zinc",
90-
},
91-
{
92-
name: "stone",
93-
label: "Stone",
94-
},
95-
{
96-
name: "slate",
97-
label: "Slate",
98-
},
99-
]
101+
return BASE_COLORS
100102
}
101103

102104
export async function getRegistryBaseColor(baseColor: string) {

0 commit comments

Comments
 (0)