motion-dom 12.14.0 → 12.16.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 +289 -108
- package/dist/es/effects/attr/index.mjs +41 -0
- package/dist/es/effects/prop/index.mjs +9 -0
- package/dist/es/effects/style/index.mjs +6 -0
- package/dist/es/effects/svg/index.mjs +41 -0
- package/dist/es/index.mjs +4 -0
- package/dist/es/render/dom/utils/camel-to-dash.mjs +5 -0
- package/dist/es/resize/handle-element.mjs +63 -0
- package/dist/es/resize/handle-window.mjs +31 -0
- package/dist/es/resize/index.mjs +8 -0
- package/dist/index.d.ts +20 -1
- package/dist/motion-dom.dev.js +289 -108
- package/dist/motion-dom.js +1 -1
- package/dist/size-rollup-style-effect.js +1 -1
- package/package.json +2 -2
package/dist/cjs/index.js
CHANGED
|
@@ -3050,6 +3050,152 @@ const acceleratedValues = new Set([
|
|
|
3050
3050
|
// "background-color"
|
|
3051
3051
|
]);
|
|
3052
3052
|
|
|
3053
|
+
function camelToDash(str) {
|
|
3054
|
+
return str.replace(/([A-Z])/g, (match) => `-${match.toLowerCase()}`);
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
function resolveElements(elementOrSelector, scope, selectorCache) {
|
|
3058
|
+
if (elementOrSelector instanceof EventTarget) {
|
|
3059
|
+
return [elementOrSelector];
|
|
3060
|
+
}
|
|
3061
|
+
else if (typeof elementOrSelector === "string") {
|
|
3062
|
+
let root = document;
|
|
3063
|
+
if (scope) {
|
|
3064
|
+
root = scope.current;
|
|
3065
|
+
}
|
|
3066
|
+
const elements = selectorCache?.[elementOrSelector] ??
|
|
3067
|
+
root.querySelectorAll(elementOrSelector);
|
|
3068
|
+
return elements ? Array.from(elements) : [];
|
|
3069
|
+
}
|
|
3070
|
+
return Array.from(elementOrSelector);
|
|
3071
|
+
}
|
|
3072
|
+
|
|
3073
|
+
function createSelectorEffect(subjectEffect) {
|
|
3074
|
+
return (subject, values) => {
|
|
3075
|
+
const elements = resolveElements(subject);
|
|
3076
|
+
const subscriptions = [];
|
|
3077
|
+
for (const element of elements) {
|
|
3078
|
+
const remove = subjectEffect(element, values);
|
|
3079
|
+
subscriptions.push(remove);
|
|
3080
|
+
}
|
|
3081
|
+
return () => {
|
|
3082
|
+
for (const remove of subscriptions)
|
|
3083
|
+
remove();
|
|
3084
|
+
};
|
|
3085
|
+
};
|
|
3086
|
+
}
|
|
3087
|
+
|
|
3088
|
+
/**
|
|
3089
|
+
* Provided a value and a ValueType, returns the value as that value type.
|
|
3090
|
+
*/
|
|
3091
|
+
const getValueAsType = (value, type) => {
|
|
3092
|
+
return type && typeof value === "number"
|
|
3093
|
+
? type.transform(value)
|
|
3094
|
+
: value;
|
|
3095
|
+
};
|
|
3096
|
+
|
|
3097
|
+
class MotionValueState {
|
|
3098
|
+
constructor() {
|
|
3099
|
+
this.latest = {};
|
|
3100
|
+
this.values = new Map();
|
|
3101
|
+
}
|
|
3102
|
+
set(name, value, render, computed, useDefaultValueType = true) {
|
|
3103
|
+
const existingValue = this.values.get(name);
|
|
3104
|
+
if (existingValue) {
|
|
3105
|
+
existingValue.onRemove();
|
|
3106
|
+
}
|
|
3107
|
+
const onChange = () => {
|
|
3108
|
+
const v = value.get();
|
|
3109
|
+
if (useDefaultValueType) {
|
|
3110
|
+
this.latest[name] = getValueAsType(v, numberValueTypes[name]);
|
|
3111
|
+
}
|
|
3112
|
+
else {
|
|
3113
|
+
this.latest[name] = v;
|
|
3114
|
+
}
|
|
3115
|
+
render && frame.render(render);
|
|
3116
|
+
};
|
|
3117
|
+
onChange();
|
|
3118
|
+
const cancelOnChange = value.on("change", onChange);
|
|
3119
|
+
computed && value.addDependent(computed);
|
|
3120
|
+
const remove = () => {
|
|
3121
|
+
cancelOnChange();
|
|
3122
|
+
render && cancelFrame(render);
|
|
3123
|
+
this.values.delete(name);
|
|
3124
|
+
computed && value.removeDependent(computed);
|
|
3125
|
+
};
|
|
3126
|
+
this.values.set(name, { value, onRemove: remove });
|
|
3127
|
+
return remove;
|
|
3128
|
+
}
|
|
3129
|
+
get(name) {
|
|
3130
|
+
return this.values.get(name)?.value;
|
|
3131
|
+
}
|
|
3132
|
+
destroy() {
|
|
3133
|
+
for (const value of this.values.values()) {
|
|
3134
|
+
value.onRemove();
|
|
3135
|
+
}
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
|
|
3139
|
+
function createEffect(addValue) {
|
|
3140
|
+
const stateCache = new WeakMap();
|
|
3141
|
+
const subscriptions = [];
|
|
3142
|
+
return (subject, values) => {
|
|
3143
|
+
const state = stateCache.get(subject) ?? new MotionValueState();
|
|
3144
|
+
stateCache.set(subject, state);
|
|
3145
|
+
for (const key in values) {
|
|
3146
|
+
const value = values[key];
|
|
3147
|
+
const remove = addValue(subject, state, key, value);
|
|
3148
|
+
subscriptions.push(remove);
|
|
3149
|
+
}
|
|
3150
|
+
return () => {
|
|
3151
|
+
for (const cancel of subscriptions)
|
|
3152
|
+
cancel();
|
|
3153
|
+
};
|
|
3154
|
+
};
|
|
3155
|
+
}
|
|
3156
|
+
|
|
3157
|
+
function canSetAsProperty(element, name) {
|
|
3158
|
+
if (!(name in element))
|
|
3159
|
+
return false;
|
|
3160
|
+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(element), name) ||
|
|
3161
|
+
Object.getOwnPropertyDescriptor(element, name);
|
|
3162
|
+
// Check if it has a setter
|
|
3163
|
+
return descriptor && typeof descriptor.set === "function";
|
|
3164
|
+
}
|
|
3165
|
+
const addAttrValue = (element, state, key, value) => {
|
|
3166
|
+
const isProp = canSetAsProperty(element, key);
|
|
3167
|
+
const name = isProp
|
|
3168
|
+
? key
|
|
3169
|
+
: key.startsWith("data") || key.startsWith("aria")
|
|
3170
|
+
? camelToDash(key)
|
|
3171
|
+
: key;
|
|
3172
|
+
/**
|
|
3173
|
+
* Set attribute directly via property if available
|
|
3174
|
+
*/
|
|
3175
|
+
const render = isProp
|
|
3176
|
+
? () => {
|
|
3177
|
+
element[name] = state.latest[key];
|
|
3178
|
+
}
|
|
3179
|
+
: () => {
|
|
3180
|
+
const v = state.latest[key];
|
|
3181
|
+
if (v === null || v === undefined) {
|
|
3182
|
+
element.removeAttribute(name);
|
|
3183
|
+
}
|
|
3184
|
+
else {
|
|
3185
|
+
element.setAttribute(name, String(v));
|
|
3186
|
+
}
|
|
3187
|
+
};
|
|
3188
|
+
return state.set(key, value, render);
|
|
3189
|
+
};
|
|
3190
|
+
const attrEffect = /*@__PURE__*/ createSelectorEffect(
|
|
3191
|
+
/*@__PURE__*/ createEffect(addAttrValue));
|
|
3192
|
+
|
|
3193
|
+
const propEffect = /*@__PURE__*/ createEffect((subject, state, key, value) => {
|
|
3194
|
+
return state.set(key, value, () => {
|
|
3195
|
+
subject[key] = state.latest[key];
|
|
3196
|
+
}, undefined, false);
|
|
3197
|
+
});
|
|
3198
|
+
|
|
3053
3199
|
/**
|
|
3054
3200
|
* Maximum time between the value of two frames, beyond which we
|
|
3055
3201
|
* assume the velocity has since been 0.
|
|
@@ -3372,106 +3518,6 @@ function motionValue(init, options) {
|
|
|
3372
3518
|
return new MotionValue(init, options);
|
|
3373
3519
|
}
|
|
3374
3520
|
|
|
3375
|
-
function resolveElements(elementOrSelector, scope, selectorCache) {
|
|
3376
|
-
if (elementOrSelector instanceof EventTarget) {
|
|
3377
|
-
return [elementOrSelector];
|
|
3378
|
-
}
|
|
3379
|
-
else if (typeof elementOrSelector === "string") {
|
|
3380
|
-
let root = document;
|
|
3381
|
-
if (scope) {
|
|
3382
|
-
root = scope.current;
|
|
3383
|
-
}
|
|
3384
|
-
const elements = selectorCache?.[elementOrSelector] ??
|
|
3385
|
-
root.querySelectorAll(elementOrSelector);
|
|
3386
|
-
return elements ? Array.from(elements) : [];
|
|
3387
|
-
}
|
|
3388
|
-
return Array.from(elementOrSelector);
|
|
3389
|
-
}
|
|
3390
|
-
|
|
3391
|
-
function createSelectorEffect(subjectEffect) {
|
|
3392
|
-
return (subject, values) => {
|
|
3393
|
-
const elements = resolveElements(subject);
|
|
3394
|
-
const subscriptions = [];
|
|
3395
|
-
for (const element of elements) {
|
|
3396
|
-
const remove = subjectEffect(element, values);
|
|
3397
|
-
subscriptions.push(remove);
|
|
3398
|
-
}
|
|
3399
|
-
return () => {
|
|
3400
|
-
for (const remove of subscriptions)
|
|
3401
|
-
remove();
|
|
3402
|
-
};
|
|
3403
|
-
};
|
|
3404
|
-
}
|
|
3405
|
-
|
|
3406
|
-
/**
|
|
3407
|
-
* Provided a value and a ValueType, returns the value as that value type.
|
|
3408
|
-
*/
|
|
3409
|
-
const getValueAsType = (value, type) => {
|
|
3410
|
-
return type && typeof value === "number"
|
|
3411
|
-
? type.transform(value)
|
|
3412
|
-
: value;
|
|
3413
|
-
};
|
|
3414
|
-
|
|
3415
|
-
class MotionValueState {
|
|
3416
|
-
constructor() {
|
|
3417
|
-
this.latest = {};
|
|
3418
|
-
this.values = new Map();
|
|
3419
|
-
}
|
|
3420
|
-
set(name, value, render, computed, useDefaultValueType = true) {
|
|
3421
|
-
const existingValue = this.values.get(name);
|
|
3422
|
-
if (existingValue) {
|
|
3423
|
-
existingValue.onRemove();
|
|
3424
|
-
}
|
|
3425
|
-
const onChange = () => {
|
|
3426
|
-
const v = value.get();
|
|
3427
|
-
if (useDefaultValueType) {
|
|
3428
|
-
this.latest[name] = getValueAsType(v, numberValueTypes[name]);
|
|
3429
|
-
}
|
|
3430
|
-
else {
|
|
3431
|
-
this.latest[name] = v;
|
|
3432
|
-
}
|
|
3433
|
-
render && frame.render(render);
|
|
3434
|
-
};
|
|
3435
|
-
onChange();
|
|
3436
|
-
const cancelOnChange = value.on("change", onChange);
|
|
3437
|
-
computed && value.addDependent(computed);
|
|
3438
|
-
const remove = () => {
|
|
3439
|
-
cancelOnChange();
|
|
3440
|
-
render && cancelFrame(render);
|
|
3441
|
-
this.values.delete(name);
|
|
3442
|
-
computed && value.removeDependent(computed);
|
|
3443
|
-
};
|
|
3444
|
-
this.values.set(name, { value, onRemove: remove });
|
|
3445
|
-
return remove;
|
|
3446
|
-
}
|
|
3447
|
-
get(name) {
|
|
3448
|
-
return this.values.get(name)?.value;
|
|
3449
|
-
}
|
|
3450
|
-
destroy() {
|
|
3451
|
-
for (const value of this.values.values()) {
|
|
3452
|
-
value.onRemove();
|
|
3453
|
-
}
|
|
3454
|
-
}
|
|
3455
|
-
}
|
|
3456
|
-
|
|
3457
|
-
function createEffect(addValue) {
|
|
3458
|
-
const stateCache = new WeakMap();
|
|
3459
|
-
const subscriptions = [];
|
|
3460
|
-
return (subject, values) => {
|
|
3461
|
-
const state = stateCache.get(subject) ?? new MotionValueState();
|
|
3462
|
-
stateCache.set(subject, state);
|
|
3463
|
-
for (const key in values) {
|
|
3464
|
-
const value = values[key];
|
|
3465
|
-
const remove = addValue(subject, state, key, value);
|
|
3466
|
-
subscriptions.push(remove);
|
|
3467
|
-
}
|
|
3468
|
-
return () => {
|
|
3469
|
-
for (const cancel of subscriptions)
|
|
3470
|
-
cancel();
|
|
3471
|
-
};
|
|
3472
|
-
};
|
|
3473
|
-
}
|
|
3474
|
-
|
|
3475
3521
|
const translateAlias = {
|
|
3476
3522
|
x: "translateX",
|
|
3477
3523
|
y: "translateY",
|
|
@@ -3513,6 +3559,11 @@ const addStyleValue = (element, state, key, value) => {
|
|
|
3513
3559
|
let computed = undefined;
|
|
3514
3560
|
if (transformProps.has(key)) {
|
|
3515
3561
|
if (!state.get("transform")) {
|
|
3562
|
+
// If this is an HTML element, we need to set the transform-box to fill-box
|
|
3563
|
+
// to normalise the transform relative to the element's bounding box
|
|
3564
|
+
if (!isHTMLElement(element) && !state.get("transformBox")) {
|
|
3565
|
+
addStyleValue(element, state, "transformBox", new MotionValue("fill-box"));
|
|
3566
|
+
}
|
|
3516
3567
|
state.set("transform", new MotionValue("none"), () => {
|
|
3517
3568
|
element.style.transform = buildTransform(state);
|
|
3518
3569
|
});
|
|
@@ -3545,6 +3596,38 @@ const addStyleValue = (element, state, key, value) => {
|
|
|
3545
3596
|
const styleEffect = /*@__PURE__*/ createSelectorEffect(
|
|
3546
3597
|
/*@__PURE__*/ createEffect(addStyleValue));
|
|
3547
3598
|
|
|
3599
|
+
const toPx = px.transform;
|
|
3600
|
+
function addSVGPathValue(element, state, key, value) {
|
|
3601
|
+
frame.render(() => element.setAttribute("pathLength", "1"));
|
|
3602
|
+
if (key === "pathOffset") {
|
|
3603
|
+
return state.set(key, value, () => element.setAttribute("stroke-dashoffset", toPx(-state.latest[key])));
|
|
3604
|
+
}
|
|
3605
|
+
else {
|
|
3606
|
+
if (!state.get("stroke-dasharray")) {
|
|
3607
|
+
state.set("stroke-dasharray", new MotionValue("1 1"), () => {
|
|
3608
|
+
const { pathLength = 1, pathSpacing } = state.latest;
|
|
3609
|
+
element.setAttribute("stroke-dasharray", `${toPx(pathLength)} ${toPx(pathSpacing ?? 1 - Number(pathLength))}`);
|
|
3610
|
+
});
|
|
3611
|
+
}
|
|
3612
|
+
return state.set(key, value, undefined, state.get("stroke-dasharray"));
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
const addSVGValue = (element, state, key, value) => {
|
|
3616
|
+
if (key.startsWith("path")) {
|
|
3617
|
+
return addSVGPathValue(element, state, key, value);
|
|
3618
|
+
}
|
|
3619
|
+
else if (key.startsWith("attr")) {
|
|
3620
|
+
return addAttrValue(element, state, convertAttrKey(key), value);
|
|
3621
|
+
}
|
|
3622
|
+
const handler = key in element.style ? addStyleValue : addAttrValue;
|
|
3623
|
+
return handler(element, state, key, value);
|
|
3624
|
+
};
|
|
3625
|
+
const svgEffect = /*@__PURE__*/ createSelectorEffect(
|
|
3626
|
+
/*@__PURE__*/ createEffect(addSVGValue));
|
|
3627
|
+
function convertAttrKey(key) {
|
|
3628
|
+
return key.replace(/^attr([A-Z])/, (_, firstChar) => firstChar.toLowerCase());
|
|
3629
|
+
}
|
|
3630
|
+
|
|
3548
3631
|
const { schedule: microtask, cancel: cancelMicrotask } =
|
|
3549
3632
|
/* @__PURE__ */ createRenderBatcher(queueMicrotask, false);
|
|
3550
3633
|
|
|
@@ -3791,6 +3874,107 @@ function getComputedStyle$1(element, name) {
|
|
|
3791
3874
|
: computedStyle[name];
|
|
3792
3875
|
}
|
|
3793
3876
|
|
|
3877
|
+
/**
|
|
3878
|
+
* Checks if an element is an SVG element in a way
|
|
3879
|
+
* that works across iframes
|
|
3880
|
+
*/
|
|
3881
|
+
function isSVGElement(element) {
|
|
3882
|
+
return motionUtils.isObject(element) && "ownerSVGElement" in element;
|
|
3883
|
+
}
|
|
3884
|
+
|
|
3885
|
+
const resizeHandlers = new WeakMap();
|
|
3886
|
+
let observer;
|
|
3887
|
+
const getSize = (borderBoxAxis, svgAxis, htmlAxis) => (target, borderBoxSize) => {
|
|
3888
|
+
if (borderBoxSize && borderBoxSize[0]) {
|
|
3889
|
+
return borderBoxSize[0][(borderBoxAxis + "Size")];
|
|
3890
|
+
}
|
|
3891
|
+
else if (isSVGElement(target) && "getBBox" in target) {
|
|
3892
|
+
return target.getBBox()[svgAxis];
|
|
3893
|
+
}
|
|
3894
|
+
else {
|
|
3895
|
+
return target[htmlAxis];
|
|
3896
|
+
}
|
|
3897
|
+
};
|
|
3898
|
+
const getWidth = /*@__PURE__*/ getSize("inline", "width", "offsetWidth");
|
|
3899
|
+
const getHeight = /*@__PURE__*/ getSize("block", "height", "offsetHeight");
|
|
3900
|
+
function notifyTarget({ target, borderBoxSize }) {
|
|
3901
|
+
resizeHandlers.get(target)?.forEach((handler) => {
|
|
3902
|
+
handler(target, {
|
|
3903
|
+
get width() {
|
|
3904
|
+
return getWidth(target, borderBoxSize);
|
|
3905
|
+
},
|
|
3906
|
+
get height() {
|
|
3907
|
+
return getHeight(target, borderBoxSize);
|
|
3908
|
+
},
|
|
3909
|
+
});
|
|
3910
|
+
});
|
|
3911
|
+
}
|
|
3912
|
+
function notifyAll(entries) {
|
|
3913
|
+
entries.forEach(notifyTarget);
|
|
3914
|
+
}
|
|
3915
|
+
function createResizeObserver() {
|
|
3916
|
+
if (typeof ResizeObserver === "undefined")
|
|
3917
|
+
return;
|
|
3918
|
+
observer = new ResizeObserver(notifyAll);
|
|
3919
|
+
}
|
|
3920
|
+
function resizeElement(target, handler) {
|
|
3921
|
+
if (!observer)
|
|
3922
|
+
createResizeObserver();
|
|
3923
|
+
const elements = resolveElements(target);
|
|
3924
|
+
elements.forEach((element) => {
|
|
3925
|
+
let elementHandlers = resizeHandlers.get(element);
|
|
3926
|
+
if (!elementHandlers) {
|
|
3927
|
+
elementHandlers = new Set();
|
|
3928
|
+
resizeHandlers.set(element, elementHandlers);
|
|
3929
|
+
}
|
|
3930
|
+
elementHandlers.add(handler);
|
|
3931
|
+
observer?.observe(element);
|
|
3932
|
+
});
|
|
3933
|
+
return () => {
|
|
3934
|
+
elements.forEach((element) => {
|
|
3935
|
+
const elementHandlers = resizeHandlers.get(element);
|
|
3936
|
+
elementHandlers?.delete(handler);
|
|
3937
|
+
if (!elementHandlers?.size) {
|
|
3938
|
+
observer?.unobserve(element);
|
|
3939
|
+
}
|
|
3940
|
+
});
|
|
3941
|
+
};
|
|
3942
|
+
}
|
|
3943
|
+
|
|
3944
|
+
const windowCallbacks = new Set();
|
|
3945
|
+
let windowResizeHandler;
|
|
3946
|
+
function createWindowResizeHandler() {
|
|
3947
|
+
windowResizeHandler = () => {
|
|
3948
|
+
const info = {
|
|
3949
|
+
get width() {
|
|
3950
|
+
return window.innerWidth;
|
|
3951
|
+
},
|
|
3952
|
+
get height() {
|
|
3953
|
+
return window.innerHeight;
|
|
3954
|
+
},
|
|
3955
|
+
};
|
|
3956
|
+
windowCallbacks.forEach((callback) => callback(info));
|
|
3957
|
+
};
|
|
3958
|
+
window.addEventListener("resize", windowResizeHandler);
|
|
3959
|
+
}
|
|
3960
|
+
function resizeWindow(callback) {
|
|
3961
|
+
windowCallbacks.add(callback);
|
|
3962
|
+
if (!windowResizeHandler)
|
|
3963
|
+
createWindowResizeHandler();
|
|
3964
|
+
return () => {
|
|
3965
|
+
windowCallbacks.delete(callback);
|
|
3966
|
+
if (!windowCallbacks.size &&
|
|
3967
|
+
typeof windowResizeHandler === "function") {
|
|
3968
|
+
window.removeEventListener("resize", windowResizeHandler);
|
|
3969
|
+
windowResizeHandler = undefined;
|
|
3970
|
+
}
|
|
3971
|
+
};
|
|
3972
|
+
}
|
|
3973
|
+
|
|
3974
|
+
function resize(a, b) {
|
|
3975
|
+
return typeof a === "function" ? resizeWindow(a) : resizeElement(a, b);
|
|
3976
|
+
}
|
|
3977
|
+
|
|
3794
3978
|
function observeTimeline(update, timeline) {
|
|
3795
3979
|
let prevProgress;
|
|
3796
3980
|
const onFrame = () => {
|
|
@@ -3918,14 +4102,6 @@ function recordStats() {
|
|
|
3918
4102
|
return reportStats;
|
|
3919
4103
|
}
|
|
3920
4104
|
|
|
3921
|
-
/**
|
|
3922
|
-
* Checks if an element is an SVG element in a way
|
|
3923
|
-
* that works across iframes
|
|
3924
|
-
*/
|
|
3925
|
-
function isSVGElement(element) {
|
|
3926
|
-
return motionUtils.isObject(element) && "ownerSVGElement" in element;
|
|
3927
|
-
}
|
|
3928
|
-
|
|
3929
4105
|
/**
|
|
3930
4106
|
* Checks if an element is specifically an SVGSVGElement (the root SVG element)
|
|
3931
4107
|
* in a way that works across iframes
|
|
@@ -4445,6 +4621,7 @@ exports.NativeAnimationWrapper = NativeAnimationWrapper;
|
|
|
4445
4621
|
exports.ViewTransitionBuilder = ViewTransitionBuilder;
|
|
4446
4622
|
exports.acceleratedValues = acceleratedValues;
|
|
4447
4623
|
exports.activeAnimations = activeAnimations;
|
|
4624
|
+
exports.addAttrValue = addAttrValue;
|
|
4448
4625
|
exports.addStyleValue = addStyleValue;
|
|
4449
4626
|
exports.alpha = alpha;
|
|
4450
4627
|
exports.analyseComplexValue = analyseComplexValue;
|
|
@@ -4453,6 +4630,7 @@ exports.animateView = animateView;
|
|
|
4453
4630
|
exports.animationMapKey = animationMapKey;
|
|
4454
4631
|
exports.applyPxDefaults = applyPxDefaults;
|
|
4455
4632
|
exports.attachSpring = attachSpring;
|
|
4633
|
+
exports.attrEffect = attrEffect;
|
|
4456
4634
|
exports.calcGeneratorDuration = calcGeneratorDuration;
|
|
4457
4635
|
exports.cancelFrame = cancelFrame;
|
|
4458
4636
|
exports.cancelMicrotask = cancelMicrotask;
|
|
@@ -4530,9 +4708,11 @@ exports.percent = percent;
|
|
|
4530
4708
|
exports.positionalKeys = positionalKeys;
|
|
4531
4709
|
exports.press = press;
|
|
4532
4710
|
exports.progressPercentage = progressPercentage;
|
|
4711
|
+
exports.propEffect = propEffect;
|
|
4533
4712
|
exports.px = px;
|
|
4534
4713
|
exports.readTransformValue = readTransformValue;
|
|
4535
4714
|
exports.recordStats = recordStats;
|
|
4715
|
+
exports.resize = resize;
|
|
4536
4716
|
exports.resolveElements = resolveElements;
|
|
4537
4717
|
exports.rgbUnit = rgbUnit;
|
|
4538
4718
|
exports.rgba = rgba;
|
|
@@ -4550,6 +4730,7 @@ exports.supportsFlags = supportsFlags;
|
|
|
4550
4730
|
exports.supportsLinearEasing = supportsLinearEasing;
|
|
4551
4731
|
exports.supportsPartialKeyframes = supportsPartialKeyframes;
|
|
4552
4732
|
exports.supportsScrollTimeline = supportsScrollTimeline;
|
|
4733
|
+
exports.svgEffect = svgEffect;
|
|
4553
4734
|
exports.sync = sync;
|
|
4554
4735
|
exports.testValueType = testValueType;
|
|
4555
4736
|
exports.time = time;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { camelToDash } from '../../render/dom/utils/camel-to-dash.mjs';
|
|
2
|
+
import { createSelectorEffect } from '../utils/create-dom-effect.mjs';
|
|
3
|
+
import { createEffect } from '../utils/create-effect.mjs';
|
|
4
|
+
|
|
5
|
+
function canSetAsProperty(element, name) {
|
|
6
|
+
if (!(name in element))
|
|
7
|
+
return false;
|
|
8
|
+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(element), name) ||
|
|
9
|
+
Object.getOwnPropertyDescriptor(element, name);
|
|
10
|
+
// Check if it has a setter
|
|
11
|
+
return descriptor && typeof descriptor.set === "function";
|
|
12
|
+
}
|
|
13
|
+
const addAttrValue = (element, state, key, value) => {
|
|
14
|
+
const isProp = canSetAsProperty(element, key);
|
|
15
|
+
const name = isProp
|
|
16
|
+
? key
|
|
17
|
+
: key.startsWith("data") || key.startsWith("aria")
|
|
18
|
+
? camelToDash(key)
|
|
19
|
+
: key;
|
|
20
|
+
/**
|
|
21
|
+
* Set attribute directly via property if available
|
|
22
|
+
*/
|
|
23
|
+
const render = isProp
|
|
24
|
+
? () => {
|
|
25
|
+
element[name] = state.latest[key];
|
|
26
|
+
}
|
|
27
|
+
: () => {
|
|
28
|
+
const v = state.latest[key];
|
|
29
|
+
if (v === null || v === undefined) {
|
|
30
|
+
element.removeAttribute(name);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
element.setAttribute(name, String(v));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return state.set(key, value, render);
|
|
37
|
+
};
|
|
38
|
+
const attrEffect = /*@__PURE__*/ createSelectorEffect(
|
|
39
|
+
/*@__PURE__*/ createEffect(addAttrValue));
|
|
40
|
+
|
|
41
|
+
export { addAttrValue, attrEffect };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createEffect } from '../utils/create-effect.mjs';
|
|
2
|
+
|
|
3
|
+
const propEffect = /*@__PURE__*/ createEffect((subject, state, key, value) => {
|
|
4
|
+
return state.set(key, value, () => {
|
|
5
|
+
subject[key] = state.latest[key];
|
|
6
|
+
}, undefined, false);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export { propEffect };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isCSSVar } from '../../render/dom/is-css-var.mjs';
|
|
2
2
|
import { transformProps } from '../../render/utils/keys-transform.mjs';
|
|
3
|
+
import { isHTMLElement } from '../../utils/is-html-element.mjs';
|
|
3
4
|
import { MotionValue } from '../../value/index.mjs';
|
|
4
5
|
import { createSelectorEffect } from '../utils/create-dom-effect.mjs';
|
|
5
6
|
import { createEffect } from '../utils/create-effect.mjs';
|
|
@@ -11,6 +12,11 @@ const addStyleValue = (element, state, key, value) => {
|
|
|
11
12
|
let computed = undefined;
|
|
12
13
|
if (transformProps.has(key)) {
|
|
13
14
|
if (!state.get("transform")) {
|
|
15
|
+
// If this is an HTML element, we need to set the transform-box to fill-box
|
|
16
|
+
// to normalise the transform relative to the element's bounding box
|
|
17
|
+
if (!isHTMLElement(element) && !state.get("transformBox")) {
|
|
18
|
+
addStyleValue(element, state, "transformBox", new MotionValue("fill-box"));
|
|
19
|
+
}
|
|
14
20
|
state.set("transform", new MotionValue("none"), () => {
|
|
15
21
|
element.style.transform = buildTransform(state);
|
|
16
22
|
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { MotionValue } from '../../value/index.mjs';
|
|
2
|
+
import { px } from '../../value/types/numbers/units.mjs';
|
|
3
|
+
import { addAttrValue } from '../attr/index.mjs';
|
|
4
|
+
import { addStyleValue } from '../style/index.mjs';
|
|
5
|
+
import { createSelectorEffect } from '../utils/create-dom-effect.mjs';
|
|
6
|
+
import { createEffect } from '../utils/create-effect.mjs';
|
|
7
|
+
import { frame } from '../../frameloop/frame.mjs';
|
|
8
|
+
|
|
9
|
+
const toPx = px.transform;
|
|
10
|
+
function addSVGPathValue(element, state, key, value) {
|
|
11
|
+
frame.render(() => element.setAttribute("pathLength", "1"));
|
|
12
|
+
if (key === "pathOffset") {
|
|
13
|
+
return state.set(key, value, () => element.setAttribute("stroke-dashoffset", toPx(-state.latest[key])));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
if (!state.get("stroke-dasharray")) {
|
|
17
|
+
state.set("stroke-dasharray", new MotionValue("1 1"), () => {
|
|
18
|
+
const { pathLength = 1, pathSpacing } = state.latest;
|
|
19
|
+
element.setAttribute("stroke-dasharray", `${toPx(pathLength)} ${toPx(pathSpacing ?? 1 - Number(pathLength))}`);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return state.set(key, value, undefined, state.get("stroke-dasharray"));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const addSVGValue = (element, state, key, value) => {
|
|
26
|
+
if (key.startsWith("path")) {
|
|
27
|
+
return addSVGPathValue(element, state, key, value);
|
|
28
|
+
}
|
|
29
|
+
else if (key.startsWith("attr")) {
|
|
30
|
+
return addAttrValue(element, state, convertAttrKey(key), value);
|
|
31
|
+
}
|
|
32
|
+
const handler = key in element.style ? addStyleValue : addAttrValue;
|
|
33
|
+
return handler(element, state, key, value);
|
|
34
|
+
};
|
|
35
|
+
const svgEffect = /*@__PURE__*/ createSelectorEffect(
|
|
36
|
+
/*@__PURE__*/ createEffect(addSVGValue));
|
|
37
|
+
function convertAttrKey(key) {
|
|
38
|
+
return key.replace(/^attr([A-Z])/, (_, firstChar) => firstChar.toLowerCase());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { svgEffect };
|
package/dist/es/index.mjs
CHANGED
|
@@ -31,7 +31,10 @@ export { supportsPartialKeyframes } from './animation/waapi/supports/partial-key
|
|
|
31
31
|
export { supportsBrowserAnimation } from './animation/waapi/supports/waapi.mjs';
|
|
32
32
|
export { acceleratedValues } from './animation/waapi/utils/accelerated-values.mjs';
|
|
33
33
|
export { generateLinearEasing } from './animation/waapi/utils/linear.mjs';
|
|
34
|
+
export { addAttrValue, attrEffect } from './effects/attr/index.mjs';
|
|
35
|
+
export { propEffect } from './effects/prop/index.mjs';
|
|
34
36
|
export { addStyleValue, styleEffect } from './effects/style/index.mjs';
|
|
37
|
+
export { svgEffect } from './effects/svg/index.mjs';
|
|
35
38
|
export { createRenderBatcher } from './frameloop/batcher.mjs';
|
|
36
39
|
export { cancelMicrotask, microtask } from './frameloop/microtask.mjs';
|
|
37
40
|
export { time } from './frameloop/sync-time.mjs';
|
|
@@ -46,6 +49,7 @@ export { getComputedStyle } from './render/dom/style-computed.mjs';
|
|
|
46
49
|
export { setStyle } from './render/dom/style-set.mjs';
|
|
47
50
|
export { positionalKeys } from './render/utils/keys-position.mjs';
|
|
48
51
|
export { transformPropOrder, transformProps } from './render/utils/keys-transform.mjs';
|
|
52
|
+
export { resize } from './resize/index.mjs';
|
|
49
53
|
export { observeTimeline } from './scroll/observe.mjs';
|
|
50
54
|
export { recordStats } from './stats/index.mjs';
|
|
51
55
|
export { activeAnimations } from './stats/animation-count.mjs';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isSVGElement } from '../utils/is-svg-element.mjs';
|
|
2
|
+
import { resolveElements } from '../utils/resolve-elements.mjs';
|
|
3
|
+
|
|
4
|
+
const resizeHandlers = new WeakMap();
|
|
5
|
+
let observer;
|
|
6
|
+
const getSize = (borderBoxAxis, svgAxis, htmlAxis) => (target, borderBoxSize) => {
|
|
7
|
+
if (borderBoxSize && borderBoxSize[0]) {
|
|
8
|
+
return borderBoxSize[0][(borderBoxAxis + "Size")];
|
|
9
|
+
}
|
|
10
|
+
else if (isSVGElement(target) && "getBBox" in target) {
|
|
11
|
+
return target.getBBox()[svgAxis];
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return target[htmlAxis];
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const getWidth = /*@__PURE__*/ getSize("inline", "width", "offsetWidth");
|
|
18
|
+
const getHeight = /*@__PURE__*/ getSize("block", "height", "offsetHeight");
|
|
19
|
+
function notifyTarget({ target, borderBoxSize }) {
|
|
20
|
+
resizeHandlers.get(target)?.forEach((handler) => {
|
|
21
|
+
handler(target, {
|
|
22
|
+
get width() {
|
|
23
|
+
return getWidth(target, borderBoxSize);
|
|
24
|
+
},
|
|
25
|
+
get height() {
|
|
26
|
+
return getHeight(target, borderBoxSize);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function notifyAll(entries) {
|
|
32
|
+
entries.forEach(notifyTarget);
|
|
33
|
+
}
|
|
34
|
+
function createResizeObserver() {
|
|
35
|
+
if (typeof ResizeObserver === "undefined")
|
|
36
|
+
return;
|
|
37
|
+
observer = new ResizeObserver(notifyAll);
|
|
38
|
+
}
|
|
39
|
+
function resizeElement(target, handler) {
|
|
40
|
+
if (!observer)
|
|
41
|
+
createResizeObserver();
|
|
42
|
+
const elements = resolveElements(target);
|
|
43
|
+
elements.forEach((element) => {
|
|
44
|
+
let elementHandlers = resizeHandlers.get(element);
|
|
45
|
+
if (!elementHandlers) {
|
|
46
|
+
elementHandlers = new Set();
|
|
47
|
+
resizeHandlers.set(element, elementHandlers);
|
|
48
|
+
}
|
|
49
|
+
elementHandlers.add(handler);
|
|
50
|
+
observer?.observe(element);
|
|
51
|
+
});
|
|
52
|
+
return () => {
|
|
53
|
+
elements.forEach((element) => {
|
|
54
|
+
const elementHandlers = resizeHandlers.get(element);
|
|
55
|
+
elementHandlers?.delete(handler);
|
|
56
|
+
if (!elementHandlers?.size) {
|
|
57
|
+
observer?.unobserve(element);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { resizeElement };
|