-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
244 lines (235 loc) · 6.89 KB
/
Copy pathindex.ts
File metadata and controls
244 lines (235 loc) · 6.89 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import type { PluginCreator, PluginAPI } from 'tailwindcss/plugin';
import {
createToggles,
encoding,
normalizeAnchorName,
positionAreaValues,
} from './utils.js';
export { encoding } from './utils.js';
const generateViewTransitionId = (str: string) => `--tw-anchor-view-transition-${encoding.encode(str)}`
const anchors = ((api: PluginAPI) => {
const { addBase, addUtilities, matchUtilities, theme } = api;
// reset the styles of anchored popovers to fix problematic UA default styles
const [popoverStyles, _, popoverToggles] = createToggles([
['inset', 'auto', '0px'],
['background-color', 'transparent', 'canvas'],
['color', 'inherit', 'canvastext'],
['margin', '0px', 'auto'],
]);
addBase({ '[popover]': popoverStyles });
// detect v4 by checking for the absence of the postcss argument
const isV4 = !('postcss' in api);
// anchor utilities (anchor-name)
matchUtilities(
{
anchor: (_, { modifier }) => {
const styles: Record<string, string> = {};
if (modifier) {
const anchorName = normalizeAnchorName(modifier, isV4);
if (anchorName) {
styles['anchor-name'] = anchorName;
}
}
return styles;
},
},
{
values: {
DEFAULT: '',
},
modifiers: 'any',
},
);
// anchor-scope (anchor-scope)
matchUtilities(
{
'anchor-scope': (_, { modifier }) => {
const styles: Record<string, string> = {};
if (modifier) {
const anchorName = normalizeAnchorName(modifier, isV4);
if (anchorName) {
styles['anchor-scope'] = anchorName;
}
}
return styles;
},
},
{
values: {
DEFAULT: '',
},
modifiers: 'any',
},
);
// anchored utility (position-area and/or position-anchor)
matchUtilities(
{
anchored: (value, { modifier }) => {
if (!value && !modifier) return {};
const viewTransitionName = modifier && generateViewTransitionId(modifier);
const anchorName = modifier && normalizeAnchorName(modifier, isV4);
return {
...(value && { 'position-area': value }),
...(anchorName && {
'position-anchor': anchorName,
'&:where(&)': {
position: 'absolute',
...(viewTransitionName && { 'view-transition-name': viewTransitionName }),
...popoverToggles.on,
/** TODO: ask community what they think about turning this on by default
* and having to opt out when you don't want it, or leaving it off by
* default and having to opt in when you do want it */
// 'position-try-fallbacks': 'flip-block, flip-inline, flip-block flip-inline',
},
}),
}
},
},
{
values: {
DEFAULT: '', // will be '' if only a name is being set
...positionAreaValues,
},
modifiers: 'any',
},
);
// anchor() utilities
;([
['top', theme('inset')],
['right', theme('inset')],
['bottom', theme('inset')],
['left', theme('inset')],
['inset', theme('inset')],
] as const).forEach(([property, themeValues]) => {
;['top', 'right', 'bottom', 'left', 'start', 'end', 'self-start', 'self-end', 'center'].forEach(
(anchorSide) => {
matchUtilities(
{
[`${property}-anchor-${anchorSide}`]: (offset, { modifier }) => {
const anchorRef = modifier ? `${normalizeAnchorName(modifier, isV4)} ` : ''
const anchorFnExpr = `anchor(${anchorRef}${anchorSide})`
const value = offset ? `calc(${anchorFnExpr} + ${offset})` : anchorFnExpr
return {
[property]: value,
...popoverToggles.on,
}
},
},
{
values: {
DEFAULT: '',
...themeValues,
},
supportsNegativeValues: true,
modifiers: 'any',
},
)
},
)
})
// anchor-size() utilities
;([
['w', 'width', theme('width')],
['h', 'height', theme('height')],
['min-w', 'min-width', theme('minWidth')],
['min-h', 'min-height', theme('minHeight')],
['max-w', 'max-width', theme('maxWidth')],
['max-h', 'max-height', theme('maxHeight')],
] as const).forEach(([propertyAbbr, property, themeValues]) => {
;['', 'width', 'height', 'block', 'inline', 'self-block', 'self-inline'].forEach(
(anchorSize) => {
const anchorSizeUtilitySuffix = anchorSize ? `-${anchorSize}` : anchorSize
matchUtilities(
{
[`${propertyAbbr}-anchor${anchorSizeUtilitySuffix}`]: (offset, { modifier }) => {
const anchorRef = modifier ? `${normalizeAnchorName(modifier, isV4)} ` : ''
const anchorFnExpr = `anchor-size(${anchorRef}${anchorSize})`
const value = offset ? `calc(${anchorFnExpr} + ${offset})` : anchorFnExpr
return {
[property]: value,
}
},
},
{
values: {
DEFAULT: '',
...themeValues,
},
supportsNegativeValues: true,
modifiers: 'any',
},
)
},
)
})
// anchor-center utilities
;([
['justify-self', 'justify-self'],
['self', 'align-self'],
['justify-items', 'justify-items'],
['items', 'align-items'],
['place-items', 'place-items'],
['place-self', 'place-self'],
] as const).forEach(([propertyAbbr, property]) => {
addUtilities({
[`.${propertyAbbr}-anchor`]: {
[property]: 'anchor-center',
...popoverToggles.on,
}
})
})
// position-visibility utilities
matchUtilities(
{
'anchored-visible': (value) => ({
'position-visibility': value,
...popoverToggles.on,
}),
},
{
values: {
always: 'always',
anchor: 'anchors-visible',
'no-overflow': 'no-overflow',
// 'valid': 'anchors-valid', // ⚠️ this is not supported anywhere yet
},
},
)
// position-try-order utilities
matchUtilities(
{
'try-order': (value) => ({
'position-try-order': value,
...popoverToggles.on,
}),
},
{
values: {
normal: 'normal',
w: 'most-width',
h: 'most-height',
},
},
)
// position-try-fallbacks utilities
matchUtilities(
{
'try': (value) => ({
'position-try-fallbacks': value,
...popoverToggles.on,
}),
},
{
values: {
none: 'none',
'flip-all': 'flip-block, flip-inline, flip-block flip-inline',
'flip-x': 'flip-inline',
'flip-y': 'flip-block',
'flip-s': 'flip-start',
...positionAreaValues,
},
},
)
}) satisfies PluginCreator;
// Cast to any to resolve d.ts generation issue
export default anchors as any;