-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
defaultTailwindConfig.ts
180 lines (150 loc) · 4.65 KB
/
defaultTailwindConfig.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import toArray from './util/toArray'
import type { PluginAPI } from 'tailwindcss/types/config'
const AMPERSAND_AFTER = /&(.+)/g
const AMPERSAND = /&/g
function stripAmpersands(string: string): string {
return typeof string === 'string'
? string.replace(AMPERSAND, '').trim()
: string
}
const EXTRA_VARIANTS = [
['all', '& *'],
['all-child', '& > *'],
['sibling', '& ~ *'],
['hocus', ['&:hover', '&:focus']],
'link',
'read-write',
['svg', '& svg'],
['even-of-type', '&:nth-of-type(even)'],
['odd-of-type', '&:nth-of-type(odd)'],
]
const EXTRA_NOT_VARIANTS = [
// Positional
['first', '&:first-child'],
['last', '&:last-child'],
['only', '&:only-child'],
['odd', '&:nth-child(odd)'],
['even', '&:nth-child(even)'],
'first-of-type',
'last-of-type',
'only-of-type',
// State
'target',
['open', '&[open]'],
// Forms
'default',
'checked',
'indeterminate',
'placeholder-shown',
'autofill',
'optional',
'required',
'valid',
'invalid',
'in-range',
'out-of-range',
'read-only',
// Content
'empty',
// Interactive
'focus-within',
'hover',
'focus',
'focus-visible',
'active',
'enabled',
'disabled',
]
function defaultVariants({ config, addVariant }: PluginAPI): void {
const extraVariants = EXTRA_VARIANTS.flatMap(v => {
let [name, selector] = toArray(v)
selector = selector || `&:${String(name)}`
const variant = [name, selector]
// Create a :not() version of the selectors above
const notVariant = [
`not-${String(name)}`,
(toArray(selector) as string[]).map(
(s: string) => `&:not(${stripAmpersands(s)})`
),
]
return [variant, notVariant]
})
// Create :not() versions of these selectors
const notPseudoVariants = EXTRA_NOT_VARIANTS.map(v => {
const [name, selector] = toArray(v)
const notConfig = [
`not-${name as string}`,
(toArray(selector || `&:${name as string}`) as string[]).map(
s => `&:not(${stripAmpersands(s)})`
),
]
return notConfig
})
const variants = [...extraVariants, ...notPseudoVariants]
for (const [name, selector] of variants) {
addVariant(name as string, toArray(selector) as string[])
}
for (const [name, selector] of variants) {
const groupSelector = (toArray(selector) as string[]).map(s =>
s.replace(AMPERSAND_AFTER, ':merge(.group)$1 &')
)
addVariant(`group-${name as string}`, groupSelector)
}
for (const [name, selector] of variants) {
const peerSelector = (toArray(selector) as string[]).map(s =>
s.replace(AMPERSAND_AFTER, ':merge(.peer)$1 ~ &')
)
addVariant(`peer-${name as string}`, peerSelector)
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/@media/any-pointer
addVariant('any-pointer-none', '@media (any-pointer: none)')
addVariant('any-pointer-fine', '@media (any-pointer: fine)')
addVariant('any-pointer-coarse', '@media (any-pointer: coarse)')
// https://developer.mozilla.org/en-US/docs/Web/CSS/@media/pointer
addVariant('pointer-none', '@media (pointer: none)')
addVariant('pointer-fine', '@media (pointer: fine)')
addVariant('pointer-coarse', '@media (pointer: coarse)')
// https://developer.mozilla.org/en-US/docs/Web/CSS/@media/any-hover
addVariant('any-hover-none', '@media (any-hover: none)')
addVariant('any-hover', '@media (any-hover: hover)')
// https://developer.mozilla.org/en-US/docs/Web/CSS/@media/hover
addVariant('can-hover', '@media (hover: hover)')
addVariant('cant-hover', '@media (hover: none)')
addVariant('screen', '@media screen')
// Light mode
// eslint-disable-next-line unicorn/prefer-spread
let [mode, className = '.light'] = ([] as string[]).concat(
config('lightMode', 'media')
)
// @ts-expect-error Source types don't include boolean
if (mode === false) mode = 'media'
if (mode === 'class') {
addVariant('light', `${className} &`)
} else if (mode === 'media') {
addVariant('light', '@media (prefers-color-scheme: light)')
}
// eslint-disable-next-line unicorn/prefer-spread
;[mode, className = '.light'] = ([] as string[]).concat(
config('lightMode', 'media')
)
if (mode === 'class') {
addVariant('light', `${className} &`)
} else if (mode === 'media') {
addVariant('light', '@media (prefers-color-scheme: light)')
}
}
const defaultTailwindConfig = {
presets: [
{
content: [''], // Silence empty content warning
theme: {
extend: {
content: { DEFAULT: '' }, // Add a `content` class
zIndex: { 1: '1' }, // Add a handy small zIndex (`z-1` / `-z-1`)
},
},
plugins: [defaultVariants], // Add extra variants
},
],
}
export default defaultTailwindConfig