@arrai-innovations/reactive-helpers 8.1.0 → 8.1.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/.eslintrc.cjs +1 -1
- package/docs.md +142 -0
- package/package.json +1 -1
- package/use/combineClasses.js +46 -4
- package/utils/classes.js +53 -0
package/.eslintrc.cjs
CHANGED
|
@@ -5,7 +5,7 @@ module.exports = {
|
|
|
5
5
|
node: true,
|
|
6
6
|
},
|
|
7
7
|
plugins: ["jsdoc"],
|
|
8
|
-
extends: ["plugin:vue/vue3-essential", "eslint:recommended", "@vue/prettier", "plugin:jsdoc/recommended
|
|
8
|
+
extends: ["plugin:vue/vue3-essential", "eslint:recommended", "@vue/prettier", "plugin:jsdoc/recommended"],
|
|
9
9
|
|
|
10
10
|
rules: {
|
|
11
11
|
"no-console": "off", // console.error is useful.
|
package/docs.md
CHANGED
|
@@ -9,6 +9,35 @@
|
|
|
9
9
|
| [utils/lifecycleDebug] | Debug lifecycle hooks |
|
|
10
10
|
| [utils/transformWalk] | Object walking utility. |
|
|
11
11
|
|
|
12
|
+
## Constants
|
|
13
|
+
|
|
14
|
+
| Name | Description |
|
|
15
|
+
| ------------------ | ----------- |
|
|
16
|
+
| [objectifyClasses] |
|
|
17
|
+
| [combineClasses] |
|
|
18
|
+
| [stringifyClass] |
|
|
19
|
+
| [stringifyClasses] |
|
|
20
|
+
|
|
21
|
+
## Functions
|
|
22
|
+
|
|
23
|
+
| Name | Description |
|
|
24
|
+
| ---------------------------- | ----------- |
|
|
25
|
+
| [useCombineClasses(classes)] |
|
|
26
|
+
|
|
27
|
+
## Typedefs
|
|
28
|
+
|
|
29
|
+
| Name | Description |
|
|
30
|
+
| -------------------- | ------------------------------------------------------------------------------------------------------ |
|
|
31
|
+
| [CSSValue] | A string representing a CSS class or a space-separated list of CSS classes. |
|
|
32
|
+
| [CSSObject] | A CSS object where keys are CSS classes and values are booleans indicating whether to apply the class. |
|
|
33
|
+
| [CSSClasses] | A mixed array containing multiple ways of specifying CSS classes. |
|
|
34
|
+
| [CSSStringOrObject] | The amalgamated classes as returned by `objectifyClasses` & `combineClasses`. |
|
|
35
|
+
| [CSSValue] | A string representing a CSS class or a space-separated list of CSS classes. |
|
|
36
|
+
| [CSSObject] | A CSS object where keys are CSS classes and values are booleans indicating whether to apply the class. |
|
|
37
|
+
| [CSSClasses] | A mixed array containing multiple ways of specifying CSS classes. |
|
|
38
|
+
| [CSSClassesWithRefs] | A mixed array containing multiple ways of specifying CSS classes. |
|
|
39
|
+
| [CSSStringOrObject] | A CSS object or a space-separated list of CSS classes. |
|
|
40
|
+
|
|
12
41
|
## utils/assignReactiveObject
|
|
13
42
|
|
|
14
43
|
Reactive object assignment utilities
|
|
@@ -320,6 +349,105 @@ const transformed = transformWalk(obj, (key, value, path) => {
|
|
|
320
349
|
// }
|
|
321
350
|
```
|
|
322
351
|
|
|
352
|
+
## objectifyClasses
|
|
353
|
+
|
|
354
|
+
**Kind**: global constant
|
|
355
|
+
**Returns**: [`CSSStringOrObject`][1] - A CSS object or a space-separated list of CSS classes.
|
|
356
|
+
|
|
357
|
+
| Param | Type | Description |
|
|
358
|
+
| ---------- | -------------- | ----------------------------------------------------------------- |
|
|
359
|
+
| ...classes | [`CSSClasses`] | A mixed array containing multiple ways of specifying CSS classes. |
|
|
360
|
+
|
|
361
|
+
## combineClasses
|
|
362
|
+
|
|
363
|
+
**Kind**: global constant
|
|
364
|
+
**Returns**: [`CSSStringOrObject`] - A CSS object or a space-separated list of CSS classes.
|
|
365
|
+
|
|
366
|
+
| Param | Type | Description |
|
|
367
|
+
| ---------- | ---------------------- | ------------------------------------------------------------------------------------------------------------- |
|
|
368
|
+
| ...classes | [`CSSClassesWithRefs`] | Handles as arguments the multiple ways of specifying CSS class related values, including refs to such values. |
|
|
369
|
+
|
|
370
|
+
## stringifyClass
|
|
371
|
+
|
|
372
|
+
**Kind**: global constant
|
|
373
|
+
**Returns**: `CSSString` - A space-separated list of CSS classes.
|
|
374
|
+
|
|
375
|
+
| Param | Type | Description |
|
|
376
|
+
| ----- | -------------- | ----------------------------------------------------------------- |
|
|
377
|
+
| cls | [`CSSClasses`] | A mixed array containing multiple ways of specifying CSS classes. |
|
|
378
|
+
|
|
379
|
+
## stringifyClasses
|
|
380
|
+
|
|
381
|
+
**Kind**: global constant
|
|
382
|
+
**Returns**: `CSSString` - A space-separated list of CSS classes.
|
|
383
|
+
|
|
384
|
+
| Param | Type | Description |
|
|
385
|
+
| ---------- | -------------- | ------------------------------------------------------------------------------ |
|
|
386
|
+
| ...classes | [`CSSClasses`] | Handles as arguments the multiple ways of specifying CSS class related values. |
|
|
387
|
+
|
|
388
|
+
## useCombineClasses(classes)
|
|
389
|
+
|
|
390
|
+
**Kind**: global function
|
|
391
|
+
**Returns**: `Ref.<CSSStringOrObject>` - A Vue ref pointing to the amalgamated CSS string or object.
|
|
392
|
+
|
|
393
|
+
| Param | Type | Description |
|
|
394
|
+
| ------- | -------------- | -------------------------------------------------------------------------------------------- |
|
|
395
|
+
| classes | [`CSSClasses`] | A mixed array containing multiple ways of specifying CSS classes. Non-ref values are cloned. |
|
|
396
|
+
|
|
397
|
+
## CSSValue
|
|
398
|
+
|
|
399
|
+
A string representing a CSS class or a space-separated list of CSS classes.
|
|
400
|
+
|
|
401
|
+
**Kind**: global typedef
|
|
402
|
+
|
|
403
|
+
## CSSObject
|
|
404
|
+
|
|
405
|
+
A CSS object where keys are CSS classes and values are booleans indicating whether to apply the class.
|
|
406
|
+
|
|
407
|
+
**Kind**: global typedef
|
|
408
|
+
|
|
409
|
+
## CSSClasses
|
|
410
|
+
|
|
411
|
+
A mixed array containing multiple ways of specifying CSS classes.
|
|
412
|
+
|
|
413
|
+
**Kind**: global typedef
|
|
414
|
+
|
|
415
|
+
## CSSStringOrObject
|
|
416
|
+
|
|
417
|
+
The amalgamated classes as returned by `objectifyClasses` & `combineClasses`.
|
|
418
|
+
|
|
419
|
+
**Kind**: global typedef
|
|
420
|
+
|
|
421
|
+
## CSSValue
|
|
422
|
+
|
|
423
|
+
A string representing a CSS class or a space-separated list of CSS classes.
|
|
424
|
+
|
|
425
|
+
**Kind**: global typedef
|
|
426
|
+
|
|
427
|
+
## CSSObject
|
|
428
|
+
|
|
429
|
+
A CSS object where keys are CSS classes and values are booleans indicating whether to apply the class.
|
|
430
|
+
|
|
431
|
+
**Kind**: global typedef
|
|
432
|
+
|
|
433
|
+
## CSSClasses
|
|
434
|
+
|
|
435
|
+
A mixed array containing multiple ways of specifying CSS classes.
|
|
436
|
+
|
|
437
|
+
**Kind**: global typedef
|
|
438
|
+
|
|
439
|
+
## CSSClassesWithRefs
|
|
440
|
+
|
|
441
|
+
A mixed array containing multiple ways of specifying CSS classes.
|
|
442
|
+
|
|
443
|
+
**Kind**: global typedef
|
|
444
|
+
|
|
445
|
+
## CSSStringOrObject
|
|
446
|
+
|
|
447
|
+
A CSS object or a space-separated list of CSS classes.
|
|
448
|
+
|
|
449
|
+
**Kind**: global typedef
|
|
450
|
+
|
|
323
451
|
<!-- LINKS -->
|
|
324
452
|
|
|
325
453
|
[utils/assignreactiveobject]: #utilsassignreactiveobject
|
|
@@ -328,6 +456,15 @@ const transformed = transformWalk(obj, (key, value, path) => {
|
|
|
328
456
|
[utils/keydiff]: #utilskeydiff
|
|
329
457
|
[utils/lifecycledebug]: #utilslifecycledebug
|
|
330
458
|
[utils/transformwalk]: #utilstransformwalk
|
|
459
|
+
[objectifyclasses]: #objectifyclasses
|
|
460
|
+
[combineclasses]: #combineclasses
|
|
461
|
+
[stringifyclass]: #stringifyclass
|
|
462
|
+
[stringifyclasses]: #stringifyclasses
|
|
463
|
+
[cssvalue]: #cssvalue
|
|
464
|
+
[cssobject]: #cssobject
|
|
465
|
+
[cssclasses]: #cssclasses
|
|
466
|
+
[cssstringorobject]: #cssstringorobject
|
|
467
|
+
[cssclasseswithrefs]: #cssclasseswithrefs
|
|
331
468
|
[~validtargetorsource]: #utilsassignreactiveobjectvalidtargetorsource
|
|
332
469
|
[`utils/assignreactiveobject`]: #utilsassignreactiveobject
|
|
333
470
|
[~debugmessagefunction]: #utilsdebugmessagedebugmessagefunction
|
|
@@ -338,6 +475,11 @@ const transformed = transformWalk(obj, (key, value, path) => {
|
|
|
338
475
|
[`utils/keydiff`]: #utilskeydiff
|
|
339
476
|
[`utils/lifecycledebug`]: #utilslifecycledebug
|
|
340
477
|
[`utils/transformwalk`]: #utilstransformwalk
|
|
478
|
+
[1]: #cssstringorobject
|
|
479
|
+
[`cssclasses`]: #cssclasses
|
|
480
|
+
[`cssstringorobject`]: #cssstringorobject
|
|
481
|
+
[`cssclasseswithrefs`]: #cssclasseswithrefs
|
|
482
|
+
[usecombineclasses(classes)]: #usecombineclassesclasses
|
|
341
483
|
[~addreactiveobject(target, source, \[exclude\], \[addedkeys\])]: #utilsassignreactiveobjectaddreactiveobjecttarget-source-exclude-addedkeys
|
|
342
484
|
[~updatereactiveobject(target, source, \[exclude\], \[samekeys\])]: #utilsassignreactiveobjectupdatereactiveobjecttarget-source-exclude-samekeys
|
|
343
485
|
[~addorupdatereactiveobject(target, source, \[exclude\], \[addedkeys\], \[samekeys\])]: #utilsassignreactiveobjectaddorupdatereactiveobjecttarget-source-exclude-addedkeys-samekeys
|
package/package.json
CHANGED
package/use/combineClasses.js
CHANGED
|
@@ -1,16 +1,58 @@
|
|
|
1
1
|
import { combineClasses } from "../utils/classes";
|
|
2
|
-
import {
|
|
2
|
+
import { cloneDeep } from "lodash-es";
|
|
3
|
+
import { isRef, ref, watch, isReactive } from "vue";
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {*} Ref A Vue ref
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {string} CSSString A string representing a CSS class or a space-separated list of CSS classes.
|
|
12
|
+
* @typedef {CSSString|CSSString[]} CSSClassNames An array of CSS string(s) or a single CSS string.
|
|
13
|
+
* @typedef {boolean|Ref<boolean>} CSSValue A truthy value or a reference to a truthy value, indicating whether to apply a CSS class, or unapply it if already applied.
|
|
14
|
+
*/
|
|
15
|
+
/* eslint-disable jsdoc/check-types */
|
|
16
|
+
// types valid for jsdoc-to-markdown, which uses the strict jsdoc.app. Object shorthand syntax doesn't work.
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {Object.<CSSString, CSSValue>} CSSObject A CSS object where keys are CSS classes and values are booleans indicating whether to apply the class.
|
|
19
|
+
*/
|
|
20
|
+
/* eslint-enable jsdoc/check-types */
|
|
21
|
+
/**
|
|
22
|
+
* A mixed array containing multiple ways of specifying CSS classes.
|
|
23
|
+
* @typedef {Array<
|
|
24
|
+
* CSSClassNames,
|
|
25
|
+
* CSSString[],
|
|
26
|
+
* CSSString,
|
|
27
|
+
* CSSObject,
|
|
28
|
+
* Ref<CSSClassNames>,
|
|
29
|
+
* Ref<CSSString[]>,
|
|
30
|
+
* Ref<CSSString>,
|
|
31
|
+
* Ref<CSSClassNames>
|
|
32
|
+
* >} CSSClasses
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {CSSString|CSSObject} CSSStringOrObject The amalgamated classes as returned by `objectifyClasses` & `combineClasses`.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
const isRefOrReactive = (v) => isRef(v) || isReactive(v);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {CSSClasses} classes A mixed array containing multiple ways of specifying CSS classes. Non-ref values are cloned.
|
|
42
|
+
* @returns {Ref<CSSStringOrObject>} A Vue ref pointing to the amalgamated CSS string or object.
|
|
43
|
+
*/
|
|
4
44
|
export function useCombineClasses(...classes) {
|
|
5
45
|
const classesComputed = ref();
|
|
6
|
-
const reactiveValues = classes.filter((c) =>
|
|
46
|
+
const reactiveValues = classes.filter((c) => isRefOrReactive(c));
|
|
47
|
+
// cloneDeep any non-reactive values, but maintain order
|
|
48
|
+
const myClasses = classes.map((c) => (isRefOrReactive(c) ? c : cloneDeep(c)));
|
|
7
49
|
if (!reactiveValues.length) {
|
|
8
|
-
classesComputed.value = combineClasses(...
|
|
50
|
+
classesComputed.value = combineClasses(...myClasses);
|
|
9
51
|
} else {
|
|
10
52
|
watch(
|
|
11
53
|
reactiveValues,
|
|
12
54
|
() => {
|
|
13
|
-
classesComputed.value = combineClasses(...
|
|
55
|
+
classesComputed.value = combineClasses(...myClasses);
|
|
14
56
|
},
|
|
15
57
|
{
|
|
16
58
|
immediate: true,
|
package/utils/classes.js
CHANGED
|
@@ -6,6 +6,46 @@ import isSet from "lodash-es/isSet";
|
|
|
6
6
|
import isString from "lodash-es/isString";
|
|
7
7
|
import { isRef, unref } from "vue";
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {*} Ref A Vue ref
|
|
11
|
+
* @private
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {string} CSSString A string representing a CSS class or a space-separated list of CSS classes.
|
|
16
|
+
* @typedef {CSSString|CSSString[]} CSSClassNames An array of CSS string(s) or a single CSS string.
|
|
17
|
+
* @typedef {boolean|Ref<boolean>} CSSValue A truthy value or a reference to a truthy value, indicating whether to apply a CSS class, or unapply it if already applied.
|
|
18
|
+
*/
|
|
19
|
+
/* eslint-disable jsdoc/check-types */
|
|
20
|
+
// types valid for jsdoc-to-markdown, which uses the strict jsdoc.app. Object shorthand syntax doesn't work.
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {Object.<CSSString, CSSValue>} CSSObject A CSS object where keys are CSS classes and values are booleans indicating whether to apply the class.
|
|
23
|
+
*/
|
|
24
|
+
/* eslint-enable jsdoc/check-types */
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {Array<CSSClassNames, CSSString[], CSSString, CSSObject>} CSSClasses A mixed array containing multiple ways of specifying CSS classes.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* A mixed array containing multiple ways of specifying CSS classes.
|
|
30
|
+
* @typedef {Array<
|
|
31
|
+
* CSSClassNames,
|
|
32
|
+
* CSSString[],
|
|
33
|
+
* CSSString,
|
|
34
|
+
* CSSObject,
|
|
35
|
+
* Ref<CSSClassNames>,
|
|
36
|
+
* Ref<CSSString[]>,
|
|
37
|
+
* Ref<CSSString>,
|
|
38
|
+
* Ref<CSSClassNames>
|
|
39
|
+
* >} CSSClassesWithRefs
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {CSSString|CSSObject} CSSStringOrObject A CSS object or a space-separated list of CSS classes.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {...CSSClasses} classes A mixed array containing multiple ways of specifying CSS classes.
|
|
47
|
+
* @returns {CSSStringOrObject} A CSS object or a space-separated list of CSS classes.
|
|
48
|
+
*/
|
|
9
49
|
export const objectifyClasses = (...classes) => {
|
|
10
50
|
const flatClasses = classes.flat(Infinity).filter(identity);
|
|
11
51
|
const objects = flatClasses.map((c) => {
|
|
@@ -71,6 +111,11 @@ const deepUnrefArrays = (val) => {
|
|
|
71
111
|
return checkedVal;
|
|
72
112
|
};
|
|
73
113
|
|
|
114
|
+
/**
|
|
115
|
+
* @param {...CSSClassesWithRefs} classes Handles as arguments the multiple ways of specifying CSS class related
|
|
116
|
+
* values, including refs to such values.
|
|
117
|
+
* @returns {CSSStringOrObject} A CSS object or a space-separated list of CSS classes.
|
|
118
|
+
*/
|
|
74
119
|
export const combineClasses = (...classes) => {
|
|
75
120
|
// we unref your refs, so probably want a computed around this
|
|
76
121
|
// ultimately, strings and objects are classes, arrays are organization and containers
|
|
@@ -88,6 +133,10 @@ export const combineClasses = (...classes) => {
|
|
|
88
133
|
return isEmpty(result) ? undefined : result;
|
|
89
134
|
};
|
|
90
135
|
|
|
136
|
+
/**
|
|
137
|
+
* @param {CSSClasses} cls A mixed array containing multiple ways of specifying CSS classes.
|
|
138
|
+
* @returns {CSSString} A space-separated list of CSS classes.
|
|
139
|
+
*/
|
|
91
140
|
export const stringifyClass = (cls) => {
|
|
92
141
|
if (isArray(cls)) {
|
|
93
142
|
return stringifyClasses(...cls);
|
|
@@ -100,6 +149,10 @@ export const stringifyClass = (cls) => {
|
|
|
100
149
|
}
|
|
101
150
|
};
|
|
102
151
|
|
|
152
|
+
/**
|
|
153
|
+
* @param {...CSSClasses} classes Handles as arguments the multiple ways of specifying CSS class related values.
|
|
154
|
+
* @returns {CSSString} A space-separated list of CSS classes.
|
|
155
|
+
*/
|
|
103
156
|
export const stringifyClasses = (...classes) =>
|
|
104
157
|
classes
|
|
105
158
|
.map(stringifyClass)
|