Skip to content

Commit

Permalink
feat: more flexible configs: disable color vars, disable global styles
Browse files Browse the repository at this point in the history
  • Loading branch information
NamesMT committed May 10, 2024
1 parent 24e0f30 commit 50aaa5d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 30 deletions.
62 changes: 48 additions & 14 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ function generateRadiusCSSVars(radius: number) {
return ` --radius: ${radius}rem;`
}

function radiusCSSVarsStyles(radius: number) {
return `
:root {
${generateRadiusCSSVars(radius)}
}
`
}

export function generateGlobalStyles() {
return `
* {
border-color: hsl(var(--border));
}
body {
color: hsl(var(--foreground));
background: hsl(var(--background));
}
`
}

function getBuiltInTheme(name: string) {
const theme = themes.find(t => t.name === name)
if (!theme)
Expand All @@ -36,16 +57,16 @@ function getColorTheme(color: ColorOptions) {

if (typeof color === 'string') {
name = color
;({ light, dark } = getBuiltInTheme(color))
; ({ light, dark } = getBuiltInTheme(color))
}
else if ('base' in color) {
name = color.base
;({ light, dark } = mergeDeep(getBuiltInTheme(color.base), color.color))
; ({ light, dark } = mergeDeep(getBuiltInTheme(color.base), color.color))
}
else {
// eslint-disable-next-line prefer-destructuring
name = color.name
;({ light, dark } = color)
; ({ light, dark } = color)
}
return { light, dark, name }
}
Expand All @@ -58,26 +79,39 @@ export function generateCSSVars(
return theme.map(t => generateCSSVars(t, false)).join('\n')

const { color = 'zinc', radius = 0.5 } = theme
const { light, dark, name } = getColorTheme(color)
const lightVars = generateColorCSSVars(light)
const darkVars = generateColorCSSVars(dark)

if (!onlyOne) {
return `.theme-${name} {
let cssStyle = ''

if (!color) {
if (radius)
cssStyle += radiusCSSVarsStyles(radius)
}
else {
const { light, dark, name } = getColorTheme(color)
const lightVars = generateColorCSSVars(light)
const darkVars = generateColorCSSVars(dark)

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

return cssStyle
}

function colorCSSVarsStyles(lightVars: string, darkVars: string, { radius, themeName }: { radius: number, themeName?: string | false }) {
return themeName
? `
.theme-${themeName} {
${lightVars}
${generateRadiusCSSVars(radius)}
}
.dark .theme-${name} {
.dark .theme-${themeName} {
${darkVars}
}`
}

return `:root {
: `
:root {
${lightVars}
${generateRadiusCSSVars(radius)}
}
.dark {
${darkVars}
}`
Expand Down
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { Preset } from 'unocss'
import type { Theme } from 'unocss/preset-mini'

import { generateCSSVars } from './generate'
import { generateCSSVars, generateGlobalStyles } from './generate'
import { themes } from './themes'
import type { PresetShadcnOptions } from './types'

export const builtinColors = themes.map(theme => theme.name)
export const builtinRadiuses = [0, 0.3, 0.5, 0.75, 1] as const

export function presetShadcn(options: PresetShadcnOptions = {}): Preset<Theme> {
/**
* @param globals Generates global variables, like *.border-color, body.color, body.background - @default true
*/
export function presetShadcn(options: PresetShadcnOptions = {}, globals?: boolean): Preset<Theme> {
return {
name: 'unocss-preset-shadcn',
preflights: [
Expand All @@ -21,14 +24,7 @@ export function presetShadcn(options: PresetShadcnOptions = {}): Preset<Theme> {
${generateCSSVars(options)}
* {
border-color: hsl(var(--border));
}
body {
color: hsl(var(--foreground));
background: hsl(var(--background));
}
${globals ? generateGlobalStyles() : ''}
`,
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type ThemeOptions = {
/**
* @default 'zinc'
*/
color?: ColorOptions
color?: ColorOptions | null
/**
* @default 0.5
*/
Expand Down
1 change: 0 additions & 1 deletion test/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
--ring: 240 5.9% 10%;
--radius: 1rem;
}

.dark {
--background: 240 10% 3.9%;
--foreground: 0 0% 98%;
Expand Down
3 changes: 1 addition & 2 deletions test/multiple.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
--ring: 240 5.9% 10%;
--radius: 0.5rem;
}

.dark .theme-zinc {
--background: 240 10% 3.9%;
--foreground: 0 0% 98%;
Expand All @@ -42,6 +41,7 @@
--input: 240 3.7% 15.9%;
--ring: 240 4.9% 83.9%;
}

.theme-neutral {
--background: 0 0% 100%;
--foreground: 0 0% 3.9%;
Expand All @@ -64,7 +64,6 @@
--ring: 0 0% 3.9%;
--radius: 0.75rem;
}

.dark .theme-neutral {
--background: 0 0% 3.9%;
--foreground: 0 0% 98%;
Expand Down
1 change: 0 additions & 1 deletion test/neutral-0.75.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
--ring: 0 0% 3.9%;
--radius: 0.75rem;
}

.dark {
--background: 0 0% 3.9%;
--foreground: 0 0% 98%;
Expand Down
1 change: 0 additions & 1 deletion test/zinc-0.5.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
--ring: 240 5.9% 10%;
--radius: 0.5rem;
}

.dark {
--background: 240 10% 3.9%;
--foreground: 0 0% 98%;
Expand Down

0 comments on commit 50aaa5d

Please sign in to comment.