-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
159 lines (145 loc) · 5.58 KB
/
index.js
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
const plugin = require('tailwindcss/plugin');
// Order of these is important because it determines which states win in a conflict.
// We mostly follow Tailwind's defaults, adding our additional states following the categories they define.
// https://github.com/tailwindlabs/tailwindcss/blob/304c2bad6cb5fcb62754a4580b1c8f4c16b946ea/src/corePlugins.js#L83
const attributes = {
boolean: [
// Conditions
'allows-removing',
'allows-sorting',
'allows-dragging',
'has-submenu',
// States
'open',
'expanded',
'entering',
'exiting',
'indeterminate',
['placeholder-shown', 'placeholder'],
'current',
'required',
'unavailable',
'invalid',
['read-only', 'readonly'],
'outside-month',
'outside-visible-range',
'pending',
// Content
'empty',
// Interactive states
'focus-within',
['hover', 'hovered'],
['focus', 'focused'],
'focus-visible',
'pressed',
'selected',
'selection-start',
'selection-end',
'dragging',
'drop-target',
'resizing',
'disabled'
],
enum: {
placement: ['left', 'right', 'top', 'bottom'],
type: ['literal', 'year', 'month', 'day'],
layout: ['grid', 'stack'],
orientation: ['horizontal', 'vertical'],
'selection-mode': ['single', 'multiple'],
'resizable-direction': ['right', 'left', 'both'],
'sort-direction': ['ascending', 'descending']
}
};
const shortNames = {
'selection-mode': 'selection',
'resizable-direction': 'resizable',
'sort-direction': 'sort'
};
// Variants we use that are already defined by Tailwind:
// https://github.com/tailwindlabs/tailwindcss/blob/a2fa6932767ab328515f743d6188c2164ad2a5de/src/corePlugins.js#L84
const nativeVariants = ['indeterminate', 'required', 'invalid', 'empty', 'focus-visible', 'focus-within', 'disabled'];
const nativeVariantSelectors = new Map([
...nativeVariants.map((variant) => [variant, `:${variant}`]),
['hovered', ':hover'],
['focused', ':focus'],
['readonly', ':read-only'],
['open', '[open]'],
['expanded', '[expanded]']
]);
// Variants where both native and RAC attributes should apply. We don't override these.
const nativeMergeSelectors = new Map([
['placeholder', ':placeholder-shown']
]);
// If no prefix is specified, we want to avoid overriding native variants on non-RAC components, so we only target elements with the data-rac attribute for those variants.
let getSelector = (prefix, attributeName, attributeValue, hoverOnlyWhenSupported) => {
let baseSelector = attributeValue ? `[data-${attributeName}="${attributeValue}"]` : `[data-${attributeName}]`;
let nativeSelector = nativeVariantSelectors.get(attributeName);
if (prefix === '' && nativeSelector) {
let wrappedNativeSelector = `&:where(:not([data-rac]))${nativeSelector}`;
let nativeSelectorGenerator = wrappedNativeSelector;
if (nativeSelector === ':hover' && hoverOnlyWhenSupported) {
nativeSelectorGenerator = wrap => `@media (hover: hover) and (pointer: fine) { ${wrap(wrappedNativeSelector)} }`;
}
return [`&:where([data-rac])${baseSelector}`, nativeSelectorGenerator];
} else if (prefix === '' && nativeMergeSelectors.has(attributeName)) {
return [`&${baseSelector}`, `&${nativeMergeSelectors.get(attributeName)}`];
} else {
return `&${baseSelector}`;
}
};
let mapSelector = (selector, fn) => {
if (Array.isArray(selector)) {
return selector.map(fn);
} else {
return fn(selector);
}
};
let wrapSelector = (selector, wrap) => {
if (typeof selector === 'function') {
return selector(wrap);
} else {
return wrap(selector);
}
};
let addVariants = (variantName, selectors, addVariant, matchVariant) => {
addVariant(variantName, mapSelector(selectors, selector => wrapSelector(selector, s => s)));
matchVariant(
'group',
(_, {modifier}) =>
modifier
? mapSelector(selectors, selector => wrapSelector(selector, s => `:merge(.group\\/${modifier})${s.slice(1)} &`))
: mapSelector(selectors, selector => wrapSelector(selector, s => `:merge(.group)${s.slice(1)} &`)),
{values: {[variantName]: variantName}}
);
matchVariant(
'peer',
(_, {modifier}) =>
modifier
? mapSelector(selectors, selector => wrapSelector(selector, s => `:merge(.peer\\/${modifier})${s.slice(1)} ~ &`))
: mapSelector(selectors, selector => wrapSelector(selector, s => `:merge(.peer)${s.slice(1)} ~ &`)),
{values: {[variantName]: variantName}}
);
};
module.exports = plugin.withOptions((options) => (({addVariant, matchVariant, config}) => {
let prefix = options?.prefix ? `${options.prefix}-` : '';
let future = config().future;
let hoverOnlyWhenSupported = future === 'all' || future?.hoverOnlyWhenSupported;
// Enum attributes go first because currently they are all non-interactive states.
Object.keys(attributes.enum).forEach((attributeName) => {
attributes.enum[attributeName].forEach(
(attributeValue) => {
let name = shortNames[attributeName] || attributeName;
let variantName = `${prefix}${name}-${attributeValue}`;
let selectors = getSelector(prefix, attributeName, attributeValue, hoverOnlyWhenSupported);
addVariants(variantName, selectors, addVariant, matchVariant);
}
);
});
attributes.boolean.forEach((attribute) => {
let variantName = Array.isArray(attribute) ? attribute[0] : attribute;
variantName = `${prefix}${variantName}`;
let attributeName = Array.isArray(attribute) ? attribute[1] : attribute;
let selectors = getSelector(prefix, attributeName, null, hoverOnlyWhenSupported);
addVariants(variantName, selectors, addVariant, matchVariant);
});
}));