motion-dom 12.7.3 → 12.7.5-alpha.0
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/cjs/index.js +2946 -475
- package/dist/es/animation/AsyncMotionValueAnimation.mjs +178 -0
- package/dist/es/animation/GroupAnimation.mjs +6 -15
- package/dist/es/animation/JSAnimation.mjs +346 -0
- package/dist/es/animation/NativeAnimation.mjs +64 -67
- package/dist/es/animation/NativeAnimationExtended.mjs +65 -0
- package/dist/es/animation/NativeAnimationWrapper.mjs +14 -0
- package/dist/es/animation/drivers/driver-frameloop.mjs +17 -0
- package/dist/es/animation/generators/inertia.mjs +87 -0
- package/dist/es/animation/generators/keyframes.mjs +49 -0
- package/dist/es/animation/generators/spring/defaults.mjs +27 -0
- package/dist/es/animation/generators/spring/find.mjs +84 -0
- package/dist/es/animation/generators/spring/index.mjs +175 -0
- package/dist/es/animation/generators/utils/velocity.mjs +9 -0
- package/dist/es/animation/keyframes/DOMKeyframesResolver.mjs +131 -0
- package/dist/es/animation/keyframes/KeyframesResolver.mjs +156 -0
- package/dist/es/animation/keyframes/get-final.mjs +3 -4
- package/dist/es/animation/keyframes/offsets/default.mjs +9 -0
- package/dist/es/animation/keyframes/offsets/fill.mjs +12 -0
- package/dist/es/animation/keyframes/offsets/time.mjs +5 -0
- package/dist/es/animation/keyframes/utils/apply-px-defaults.mjs +11 -0
- package/dist/es/animation/keyframes/utils/fill-wildcards.mjs +7 -0
- package/dist/es/animation/keyframes/utils/is-none.mjs +15 -0
- package/dist/es/animation/keyframes/utils/make-none-animatable.mjs +30 -0
- package/dist/es/animation/keyframes/utils/unit-conversion.mjs +36 -0
- package/dist/es/animation/utils/WithPromise.mjs +28 -0
- package/dist/es/animation/utils/active-animations.mjs +9 -0
- package/dist/es/animation/utils/can-animate.mjs +42 -0
- package/dist/es/animation/utils/css-variables-conversion.mjs +41 -0
- package/dist/es/animation/utils/is-animatable.mjs +30 -0
- package/dist/es/animation/utils/is-css-variable.mjs +15 -0
- package/dist/es/animation/utils/replace-transition-type.mjs +18 -0
- package/dist/es/animation/waapi/easing/is-supported.mjs +1 -1
- package/dist/es/animation/waapi/easing/map-easing.mjs +5 -3
- package/dist/es/animation/waapi/start-waapi-animation.mjs +6 -4
- package/dist/es/animation/waapi/supports/waapi.mjs +39 -0
- package/dist/es/animation/waapi/utils/accelerated-values.mjs +14 -0
- package/dist/es/animation/waapi/utils/apply-generator.mjs +2 -1
- package/dist/es/animation/waapi/utils/unsupported-easing.mjs +18 -0
- package/dist/es/frameloop/batcher.mjs +2 -1
- package/dist/es/frameloop/order.mjs +1 -0
- package/dist/es/index.mjs +49 -2
- package/dist/es/render/dom/is-css-var.mjs +3 -0
- package/dist/es/render/dom/parse-transform.mjs +83 -0
- package/dist/es/render/dom/style-computed.mjs +10 -0
- package/dist/es/render/dom/style-set.mjs +9 -0
- package/dist/es/render/utils/keys-position.mjs +13 -0
- package/dist/es/render/utils/keys-transform.mjs +28 -0
- package/dist/es/scroll/observe.mjs +18 -0
- package/dist/es/stats/index.mjs +2 -0
- package/dist/es/utils/interpolate.mjs +74 -0
- package/dist/es/utils/mix/color.mjs +47 -0
- package/dist/es/utils/mix/complex.mjs +92 -0
- package/dist/es/utils/mix/immediate.mjs +5 -0
- package/dist/es/utils/mix/index.mjs +14 -0
- package/dist/es/utils/mix/number.mjs +26 -0
- package/dist/es/utils/mix/visibility.mjs +16 -0
- package/dist/es/value/index.mjs +3 -1
- package/dist/es/value/types/auto.mjs +9 -0
- package/dist/es/value/types/color/hex.mjs +40 -0
- package/dist/es/value/types/color/hsla-to-rgba.mjs +42 -0
- package/dist/es/value/types/color/hsla.mjs +22 -0
- package/dist/es/value/types/color/index.mjs +27 -0
- package/dist/es/value/types/color/rgba.mjs +25 -0
- package/dist/es/value/types/color/utils.mjs +29 -0
- package/dist/es/value/types/complex/filter.mjs +30 -0
- package/dist/es/value/types/complex/index.mjs +91 -0
- package/dist/es/value/types/dimensions.mjs +15 -0
- package/dist/es/value/types/int.mjs +8 -0
- package/dist/es/value/types/maps/defaults.mjs +30 -0
- package/dist/es/value/types/maps/number.mjs +51 -0
- package/dist/es/value/types/maps/transform.mjs +31 -0
- package/dist/es/value/types/numbers/index.mjs +17 -0
- package/dist/es/value/types/numbers/units.mjs +18 -0
- package/dist/es/value/types/test.mjs +6 -0
- package/dist/es/value/types/utils/animatable-none.mjs +15 -0
- package/dist/es/value/types/utils/color-regex.mjs +3 -0
- package/dist/es/value/types/utils/find.mjs +15 -0
- package/dist/es/value/types/utils/float-regex.mjs +3 -0
- package/dist/es/value/types/utils/get-as-type.mjs +10 -0
- package/dist/es/value/types/utils/is-nullish.mjs +5 -0
- package/dist/es/value/types/utils/sanitize.mjs +5 -0
- package/dist/es/value/types/utils/single-color-regex.mjs +3 -0
- package/dist/es/view/start.mjs +4 -3
- package/dist/index.d.ts +945 -444
- package/dist/motion-dom.dev.js +2945 -475
- package/dist/motion-dom.js +1 -1
- package/package.json +3 -3
- package/dist/es/animation/keyframes/hydrate.mjs +0 -26
- package/dist/es/animation/waapi/utils/attach-timeline.mjs +0 -6
- package/dist/es/render/dom/style.mjs +0 -15
- package/dist/es/utils/is-bezier-definition.mjs +0 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { invariant, clamp, MotionGlobalConfig, noop, pipe, progress } from 'motion-utils';
|
|
2
|
+
import { mix } from './mix/index.mjs';
|
|
3
|
+
|
|
4
|
+
function createMixers(output, ease, customMixer) {
|
|
5
|
+
const mixers = [];
|
|
6
|
+
const mixerFactory = customMixer || MotionGlobalConfig.mix || mix;
|
|
7
|
+
const numMixers = output.length - 1;
|
|
8
|
+
for (let i = 0; i < numMixers; i++) {
|
|
9
|
+
let mixer = mixerFactory(output[i], output[i + 1]);
|
|
10
|
+
if (ease) {
|
|
11
|
+
const easingFunction = Array.isArray(ease) ? ease[i] || noop : ease;
|
|
12
|
+
mixer = pipe(easingFunction, mixer);
|
|
13
|
+
}
|
|
14
|
+
mixers.push(mixer);
|
|
15
|
+
}
|
|
16
|
+
return mixers;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Create a function that maps from a numerical input array to a generic output array.
|
|
20
|
+
*
|
|
21
|
+
* Accepts:
|
|
22
|
+
* - Numbers
|
|
23
|
+
* - Colors (hex, hsl, hsla, rgb, rgba)
|
|
24
|
+
* - Complex (combinations of one or more numbers or strings)
|
|
25
|
+
*
|
|
26
|
+
* ```jsx
|
|
27
|
+
* const mixColor = interpolate([0, 1], ['#fff', '#000'])
|
|
28
|
+
*
|
|
29
|
+
* mixColor(0.5) // 'rgba(128, 128, 128, 1)'
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* TODO Revist this approach once we've moved to data models for values,
|
|
33
|
+
* probably not needed to pregenerate mixer functions.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
function interpolate(input, output, { clamp: isClamp = true, ease, mixer } = {}) {
|
|
38
|
+
const inputLength = input.length;
|
|
39
|
+
invariant(inputLength === output.length, "Both input and output ranges must be the same length");
|
|
40
|
+
/**
|
|
41
|
+
* If we're only provided a single input, we can just make a function
|
|
42
|
+
* that returns the output.
|
|
43
|
+
*/
|
|
44
|
+
if (inputLength === 1)
|
|
45
|
+
return () => output[0];
|
|
46
|
+
if (inputLength === 2 && output[0] === output[1])
|
|
47
|
+
return () => output[1];
|
|
48
|
+
const isZeroDeltaRange = input[0] === input[1];
|
|
49
|
+
// If input runs highest -> lowest, reverse both arrays
|
|
50
|
+
if (input[0] > input[inputLength - 1]) {
|
|
51
|
+
input = [...input].reverse();
|
|
52
|
+
output = [...output].reverse();
|
|
53
|
+
}
|
|
54
|
+
const mixers = createMixers(output, ease, mixer);
|
|
55
|
+
const numMixers = mixers.length;
|
|
56
|
+
const interpolator = (v) => {
|
|
57
|
+
if (isZeroDeltaRange && v < input[0])
|
|
58
|
+
return output[0];
|
|
59
|
+
let i = 0;
|
|
60
|
+
if (numMixers > 1) {
|
|
61
|
+
for (; i < input.length - 2; i++) {
|
|
62
|
+
if (v < input[i + 1])
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const progressInRange = progress(input[i], input[i + 1], v);
|
|
67
|
+
return mixers[i](progressInRange);
|
|
68
|
+
};
|
|
69
|
+
return isClamp
|
|
70
|
+
? (v) => interpolator(clamp(input[0], input[inputLength - 1], v))
|
|
71
|
+
: interpolator;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { interpolate };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { warning } from 'motion-utils';
|
|
2
|
+
import { hex } from '../../value/types/color/hex.mjs';
|
|
3
|
+
import { hsla } from '../../value/types/color/hsla.mjs';
|
|
4
|
+
import { hslaToRgba } from '../../value/types/color/hsla-to-rgba.mjs';
|
|
5
|
+
import { rgba } from '../../value/types/color/rgba.mjs';
|
|
6
|
+
import { mixImmediate } from './immediate.mjs';
|
|
7
|
+
import { mixNumber } from './number.mjs';
|
|
8
|
+
|
|
9
|
+
// Linear color space blending
|
|
10
|
+
// Explained https://www.youtube.com/watch?v=LKnqECcg6Gw
|
|
11
|
+
// Demonstrated http://codepen.io/osublake/pen/xGVVaN
|
|
12
|
+
const mixLinearColor = (from, to, v) => {
|
|
13
|
+
const fromExpo = from * from;
|
|
14
|
+
const expo = v * (to * to - fromExpo) + fromExpo;
|
|
15
|
+
return expo < 0 ? 0 : Math.sqrt(expo);
|
|
16
|
+
};
|
|
17
|
+
const colorTypes = [hex, rgba, hsla];
|
|
18
|
+
const getColorType = (v) => colorTypes.find((type) => type.test(v));
|
|
19
|
+
function asRGBA(color) {
|
|
20
|
+
const type = getColorType(color);
|
|
21
|
+
warning(Boolean(type), `'${color}' is not an animatable color. Use the equivalent color code instead.`);
|
|
22
|
+
if (!Boolean(type))
|
|
23
|
+
return false;
|
|
24
|
+
let model = type.parse(color);
|
|
25
|
+
if (type === hsla) {
|
|
26
|
+
// TODO Remove this cast - needed since Motion's stricter typing
|
|
27
|
+
model = hslaToRgba(model);
|
|
28
|
+
}
|
|
29
|
+
return model;
|
|
30
|
+
}
|
|
31
|
+
const mixColor = (from, to) => {
|
|
32
|
+
const fromRGBA = asRGBA(from);
|
|
33
|
+
const toRGBA = asRGBA(to);
|
|
34
|
+
if (!fromRGBA || !toRGBA) {
|
|
35
|
+
return mixImmediate(from, to);
|
|
36
|
+
}
|
|
37
|
+
const blended = { ...fromRGBA };
|
|
38
|
+
return (v) => {
|
|
39
|
+
blended.red = mixLinearColor(fromRGBA.red, toRGBA.red, v);
|
|
40
|
+
blended.green = mixLinearColor(fromRGBA.green, toRGBA.green, v);
|
|
41
|
+
blended.blue = mixLinearColor(fromRGBA.blue, toRGBA.blue, v);
|
|
42
|
+
blended.alpha = mixNumber(fromRGBA.alpha, toRGBA.alpha, v);
|
|
43
|
+
return rgba.transform(blended);
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { mixColor, mixLinearColor };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { pipe, warning } from 'motion-utils';
|
|
2
|
+
import { isCSSVariableToken } from '../../animation/utils/is-css-variable.mjs';
|
|
3
|
+
import { color } from '../../value/types/color/index.mjs';
|
|
4
|
+
import { complex, analyseComplexValue } from '../../value/types/complex/index.mjs';
|
|
5
|
+
import { mixColor } from './color.mjs';
|
|
6
|
+
import { mixImmediate } from './immediate.mjs';
|
|
7
|
+
import { mixNumber as mixNumber$1 } from './number.mjs';
|
|
8
|
+
import { invisibleValues, mixVisibility } from './visibility.mjs';
|
|
9
|
+
|
|
10
|
+
function mixNumber(a, b) {
|
|
11
|
+
return (p) => mixNumber$1(a, b, p);
|
|
12
|
+
}
|
|
13
|
+
function getMixer(a) {
|
|
14
|
+
if (typeof a === "number") {
|
|
15
|
+
return mixNumber;
|
|
16
|
+
}
|
|
17
|
+
else if (typeof a === "string") {
|
|
18
|
+
return isCSSVariableToken(a)
|
|
19
|
+
? mixImmediate
|
|
20
|
+
: color.test(a)
|
|
21
|
+
? mixColor
|
|
22
|
+
: mixComplex;
|
|
23
|
+
}
|
|
24
|
+
else if (Array.isArray(a)) {
|
|
25
|
+
return mixArray;
|
|
26
|
+
}
|
|
27
|
+
else if (typeof a === "object") {
|
|
28
|
+
return color.test(a) ? mixColor : mixObject;
|
|
29
|
+
}
|
|
30
|
+
return mixImmediate;
|
|
31
|
+
}
|
|
32
|
+
function mixArray(a, b) {
|
|
33
|
+
const output = [...a];
|
|
34
|
+
const numValues = output.length;
|
|
35
|
+
const blendValue = a.map((v, i) => getMixer(v)(v, b[i]));
|
|
36
|
+
return (p) => {
|
|
37
|
+
for (let i = 0; i < numValues; i++) {
|
|
38
|
+
output[i] = blendValue[i](p);
|
|
39
|
+
}
|
|
40
|
+
return output;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function mixObject(a, b) {
|
|
44
|
+
const output = { ...a, ...b };
|
|
45
|
+
const blendValue = {};
|
|
46
|
+
for (const key in output) {
|
|
47
|
+
if (a[key] !== undefined && b[key] !== undefined) {
|
|
48
|
+
blendValue[key] = getMixer(a[key])(a[key], b[key]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return (v) => {
|
|
52
|
+
for (const key in blendValue) {
|
|
53
|
+
output[key] = blendValue[key](v);
|
|
54
|
+
}
|
|
55
|
+
return output;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function matchOrder(origin, target) {
|
|
59
|
+
const orderedOrigin = [];
|
|
60
|
+
const pointers = { color: 0, var: 0, number: 0 };
|
|
61
|
+
for (let i = 0; i < target.values.length; i++) {
|
|
62
|
+
const type = target.types[i];
|
|
63
|
+
const originIndex = origin.indexes[type][pointers[type]];
|
|
64
|
+
const originValue = origin.values[originIndex] ?? 0;
|
|
65
|
+
orderedOrigin[i] = originValue;
|
|
66
|
+
pointers[type]++;
|
|
67
|
+
}
|
|
68
|
+
return orderedOrigin;
|
|
69
|
+
}
|
|
70
|
+
const mixComplex = (origin, target) => {
|
|
71
|
+
const template = complex.createTransformer(target);
|
|
72
|
+
const originStats = analyseComplexValue(origin);
|
|
73
|
+
const targetStats = analyseComplexValue(target);
|
|
74
|
+
const canInterpolate = originStats.indexes.var.length === targetStats.indexes.var.length &&
|
|
75
|
+
originStats.indexes.color.length === targetStats.indexes.color.length &&
|
|
76
|
+
originStats.indexes.number.length >= targetStats.indexes.number.length;
|
|
77
|
+
if (canInterpolate) {
|
|
78
|
+
if ((invisibleValues.has(origin) &&
|
|
79
|
+
!targetStats.values.length) ||
|
|
80
|
+
(invisibleValues.has(target) &&
|
|
81
|
+
!originStats.values.length)) {
|
|
82
|
+
return mixVisibility(origin, target);
|
|
83
|
+
}
|
|
84
|
+
return pipe(mixArray(matchOrder(originStats, targetStats), targetStats.values), template);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
warning(true, `Complex values '${origin}' and '${target}' too different to mix. Ensure all colors are of the same type, and that each contains the same quantity of number and color values. Falling back to instant transition.`);
|
|
88
|
+
return mixImmediate(origin, target);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export { getMixer, mixArray, mixComplex, mixObject };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getMixer } from './complex.mjs';
|
|
2
|
+
import { mixNumber } from './number.mjs';
|
|
3
|
+
|
|
4
|
+
function mix(from, to, p) {
|
|
5
|
+
if (typeof from === "number" &&
|
|
6
|
+
typeof to === "number" &&
|
|
7
|
+
typeof p === "number") {
|
|
8
|
+
return mixNumber(from, to, p);
|
|
9
|
+
}
|
|
10
|
+
const mixer = getMixer(from);
|
|
11
|
+
return mixer(from, to);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { mix };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Value in range from progress
|
|
3
|
+
|
|
4
|
+
Given a lower limit and an upper limit, we return the value within
|
|
5
|
+
that range as expressed by progress (usually a number from 0 to 1)
|
|
6
|
+
|
|
7
|
+
So progress = 0.5 would change
|
|
8
|
+
|
|
9
|
+
from -------- to
|
|
10
|
+
|
|
11
|
+
to
|
|
12
|
+
|
|
13
|
+
from ---- to
|
|
14
|
+
|
|
15
|
+
E.g. from = 10, to = 20, progress = 0.5 => 15
|
|
16
|
+
|
|
17
|
+
@param [number]: Lower limit of range
|
|
18
|
+
@param [number]: Upper limit of range
|
|
19
|
+
@param [number]: The progress between lower and upper limits expressed 0-1
|
|
20
|
+
@return [number]: Value as calculated from progress within range (not limited within range)
|
|
21
|
+
*/
|
|
22
|
+
const mixNumber = (from, to, progress) => {
|
|
23
|
+
return from + (to - from) * progress;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { mixNumber };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const invisibleValues = new Set(["none", "hidden"]);
|
|
2
|
+
/**
|
|
3
|
+
* Returns a function that, when provided a progress value between 0 and 1,
|
|
4
|
+
* will return the "none" or "hidden" string only when the progress is that of
|
|
5
|
+
* the origin or target.
|
|
6
|
+
*/
|
|
7
|
+
function mixVisibility(origin, target) {
|
|
8
|
+
if (invisibleValues.has(origin)) {
|
|
9
|
+
return (p) => (p <= 0 ? origin : target);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return (p) => (p >= 1 ? target : origin);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { invisibleValues, mixVisibility };
|
package/dist/es/value/index.mjs
CHANGED
|
@@ -30,7 +30,7 @@ class MotionValue {
|
|
|
30
30
|
* This will be replaced by the build step with the latest version number.
|
|
31
31
|
* When MotionValues are provided to motion components, warn if versions are mixed.
|
|
32
32
|
*/
|
|
33
|
-
this.version = "12.7.
|
|
33
|
+
this.version = "12.7.5-alpha.0";
|
|
34
34
|
/**
|
|
35
35
|
* Tracks whether this value can output a velocity. Currently this is only true
|
|
36
36
|
* if the value is numerical, but we might be able to widen the scope here and support
|
|
@@ -174,6 +174,8 @@ class MotionValue {
|
|
|
174
174
|
* @public
|
|
175
175
|
*/
|
|
176
176
|
set(v, render = true) {
|
|
177
|
+
if (v === "none")
|
|
178
|
+
console.trace();
|
|
177
179
|
if (!render || !this.passiveEffect) {
|
|
178
180
|
this.updateAndNotify(v, render);
|
|
179
181
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { rgba } from './rgba.mjs';
|
|
2
|
+
import { isColorString } from './utils.mjs';
|
|
3
|
+
|
|
4
|
+
function parseHex(v) {
|
|
5
|
+
let r = "";
|
|
6
|
+
let g = "";
|
|
7
|
+
let b = "";
|
|
8
|
+
let a = "";
|
|
9
|
+
// If we have 6 characters, ie #FF0000
|
|
10
|
+
if (v.length > 5) {
|
|
11
|
+
r = v.substring(1, 3);
|
|
12
|
+
g = v.substring(3, 5);
|
|
13
|
+
b = v.substring(5, 7);
|
|
14
|
+
a = v.substring(7, 9);
|
|
15
|
+
// Or we have 3 characters, ie #F00
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
r = v.substring(1, 2);
|
|
19
|
+
g = v.substring(2, 3);
|
|
20
|
+
b = v.substring(3, 4);
|
|
21
|
+
a = v.substring(4, 5);
|
|
22
|
+
r += r;
|
|
23
|
+
g += g;
|
|
24
|
+
b += b;
|
|
25
|
+
a += a;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
red: parseInt(r, 16),
|
|
29
|
+
green: parseInt(g, 16),
|
|
30
|
+
blue: parseInt(b, 16),
|
|
31
|
+
alpha: a ? parseInt(a, 16) / 255 : 1,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const hex = {
|
|
35
|
+
test: /*@__PURE__*/ isColorString("#"),
|
|
36
|
+
parse: parseHex,
|
|
37
|
+
transform: rgba.transform,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { hex };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Adapted from https://gist.github.com/mjackson/5311256
|
|
2
|
+
function hueToRgb(p, q, t) {
|
|
3
|
+
if (t < 0)
|
|
4
|
+
t += 1;
|
|
5
|
+
if (t > 1)
|
|
6
|
+
t -= 1;
|
|
7
|
+
if (t < 1 / 6)
|
|
8
|
+
return p + (q - p) * 6 * t;
|
|
9
|
+
if (t < 1 / 2)
|
|
10
|
+
return q;
|
|
11
|
+
if (t < 2 / 3)
|
|
12
|
+
return p + (q - p) * (2 / 3 - t) * 6;
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
function hslaToRgba({ hue, saturation, lightness, alpha }) {
|
|
16
|
+
hue /= 360;
|
|
17
|
+
saturation /= 100;
|
|
18
|
+
lightness /= 100;
|
|
19
|
+
let red = 0;
|
|
20
|
+
let green = 0;
|
|
21
|
+
let blue = 0;
|
|
22
|
+
if (!saturation) {
|
|
23
|
+
red = green = blue = lightness;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const q = lightness < 0.5
|
|
27
|
+
? lightness * (1 + saturation)
|
|
28
|
+
: lightness + saturation - lightness * saturation;
|
|
29
|
+
const p = 2 * lightness - q;
|
|
30
|
+
red = hueToRgb(p, q, hue + 1 / 3);
|
|
31
|
+
green = hueToRgb(p, q, hue);
|
|
32
|
+
blue = hueToRgb(p, q, hue - 1 / 3);
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
red: Math.round(red * 255),
|
|
36
|
+
green: Math.round(green * 255),
|
|
37
|
+
blue: Math.round(blue * 255),
|
|
38
|
+
alpha,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { hslaToRgba };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { alpha } from '../numbers/index.mjs';
|
|
2
|
+
import { percent } from '../numbers/units.mjs';
|
|
3
|
+
import { sanitize } from '../utils/sanitize.mjs';
|
|
4
|
+
import { isColorString, splitColor } from './utils.mjs';
|
|
5
|
+
|
|
6
|
+
const hsla = {
|
|
7
|
+
test: /*@__PURE__*/ isColorString("hsl", "hue"),
|
|
8
|
+
parse: /*@__PURE__*/ splitColor("hue", "saturation", "lightness"),
|
|
9
|
+
transform: ({ hue, saturation, lightness, alpha: alpha$1 = 1 }) => {
|
|
10
|
+
return ("hsla(" +
|
|
11
|
+
Math.round(hue) +
|
|
12
|
+
", " +
|
|
13
|
+
percent.transform(sanitize(saturation)) +
|
|
14
|
+
", " +
|
|
15
|
+
percent.transform(sanitize(lightness)) +
|
|
16
|
+
", " +
|
|
17
|
+
sanitize(alpha.transform(alpha$1)) +
|
|
18
|
+
")");
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { hsla };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { hex } from './hex.mjs';
|
|
2
|
+
import { hsla } from './hsla.mjs';
|
|
3
|
+
import { rgba } from './rgba.mjs';
|
|
4
|
+
|
|
5
|
+
const color = {
|
|
6
|
+
test: (v) => rgba.test(v) || hex.test(v) || hsla.test(v),
|
|
7
|
+
parse: (v) => {
|
|
8
|
+
if (rgba.test(v)) {
|
|
9
|
+
return rgba.parse(v);
|
|
10
|
+
}
|
|
11
|
+
else if (hsla.test(v)) {
|
|
12
|
+
return hsla.parse(v);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return hex.parse(v);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
transform: (v) => {
|
|
19
|
+
return typeof v === "string"
|
|
20
|
+
? v
|
|
21
|
+
: v.hasOwnProperty("red")
|
|
22
|
+
? rgba.transform(v)
|
|
23
|
+
: hsla.transform(v);
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { color };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { clamp } from 'motion-utils';
|
|
2
|
+
import { number, alpha } from '../numbers/index.mjs';
|
|
3
|
+
import { sanitize } from '../utils/sanitize.mjs';
|
|
4
|
+
import { isColorString, splitColor } from './utils.mjs';
|
|
5
|
+
|
|
6
|
+
const clampRgbUnit = (v) => clamp(0, 255, v);
|
|
7
|
+
const rgbUnit = {
|
|
8
|
+
...number,
|
|
9
|
+
transform: (v) => Math.round(clampRgbUnit(v)),
|
|
10
|
+
};
|
|
11
|
+
const rgba = {
|
|
12
|
+
test: /*@__PURE__*/ isColorString("rgb", "red"),
|
|
13
|
+
parse: /*@__PURE__*/ splitColor("red", "green", "blue"),
|
|
14
|
+
transform: ({ red, green, blue, alpha: alpha$1 = 1 }) => "rgba(" +
|
|
15
|
+
rgbUnit.transform(red) +
|
|
16
|
+
", " +
|
|
17
|
+
rgbUnit.transform(green) +
|
|
18
|
+
", " +
|
|
19
|
+
rgbUnit.transform(blue) +
|
|
20
|
+
", " +
|
|
21
|
+
sanitize(alpha.transform(alpha$1)) +
|
|
22
|
+
")",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { rgbUnit, rgba };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { floatRegex } from '../utils/float-regex.mjs';
|
|
2
|
+
import { isNullish } from '../utils/is-nullish.mjs';
|
|
3
|
+
import { singleColorRegex } from '../utils/single-color-regex.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if the provided string is a color, ie rgba(0,0,0,0) or #000,
|
|
7
|
+
* but false if a number or multiple colors
|
|
8
|
+
*/
|
|
9
|
+
const isColorString = (type, testProp) => (v) => {
|
|
10
|
+
return Boolean((typeof v === "string" &&
|
|
11
|
+
singleColorRegex.test(v) &&
|
|
12
|
+
v.startsWith(type)) ||
|
|
13
|
+
(testProp &&
|
|
14
|
+
!isNullish(v) &&
|
|
15
|
+
Object.prototype.hasOwnProperty.call(v, testProp)));
|
|
16
|
+
};
|
|
17
|
+
const splitColor = (aName, bName, cName) => (v) => {
|
|
18
|
+
if (typeof v !== "string")
|
|
19
|
+
return v;
|
|
20
|
+
const [a, b, c, alpha] = v.match(floatRegex);
|
|
21
|
+
return {
|
|
22
|
+
[aName]: parseFloat(a),
|
|
23
|
+
[bName]: parseFloat(b),
|
|
24
|
+
[cName]: parseFloat(c),
|
|
25
|
+
alpha: alpha !== undefined ? parseFloat(alpha) : 1,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { isColorString, splitColor };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { complex } from './index.mjs';
|
|
2
|
+
import { floatRegex } from '../utils/float-regex.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Properties that should default to 1 or 100%
|
|
6
|
+
*/
|
|
7
|
+
const maxDefaults = new Set(["brightness", "contrast", "saturate", "opacity"]);
|
|
8
|
+
function applyDefaultFilter(v) {
|
|
9
|
+
const [name, value] = v.slice(0, -1).split("(");
|
|
10
|
+
if (name === "drop-shadow")
|
|
11
|
+
return v;
|
|
12
|
+
const [number] = value.match(floatRegex) || [];
|
|
13
|
+
if (!number)
|
|
14
|
+
return v;
|
|
15
|
+
const unit = value.replace(number, "");
|
|
16
|
+
let defaultValue = maxDefaults.has(name) ? 1 : 0;
|
|
17
|
+
if (number !== value)
|
|
18
|
+
defaultValue *= 100;
|
|
19
|
+
return name + "(" + defaultValue + unit + ")";
|
|
20
|
+
}
|
|
21
|
+
const functionRegex = /\b([a-z-]*)\(.*?\)/gu;
|
|
22
|
+
const filter = {
|
|
23
|
+
...complex,
|
|
24
|
+
getAnimatableNone: (v) => {
|
|
25
|
+
const functions = v.match(functionRegex);
|
|
26
|
+
return functions ? functions.map(applyDefaultFilter).join(" ") : v;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { filter };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { color } from '../color/index.mjs';
|
|
2
|
+
import { colorRegex } from '../utils/color-regex.mjs';
|
|
3
|
+
import { floatRegex } from '../utils/float-regex.mjs';
|
|
4
|
+
import { sanitize } from '../utils/sanitize.mjs';
|
|
5
|
+
|
|
6
|
+
function test(v) {
|
|
7
|
+
return (isNaN(v) &&
|
|
8
|
+
typeof v === "string" &&
|
|
9
|
+
(v.match(floatRegex)?.length || 0) +
|
|
10
|
+
(v.match(colorRegex)?.length || 0) >
|
|
11
|
+
0);
|
|
12
|
+
}
|
|
13
|
+
const NUMBER_TOKEN = "number";
|
|
14
|
+
const COLOR_TOKEN = "color";
|
|
15
|
+
const VAR_TOKEN = "var";
|
|
16
|
+
const VAR_FUNCTION_TOKEN = "var(";
|
|
17
|
+
const SPLIT_TOKEN = "${}";
|
|
18
|
+
// this regex consists of the `singleCssVariableRegex|rgbHSLValueRegex|digitRegex`
|
|
19
|
+
const complexRegex = /var\s*\(\s*--(?:[\w-]+\s*|[\w-]+\s*,(?:\s*[^)(\s]|\s*\((?:[^)(]|\([^)(]*\))*\))+\s*)\)|#[\da-f]{3,8}|(?:rgb|hsl)a?\((?:-?[\d.]+%?[,\s]+){2}-?[\d.]+%?\s*(?:[,/]\s*)?(?:\b\d+(?:\.\d+)?|\.\d+)?%?\)|-?(?:\d+(?:\.\d+)?|\.\d+)/giu;
|
|
20
|
+
function analyseComplexValue(value) {
|
|
21
|
+
const originalValue = value.toString();
|
|
22
|
+
const values = [];
|
|
23
|
+
const indexes = {
|
|
24
|
+
color: [],
|
|
25
|
+
number: [],
|
|
26
|
+
var: [],
|
|
27
|
+
};
|
|
28
|
+
const types = [];
|
|
29
|
+
let i = 0;
|
|
30
|
+
const tokenised = originalValue.replace(complexRegex, (parsedValue) => {
|
|
31
|
+
if (color.test(parsedValue)) {
|
|
32
|
+
indexes.color.push(i);
|
|
33
|
+
types.push(COLOR_TOKEN);
|
|
34
|
+
values.push(color.parse(parsedValue));
|
|
35
|
+
}
|
|
36
|
+
else if (parsedValue.startsWith(VAR_FUNCTION_TOKEN)) {
|
|
37
|
+
indexes.var.push(i);
|
|
38
|
+
types.push(VAR_TOKEN);
|
|
39
|
+
values.push(parsedValue);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
indexes.number.push(i);
|
|
43
|
+
types.push(NUMBER_TOKEN);
|
|
44
|
+
values.push(parseFloat(parsedValue));
|
|
45
|
+
}
|
|
46
|
+
++i;
|
|
47
|
+
return SPLIT_TOKEN;
|
|
48
|
+
});
|
|
49
|
+
const split = tokenised.split(SPLIT_TOKEN);
|
|
50
|
+
return { values, split, indexes, types };
|
|
51
|
+
}
|
|
52
|
+
function parseComplexValue(v) {
|
|
53
|
+
return analyseComplexValue(v).values;
|
|
54
|
+
}
|
|
55
|
+
function createTransformer(source) {
|
|
56
|
+
const { split, types } = analyseComplexValue(source);
|
|
57
|
+
const numSections = split.length;
|
|
58
|
+
return (v) => {
|
|
59
|
+
let output = "";
|
|
60
|
+
for (let i = 0; i < numSections; i++) {
|
|
61
|
+
output += split[i];
|
|
62
|
+
if (v[i] !== undefined) {
|
|
63
|
+
const type = types[i];
|
|
64
|
+
if (type === NUMBER_TOKEN) {
|
|
65
|
+
output += sanitize(v[i]);
|
|
66
|
+
}
|
|
67
|
+
else if (type === COLOR_TOKEN) {
|
|
68
|
+
output += color.transform(v[i]);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
output += v[i];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return output;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const convertNumbersToZero = (v) => typeof v === "number" ? 0 : v;
|
|
79
|
+
function getAnimatableNone(v) {
|
|
80
|
+
const parsed = parseComplexValue(v);
|
|
81
|
+
const transformer = createTransformer(v);
|
|
82
|
+
return transformer(parsed.map(convertNumbersToZero));
|
|
83
|
+
}
|
|
84
|
+
const complex = {
|
|
85
|
+
test,
|
|
86
|
+
parse: parseComplexValue,
|
|
87
|
+
createTransformer,
|
|
88
|
+
getAnimatableNone,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { analyseComplexValue, complex };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { auto } from './auto.mjs';
|
|
2
|
+
import { number } from './numbers/index.mjs';
|
|
3
|
+
import { px, percent, degrees, vw, vh } from './numbers/units.mjs';
|
|
4
|
+
import { testValueType } from './test.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A list of value types commonly used for dimensions
|
|
8
|
+
*/
|
|
9
|
+
const dimensionValueTypes = [number, px, percent, degrees, vw, vh, auto];
|
|
10
|
+
/**
|
|
11
|
+
* Tests a dimensional value against the list of dimension ValueTypes
|
|
12
|
+
*/
|
|
13
|
+
const findDimensionValueType = (v) => dimensionValueTypes.find(testValueType(v));
|
|
14
|
+
|
|
15
|
+
export { dimensionValueTypes, findDimensionValueType };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { color } from '../color/index.mjs';
|
|
2
|
+
import { filter } from '../complex/filter.mjs';
|
|
3
|
+
import { numberValueTypes } from './number.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A map of default value types for common values
|
|
7
|
+
*/
|
|
8
|
+
const defaultValueTypes = {
|
|
9
|
+
...numberValueTypes,
|
|
10
|
+
// Color props
|
|
11
|
+
color,
|
|
12
|
+
backgroundColor: color,
|
|
13
|
+
outlineColor: color,
|
|
14
|
+
fill: color,
|
|
15
|
+
stroke: color,
|
|
16
|
+
// Border props
|
|
17
|
+
borderColor: color,
|
|
18
|
+
borderTopColor: color,
|
|
19
|
+
borderRightColor: color,
|
|
20
|
+
borderBottomColor: color,
|
|
21
|
+
borderLeftColor: color,
|
|
22
|
+
filter,
|
|
23
|
+
WebkitFilter: filter,
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Gets the default ValueType for the provided value key
|
|
27
|
+
*/
|
|
28
|
+
const getDefaultValueType = (key) => defaultValueTypes[key];
|
|
29
|
+
|
|
30
|
+
export { defaultValueTypes, getDefaultValueType };
|