@bamboocss/shared 1.11.2 → 1.11.4

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.
@@ -1,501 +0,0 @@
1
- //#region src/assert.ts
2
- const isString = (v) => typeof v === "string";
3
- const isBoolean = (v) => typeof v === "boolean";
4
- const isFunction = (v) => typeof v === "function";
5
- function isObject(value) {
6
- return typeof value === "object" && value != null && !Array.isArray(value);
7
- }
8
- const isSymbol = (v) => typeof v === "symbol";
9
- const isObjectOrArray = (obj) => typeof obj === "object" && obj !== null;
10
- //#endregion
11
- //#region src/memo.ts
12
- const memo = (fn) => {
13
- const cache = /* @__PURE__ */ new Map();
14
- const get = (...args) => {
15
- const key = JSON.stringify(args);
16
- if (cache.has(key)) return cache.get(key);
17
- const result = fn(...args);
18
- cache.set(key, result);
19
- return result;
20
- };
21
- return get;
22
- };
23
- //#endregion
24
- //#region src/compact.ts
25
- function compact(value) {
26
- return Object.fromEntries(Object.entries(value ?? {}).filter(([_, value]) => value !== void 0));
27
- }
28
- //#endregion
29
- //#region src/condition.ts
30
- const isBaseCondition = (v) => v === "base";
31
- function filterBaseConditions(c) {
32
- return c.slice().filter((v) => !isBaseCondition(v));
33
- }
34
- //#endregion
35
- //#region src/hash.ts
36
- function toChar(code) {
37
- return String.fromCharCode(code + (code > 25 ? 39 : 97));
38
- }
39
- function toName(code) {
40
- let name = "";
41
- let x;
42
- for (x = Math.abs(code); x > 52; x = x / 52 | 0) name = toChar(x % 52) + name;
43
- return toChar(x % 52) + name;
44
- }
45
- function toPhash(h, x) {
46
- let i = x.length;
47
- while (i) h = h * 33 ^ x.charCodeAt(--i);
48
- return h;
49
- }
50
- function toHash(value) {
51
- return toName(toPhash(5381, value) >>> 0);
52
- }
53
- //#endregion
54
- //#region src/important.ts
55
- const importantRegex = /\s*!(important)?/i;
56
- function isImportant(value) {
57
- return typeof value === "string" ? importantRegex.test(value) : false;
58
- }
59
- function withoutImportant(value) {
60
- return typeof value === "string" ? value.replace(importantRegex, "").trim() : value;
61
- }
62
- function withoutSpace(str) {
63
- return typeof str === "string" ? str.replaceAll(" ", "_") : str;
64
- }
65
- function markImportant(obj) {
66
- if (typeof obj !== "object" || obj === null) return obj;
67
- const result = Array.isArray(obj) ? [] : {};
68
- const stack = [{
69
- obj,
70
- result
71
- }];
72
- while (stack.length > 0) {
73
- const { obj, result } = stack.pop();
74
- for (const [key, value] of Object.entries(obj)) if (typeof value === "string" || typeof value === "number") result[key] = `${value} !important`;
75
- else if (typeof value === "object" && value !== null) {
76
- const next = Array.isArray(value) ? [] : {};
77
- result[key] = next;
78
- stack.push({
79
- obj: value,
80
- result: next
81
- });
82
- } else result[key] = value;
83
- }
84
- return result;
85
- }
86
- //#endregion
87
- //#region src/merge-props.ts
88
- const MERGE_OMIT = new Set([
89
- "__proto__",
90
- "constructor",
91
- "prototype"
92
- ]);
93
- function mergeProps(...sources) {
94
- return sources.reduce((prev, obj) => {
95
- if (!obj) return prev;
96
- Object.keys(obj).forEach((key) => {
97
- if (MERGE_OMIT.has(key)) return;
98
- const prevValue = prev[key];
99
- const value = obj[key];
100
- if (isObject(prevValue) && isObject(value)) prev[key] = mergeProps(prevValue, value);
101
- else prev[key] = value;
102
- });
103
- return prev;
104
- }, {});
105
- }
106
- //#endregion
107
- //#region src/walk-object.ts
108
- const isNotNullish = (element) => element != null;
109
- function walkObject(target, predicate, options = {}) {
110
- const { stop, getKey } = options;
111
- function inner(value, path = []) {
112
- if (isObjectOrArray(value)) {
113
- const result = {};
114
- for (const [prop, child] of Object.entries(value)) {
115
- const key = getKey?.(prop, child) ?? prop;
116
- const childPath = [...path, key];
117
- if (stop?.(value, childPath)) return predicate(value, path);
118
- const next = inner(child, childPath);
119
- if (isNotNullish(next)) result[key] = next;
120
- }
121
- return result;
122
- }
123
- return predicate(value, path);
124
- }
125
- return inner(target);
126
- }
127
- function mapObject(obj, fn) {
128
- if (Array.isArray(obj)) return obj.map((value) => fn(value));
129
- if (!isObject(obj)) return fn(obj);
130
- return walkObject(obj, (value) => fn(value));
131
- }
132
- //#endregion
133
- //#region src/normalize-style-object.ts
134
- function toResponsiveObject(values, breakpoints) {
135
- return values.reduce((acc, current, index) => {
136
- const key = breakpoints[index];
137
- if (current != null) acc[key] = current;
138
- return acc;
139
- }, {});
140
- }
141
- function normalizeStyleObject(styles, context, shorthand = true) {
142
- const { utility, conditions } = context;
143
- const { hasShorthand, resolveShorthand } = utility;
144
- return walkObject(styles, (value) => {
145
- return Array.isArray(value) ? toResponsiveObject(value, conditions.breakpoints.keys) : value;
146
- }, {
147
- stop: (value) => Array.isArray(value),
148
- getKey: shorthand ? (prop) => hasShorthand ? resolveShorthand(prop) : prop : void 0
149
- });
150
- }
151
- //#endregion
152
- //#region src/classname.ts
153
- const fallbackCondition = {
154
- shift: (v) => v,
155
- finalize: (v) => v,
156
- breakpoints: { keys: [] }
157
- };
158
- const sanitize = (value) => typeof value === "string" ? value.replaceAll(/[\n\s]+/g, " ") : value;
159
- const ENTRY_SEP = "]___[";
160
- const COND_SEP = "<___>";
161
- function createCss(context) {
162
- const { utility, hash, grouped, conditions: conds = fallbackCondition } = context;
163
- const formatClassName = (str) => [utility.prefix, str].filter(Boolean).join("-");
164
- const hashFn = (conditions, className) => {
165
- let result;
166
- if (hash) {
167
- const baseArray = [...conds.finalize(conditions), className];
168
- result = formatClassName(utility.toHash(baseArray, toHash));
169
- } else result = [...conds.finalize(conditions), formatClassName(className)].join(":");
170
- return result;
171
- };
172
- if (grouped) return memo(({ base, ...styles } = {}) => {
173
- const normalizedObject = normalizeStyleObject(Object.assign(styles, base), context);
174
- const hashes = [];
175
- walkObject(normalizedObject, (value, paths) => {
176
- if (value == null) return;
177
- const [prop, ...allConditions] = conds.shift(paths);
178
- const conditions = filterBaseConditions(allConditions);
179
- const parts = [`${prop}${ENTRY_SEP}value:${value}`];
180
- if (conditions.length) parts.push(`cond:${conditions.join(COND_SEP)}`);
181
- hashes.push(parts.join(ENTRY_SEP));
182
- });
183
- if (hashes.length === 0) return "";
184
- hashes.sort();
185
- const groupId = hashes.join("|");
186
- return formatClassName(utility.toHash(["grouped", groupId], toHash));
187
- });
188
- return memo(({ base, ...styles } = {}) => {
189
- const normalizedObject = normalizeStyleObject(Object.assign(styles, base), context);
190
- const classNames = /* @__PURE__ */ new Set();
191
- walkObject(normalizedObject, (value, paths) => {
192
- if (value == null) return;
193
- const important = isImportant(value);
194
- const [prop, ...allConditions] = conds.shift(paths);
195
- let className = hashFn(filterBaseConditions(allConditions), utility.transform(prop, withoutImportant(sanitize(value))).className);
196
- if (important) className = `${className}!`;
197
- classNames.add(className);
198
- });
199
- return Array.from(classNames).join(" ");
200
- });
201
- }
202
- function compactStyles(...styles) {
203
- return styles.flat().filter((style) => isObject(style) && Object.keys(compact(style)).length > 0);
204
- }
205
- function createMergeCss(context) {
206
- function resolve(styles) {
207
- const allStyles = compactStyles(...styles);
208
- if (allStyles.length === 1) return allStyles;
209
- return allStyles.map((style) => normalizeStyleObject(style, context));
210
- }
211
- function mergeCss(...styles) {
212
- return mergeProps(...resolve(styles));
213
- }
214
- function assignCss(...styles) {
215
- return Object.assign({}, ...resolve(styles));
216
- }
217
- return {
218
- mergeCss: memo(mergeCss),
219
- assignCss
220
- };
221
- }
222
- //#endregion
223
- //#region src/hypenate-property.ts
224
- const wordRegex = /([A-Z])/g;
225
- const msRegex = /^ms-/;
226
- const hypenateProperty = memo((property) => {
227
- if (property.startsWith("--")) return property;
228
- return property.replace(wordRegex, "-$1").replace(msRegex, "-ms-").toLowerCase();
229
- });
230
- //#endregion
231
- //#region src/is-css-function.ts
232
- const fnRegExp = new RegExp(`^(${[
233
- "min",
234
- "max",
235
- "clamp",
236
- "calc"
237
- ].join("|")})\\(.*\\)`);
238
- const isCssFunction = (v) => typeof v === "string" && fnRegExp.test(v);
239
- //#endregion
240
- //#region src/is-css-unit.ts
241
- const lengthUnitsPattern = `(?:${"cm,mm,Q,in,pc,pt,px,em,ex,ch,rem,lh,rlh,vw,vh,vmin,vmax,vb,vi,svw,svh,lvw,lvh,dvw,dvh,cqw,cqh,cqi,cqb,cqmin,cqmax,%".split(",").join("|")})`;
242
- const lengthRegExp = new RegExp(`^[+-]?[0-9]*.?[0-9]+(?:[eE][+-]?[0-9]+)?${lengthUnitsPattern}$`);
243
- const isCssUnit = (v) => typeof v === "string" && lengthRegExp.test(v);
244
- //#endregion
245
- //#region src/is-css-var.ts
246
- const isCssVar = (v) => typeof v === "string" && /^var\(--.+\)$/.test(v);
247
- //#endregion
248
- //#region src/pattern-fns.ts
249
- const patternFns = {
250
- map: mapObject,
251
- isCssFunction,
252
- isCssVar,
253
- isCssUnit
254
- };
255
- const getPatternStyles = (pattern, styles) => {
256
- if (!pattern?.defaultValues) return styles;
257
- const defaults = typeof pattern.defaultValues === "function" ? pattern.defaultValues(styles) : pattern.defaultValues;
258
- return Object.assign({}, defaults, compact(styles));
259
- };
260
- //#endregion
261
- //#region src/slot.ts
262
- const getSlotRecipes = (recipe = {}) => {
263
- const init = (slot) => ({
264
- className: [recipe.className, slot].filter(Boolean).join("__"),
265
- base: recipe.base?.[slot] ?? {},
266
- variants: {},
267
- defaultVariants: recipe.defaultVariants ?? {},
268
- compoundVariants: recipe.compoundVariants ? getSlotCompoundVariant(recipe.compoundVariants, slot) : []
269
- });
270
- const recipeParts = (recipe.slots ?? []).map((slot) => [slot, init(slot)]);
271
- for (const [variantsKey, variantsSpec] of Object.entries(recipe.variants ?? {})) for (const [variantKey, variantSpec] of Object.entries(variantsSpec)) recipeParts.forEach(([slot, slotRecipe]) => {
272
- slotRecipe.variants[variantsKey] ??= {};
273
- slotRecipe.variants[variantsKey][variantKey] = variantSpec[slot] ?? {};
274
- });
275
- return Object.fromEntries(recipeParts);
276
- };
277
- const getSlotCompoundVariant = (compoundVariants, slotName) => compoundVariants.filter((compoundVariant) => compoundVariant.css[slotName]).map((compoundVariant) => ({
278
- ...compoundVariant,
279
- css: compoundVariant.css[slotName]
280
- }));
281
- //#endregion
282
- //#region src/split-props.ts
283
- function splitProps(props, ...keys) {
284
- const descriptors = Object.getOwnPropertyDescriptors(props);
285
- const dKeys = Object.keys(descriptors);
286
- const split = (k) => {
287
- const clone = {};
288
- for (let i = 0; i < k.length; i++) {
289
- const key = k[i];
290
- if (descriptors[key]) {
291
- Object.defineProperty(clone, key, descriptors[key]);
292
- delete descriptors[key];
293
- }
294
- }
295
- return clone;
296
- };
297
- const fn = (key) => split(Array.isArray(key) ? key : dKeys.filter(key));
298
- return keys.map(fn).concat(split(dKeys));
299
- }
300
- //#endregion
301
- //#region src/uniq.ts
302
- const uniq = (...items) => {
303
- const set = items.reduce((acc, currItems) => {
304
- if (currItems) currItems.forEach((item) => acc.add(item));
305
- return acc;
306
- }, /* @__PURE__ */ new Set([]));
307
- return Array.from(set);
308
- };
309
- //#endregion
310
- Object.defineProperty(exports, "compact", {
311
- enumerable: true,
312
- get: function() {
313
- return compact;
314
- }
315
- });
316
- Object.defineProperty(exports, "createCss", {
317
- enumerable: true,
318
- get: function() {
319
- return createCss;
320
- }
321
- });
322
- Object.defineProperty(exports, "createMergeCss", {
323
- enumerable: true,
324
- get: function() {
325
- return createMergeCss;
326
- }
327
- });
328
- Object.defineProperty(exports, "filterBaseConditions", {
329
- enumerable: true,
330
- get: function() {
331
- return filterBaseConditions;
332
- }
333
- });
334
- Object.defineProperty(exports, "getPatternStyles", {
335
- enumerable: true,
336
- get: function() {
337
- return getPatternStyles;
338
- }
339
- });
340
- Object.defineProperty(exports, "getSlotCompoundVariant", {
341
- enumerable: true,
342
- get: function() {
343
- return getSlotCompoundVariant;
344
- }
345
- });
346
- Object.defineProperty(exports, "getSlotRecipes", {
347
- enumerable: true,
348
- get: function() {
349
- return getSlotRecipes;
350
- }
351
- });
352
- Object.defineProperty(exports, "hypenateProperty", {
353
- enumerable: true,
354
- get: function() {
355
- return hypenateProperty;
356
- }
357
- });
358
- Object.defineProperty(exports, "isBaseCondition", {
359
- enumerable: true,
360
- get: function() {
361
- return isBaseCondition;
362
- }
363
- });
364
- Object.defineProperty(exports, "isBoolean", {
365
- enumerable: true,
366
- get: function() {
367
- return isBoolean;
368
- }
369
- });
370
- Object.defineProperty(exports, "isCssFunction", {
371
- enumerable: true,
372
- get: function() {
373
- return isCssFunction;
374
- }
375
- });
376
- Object.defineProperty(exports, "isCssUnit", {
377
- enumerable: true,
378
- get: function() {
379
- return isCssUnit;
380
- }
381
- });
382
- Object.defineProperty(exports, "isCssVar", {
383
- enumerable: true,
384
- get: function() {
385
- return isCssVar;
386
- }
387
- });
388
- Object.defineProperty(exports, "isFunction", {
389
- enumerable: true,
390
- get: function() {
391
- return isFunction;
392
- }
393
- });
394
- Object.defineProperty(exports, "isImportant", {
395
- enumerable: true,
396
- get: function() {
397
- return isImportant;
398
- }
399
- });
400
- Object.defineProperty(exports, "isObject", {
401
- enumerable: true,
402
- get: function() {
403
- return isObject;
404
- }
405
- });
406
- Object.defineProperty(exports, "isObjectOrArray", {
407
- enumerable: true,
408
- get: function() {
409
- return isObjectOrArray;
410
- }
411
- });
412
- Object.defineProperty(exports, "isString", {
413
- enumerable: true,
414
- get: function() {
415
- return isString;
416
- }
417
- });
418
- Object.defineProperty(exports, "isSymbol", {
419
- enumerable: true,
420
- get: function() {
421
- return isSymbol;
422
- }
423
- });
424
- Object.defineProperty(exports, "mapObject", {
425
- enumerable: true,
426
- get: function() {
427
- return mapObject;
428
- }
429
- });
430
- Object.defineProperty(exports, "markImportant", {
431
- enumerable: true,
432
- get: function() {
433
- return markImportant;
434
- }
435
- });
436
- Object.defineProperty(exports, "memo", {
437
- enumerable: true,
438
- get: function() {
439
- return memo;
440
- }
441
- });
442
- Object.defineProperty(exports, "mergeProps", {
443
- enumerable: true,
444
- get: function() {
445
- return mergeProps;
446
- }
447
- });
448
- Object.defineProperty(exports, "normalizeStyleObject", {
449
- enumerable: true,
450
- get: function() {
451
- return normalizeStyleObject;
452
- }
453
- });
454
- Object.defineProperty(exports, "patternFns", {
455
- enumerable: true,
456
- get: function() {
457
- return patternFns;
458
- }
459
- });
460
- Object.defineProperty(exports, "splitProps", {
461
- enumerable: true,
462
- get: function() {
463
- return splitProps;
464
- }
465
- });
466
- Object.defineProperty(exports, "toHash", {
467
- enumerable: true,
468
- get: function() {
469
- return toHash;
470
- }
471
- });
472
- Object.defineProperty(exports, "toResponsiveObject", {
473
- enumerable: true,
474
- get: function() {
475
- return toResponsiveObject;
476
- }
477
- });
478
- Object.defineProperty(exports, "uniq", {
479
- enumerable: true,
480
- get: function() {
481
- return uniq;
482
- }
483
- });
484
- Object.defineProperty(exports, "walkObject", {
485
- enumerable: true,
486
- get: function() {
487
- return walkObject;
488
- }
489
- });
490
- Object.defineProperty(exports, "withoutImportant", {
491
- enumerable: true,
492
- get: function() {
493
- return withoutImportant;
494
- }
495
- });
496
- Object.defineProperty(exports, "withoutSpace", {
497
- enumerable: true,
498
- get: function() {
499
- return withoutSpace;
500
- }
501
- });