-
-
Notifications
You must be signed in to change notification settings - Fork 780
/
index.ts
107 lines (96 loc) · 2.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import type { Postprocessor, Preflight, PreflightContext, Preset, PresetOptions } from '@unocss/core'
import { preflights } from './preflights'
import { rules } from './rules'
import type { Theme, ThemeAnimation } from './theme'
import { theme } from './theme'
import { variants } from './variants'
export { preflights } from './preflights'
export { theme, colors } from './theme'
export { parseColor } from './utils'
export type { ThemeAnimation, Theme }
export interface DarkModeSelectors {
/**
* Selector for light variant.
*
* @default '.light'
*/
light?: string
/**
* Selector for dark variant.
*
* @default '.dark'
*/
dark?: string
}
export interface PresetMiniOptions extends PresetOptions {
/**
* Dark mode options
*
* @default 'class'
*/
dark?: 'class' | 'media' | DarkModeSelectors
/**
* Generate pesudo selector as `[group=""]` instead of `.group`
*
* @default false
*/
attributifyPseudo?: Boolean
/**
* Prefix for CSS variables.
*
* @default 'un-'
*/
variablePrefix?: string
/**
* Utils prefix
*
* @default undefined
*/
prefix?: string | string[]
/**
* Generate preflight
*
* @default true
*/
preflight?: boolean
}
export const presetMini = (options: PresetMiniOptions = {}): Preset<Theme> => {
options.dark = options.dark ?? 'class'
options.attributifyPseudo = options.attributifyPseudo ?? false
options.preflight = options.preflight ?? true
return {
name: '@unocss/preset-mini',
theme,
rules,
variants: variants(options),
options,
postprocess: options.variablePrefix && options.variablePrefix !== 'un-'
? VarPrefixPostprocessor(options.variablePrefix)
: undefined,
preflights: options.preflight ? normalizePreflights(preflights, options.variablePrefix) : [],
prefix: options.prefix,
}
}
export default presetMini
export function VarPrefixPostprocessor(prefix: string): Postprocessor {
return (obj) => {
obj.entries.forEach((i) => {
i[0] = i[0].replace(/^--un-/, `--${prefix}`)
if (typeof i[1] === 'string')
i[1] = i[1].replace(/var\(--un-/g, `var(--${prefix}`)
})
}
}
export function normalizePreflights(preflights: Preflight[], variablePrefix?: string) {
if (variablePrefix && variablePrefix !== 'un-') {
return preflights.map(p => ({
...p,
getCSS: (() => async (ctx: PreflightContext) => {
const css = await p.getCSS(ctx)
if (css)
return css.replace(/--un-/g, `--${variablePrefix}`)
})(),
}))
}
return preflights
}