Skip to content

Commit

Permalink
feat: support custom dark mode selector (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainstar authored Jul 5, 2024
1 parent 6676c88 commit d144e59
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ function generateColorCSSVars(color: ThemeCSSVars) {
.join('\n')
}

function colorCSSVarsStyles(lightVars: string, darkVars: string, { radius, themeName }: { radius?: number | false, themeName?: string | false }) {
function colorCSSVarsStyles(lightVars: string, darkVars: string, { radius, themeName, darkSelector }: { radius?: number | false, themeName?: string | false, darkSelector: string }) {
return `
${themeName ? `.theme-${themeName}` : ':root'} {
${lightVars}
${radius ? generateRadiusCSSVars(radius) : ''}
}
${themeName ? `.dark .theme-${themeName}` : '.dark'} {
${themeName ? `${darkSelector} .theme-${themeName}` : darkSelector} {
${darkVars}
}`
}
Expand Down Expand Up @@ -89,7 +89,7 @@ export function generateCSSVars(
if (Array.isArray(theme))
return theme.map(t => generateCSSVars(t, false)).join('\n')

const { color = 'zinc', radius = 0.5 } = theme
const { color = 'zinc', radius = 0.5, darkSelector = '.dark' } = theme

let cssStyle = ''

Expand All @@ -102,7 +102,7 @@ export function generateCSSVars(
const lightVars = generateColorCSSVars(light)
const darkVars = generateColorCSSVars(dark)

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

return cssStyle
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export interface ThemeOptions {
* @default 0.5
*/
radius?: number | false,
/**
* @default '.dark'
*/
darkSelector?: string,
}

export type PresetShadcnOptions = ArrayOrSingle<ThemeOptions>
8 changes: 8 additions & 0 deletions test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ describe('generate-theme-css-var', () => {
]),
).toMatchFileSnapshot('snapshot/multiple.css')
})

it('custom dark selector', async () => {
await expect(
generateCSSVars({
darkSelector: '.custom-dark',
}),
).toMatchFileSnapshot('snapshot/dark-selector.css')
})
})
44 changes: 44 additions & 0 deletions test/snapshot/dark-selector.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

:root {
--background: 0 0% 100%;
--foreground: 240 10% 3.9%;
--card: 0 0% 100%;
--card-foreground: 240 10% 3.9%;
--popover: 0 0% 100%;
--popover-foreground: 240 10% 3.9%;
--primary: 240 5.9% 10%;
--primary-foreground: 0 0% 98%;
--secondary: 240 4.8% 95.9%;
--secondary-foreground: 240 5.9% 10%;
--muted: 240 4.8% 95.9%;
--muted-foreground: 240 3.8% 46.1%;
--accent: 240 4.8% 95.9%;
--accent-foreground: 240 5.9% 10%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 0 0% 98%;
--border: 240 5.9% 90%;
--input: 240 5.9% 90%;
--ring: 240 5.9% 10%;
--radius: 0.5rem;
}
.custom-dark {
--background: 240 10% 3.9%;
--foreground: 0 0% 98%;
--card: 240 10% 3.9%;
--card-foreground: 0 0% 98%;
--popover: 240 10% 3.9%;
--popover-foreground: 0 0% 98%;
--primary: 0 0% 98%;
--primary-foreground: 240 5.9% 10%;
--secondary: 240 3.7% 15.9%;
--secondary-foreground: 0 0% 98%;
--muted: 240 3.7% 15.9%;
--muted-foreground: 240 5% 64.9%;
--accent: 240 3.7% 15.9%;
--accent-foreground: 0 0% 98%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 0 0% 98%;
--border: 240 3.7% 15.9%;
--input: 240 3.7% 15.9%;
--ring: 240 4.9% 83.9%;
}

0 comments on commit d144e59

Please sign in to comment.