Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow disable color vars, radius, global styles #5

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,42 @@ function generateColorCSSVars(color: ThemeCSSVars) {
.join('\n')
}

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

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 Down Expand Up @@ -58,27 +90,20 @@ 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} {
${lightVars}
${generateRadiusCSSVars(radius)}
}
let cssStyle = ''

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

return `:root {
${lightVars}
${generateRadiusCSSVars(radius)}
}
cssStyle += colorCSSVarsStyles(lightVars, darkVars, { radius, themeName: !onlyOne && name })
}

.dark {
${darkVars}
}`
return cssStyle
}
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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> {
NamesMT marked this conversation as resolved.
Show resolved Hide resolved
return {
name: 'unocss-preset-shadcn',
preflights: [
Expand All @@ -21,14 +25,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() : ''}
hyoban marked this conversation as resolved.
Show resolved Hide resolved
`,
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export type ThemeOptions = {
/**
* @default 'zinc'
*/
color?: ColorOptions
color?: ColorOptions | false
/**
* @default 0.5
*/
radius?: number
radius?: number | false
}

export type PresetShadcnOptions = ArrayOrSingle<ThemeOptions>
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
Loading