Skip to content

Commit 69913b8

Browse files
committed
优化主题样式和全局样式引入
1 parent 79fdf09 commit 69913b8

File tree

3 files changed

+74
-65
lines changed

3 files changed

+74
-65
lines changed

src/assets/styles/globals.css

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* {
1313
scrollbar-color: hsl(var(--scrollbar-color)) transparent;
1414
scrollbar-width: thin;
15-
border-color: hsl(var(--border));
1615
}
1716

1817
html {
@@ -22,8 +21,6 @@ html {
2221
body {
2322
box-sizing: border-box;
2423
margin: 0;
25-
color: hsl(var(--foreground));
26-
background: hsl(var(--background));
2724
-webkit-tap-highlight-color: transparent;
2825
}
2926

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import './utils/system.copyright'
1010

1111
// 加载 svg 图标
1212
import 'virtual:svg-icons-register'
13-
import 'virtual:uno.css'
13+
// UnoCSS
1414
import '@unocss/reset/tailwind-compat.css'
15+
import 'virtual:uno.css'
1516
// 全局样式
1617
import '@/assets/styles/globals.css'
1718

uno.config.ts

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Theme } from 'unocss/preset-uno'
2-
import { entriesToCss, toArray } from '@unocss/core'
2+
import { entriesToCss } from '@unocss/core'
33
import presetLegacyCompat from '@unocss/preset-legacy-compat'
44
import presetRemToPx from '@unocss/preset-rem-to-px'
55
import presetSafeArea from '@yeungkc/unocss-preset-safe-area'
@@ -55,66 +55,6 @@ export default defineConfig<Theme>({
5555
return cls.join(' ')
5656
}],
5757
],
58-
preflights: [
59-
{
60-
getCSS: () => {
61-
const returnCss: any = []
62-
// 明亮主题
63-
const lightCss = entriesToCss(Object.entries(lightTheme))
64-
const lightRoots = toArray([`*,::before,::after`, `::backdrop`])
65-
returnCss.push(lightRoots.map(root => `${root}{${lightCss}}`).join(''))
66-
// 暗黑主题
67-
const darkCss = entriesToCss(Object.entries(darkTheme))
68-
const darkRoots = toArray([`html.dark,html.dark *,html.dark ::before,html.dark ::after`, `html.dark ::backdrop`])
69-
returnCss.push(darkRoots.map(root => `${root}{${darkCss}}`).join(''))
70-
71-
return returnCss.join('')
72-
},
73-
},
74-
],
75-
theme: {
76-
colors: {
77-
border: 'hsl(var(--border))',
78-
input: 'hsl(var(--input))',
79-
ring: 'hsl(var(--ring))',
80-
background: 'hsl(var(--background))',
81-
foreground: 'hsl(var(--foreground))',
82-
primary: {
83-
DEFAULT: 'hsl(var(--primary))',
84-
foreground: 'hsl(var(--primary-foreground))',
85-
},
86-
secondary: {
87-
DEFAULT: 'hsl(var(--secondary))',
88-
foreground: 'hsl(var(--secondary-foreground))',
89-
},
90-
destructive: {
91-
DEFAULT: 'hsl(var(--destructive))',
92-
foreground: 'hsl(var(--destructive-foreground))',
93-
},
94-
muted: {
95-
DEFAULT: 'hsl(var(--muted))',
96-
foreground: 'hsl(var(--muted-foreground))',
97-
},
98-
accent: {
99-
DEFAULT: 'hsl(var(--accent))',
100-
foreground: 'hsl(var(--accent-foreground))',
101-
},
102-
popover: {
103-
DEFAULT: 'hsl(var(--popover))',
104-
foreground: 'hsl(var(--popover-foreground))',
105-
},
106-
card: {
107-
DEFAULT: 'hsl(var(--card))',
108-
foreground: 'hsl(var(--card-foreground))',
109-
},
110-
},
111-
borderRadius: {
112-
xl: 'calc(var(--radius) + 4px)',
113-
lg: 'var(--radius)',
114-
md: 'calc(var(--radius) - 2px)',
115-
sm: 'calc(var(--radius) - 4px)',
116-
},
117-
},
11858
presets: [
11959
presetWind3(),
12060
presetAnimations(),
@@ -131,6 +71,77 @@ export default defineConfig<Theme>({
13171
presetLegacyCompat({
13272
legacyColorSpace: true,
13373
}),
74+
{
75+
name: 'unocss-preset-shadcn',
76+
preflights: [
77+
{
78+
getCSS: () => {
79+
const returnCss: any = []
80+
// 明亮主题
81+
const lightCss = entriesToCss(Object.entries(lightTheme))
82+
returnCss.push(`:root{${lightCss}}`)
83+
// 暗黑主题
84+
const darkCss = entriesToCss(Object.entries(darkTheme))
85+
returnCss.push(`html.dark{${darkCss}}`)
86+
return `
87+
${returnCss.join('\n')}
88+
89+
* {
90+
border-color: hsl(var(--border));
91+
}
92+
93+
body {
94+
color: hsl(var(--foreground));
95+
background: hsl(var(--background));
96+
}
97+
`
98+
},
99+
},
100+
],
101+
theme: {
102+
colors: {
103+
border: 'hsl(var(--border))',
104+
input: 'hsl(var(--input))',
105+
ring: 'hsl(var(--ring))',
106+
background: 'hsl(var(--background))',
107+
foreground: 'hsl(var(--foreground))',
108+
primary: {
109+
DEFAULT: 'hsl(var(--primary))',
110+
foreground: 'hsl(var(--primary-foreground))',
111+
},
112+
secondary: {
113+
DEFAULT: 'hsl(var(--secondary))',
114+
foreground: 'hsl(var(--secondary-foreground))',
115+
},
116+
destructive: {
117+
DEFAULT: 'hsl(var(--destructive))',
118+
foreground: 'hsl(var(--destructive-foreground))',
119+
},
120+
muted: {
121+
DEFAULT: 'hsl(var(--muted))',
122+
foreground: 'hsl(var(--muted-foreground))',
123+
},
124+
accent: {
125+
DEFAULT: 'hsl(var(--accent))',
126+
foreground: 'hsl(var(--accent-foreground))',
127+
},
128+
popover: {
129+
DEFAULT: 'hsl(var(--popover))',
130+
foreground: 'hsl(var(--popover-foreground))',
131+
},
132+
card: {
133+
DEFAULT: 'hsl(var(--card))',
134+
foreground: 'hsl(var(--card-foreground))',
135+
},
136+
},
137+
borderRadius: {
138+
xl: 'calc(var(--radius) + 4px)',
139+
lg: 'var(--radius)',
140+
md: 'calc(var(--radius) - 2px)',
141+
sm: 'calc(var(--radius) - 4px)',
142+
},
143+
},
144+
},
134145
],
135146
transformers: [
136147
transformerDirectives(),

0 commit comments

Comments
 (0)