Skip to content

Commit 549c991

Browse files
authored
feat: support for set dark to default theme (#78)
* feat: support for set dark to default theme * test: support for set dark to default theme
1 parent 1877238 commit 549c991

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/index.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ interface ThemeValue {
4141
export function presetTheme<T extends Record<string, any>>(options: PresetThemeOptions<T>): Preset<T> {
4242
const { prefix = '--un-preset-theme', theme } = options
4343
const selectors: Selectors = { light: ':root', ...options.selectors }
44-
if (!theme.light)
44+
let originalThemeKey: 'dark' | 'light' = 'light'
45+
if (!theme.dark) {
46+
theme.dark = {} as T
47+
originalThemeKey = 'dark'
48+
}
49+
if (!theme.light) {
4550
theme.light = {} as T
51+
originalThemeKey = 'light'
52+
}
4653
const keys = Object.keys(theme)
4754
const varsRE = new RegExp(`var\\((${prefix}[\\w-]*)\\)`)
4855
const themeValues = new Map<string, ThemeValue>()
@@ -62,7 +69,7 @@ export function presetTheme<T extends Record<string, any>>(options: PresetThemeO
6269
(obj, key) => {
6370
let themeValue
6471
= getThemeVal(theme[key], themeKeys, index)
65-
|| (key === 'light' ? getThemeVal(originalTheme, themeKeys) : null)
72+
|| (key === originalThemeKey ? getThemeVal(originalTheme, themeKeys) : null)
6673
if (themeValue) {
6774
if (isColor) {
6875
const cssColor = parseCssColor(themeValue)
@@ -230,7 +237,7 @@ export function presetTheme<T extends Record<string, any>>(options: PresetThemeO
230237

231238
return res
232239
.sort((a, b) => {
233-
const regexStr = `^${selectors.light}|^@media \\(prefers-color-scheme:`
240+
const regexStr = `^${selectors[originalThemeKey]}|^@media \\(prefers-color-scheme:`
234241
if (a.match(regexStr)?.length)
235242
return b.match(regexStr)?.length ? 0 : -1
236243
return 1

tests/index.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,44 @@ describe('theme', () => {
186186
`)
187187
})
188188

189+
it('selectors for default dark theme', async () => {
190+
const uno = createGenerator({
191+
theme: {
192+
colors: {
193+
primary: '#123456',
194+
},
195+
},
196+
presets: [
197+
presetUno(),
198+
presetTheme<Theme>({
199+
theme: {
200+
light: {
201+
colors: {
202+
primary: '#654321',
203+
},
204+
},
205+
},
206+
selectors: {
207+
dark: ':root,.dark',
208+
light: '.light',
209+
},
210+
}),
211+
],
212+
})
213+
214+
const targets = ['text-primary']
215+
const { css } = await uno.generate(targets.join('\n'))
216+
expect(css).toMatchInlineSnapshot(`
217+
"/* layer: preflights */
218+
*,::before,::after{--un-rotate:0;--un-rotate-x:0;--un-rotate-y:0;--un-rotate-z:0;--un-scale-x:1;--un-scale-y:1;--un-scale-z:1;--un-skew-x:0;--un-skew-y:0;--un-translate-x:0;--un-translate-y:0;--un-translate-z:0;--un-pan-x: ;--un-pan-y: ;--un-pinch-zoom: ;--un-scroll-snap-strictness:proximity;--un-ordinal: ;--un-slashed-zero: ;--un-numeric-figure: ;--un-numeric-spacing: ;--un-numeric-fraction: ;--un-border-spacing-x:0;--un-border-spacing-y:0;--un-ring-offset-shadow:0 0 rgb(0 0 0 / 0);--un-ring-shadow:0 0 rgb(0 0 0 / 0);--un-shadow-inset: ;--un-shadow:0 0 rgb(0 0 0 / 0);--un-ring-inset: ;--un-ring-offset-width:0px;--un-ring-offset-color:#fff;--un-ring-width:0px;--un-ring-color:rgb(147 197 253 / 0.5);--un-blur: ;--un-brightness: ;--un-contrast: ;--un-drop-shadow: ;--un-grayscale: ;--un-hue-rotate: ;--un-invert: ;--un-saturate: ;--un-sepia: ;--un-backdrop-blur: ;--un-backdrop-brightness: ;--un-backdrop-contrast: ;--un-backdrop-grayscale: ;--un-backdrop-hue-rotate: ;--un-backdrop-invert: ;--un-backdrop-opacity: ;--un-backdrop-saturate: ;--un-backdrop-sepia: ;}::backdrop{--un-rotate:0;--un-rotate-x:0;--un-rotate-y:0;--un-rotate-z:0;--un-scale-x:1;--un-scale-y:1;--un-scale-z:1;--un-skew-x:0;--un-skew-y:0;--un-translate-x:0;--un-translate-y:0;--un-translate-z:0;--un-pan-x: ;--un-pan-y: ;--un-pinch-zoom: ;--un-scroll-snap-strictness:proximity;--un-ordinal: ;--un-slashed-zero: ;--un-numeric-figure: ;--un-numeric-spacing: ;--un-numeric-fraction: ;--un-border-spacing-x:0;--un-border-spacing-y:0;--un-ring-offset-shadow:0 0 rgb(0 0 0 / 0);--un-ring-shadow:0 0 rgb(0 0 0 / 0);--un-shadow-inset: ;--un-shadow:0 0 rgb(0 0 0 / 0);--un-ring-inset: ;--un-ring-offset-width:0px;--un-ring-offset-color:#fff;--un-ring-width:0px;--un-ring-color:rgb(147 197 253 / 0.5);--un-blur: ;--un-brightness: ;--un-contrast: ;--un-drop-shadow: ;--un-grayscale: ;--un-hue-rotate: ;--un-invert: ;--un-saturate: ;--un-sepia: ;--un-backdrop-blur: ;--un-backdrop-brightness: ;--un-backdrop-contrast: ;--un-backdrop-grayscale: ;--un-backdrop-hue-rotate: ;--un-backdrop-invert: ;--un-backdrop-opacity: ;--un-backdrop-saturate: ;--un-backdrop-sepia: ;}
219+
/* layer: theme */
220+
:root,.dark{--un-preset-theme-colors-primary:18 52 86;}
221+
.light{--un-preset-theme-colors-primary:101 67 33;}
222+
/* layer: default */
223+
.text-primary{--un-text-opacity:1;color:rgb(var(--un-preset-theme-colors-primary) / var(--un-text-opacity));}"
224+
`)
225+
})
226+
189227
it('color opacity', async () => {
190228
const uno = createUno(
191229
{

0 commit comments

Comments
 (0)