Skip to content

Commit d144e59

Browse files
authored
feat: support custom dark mode selector (#10)
1 parent 6676c88 commit d144e59

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/generate.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ function generateColorCSSVars(color: ThemeCSSVars) {
1515
.join('\n')
1616
}
1717

18-
function colorCSSVarsStyles(lightVars: string, darkVars: string, { radius, themeName }: { radius?: number | false, themeName?: string | false }) {
18+
function colorCSSVarsStyles(lightVars: string, darkVars: string, { radius, themeName, darkSelector }: { radius?: number | false, themeName?: string | false, darkSelector: string }) {
1919
return `
2020
${themeName ? `.theme-${themeName}` : ':root'} {
2121
${lightVars}
2222
${radius ? generateRadiusCSSVars(radius) : ''}
2323
}
24-
${themeName ? `.dark .theme-${themeName}` : '.dark'} {
24+
${themeName ? `${darkSelector} .theme-${themeName}` : darkSelector} {
2525
${darkVars}
2626
}`
2727
}
@@ -89,7 +89,7 @@ export function generateCSSVars(
8989
if (Array.isArray(theme))
9090
return theme.map(t => generateCSSVars(t, false)).join('\n')
9191

92-
const { color = 'zinc', radius = 0.5 } = theme
92+
const { color = 'zinc', radius = 0.5, darkSelector = '.dark' } = theme
9393

9494
let cssStyle = ''
9595

@@ -102,7 +102,7 @@ export function generateCSSVars(
102102
const lightVars = generateColorCSSVars(light)
103103
const darkVars = generateColorCSSVars(dark)
104104

105-
cssStyle += colorCSSVarsStyles(lightVars, darkVars, { radius, themeName: !onlyOne && name })
105+
cssStyle += colorCSSVarsStyles(lightVars, darkVars, { radius, themeName: !onlyOne && name, darkSelector })
106106
}
107107

108108
return cssStyle

src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export interface ThemeOptions {
2020
* @default 0.5
2121
*/
2222
radius?: number | false,
23+
/**
24+
* @default '.dark'
25+
*/
26+
darkSelector?: string,
2327
}
2428

2529
export type PresetShadcnOptions = ArrayOrSingle<ThemeOptions>

test/generate.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,12 @@ describe('generate-theme-css-var', () => {
154154
]),
155155
).toMatchFileSnapshot('snapshot/multiple.css')
156156
})
157+
158+
it('custom dark selector', async () => {
159+
await expect(
160+
generateCSSVars({
161+
darkSelector: '.custom-dark',
162+
}),
163+
).toMatchFileSnapshot('snapshot/dark-selector.css')
164+
})
157165
})

test/snapshot/dark-selector.css

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
:root {
3+
--background: 0 0% 100%;
4+
--foreground: 240 10% 3.9%;
5+
--card: 0 0% 100%;
6+
--card-foreground: 240 10% 3.9%;
7+
--popover: 0 0% 100%;
8+
--popover-foreground: 240 10% 3.9%;
9+
--primary: 240 5.9% 10%;
10+
--primary-foreground: 0 0% 98%;
11+
--secondary: 240 4.8% 95.9%;
12+
--secondary-foreground: 240 5.9% 10%;
13+
--muted: 240 4.8% 95.9%;
14+
--muted-foreground: 240 3.8% 46.1%;
15+
--accent: 240 4.8% 95.9%;
16+
--accent-foreground: 240 5.9% 10%;
17+
--destructive: 0 84.2% 60.2%;
18+
--destructive-foreground: 0 0% 98%;
19+
--border: 240 5.9% 90%;
20+
--input: 240 5.9% 90%;
21+
--ring: 240 5.9% 10%;
22+
--radius: 0.5rem;
23+
}
24+
.custom-dark {
25+
--background: 240 10% 3.9%;
26+
--foreground: 0 0% 98%;
27+
--card: 240 10% 3.9%;
28+
--card-foreground: 0 0% 98%;
29+
--popover: 240 10% 3.9%;
30+
--popover-foreground: 0 0% 98%;
31+
--primary: 0 0% 98%;
32+
--primary-foreground: 240 5.9% 10%;
33+
--secondary: 240 3.7% 15.9%;
34+
--secondary-foreground: 0 0% 98%;
35+
--muted: 240 3.7% 15.9%;
36+
--muted-foreground: 240 5% 64.9%;
37+
--accent: 240 3.7% 15.9%;
38+
--accent-foreground: 0 0% 98%;
39+
--destructive: 0 62.8% 30.6%;
40+
--destructive-foreground: 0 0% 98%;
41+
--border: 240 3.7% 15.9%;
42+
--input: 240 3.7% 15.9%;
43+
--ring: 240 4.9% 83.9%;
44+
}

0 commit comments

Comments
 (0)