motion 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.
@@ -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 };
@@ -0,0 +1,5 @@
1
+ function camelToDash(str) {
2
+ return str.replace(/([A-Z])/g, (match) => `-${match.toLowerCase()}`);
3
+ }
4
+
5
+ export { camelToDash };
@@ -1,30 +1,29 @@
1
- import { resolveElements } from '../../../../../../motion-dom/dist/es/utils/resolve-elements.mjs';
2
- import { isSVGElement } from '../../../../../../motion-dom/dist/es/utils/is-svg-element.mjs';
1
+ import { isSVGElement } from '../utils/is-svg-element.mjs';
2
+ import { resolveElements } from '../utils/resolve-elements.mjs';
3
3
 
4
4
  const resizeHandlers = new WeakMap();
5
5
  let observer;
6
- function getElementSize(target, borderBoxSize) {
7
- if (borderBoxSize) {
8
- const { inlineSize, blockSize } = borderBoxSize[0];
9
- return { width: inlineSize, height: blockSize };
6
+ const getSize = (borderBoxAxis, svgAxis, htmlAxis) => (target, borderBoxSize) => {
7
+ if (borderBoxSize && borderBoxSize[0]) {
8
+ return borderBoxSize[0][(borderBoxAxis + "Size")];
10
9
  }
11
10
  else if (isSVGElement(target) && "getBBox" in target) {
12
- return target.getBBox();
11
+ return target.getBBox()[svgAxis];
13
12
  }
14
13
  else {
15
- return {
16
- width: target.offsetWidth,
17
- height: target.offsetHeight,
18
- };
14
+ return target[htmlAxis];
19
15
  }
20
- }
21
- function notifyTarget({ target, contentRect, borderBoxSize, }) {
16
+ };
17
+ const getWidth = /*@__PURE__*/ getSize("inline", "width", "offsetWidth");
18
+ const getHeight = /*@__PURE__*/ getSize("block", "height", "offsetHeight");
19
+ function notifyTarget({ target, borderBoxSize }) {
22
20
  resizeHandlers.get(target)?.forEach((handler) => {
23
- handler({
24
- target,
25
- contentSize: contentRect,
26
- get size() {
27
- return getElementSize(target, borderBoxSize);
21
+ handler(target, {
22
+ get width() {
23
+ return getWidth(target, borderBoxSize);
24
+ },
25
+ get height() {
26
+ return getHeight(target, borderBoxSize);
28
27
  },
29
28
  });
30
29
  });
@@ -2,14 +2,13 @@ const windowCallbacks = new Set();
2
2
  let windowResizeHandler;
3
3
  function createWindowResizeHandler() {
4
4
  windowResizeHandler = () => {
5
- const size = {
6
- width: window.innerWidth,
7
- height: window.innerHeight,
8
- };
9
5
  const info = {
10
- target: window,
11
- size,
12
- contentSize: size,
6
+ get width() {
7
+ return window.innerWidth;
8
+ },
9
+ get height() {
10
+ return window.innerHeight;
11
+ },
13
12
  };
14
13
  windowCallbacks.forEach((callback) => callback(info));
15
14
  };
@@ -21,7 +20,9 @@ function resizeWindow(callback) {
21
20
  createWindowResizeHandler();
22
21
  return () => {
23
22
  windowCallbacks.delete(callback);
24
- if (!windowCallbacks.size && windowResizeHandler) {
23
+ if (!windowCallbacks.size &&
24
+ typeof windowResizeHandler === "function") {
25
+ window.removeEventListener("resize", windowResizeHandler);
25
26
  windowResizeHandler = undefined;
26
27
  }
27
28
  };