@krate/runtime 0.5.0-alpha.4 → 0.5.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/action-context.d.ts +5 -1
- package/dist/action-context.js +33 -3
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +83 -4
- package/dist/actions.test.js +61 -1
- package/dist/cache-scope.js +4 -1
- package/dist/cache.test.js +16 -0
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +75 -2
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +7 -0
- package/dist/dom.js +69 -3
- package/dist/error-overlay.js +4 -4
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +65 -2
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +239 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/router-nav.test.js +38 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.js +116 -32
- package/dist/server-async.test.js +20 -0
- package/dist/server-renderer.js +109 -22
- package/dist/server.d.ts +1 -0
- package/dist/server.js +90 -11
- package/dist/server.test.js +20 -0
- package/dist/signal.d.ts +3 -2
- package/dist/signal.js +104 -19
- package/dist/slots.js +12 -1
- package/package.json +2 -2
package/dist/class-helpers.js
CHANGED
|
@@ -7,8 +7,14 @@
|
|
|
7
7
|
function flattenClass(value, out) {
|
|
8
8
|
if (value == null || value === false || value === true || value === '')
|
|
9
9
|
return;
|
|
10
|
-
if (typeof value === 'string'
|
|
11
|
-
out.push(
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
out.push(value);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === 'number') {
|
|
15
|
+
// clsx drops every falsy value, so 0 and NaN vanish.
|
|
16
|
+
if (value)
|
|
17
|
+
out.push(String(value));
|
|
12
18
|
return;
|
|
13
19
|
}
|
|
14
20
|
if (Array.isArray(value)) {
|
|
@@ -31,43 +37,362 @@ export function clsx(...inputs) {
|
|
|
31
37
|
return out.join(' ');
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
|
-
* Merge conflicting Tailwind utilities
|
|
35
|
-
*
|
|
36
|
-
* `
|
|
37
|
-
* compiler folds literal calls
|
|
40
|
+
* Merge conflicting Tailwind utilities: later classes win over earlier ones in
|
|
41
|
+
* the same utility group and variant chain (`hover:p-2` never conflicts with
|
|
42
|
+
* `p-4`). Classes the helper does not recognise only dedupe on an exact match.
|
|
43
|
+
* The compiler folds literal calls; this covers dynamic joins.
|
|
38
44
|
*/
|
|
39
45
|
export function twMerge(...inputs) {
|
|
40
46
|
const tokens = [];
|
|
41
47
|
for (const input of inputs)
|
|
42
48
|
flattenClass(input, tokens);
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
// Split on whitespace so "a b" strings from clsx are handled per class.
|
|
50
|
+
const classes = tokens.flatMap((t) => t.split(/\s+/)).filter(Boolean);
|
|
51
|
+
const byKey = new Map();
|
|
52
|
+
for (const token of classes) {
|
|
53
|
+
const { scope, group } = classify(token);
|
|
54
|
+
if (group === null) {
|
|
55
|
+
byKey.delete('\0' + token);
|
|
56
|
+
byKey.set('\0' + token, token);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
// Later class wins and takes the later position, like tailwind-merge.
|
|
60
|
+
const key = scope + group;
|
|
61
|
+
byKey.delete(key);
|
|
62
|
+
for (const loser of CONFLICTS[group] ?? [])
|
|
63
|
+
byKey.delete(scope + loser);
|
|
64
|
+
byKey.set(key, token);
|
|
50
65
|
}
|
|
51
|
-
return
|
|
66
|
+
return Array.from(byKey.values()).join(' ');
|
|
52
67
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
// Groups a later class of the key group also overrides when seen earlier.
|
|
69
|
+
const CONFLICTS = {
|
|
70
|
+
p: ['px', 'py', 'pt', 'pr', 'pb', 'pl', 'ps', 'pe'],
|
|
71
|
+
px: ['pr', 'pl', 'ps', 'pe'],
|
|
72
|
+
py: ['pt', 'pb'],
|
|
73
|
+
m: ['mx', 'my', 'mt', 'mr', 'mb', 'ml', 'ms', 'me'],
|
|
74
|
+
mx: ['mr', 'ml', 'ms', 'me'],
|
|
75
|
+
my: ['mt', 'mb'],
|
|
76
|
+
inset: ['inset-x', 'inset-y', 'top', 'right', 'bottom', 'left', 'start', 'end'],
|
|
77
|
+
'inset-x': ['right', 'left', 'start', 'end'],
|
|
78
|
+
'inset-y': ['top', 'bottom'],
|
|
79
|
+
gap: ['gap-x', 'gap-y'],
|
|
80
|
+
size: ['w', 'h'],
|
|
81
|
+
overflow: ['overflow-x', 'overflow-y'],
|
|
82
|
+
scale: ['scale-x', 'scale-y'],
|
|
83
|
+
rounded: ['rounded-t', 'rounded-r', 'rounded-b', 'rounded-l', 'rounded-s', 'rounded-e', 'rounded-tl', 'rounded-tr', 'rounded-br', 'rounded-bl', 'rounded-ss', 'rounded-se', 'rounded-ee', 'rounded-es'],
|
|
84
|
+
'rounded-t': ['rounded-tl', 'rounded-tr'],
|
|
85
|
+
'rounded-r': ['rounded-tr', 'rounded-br'],
|
|
86
|
+
'rounded-b': ['rounded-br', 'rounded-bl'],
|
|
87
|
+
'rounded-l': ['rounded-tl', 'rounded-bl'],
|
|
88
|
+
'rounded-s': ['rounded-ss', 'rounded-es'],
|
|
89
|
+
'rounded-e': ['rounded-se', 'rounded-ee'],
|
|
90
|
+
'border-w': ['border-w-x', 'border-w-y', 'border-w-t', 'border-w-r', 'border-w-b', 'border-w-l', 'border-w-s', 'border-w-e'],
|
|
91
|
+
'border-w-x': ['border-w-r', 'border-w-l', 'border-w-s', 'border-w-e'],
|
|
92
|
+
'border-w-y': ['border-w-t', 'border-w-b'],
|
|
93
|
+
'border-color': ['border-color-x', 'border-color-y', 'border-color-t', 'border-color-r', 'border-color-b', 'border-color-l', 'border-color-s', 'border-color-e'],
|
|
94
|
+
'border-color-x': ['border-color-r', 'border-color-l', 'border-color-s', 'border-color-e'],
|
|
95
|
+
'border-color-y': ['border-color-t', 'border-color-b'],
|
|
96
|
+
'font-size': ['leading'],
|
|
97
|
+
};
|
|
98
|
+
/** Split on `sep` outside [] and () so arbitrary values stay intact. */
|
|
99
|
+
function splitTop(s, sep) {
|
|
100
|
+
const out = [];
|
|
101
|
+
let depth = 0;
|
|
102
|
+
let start = 0;
|
|
103
|
+
for (let i = 0; i < s.length; i++) {
|
|
104
|
+
const c = s[i];
|
|
105
|
+
if (c === '[' || c === '(')
|
|
106
|
+
depth++;
|
|
107
|
+
else if (c === ']' || c === ')')
|
|
108
|
+
depth = Math.max(0, depth - 1);
|
|
109
|
+
else if (c === sep && depth === 0) {
|
|
110
|
+
out.push(s.slice(start, i));
|
|
111
|
+
start = i + 1;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
out.push(s.slice(start));
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
const LEN_HINT = /^(length|size|percentage|number|position|image|url|family|weight|line-width|bg-size)$/;
|
|
118
|
+
function arbitraryKind(value) {
|
|
119
|
+
let inner;
|
|
120
|
+
if (value.startsWith('[') && value.endsWith(']'))
|
|
121
|
+
inner = value.slice(1, -1);
|
|
122
|
+
else if (value.startsWith('(') && value.endsWith(')'))
|
|
123
|
+
inner = value.slice(1, -1);
|
|
124
|
+
else
|
|
125
|
+
return null;
|
|
126
|
+
const m = /^([a-z-]+):(.*)$/.exec(inner);
|
|
127
|
+
if (m && !m[2].startsWith(':')) {
|
|
128
|
+
const hint = m[1];
|
|
129
|
+
if (hint === 'color')
|
|
130
|
+
return 'color';
|
|
131
|
+
if (hint === 'number' || hint === 'weight')
|
|
132
|
+
return 'number';
|
|
133
|
+
if (LEN_HINT.test(hint))
|
|
134
|
+
return hint === 'family' || hint === 'image' || hint === 'url' ? 'other' : 'length';
|
|
135
|
+
}
|
|
136
|
+
if (value.startsWith('('))
|
|
137
|
+
return 'unknown';
|
|
138
|
+
const v = inner.replace(/_/g, ' ');
|
|
139
|
+
if (/^(#|rgb|hsl|hwb|lab|lch|oklab|oklch|color\()/i.test(v))
|
|
140
|
+
return 'color';
|
|
141
|
+
if (/^(calc|min|max|clamp)\(/.test(v) || /^[+-]?(\d+\.?\d*|\.\d+)(%|[a-z]+)$/i.test(v))
|
|
142
|
+
return 'length';
|
|
143
|
+
if (/^[+-]?(\d+\.?\d*|\.\d+)$/.test(v))
|
|
144
|
+
return 'number';
|
|
145
|
+
return 'unknown';
|
|
146
|
+
}
|
|
147
|
+
const isNumeric = (v) => /^[+-]?(\d+\.?\d*|\.\d+)$/.test(v) || /^\d+\/\d+$/.test(v);
|
|
148
|
+
// Groups whose every value collapses into one group. Longest head wins.
|
|
149
|
+
const SIMPLE = new Set([
|
|
150
|
+
'p', 'px', 'py', 'pt', 'pr', 'pb', 'pl', 'ps', 'pe',
|
|
151
|
+
'm', 'mx', 'my', 'mt', 'mr', 'mb', 'ml', 'ms', 'me',
|
|
152
|
+
'gap', 'gap-x', 'gap-y', 'space-x', 'space-y',
|
|
153
|
+
'inset', 'inset-x', 'inset-y', 'top', 'right', 'bottom', 'left', 'start', 'end',
|
|
154
|
+
'z', 'order', 'opacity', 'duration', 'delay', 'ease', 'animate',
|
|
155
|
+
'basis', 'grow', 'shrink', 'w', 'h', 'size', 'min-w', 'max-w', 'min-h', 'max-h',
|
|
156
|
+
'grid-cols', 'grid-rows', 'col-span', 'col-start', 'col-end', 'row-span', 'row-start', 'row-end',
|
|
157
|
+
'leading', 'tracking', 'line-clamp', 'columns', 'aspect',
|
|
158
|
+
'blur', 'brightness', 'contrast', 'grayscale', 'hue-rotate', 'invert', 'saturate', 'sepia',
|
|
159
|
+
'backdrop-blur', 'backdrop-brightness', 'backdrop-contrast', 'backdrop-grayscale',
|
|
160
|
+
'backdrop-hue-rotate', 'backdrop-invert', 'backdrop-opacity', 'backdrop-saturate', 'backdrop-sepia',
|
|
161
|
+
'scale', 'scale-x', 'scale-y', 'rotate', 'translate-x', 'translate-y', 'skew-x', 'skew-y',
|
|
162
|
+
'origin', 'cursor', 'select', 'underline-offset', 'indent', 'perspective', 'will-change',
|
|
163
|
+
'overflow', 'overflow-x', 'overflow-y', 'overscroll', 'pointer-events', 'resize', 'whitespace',
|
|
164
|
+
'rounded', 'rounded-t', 'rounded-r', 'rounded-b', 'rounded-l', 'rounded-s', 'rounded-e',
|
|
165
|
+
'rounded-tl', 'rounded-tr', 'rounded-br', 'rounded-bl', 'rounded-ss', 'rounded-se', 'rounded-ee', 'rounded-es',
|
|
166
|
+
'transition',
|
|
167
|
+
]);
|
|
168
|
+
const FONT_WEIGHTS = new Set(['thin', 'extralight', 'light', 'normal', 'medium', 'semibold', 'bold', 'extrabold', 'black']);
|
|
169
|
+
const TEXT_SIZES = /^(xs|sm|base|lg|xl|\d+xl)$/;
|
|
170
|
+
const BORDER_STYLES = new Set(['solid', 'dashed', 'dotted', 'double', 'hidden', 'none']);
|
|
171
|
+
const BG_POSITIONS = new Set(['bottom', 'center', 'left', 'left-bottom', 'left-top', 'right', 'right-bottom', 'right-top', 'top']);
|
|
172
|
+
// Standalone (value-less) utilities mapped to a group.
|
|
173
|
+
const STANDALONE = {};
|
|
174
|
+
for (const d of ['block', 'inline-block', 'inline', 'flex', 'inline-flex', 'table', 'inline-table', 'table-caption', 'table-cell', 'table-column', 'table-column-group', 'table-footer-group', 'table-header-group', 'table-row-group', 'table-row', 'flow-root', 'grid', 'inline-grid', 'contents', 'list-item', 'hidden'])
|
|
175
|
+
STANDALONE[d] = 'display';
|
|
176
|
+
for (const d of ['static', 'fixed', 'absolute', 'relative', 'sticky'])
|
|
177
|
+
STANDALONE[d] = 'position';
|
|
178
|
+
for (const d of ['visible', 'invisible', 'collapse'])
|
|
179
|
+
STANDALONE[d] = 'visibility';
|
|
180
|
+
for (const d of ['italic', 'not-italic'])
|
|
181
|
+
STANDALONE[d] = 'font-style';
|
|
182
|
+
for (const d of ['uppercase', 'lowercase', 'capitalize', 'normal-case'])
|
|
183
|
+
STANDALONE[d] = 'text-transform';
|
|
184
|
+
for (const d of ['underline', 'overline', 'line-through', 'no-underline'])
|
|
185
|
+
STANDALONE[d] = 'text-decoration';
|
|
186
|
+
for (const d of ['flex-row', 'flex-row-reverse', 'flex-col', 'flex-col-reverse'])
|
|
187
|
+
STANDALONE[d] = 'flex-direction';
|
|
188
|
+
for (const d of ['flex-wrap', 'flex-nowrap', 'flex-wrap-reverse'])
|
|
189
|
+
STANDALONE[d] = 'flex-wrap';
|
|
190
|
+
STANDALONE['truncate'] = 'truncate';
|
|
191
|
+
function classify(token) {
|
|
192
|
+
const parts = splitTop(token, ':');
|
|
193
|
+
let base = parts.pop();
|
|
194
|
+
const variants = parts.sort();
|
|
195
|
+
let important = '';
|
|
196
|
+
if (base.startsWith('!')) {
|
|
197
|
+
important = '!';
|
|
198
|
+
base = base.slice(1);
|
|
199
|
+
}
|
|
200
|
+
if (base.endsWith('!')) {
|
|
201
|
+
important = '!';
|
|
202
|
+
base = base.slice(0, -1);
|
|
203
|
+
}
|
|
204
|
+
const scope = variants.join(':') + ':' + important;
|
|
205
|
+
if (base.startsWith('-'))
|
|
206
|
+
base = base.slice(1);
|
|
207
|
+
// Arbitrary property: [mask-type:alpha]
|
|
208
|
+
if (base.startsWith('[')) {
|
|
209
|
+
const m = /^\[([a-z-]+):/.exec(base);
|
|
210
|
+
return { scope, group: m ? 'arb:' + m[1] : null };
|
|
211
|
+
}
|
|
212
|
+
// Opacity modifier or fraction: drop a trailing /value.
|
|
213
|
+
const slash = splitTop(base, '/');
|
|
214
|
+
if (slash.length > 1)
|
|
215
|
+
base = slash.slice(0, -1).join('/');
|
|
216
|
+
const group = utilityGroup(base);
|
|
217
|
+
return { scope, group };
|
|
218
|
+
}
|
|
219
|
+
/** Longest dash-separated head of `base` found in `set`, with the rest. */
|
|
220
|
+
function splitHead(base, set) {
|
|
221
|
+
const segs = splitTop(base, '-');
|
|
222
|
+
for (let n = Math.min(segs.length, 3); n >= 1; n--) {
|
|
223
|
+
const head = segs.slice(0, n).join('-');
|
|
224
|
+
if (set.has(head))
|
|
225
|
+
return [head, segs.slice(n).join('-')];
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
function utilityGroup(base) {
|
|
230
|
+
if (STANDALONE[base])
|
|
231
|
+
return STANDALONE[base];
|
|
232
|
+
const segs = splitTop(base, '-');
|
|
233
|
+
const head = segs[0];
|
|
234
|
+
const rest = segs.slice(1).join('-');
|
|
235
|
+
// Colour/size/width/style families that share a head.
|
|
236
|
+
switch (head) {
|
|
237
|
+
case 'text': {
|
|
238
|
+
if (rest === '')
|
|
239
|
+
return null;
|
|
240
|
+
if (TEXT_SIZES.test(rest))
|
|
241
|
+
return 'font-size';
|
|
242
|
+
if (['left', 'center', 'right', 'justify', 'start', 'end'].includes(rest))
|
|
243
|
+
return 'text-align';
|
|
244
|
+
if (['wrap', 'nowrap', 'balance', 'pretty'].includes(rest))
|
|
245
|
+
return 'text-wrap';
|
|
246
|
+
if (['ellipsis', 'clip'].includes(rest))
|
|
247
|
+
return 'text-overflow';
|
|
248
|
+
const k = arbitraryKind(rest);
|
|
249
|
+
if (k === 'length')
|
|
250
|
+
return 'font-size';
|
|
251
|
+
return 'text-color';
|
|
252
|
+
}
|
|
253
|
+
case 'font': {
|
|
254
|
+
const k = arbitraryKind(rest);
|
|
255
|
+
if (FONT_WEIGHTS.has(rest) || k === 'number')
|
|
256
|
+
return 'font-weight';
|
|
257
|
+
if (rest.startsWith('stretch'))
|
|
258
|
+
return 'font-stretch';
|
|
259
|
+
return 'font-family';
|
|
260
|
+
}
|
|
261
|
+
case 'bg': {
|
|
262
|
+
if (rest === '')
|
|
263
|
+
return null;
|
|
264
|
+
if (['auto', 'cover', 'contain'].includes(rest))
|
|
265
|
+
return 'bg-size';
|
|
266
|
+
if (BG_POSITIONS.has(rest))
|
|
267
|
+
return 'bg-position';
|
|
268
|
+
if (/^(repeat|no-repeat)/.test(rest))
|
|
269
|
+
return 'bg-repeat';
|
|
270
|
+
if (['fixed', 'local', 'scroll'].includes(rest))
|
|
271
|
+
return 'bg-attachment';
|
|
272
|
+
if (rest.startsWith('clip-'))
|
|
273
|
+
return 'bg-clip';
|
|
274
|
+
if (rest.startsWith('origin-'))
|
|
275
|
+
return 'bg-origin';
|
|
276
|
+
if (rest.startsWith('blend-'))
|
|
277
|
+
return 'bg-blend';
|
|
278
|
+
if (rest === 'none' || /^(gradient-to|linear|radial|conic)/.test(rest))
|
|
279
|
+
return 'bg-image';
|
|
280
|
+
const k = arbitraryKind(rest);
|
|
281
|
+
if (k === 'length')
|
|
282
|
+
return 'bg-size';
|
|
283
|
+
if (rest.startsWith('[') && /^\[(position|percentage):/.test(rest))
|
|
284
|
+
return 'bg-position';
|
|
285
|
+
if (rest.startsWith('[') && (/^\[(image|url):/.test(rest) || rest.startsWith('[url(')))
|
|
286
|
+
return 'bg-image';
|
|
287
|
+
return 'bg-color';
|
|
288
|
+
}
|
|
289
|
+
case 'border': {
|
|
290
|
+
return widthColorStyle(rest, 'border');
|
|
291
|
+
}
|
|
292
|
+
case 'divide': {
|
|
293
|
+
if (rest === 'x' || rest === 'y' || rest.startsWith('x-') || rest.startsWith('y-'))
|
|
294
|
+
return 'divide-' + rest[0];
|
|
295
|
+
if (rest === 'reverse' || rest.endsWith('-reverse'))
|
|
296
|
+
return 'divide-reverse';
|
|
297
|
+
if (BORDER_STYLES.has(rest))
|
|
298
|
+
return 'divide-style';
|
|
299
|
+
return 'divide-color';
|
|
300
|
+
}
|
|
301
|
+
case 'outline': {
|
|
302
|
+
if (rest.startsWith('offset'))
|
|
303
|
+
return 'outline-offset';
|
|
304
|
+
return widthColorStyle(rest, 'outline');
|
|
305
|
+
}
|
|
306
|
+
case 'ring': {
|
|
307
|
+
if (rest.startsWith('offset-')) {
|
|
308
|
+
const r = rest.slice('offset-'.length);
|
|
309
|
+
const k = arbitraryKind(r);
|
|
310
|
+
return isNumeric(r) || k === 'length' || k === 'number' ? 'ring-offset-w' : 'ring-offset-color';
|
|
311
|
+
}
|
|
312
|
+
if (rest === 'inset')
|
|
313
|
+
return 'ring-inset';
|
|
314
|
+
if (rest === '')
|
|
315
|
+
return 'ring-w';
|
|
316
|
+
const k = arbitraryKind(rest);
|
|
317
|
+
return isNumeric(rest) || k === 'length' || k === 'number' ? 'ring-w' : 'ring-color';
|
|
318
|
+
}
|
|
319
|
+
case 'shadow': {
|
|
320
|
+
if (rest === '')
|
|
321
|
+
return 'shadow';
|
|
322
|
+
const k = arbitraryKind(rest);
|
|
323
|
+
if (k === 'color')
|
|
324
|
+
return 'shadow-color';
|
|
325
|
+
if (k === 'length' || k === 'number' || k === 'other' || (k === 'unknown' && rest.startsWith('[')))
|
|
326
|
+
return 'shadow';
|
|
327
|
+
if (/^(2xs|xs|sm|md|lg|xl|2xl|none|inner)$/.test(rest))
|
|
328
|
+
return 'shadow';
|
|
329
|
+
return 'shadow-color';
|
|
330
|
+
}
|
|
331
|
+
case 'stroke': {
|
|
332
|
+
const k = arbitraryKind(rest);
|
|
333
|
+
return isNumeric(rest) || k === 'length' || k === 'number' ? 'stroke-w' : 'stroke-color';
|
|
334
|
+
}
|
|
335
|
+
case 'fill':
|
|
336
|
+
return 'fill';
|
|
337
|
+
case 'decoration': {
|
|
338
|
+
if (['solid', 'double', 'dotted', 'dashed', 'wavy'].includes(rest))
|
|
339
|
+
return 'decoration-style';
|
|
340
|
+
const k = arbitraryKind(rest);
|
|
341
|
+
if (isNumeric(rest) || k === 'length' || k === 'number' || rest === 'from-font' || rest === 'auto')
|
|
342
|
+
return 'decoration-thickness';
|
|
343
|
+
return 'decoration-color';
|
|
344
|
+
}
|
|
345
|
+
case 'object':
|
|
346
|
+
return ['contain', 'cover', 'fill', 'none', 'scale-down'].includes(rest) ? 'object-fit' : 'object-position';
|
|
347
|
+
case 'list':
|
|
348
|
+
if (rest === 'inside' || rest === 'outside')
|
|
349
|
+
return 'list-position';
|
|
350
|
+
if (rest.startsWith('image'))
|
|
351
|
+
return 'list-image';
|
|
352
|
+
return 'list-style-type';
|
|
353
|
+
case 'flex':
|
|
354
|
+
if (rest === '')
|
|
355
|
+
return null;
|
|
356
|
+
return 'flex';
|
|
357
|
+
case 'grid':
|
|
358
|
+
if (rest.startsWith('flow'))
|
|
359
|
+
return 'grid-flow';
|
|
360
|
+
break;
|
|
361
|
+
case 'shrink':
|
|
362
|
+
case 'grow':
|
|
363
|
+
return head;
|
|
364
|
+
case 'transition':
|
|
365
|
+
return 'transition';
|
|
366
|
+
case 'inset':
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
const hit = splitHead(base, SIMPLE);
|
|
370
|
+
if (hit) {
|
|
371
|
+
return hit[0];
|
|
372
|
+
}
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
/** Group for border/outline: width, style or colour, with optional side. */
|
|
376
|
+
function widthColorStyle(rest, name) {
|
|
377
|
+
let side = '';
|
|
378
|
+
let value = rest;
|
|
379
|
+
const m = /^(x|y|t|r|b|l|s|e)(?:-(.*))?$/.exec(rest);
|
|
380
|
+
if (name === 'border' && m) {
|
|
381
|
+
side = '-' + m[1];
|
|
382
|
+
value = m[2] ?? '';
|
|
383
|
+
}
|
|
384
|
+
if (name === 'border' && (rest === 'collapse' || rest === 'separate'))
|
|
385
|
+
return 'border-collapse';
|
|
386
|
+
if (name === 'border' && rest.startsWith('spacing'))
|
|
387
|
+
return 'border-spacing';
|
|
388
|
+
if (value === '')
|
|
389
|
+
return name === 'border' ? 'border-w' + side : name + '-w';
|
|
390
|
+
if (BORDER_STYLES.has(value))
|
|
391
|
+
return name === 'border' ? 'border-style' : name === 'outline' && value !== 'hidden' ? 'outline-style' : name + '-style';
|
|
392
|
+
const k = arbitraryKind(value);
|
|
393
|
+
if (isNumeric(value) || k === 'length' || k === 'number')
|
|
394
|
+
return name === 'border' ? 'border-w' + side : name + '-w';
|
|
395
|
+
return name === 'border' ? 'border-color' + side : name + '-color';
|
|
71
396
|
}
|
|
72
397
|
/** `cn` - `twMerge` over `clsx`, the shadcn/ui convention. */
|
|
73
398
|
export function cn(...inputs) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { cn, twMerge, clsx, cva } from './class-helpers';
|
|
4
|
+
// Each case: input classes, expected merged output (later wins).
|
|
5
|
+
const same = (a, b) => {
|
|
6
|
+
expect(twMerge(a, b)).toBe(b);
|
|
7
|
+
};
|
|
8
|
+
const apart = (a, b) => {
|
|
9
|
+
expect(twMerge(a, b).split(' ').sort()).toEqual([a, b].sort());
|
|
10
|
+
};
|
|
11
|
+
describe('twMerge numeric and arbitrary values', () => {
|
|
12
|
+
it('collapses multi-digit values', () => {
|
|
13
|
+
same('duration-700', 'duration-500');
|
|
14
|
+
same('delay-150', 'delay-300');
|
|
15
|
+
same('duration-75', 'duration-1000');
|
|
16
|
+
expect(cn('duration-700', 'duration-500')).toBe('duration-500');
|
|
17
|
+
});
|
|
18
|
+
it('collapses arbitrary and variable values', () => {
|
|
19
|
+
same('duration-500', 'duration-[2s]');
|
|
20
|
+
same('duration-[2s]', 'duration-(--d)');
|
|
21
|
+
same('p-4', 'p-[3px]');
|
|
22
|
+
same('w-1/2', 'w-[200px]');
|
|
23
|
+
same('mt-2', 'mt-(--gap)');
|
|
24
|
+
});
|
|
25
|
+
it('handles decimals, fractions, negatives, modifiers and important', () => {
|
|
26
|
+
same('p-0.5', 'p-12');
|
|
27
|
+
same('w-1/2', 'w-1/3');
|
|
28
|
+
same('-mt-2', 'mt-4');
|
|
29
|
+
same('mt-4', '-mt-2');
|
|
30
|
+
same('bg-red-500/50', 'bg-blue-500');
|
|
31
|
+
same('bg-red-500', 'bg-blue-500/20');
|
|
32
|
+
same('!p-2', '!p-4');
|
|
33
|
+
same('p-2!', 'p-4!');
|
|
34
|
+
same('!p-2', 'p-4!');
|
|
35
|
+
apart('!p-2', 'p-4');
|
|
36
|
+
});
|
|
37
|
+
it('scopes by variant chain', () => {
|
|
38
|
+
apart('hover:duration-500', 'duration-700');
|
|
39
|
+
same('hover:duration-500', 'hover:duration-700');
|
|
40
|
+
same('md:hover:p-2', 'hover:md:p-4');
|
|
41
|
+
apart('md:p-2', 'p-4');
|
|
42
|
+
same('[&>*]:p-2', '[&>*]:p-4');
|
|
43
|
+
apart('[&>*]:p-2', '[&_a]:p-4');
|
|
44
|
+
});
|
|
45
|
+
it('keeps unrelated and unknown classes', () => {
|
|
46
|
+
expect(twMerge('px-2', 'py-2')).toBe('px-2 py-2');
|
|
47
|
+
expect(twMerge('krate-card', 'krate-card-title', 'krate-card')).toBe('krate-card-title krate-card');
|
|
48
|
+
expect(twMerge('foo-bar', 'foo-baz')).toBe('foo-bar foo-baz');
|
|
49
|
+
expect(twMerge('a b', 'c a')).toBe('b c a');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe('twMerge groups', () => {
|
|
53
|
+
it('spacing and sizing', () => {
|
|
54
|
+
same('p-2', 'p-4');
|
|
55
|
+
same('px-2', 'px-4');
|
|
56
|
+
apart('pt-2', 'pb-2');
|
|
57
|
+
expect(twMerge('px-2', 'py-2', 'p-4')).toBe('p-4');
|
|
58
|
+
expect(twMerge('p-4', 'px-2')).toBe('p-4 px-2');
|
|
59
|
+
same('m-2', 'm-8');
|
|
60
|
+
same('gap-2', 'gap-10');
|
|
61
|
+
apart('gap-x-2', 'gap-y-2');
|
|
62
|
+
expect(twMerge('gap-x-2', 'gap-4')).toBe('gap-4');
|
|
63
|
+
same('w-10', 'w-full');
|
|
64
|
+
same('h-10', 'h-screen');
|
|
65
|
+
apart('w-10', 'h-10');
|
|
66
|
+
expect(twMerge('w-4', 'h-4', 'size-8')).toBe('size-8');
|
|
67
|
+
same('min-w-0', 'min-w-[10px]');
|
|
68
|
+
apart('min-w-0', 'max-w-0');
|
|
69
|
+
same('z-10', 'z-50');
|
|
70
|
+
same('opacity-50', 'opacity-100');
|
|
71
|
+
});
|
|
72
|
+
it('text size versus color', () => {
|
|
73
|
+
apart('text-sm', 'text-red-500');
|
|
74
|
+
same('text-sm', 'text-lg');
|
|
75
|
+
same('text-sm', 'text-2xl');
|
|
76
|
+
same('text-red-500', 'text-blue-500');
|
|
77
|
+
same('text-red-500', 'text-[#fff]');
|
|
78
|
+
same('text-sm', 'text-[14px]');
|
|
79
|
+
apart('text-[14px]', 'text-[#fff]');
|
|
80
|
+
apart('text-sm', 'text-center');
|
|
81
|
+
same('text-left', 'text-center');
|
|
82
|
+
expect(twMerge('leading-6', 'text-lg')).toBe('text-lg');
|
|
83
|
+
same('leading-6', 'leading-tight');
|
|
84
|
+
same('tracking-tight', 'tracking-wide');
|
|
85
|
+
});
|
|
86
|
+
it('background', () => {
|
|
87
|
+
same('bg-red-500', 'bg-blue-500');
|
|
88
|
+
apart('bg-red-500', 'bg-cover');
|
|
89
|
+
apart('bg-red-500', 'bg-center');
|
|
90
|
+
same('bg-cover', 'bg-contain');
|
|
91
|
+
same('bg-center', 'bg-top');
|
|
92
|
+
apart('bg-cover', 'bg-center');
|
|
93
|
+
same('bg-red-500', 'bg-[#fff]');
|
|
94
|
+
same('bg-cover', 'bg-[length:10px_10px]');
|
|
95
|
+
apart('bg-no-repeat', 'bg-cover');
|
|
96
|
+
});
|
|
97
|
+
it('border', () => {
|
|
98
|
+
apart('border-2', 'border-red-500');
|
|
99
|
+
same('border-2', 'border-4');
|
|
100
|
+
same('border', 'border-2');
|
|
101
|
+
same('border-red-500', 'border-blue-500');
|
|
102
|
+
apart('border-solid', 'border-2');
|
|
103
|
+
same('border-solid', 'border-dashed');
|
|
104
|
+
apart('border-t-2', 'border-b-2');
|
|
105
|
+
same('border-t-2', 'border-t-4');
|
|
106
|
+
apart('border-t-2', 'border-t-red-500');
|
|
107
|
+
expect(twMerge('border-t-2', 'border-x-2', 'border-4')).toBe('border-4');
|
|
108
|
+
same('border-[3px]', 'border-2');
|
|
109
|
+
apart('border-[3px]', 'border-[#fff]');
|
|
110
|
+
same('rounded', 'rounded-lg');
|
|
111
|
+
same('rounded-t-md', 'rounded-t-lg');
|
|
112
|
+
apart('rounded-t-md', 'rounded-b-md');
|
|
113
|
+
expect(twMerge('rounded-tl-md', 'rounded-t-lg')).toBe('rounded-t-lg');
|
|
114
|
+
});
|
|
115
|
+
it('shadow, font, motion, layout', () => {
|
|
116
|
+
same('shadow-sm', 'shadow-lg');
|
|
117
|
+
same('shadow', 'shadow-md');
|
|
118
|
+
apart('shadow-md', 'shadow-red-500');
|
|
119
|
+
same('shadow-red-500', 'shadow-blue-500');
|
|
120
|
+
same('font-bold', 'font-light');
|
|
121
|
+
apart('font-bold', 'font-sans');
|
|
122
|
+
same('font-sans', 'font-mono');
|
|
123
|
+
same('transition', 'transition-colors');
|
|
124
|
+
same('transition-all', 'transition-none');
|
|
125
|
+
same('ease-in', 'ease-out');
|
|
126
|
+
same('ease-in', 'ease-[cubic-bezier(0.4,0,0.2,1)]');
|
|
127
|
+
same('animate-spin', 'animate-pulse');
|
|
128
|
+
same('animate-spin', 'animate-none');
|
|
129
|
+
same('grid-cols-2', 'grid-cols-12');
|
|
130
|
+
same('grid-cols-2', 'grid-cols-[1fr_2fr]');
|
|
131
|
+
same('flex', 'block');
|
|
132
|
+
same('hidden', 'flex');
|
|
133
|
+
apart('flex', 'flex-col');
|
|
134
|
+
same('flex-row', 'flex-col');
|
|
135
|
+
same('absolute', 'relative');
|
|
136
|
+
same('ring-2', 'ring-4');
|
|
137
|
+
apart('ring-2', 'ring-red-500');
|
|
138
|
+
same('outline-2', 'outline-4');
|
|
139
|
+
apart('outline-2', 'outline-red-500');
|
|
140
|
+
});
|
|
141
|
+
it('arbitrary properties', () => {
|
|
142
|
+
same('[mask-type:alpha]', '[mask-type:luminance]');
|
|
143
|
+
apart('[mask-type:alpha]', '[color:red]');
|
|
144
|
+
});
|
|
145
|
+
it('works through clsx and cva-style inputs', () => {
|
|
146
|
+
expect(cn('p-2', false && 'p-3', ['px-4', { 'px-6': true }])).toBe('p-2 px-6');
|
|
147
|
+
expect(clsx('a', null, 'b')).toBe('a b');
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
// The compiler folds literal cn/twMerge calls with a Go port of the merge; the
|
|
151
|
+
// same vectors run in packages/compiler/internal/irtree/classmerge_test.go.
|
|
152
|
+
describe('shared class-merge vectors', () => {
|
|
153
|
+
const vectors = JSON.parse(readFileSync('src/testdata/class-merge-vectors.json', 'utf8'));
|
|
154
|
+
const fns = { twMerge, cn, clsx };
|
|
155
|
+
it('has vectors', () => {
|
|
156
|
+
expect(vectors.length).toBeGreaterThan(100);
|
|
157
|
+
expect(vectors.some((v) => v.fn === 'cva')).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
for (const v of vectors) {
|
|
160
|
+
it(`${v.fn}: ${v.name}`, () => {
|
|
161
|
+
if (v.fn === 'cva') {
|
|
162
|
+
expect(cva(v.base ?? '', v.config)(v.selection)).toBe(v.expected);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const args = v.src ? new Function(`return [${v.src}]`)() : (v.args ?? []);
|
|
166
|
+
expect(fns[v.fn](...args)).toBe(v.expected);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
});
|
package/dist/context.d.ts
CHANGED
|
@@ -22,16 +22,22 @@ export interface Context<T> {
|
|
|
22
22
|
useContext: () => T;
|
|
23
23
|
/** Alias for {@link Context.useContext}. */
|
|
24
24
|
use: () => T;
|
|
25
|
+
/**
|
|
26
|
+
* Like useContext, but returns a getter that keeps reading the provider's
|
|
27
|
+
* live value after the call (inside a list row rendered by
|
|
28
|
+
* {@link krateContextRows}).
|
|
29
|
+
*/
|
|
30
|
+
useContextLive: () => () => T;
|
|
25
31
|
}
|
|
26
32
|
export declare function resetContexts(): void;
|
|
27
33
|
/**
|
|
28
34
|
* Create a context that carries a CSS scope handle (see {@link CSSScope}). The
|
|
29
|
-
* default is a harmless placeholder
|
|
35
|
+
* default is a harmless placeholder - a `<Provider>` value always wins - so no
|
|
30
36
|
* cast is needed:
|
|
31
37
|
*
|
|
32
38
|
* ```ts
|
|
33
39
|
* const TabsCtx = createScope<string>();
|
|
34
|
-
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}
|
|
40
|
+
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}>...</Provider>
|
|
35
41
|
* // consume: const tab = TabsCtx.use();
|
|
36
42
|
* ```
|
|
37
43
|
*/
|
|
@@ -39,3 +45,11 @@ export declare function createScope<T>(): Context<CSSScope<T>>;
|
|
|
39
45
|
/** Like {@link createScope} but for a `createCSSFlags` handle. */
|
|
40
46
|
export declare function createScopeFlags<K extends string>(): Context<CSSFlagsScope<K>>;
|
|
41
47
|
export declare function createContext<T>(defaultValue: T): Context<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Wraps a list row render function so the provider values of the enclosing
|
|
50
|
+
* `<Ctx.Provider value={...}>` (given as getters over the provider's state)
|
|
51
|
+
* are visible to the rows it creates, and stay live when the state changes.
|
|
52
|
+
* The compiler flattens providers at build time, so rows rendered later by
|
|
53
|
+
* the list runtime have no provider on the stack otherwise.
|
|
54
|
+
*/
|
|
55
|
+
export declare function krateContextRows<A extends unknown[], R>(entries: Array<[Context<any>, () => unknown]>, render: (...args: A) => R): (...args: A) => R;
|