motion 11.11.12 → 11.11.14
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/.turbo/turbo-build.log +12 -9
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/react-client.js +2 -2
- package/dist/es/framer-motion/dist/es/animation/animators/BaseAnimation.mjs +2 -1
- package/dist/es/framer-motion/dist/es/animation/animators/MainThreadAnimation.mjs +5 -1
- package/dist/es/framer-motion/dist/es/animation/hooks/animation-controls.mjs +80 -0
- package/dist/es/framer-motion/dist/es/animation/hooks/use-animate-style.mjs +17 -0
- package/dist/es/framer-motion/dist/es/animation/hooks/use-animate.mjs +17 -0
- package/dist/es/framer-motion/dist/es/animation/hooks/use-animated-state.mjs +64 -0
- package/dist/es/framer-motion/dist/es/animation/hooks/use-animation.mjs +41 -0
- package/dist/es/framer-motion/dist/es/animation/interfaces/motion-value.mjs +8 -0
- package/dist/es/framer-motion/dist/es/animation/optimized-appear/handoff.mjs +40 -0
- package/dist/es/framer-motion/dist/es/animation/optimized-appear/start.mjs +173 -0
- package/dist/es/framer-motion/dist/es/animation/optimized-appear/store-id.mjs +8 -0
- package/dist/es/framer-motion/dist/es/animation/optimized-appear/store.mjs +4 -0
- package/dist/es/framer-motion/dist/es/components/AnimatePresence/PopChild.mjs +77 -0
- package/dist/es/framer-motion/dist/es/components/AnimatePresence/PresenceChild.mjs +61 -0
- package/dist/es/framer-motion/dist/es/components/AnimatePresence/index.mjs +163 -0
- package/dist/es/framer-motion/dist/es/components/AnimatePresence/use-presence.mjs +27 -1
- package/dist/es/framer-motion/dist/es/components/AnimatePresence/utils.mjs +14 -0
- package/dist/es/framer-motion/dist/es/components/AnimateSharedLayout.mjs +15 -0
- package/dist/es/framer-motion/dist/es/components/LayoutGroup/index.mjs +32 -0
- package/dist/es/framer-motion/dist/es/components/LazyMotion/index.mjs +68 -0
- package/dist/es/framer-motion/dist/es/components/MotionConfig/index.mjs +48 -0
- package/dist/es/framer-motion/dist/es/components/Reorder/Group.mjs +53 -0
- package/dist/es/framer-motion/dist/es/components/Reorder/Item.mjs +34 -0
- package/dist/es/framer-motion/dist/es/components/Reorder/namespace.mjs +2 -0
- package/dist/es/framer-motion/dist/es/components/Reorder/utils/check-reorder.mjs +24 -0
- package/dist/es/framer-motion/dist/es/context/DeprecatedLayoutGroupContext.mjs +10 -0
- package/dist/es/framer-motion/dist/es/context/ReorderContext.mjs +6 -0
- package/dist/es/framer-motion/dist/es/events/use-dom-event.mjs +34 -0
- package/dist/es/framer-motion/dist/es/frameloop/batcher.mjs +4 -1
- package/dist/es/framer-motion/dist/es/gestures/drag/use-drag-controls.mjs +88 -0
- package/dist/es/framer-motion/dist/es/motion/utils/is-motion-component.mjs +12 -0
- package/dist/es/framer-motion/dist/es/motion/utils/unwrap-motion-component.mjs +17 -0
- package/dist/es/framer-motion/dist/es/projection/node/group.mjs +24 -0
- package/dist/es/framer-motion/dist/es/projection/use-instant-layout-transition.mjs +14 -0
- package/dist/es/framer-motion/dist/es/projection/use-reset-projection.mjs +14 -0
- package/dist/es/framer-motion/dist/es/render/components/create-proxy.mjs +38 -0
- package/dist/es/framer-motion/dist/es/render/components/m/proxy.mjs +6 -0
- package/dist/es/framer-motion/dist/es/render/components/motion/proxy.mjs +6 -0
- package/dist/es/framer-motion/dist/es/render/dom/features-animation.mjs +14 -0
- package/dist/es/framer-motion/dist/es/render/dom/features-max.mjs +14 -0
- package/dist/es/framer-motion/dist/es/render/dom/features-min.mjs +12 -0
- package/dist/es/framer-motion/dist/es/render/utils/motion-values.mjs +1 -1
- package/dist/es/framer-motion/dist/es/utils/array.mjs +11 -1
- package/dist/es/framer-motion/dist/es/utils/reduced-motion/use-reduced-motion-config.mjs +19 -0
- package/dist/es/framer-motion/dist/es/utils/reduced-motion/use-reduced-motion.mjs +47 -0
- package/dist/es/framer-motion/dist/es/utils/use-animation-frame.mjs +21 -0
- package/dist/es/framer-motion/dist/es/utils/use-cycle.mjs +47 -0
- package/dist/es/framer-motion/dist/es/utils/use-force-update.mjs +19 -0
- package/dist/es/framer-motion/dist/es/utils/use-in-view.mjs +23 -0
- package/dist/es/framer-motion/dist/es/utils/use-instant-transition-state.mjs +5 -0
- package/dist/es/framer-motion/dist/es/utils/use-instant-transition.mjs +41 -0
- package/dist/es/framer-motion/dist/es/utils/use-is-mounted.mjs +15 -0
- package/dist/es/framer-motion/dist/es/utils/use-motion-value-event.mjs +13 -0
- package/dist/es/framer-motion/dist/es/utils/use-unmount-effect.mjs +7 -0
- package/dist/es/framer-motion/dist/es/value/index.mjs +8 -2
- package/dist/es/framer-motion/dist/es/value/scroll/use-element-scroll.mjs +14 -0
- package/dist/es/framer-motion/dist/es/value/scroll/use-viewport-scroll.mjs +14 -0
- package/dist/es/framer-motion/dist/es/value/types/color/rgba.mjs +1 -1
- package/dist/es/framer-motion/dist/es/value/use-combine-values.mjs +37 -0
- package/dist/es/framer-motion/dist/es/value/use-computed.mjs +19 -0
- package/dist/es/framer-motion/dist/es/value/use-inverted-scale.mjs +52 -0
- package/dist/es/framer-motion/dist/es/value/use-motion-template.mjs +45 -0
- package/dist/es/framer-motion/dist/es/value/use-motion-value.mjs +38 -0
- package/dist/es/framer-motion/dist/es/value/use-scroll.mjs +39 -0
- package/dist/es/framer-motion/dist/es/value/use-spring.mjs +85 -0
- package/dist/es/framer-motion/dist/es/value/use-time.mjs +10 -0
- package/dist/es/framer-motion/dist/es/value/use-transform.mjs +29 -0
- package/dist/es/framer-motion/dist/es/value/use-velocity.mjs +35 -0
- package/dist/es/framer-motion/dist/es/value/use-will-change/WillChangeMotionValue.mjs +22 -0
- package/dist/es/framer-motion/dist/es/value/use-will-change/get-will-change-name.mjs +14 -0
- package/dist/es/framer-motion/dist/es/value/use-will-change/index.mjs +8 -0
- package/dist/es/motion/lib/react.mjs +110 -0
- package/dist/motion.dev.js +2 -2
- package/dist/motion.js +1 -1
- package/dist/react.d.ts +1 -0
- package/package.json +17 -17
- package/react/package.json +1 -1
- package/react-client/package.json +1 -1
- package/react-m/package.json +1 -1
- package/rollup.config.mjs +12 -2
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { useId, useCallback, useMemo } from 'react';
|
|
5
|
+
import { PresenceContext } from '../../context/PresenceContext.mjs';
|
|
6
|
+
import { useConstant } from '../../utils/use-constant.mjs';
|
|
7
|
+
import { PopChild } from './PopChild.mjs';
|
|
8
|
+
|
|
9
|
+
const PresenceChild = ({ children, initial, isPresent, onExitComplete, custom, presenceAffectsLayout, mode, }) => {
|
|
10
|
+
const presenceChildren = useConstant(newChildrenMap);
|
|
11
|
+
const id = useId();
|
|
12
|
+
const memoizedOnExitComplete = useCallback((childId) => {
|
|
13
|
+
presenceChildren.set(childId, true);
|
|
14
|
+
for (const isComplete of presenceChildren.values()) {
|
|
15
|
+
if (!isComplete)
|
|
16
|
+
return; // can stop searching when any is incomplete
|
|
17
|
+
}
|
|
18
|
+
onExitComplete && onExitComplete();
|
|
19
|
+
}, [presenceChildren, onExitComplete]);
|
|
20
|
+
const context = useMemo(() => ({
|
|
21
|
+
id,
|
|
22
|
+
initial,
|
|
23
|
+
isPresent,
|
|
24
|
+
custom,
|
|
25
|
+
onExitComplete: memoizedOnExitComplete,
|
|
26
|
+
register: (childId) => {
|
|
27
|
+
presenceChildren.set(childId, false);
|
|
28
|
+
return () => presenceChildren.delete(childId);
|
|
29
|
+
},
|
|
30
|
+
}),
|
|
31
|
+
/**
|
|
32
|
+
* If the presence of a child affects the layout of the components around it,
|
|
33
|
+
* we want to make a new context value to ensure they get re-rendered
|
|
34
|
+
* so they can detect that layout change.
|
|
35
|
+
*/
|
|
36
|
+
presenceAffectsLayout
|
|
37
|
+
? [Math.random(), memoizedOnExitComplete]
|
|
38
|
+
: [isPresent, memoizedOnExitComplete]);
|
|
39
|
+
useMemo(() => {
|
|
40
|
+
presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
|
|
41
|
+
}, [isPresent]);
|
|
42
|
+
/**
|
|
43
|
+
* If there's no `motion` components to fire exit animations, we want to remove this
|
|
44
|
+
* component immediately.
|
|
45
|
+
*/
|
|
46
|
+
React.useEffect(() => {
|
|
47
|
+
!isPresent &&
|
|
48
|
+
!presenceChildren.size &&
|
|
49
|
+
onExitComplete &&
|
|
50
|
+
onExitComplete();
|
|
51
|
+
}, [isPresent]);
|
|
52
|
+
if (mode === "popLayout") {
|
|
53
|
+
children = jsx(PopChild, { isPresent: isPresent, children: children });
|
|
54
|
+
}
|
|
55
|
+
return (jsx(PresenceContext.Provider, { value: context, children: children }));
|
|
56
|
+
};
|
|
57
|
+
function newChildrenMap() {
|
|
58
|
+
return new Map();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { PresenceChild };
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
3
|
+
import { useMemo, useRef, useState, useContext } from 'react';
|
|
4
|
+
import { PresenceChild } from './PresenceChild.mjs';
|
|
5
|
+
import { LayoutGroupContext } from '../../context/LayoutGroupContext.mjs';
|
|
6
|
+
import { invariant } from '../../utils/errors.mjs';
|
|
7
|
+
import { useConstant } from '../../utils/use-constant.mjs';
|
|
8
|
+
import { onlyElements, getChildKey } from './utils.mjs';
|
|
9
|
+
import { useIsomorphicLayoutEffect } from '../../utils/use-isomorphic-effect.mjs';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `AnimatePresence` enables the animation of components that have been removed from the tree.
|
|
13
|
+
*
|
|
14
|
+
* When adding/removing more than a single child, every child **must** be given a unique `key` prop.
|
|
15
|
+
*
|
|
16
|
+
* Any `motion` components that have an `exit` property defined will animate out when removed from
|
|
17
|
+
* the tree.
|
|
18
|
+
*
|
|
19
|
+
* ```jsx
|
|
20
|
+
* import { motion, AnimatePresence } from 'framer-motion'
|
|
21
|
+
*
|
|
22
|
+
* export const Items = ({ items }) => (
|
|
23
|
+
* <AnimatePresence>
|
|
24
|
+
* {items.map(item => (
|
|
25
|
+
* <motion.div
|
|
26
|
+
* key={item.id}
|
|
27
|
+
* initial={{ opacity: 0 }}
|
|
28
|
+
* animate={{ opacity: 1 }}
|
|
29
|
+
* exit={{ opacity: 0 }}
|
|
30
|
+
* />
|
|
31
|
+
* ))}
|
|
32
|
+
* </AnimatePresence>
|
|
33
|
+
* )
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* You can sequence exit animations throughout a tree using variants.
|
|
37
|
+
*
|
|
38
|
+
* If a child contains multiple `motion` components with `exit` props, it will only unmount the child
|
|
39
|
+
* once all `motion` components have finished animating out. Likewise, any components using
|
|
40
|
+
* `usePresence` all need to call `safeToRemove`.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
const AnimatePresence = ({ children, exitBeforeEnter, custom, initial = true, onExitComplete, presenceAffectsLayout = true, mode = "sync", }) => {
|
|
45
|
+
invariant(!exitBeforeEnter, "Replace exitBeforeEnter with mode='wait'");
|
|
46
|
+
/**
|
|
47
|
+
* Filter any children that aren't ReactElements. We can only track components
|
|
48
|
+
* between renders with a props.key.
|
|
49
|
+
*/
|
|
50
|
+
const presentChildren = useMemo(() => onlyElements(children), [children]);
|
|
51
|
+
/**
|
|
52
|
+
* Track the keys of the currently rendered children. This is used to
|
|
53
|
+
* determine which children are exiting.
|
|
54
|
+
*/
|
|
55
|
+
const presentKeys = presentChildren.map(getChildKey);
|
|
56
|
+
/**
|
|
57
|
+
* If `initial={false}` we only want to pass this to components in the first render.
|
|
58
|
+
*/
|
|
59
|
+
const isInitialRender = useRef(true);
|
|
60
|
+
/**
|
|
61
|
+
* A ref containing the currently present children. When all exit animations
|
|
62
|
+
* are complete, we use this to re-render the component with the latest children
|
|
63
|
+
* *committed* rather than the latest children *rendered*.
|
|
64
|
+
*/
|
|
65
|
+
const pendingPresentChildren = useRef(presentChildren);
|
|
66
|
+
/**
|
|
67
|
+
* Track which exiting children have finished animating out.
|
|
68
|
+
*/
|
|
69
|
+
const exitComplete = useConstant(() => new Map());
|
|
70
|
+
/**
|
|
71
|
+
* Save children to render as React state. To ensure this component is concurrent-safe,
|
|
72
|
+
* we check for exiting children via an effect.
|
|
73
|
+
*/
|
|
74
|
+
const [diffedChildren, setDiffedChildren] = useState(presentChildren);
|
|
75
|
+
const [renderedChildren, setRenderedChildren] = useState(presentChildren);
|
|
76
|
+
useIsomorphicLayoutEffect(() => {
|
|
77
|
+
isInitialRender.current = false;
|
|
78
|
+
pendingPresentChildren.current = presentChildren;
|
|
79
|
+
/**
|
|
80
|
+
* Update complete status of exiting children.
|
|
81
|
+
*/
|
|
82
|
+
for (let i = 0; i < renderedChildren.length; i++) {
|
|
83
|
+
const key = getChildKey(renderedChildren[i]);
|
|
84
|
+
if (!presentKeys.includes(key)) {
|
|
85
|
+
if (exitComplete.get(key) !== true) {
|
|
86
|
+
exitComplete.set(key, false);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
exitComplete.delete(key);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}, [renderedChildren, presentKeys.length, presentKeys.join("-")]);
|
|
94
|
+
const exitingChildren = [];
|
|
95
|
+
if (presentChildren !== diffedChildren) {
|
|
96
|
+
let nextChildren = [...presentChildren];
|
|
97
|
+
/**
|
|
98
|
+
* Loop through all the currently rendered components and decide which
|
|
99
|
+
* are exiting.
|
|
100
|
+
*/
|
|
101
|
+
for (let i = 0; i < renderedChildren.length; i++) {
|
|
102
|
+
const child = renderedChildren[i];
|
|
103
|
+
const key = getChildKey(child);
|
|
104
|
+
if (!presentKeys.includes(key)) {
|
|
105
|
+
nextChildren.splice(i, 0, child);
|
|
106
|
+
exitingChildren.push(child);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* If we're in "wait" mode, and we have exiting children, we want to
|
|
111
|
+
* only render these until they've all exited.
|
|
112
|
+
*/
|
|
113
|
+
if (mode === "wait" && exitingChildren.length) {
|
|
114
|
+
nextChildren = exitingChildren;
|
|
115
|
+
}
|
|
116
|
+
setRenderedChildren(onlyElements(nextChildren));
|
|
117
|
+
setDiffedChildren(presentChildren);
|
|
118
|
+
/**
|
|
119
|
+
* Early return to ensure once we've set state with the latest diffed
|
|
120
|
+
* children, we can immediately re-render.
|
|
121
|
+
*/
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (process.env.NODE_ENV !== "production" &&
|
|
125
|
+
mode === "wait" &&
|
|
126
|
+
renderedChildren.length > 1) {
|
|
127
|
+
console.warn(`You're attempting to animate multiple children within AnimatePresence, but its mode is set to "wait". This will lead to odd visual behaviour.`);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* If we've been provided a forceRender function by the LayoutGroupContext,
|
|
131
|
+
* we can use it to force a re-render amongst all surrounding components once
|
|
132
|
+
* all components have finished animating out.
|
|
133
|
+
*/
|
|
134
|
+
const { forceRender } = useContext(LayoutGroupContext);
|
|
135
|
+
return (jsx(Fragment, { children: renderedChildren.map((child) => {
|
|
136
|
+
const key = getChildKey(child);
|
|
137
|
+
const isPresent = presentChildren === renderedChildren ||
|
|
138
|
+
presentKeys.includes(key);
|
|
139
|
+
const onExit = () => {
|
|
140
|
+
if (exitComplete.has(key)) {
|
|
141
|
+
exitComplete.set(key, true);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
let isEveryExitComplete = true;
|
|
147
|
+
exitComplete.forEach((isExitComplete) => {
|
|
148
|
+
if (!isExitComplete)
|
|
149
|
+
isEveryExitComplete = false;
|
|
150
|
+
});
|
|
151
|
+
if (isEveryExitComplete) {
|
|
152
|
+
forceRender === null || forceRender === void 0 ? void 0 : forceRender();
|
|
153
|
+
setRenderedChildren(pendingPresentChildren.current);
|
|
154
|
+
onExitComplete && onExitComplete();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
return (jsx(PresenceChild, { isPresent: isPresent, initial: !isInitialRender.current || initial
|
|
158
|
+
? undefined
|
|
159
|
+
: false, custom: isPresent ? undefined : custom, presenceAffectsLayout: presenceAffectsLayout, mode: mode, onExitComplete: isPresent ? undefined : onExit, children: child }, key));
|
|
160
|
+
}) }));
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export { AnimatePresence };
|
|
@@ -36,5 +36,31 @@ function usePresence() {
|
|
|
36
36
|
const safeToRemove = useCallback(() => onExitComplete && onExitComplete(id), [id, onExitComplete]);
|
|
37
37
|
return !isPresent && onExitComplete ? [false, safeToRemove] : [true];
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Similar to `usePresence`, except `useIsPresent` simply returns whether or not the component is present.
|
|
41
|
+
* There is no `safeToRemove` function.
|
|
42
|
+
*
|
|
43
|
+
* ```jsx
|
|
44
|
+
* import { useIsPresent } from "framer-motion"
|
|
45
|
+
*
|
|
46
|
+
* export const Component = () => {
|
|
47
|
+
* const isPresent = useIsPresent()
|
|
48
|
+
*
|
|
49
|
+
* useEffect(() => {
|
|
50
|
+
* !isPresent && console.log("I've been removed!")
|
|
51
|
+
* }, [isPresent])
|
|
52
|
+
*
|
|
53
|
+
* return <div />
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
function useIsPresent() {
|
|
60
|
+
return isPresent(useContext(PresenceContext));
|
|
61
|
+
}
|
|
62
|
+
function isPresent(context) {
|
|
63
|
+
return context === null ? true : context.isPresent;
|
|
64
|
+
}
|
|
39
65
|
|
|
40
|
-
export { usePresence };
|
|
66
|
+
export { isPresent, useIsPresent, usePresence };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Children, isValidElement } from 'react';
|
|
2
|
+
|
|
3
|
+
const getChildKey = (child) => child.key || "";
|
|
4
|
+
function onlyElements(children) {
|
|
5
|
+
const filtered = [];
|
|
6
|
+
// We use forEach here instead of map as map mutates the component key by preprending `.$`
|
|
7
|
+
Children.forEach(children, (child) => {
|
|
8
|
+
if (isValidElement(child))
|
|
9
|
+
filtered.push(child);
|
|
10
|
+
});
|
|
11
|
+
return filtered;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { getChildKey, onlyElements };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { invariant } from '../utils/errors.mjs';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { useConstant } from '../utils/use-constant.mjs';
|
|
5
|
+
import { LayoutGroup } from './LayoutGroup/index.mjs';
|
|
6
|
+
|
|
7
|
+
let id = 0;
|
|
8
|
+
const AnimateSharedLayout = ({ children }) => {
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
invariant(false, "AnimateSharedLayout is deprecated: https://www.framer.com/docs/guide-upgrade/##shared-layout-animations");
|
|
11
|
+
}, []);
|
|
12
|
+
return (jsx(LayoutGroup, { id: useConstant(() => `asl-${id++}`), children: children }));
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { AnimateSharedLayout };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { useContext, useRef, useMemo } from 'react';
|
|
4
|
+
import { LayoutGroupContext } from '../../context/LayoutGroupContext.mjs';
|
|
5
|
+
import { DeprecatedLayoutGroupContext } from '../../context/DeprecatedLayoutGroupContext.mjs';
|
|
6
|
+
import { useForceUpdate } from '../../utils/use-force-update.mjs';
|
|
7
|
+
import { nodeGroup } from '../../projection/node/group.mjs';
|
|
8
|
+
|
|
9
|
+
const shouldInheritGroup = (inherit) => inherit === true;
|
|
10
|
+
const shouldInheritId = (inherit) => shouldInheritGroup(inherit === true) || inherit === "id";
|
|
11
|
+
const LayoutGroup = ({ children, id, inherit = true }) => {
|
|
12
|
+
const layoutGroupContext = useContext(LayoutGroupContext);
|
|
13
|
+
const deprecatedLayoutGroupContext = useContext(DeprecatedLayoutGroupContext);
|
|
14
|
+
const [forceRender, key] = useForceUpdate();
|
|
15
|
+
const context = useRef(null);
|
|
16
|
+
const upstreamId = layoutGroupContext.id || deprecatedLayoutGroupContext;
|
|
17
|
+
if (context.current === null) {
|
|
18
|
+
if (shouldInheritId(inherit) && upstreamId) {
|
|
19
|
+
id = id ? upstreamId + "-" + id : upstreamId;
|
|
20
|
+
}
|
|
21
|
+
context.current = {
|
|
22
|
+
id,
|
|
23
|
+
group: shouldInheritGroup(inherit)
|
|
24
|
+
? layoutGroupContext.group || nodeGroup()
|
|
25
|
+
: nodeGroup(),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const memoizedContext = useMemo(() => ({ ...context.current, forceRender }), [key]);
|
|
29
|
+
return (jsx(LayoutGroupContext.Provider, { value: memoizedContext, children: children }));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { LayoutGroup };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { useState, useRef, useEffect } from 'react';
|
|
4
|
+
import { LazyContext } from '../../context/LazyContext.mjs';
|
|
5
|
+
import { loadFeatures } from '../../motion/features/load-features.mjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Used in conjunction with the `m` component to reduce bundle size.
|
|
9
|
+
*
|
|
10
|
+
* `m` is a version of the `motion` component that only loads functionality
|
|
11
|
+
* critical for the initial render.
|
|
12
|
+
*
|
|
13
|
+
* `LazyMotion` can then be used to either synchronously or asynchronously
|
|
14
|
+
* load animation and gesture support.
|
|
15
|
+
*
|
|
16
|
+
* ```jsx
|
|
17
|
+
* // Synchronous loading
|
|
18
|
+
* import { LazyMotion, m, domAnimation } from "framer-motion"
|
|
19
|
+
*
|
|
20
|
+
* function App() {
|
|
21
|
+
* return (
|
|
22
|
+
* <LazyMotion features={domAnimation}>
|
|
23
|
+
* <m.div animate={{ scale: 2 }} />
|
|
24
|
+
* </LazyMotion>
|
|
25
|
+
* )
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // Asynchronous loading
|
|
29
|
+
* import { LazyMotion, m } from "framer-motion"
|
|
30
|
+
*
|
|
31
|
+
* function App() {
|
|
32
|
+
* return (
|
|
33
|
+
* <LazyMotion features={() => import('./path/to/domAnimation')}>
|
|
34
|
+
* <m.div animate={{ scale: 2 }} />
|
|
35
|
+
* </LazyMotion>
|
|
36
|
+
* )
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
function LazyMotion({ children, features, strict = false }) {
|
|
43
|
+
const [, setIsLoaded] = useState(!isLazyBundle(features));
|
|
44
|
+
const loadedRenderer = useRef(undefined);
|
|
45
|
+
/**
|
|
46
|
+
* If this is a synchronous load, load features immediately
|
|
47
|
+
*/
|
|
48
|
+
if (!isLazyBundle(features)) {
|
|
49
|
+
const { renderer, ...loadedFeatures } = features;
|
|
50
|
+
loadedRenderer.current = renderer;
|
|
51
|
+
loadFeatures(loadedFeatures);
|
|
52
|
+
}
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (isLazyBundle(features)) {
|
|
55
|
+
features().then(({ renderer, ...loadedFeatures }) => {
|
|
56
|
+
loadFeatures(loadedFeatures);
|
|
57
|
+
loadedRenderer.current = renderer;
|
|
58
|
+
setIsLoaded(true);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}, []);
|
|
62
|
+
return (jsx(LazyContext.Provider, { value: { renderer: loadedRenderer.current, strict }, children: children }));
|
|
63
|
+
}
|
|
64
|
+
function isLazyBundle(features) {
|
|
65
|
+
return typeof features === "function";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { LazyMotion };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { useContext, useMemo } from 'react';
|
|
4
|
+
import { MotionConfigContext } from '../../context/MotionConfigContext.mjs';
|
|
5
|
+
import { loadExternalIsValidProp } from '../../render/dom/utils/filter-props.mjs';
|
|
6
|
+
import { useConstant } from '../../utils/use-constant.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `MotionConfig` is used to set configuration options for all children `motion` components.
|
|
10
|
+
*
|
|
11
|
+
* ```jsx
|
|
12
|
+
* import { motion, MotionConfig } from "framer-motion"
|
|
13
|
+
*
|
|
14
|
+
* export function App() {
|
|
15
|
+
* return (
|
|
16
|
+
* <MotionConfig transition={{ type: "spring" }}>
|
|
17
|
+
* <motion.div animate={{ x: 100 }} />
|
|
18
|
+
* </MotionConfig>
|
|
19
|
+
* )
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
function MotionConfig({ children, isValidProp, ...config }) {
|
|
26
|
+
isValidProp && loadExternalIsValidProp(isValidProp);
|
|
27
|
+
/**
|
|
28
|
+
* Inherit props from any parent MotionConfig components
|
|
29
|
+
*/
|
|
30
|
+
config = { ...useContext(MotionConfigContext), ...config };
|
|
31
|
+
/**
|
|
32
|
+
* Don't allow isStatic to change between renders as it affects how many hooks
|
|
33
|
+
* motion components fire.
|
|
34
|
+
*/
|
|
35
|
+
config.isStatic = useConstant(() => config.isStatic);
|
|
36
|
+
/**
|
|
37
|
+
* Creating a new config context object will re-render every `motion` component
|
|
38
|
+
* every time it renders. So we only want to create a new one sparingly.
|
|
39
|
+
*/
|
|
40
|
+
const context = useMemo(() => config, [
|
|
41
|
+
JSON.stringify(config.transition),
|
|
42
|
+
config.transformPagePoint,
|
|
43
|
+
config.reducedMotion,
|
|
44
|
+
]);
|
|
45
|
+
return (jsx(MotionConfigContext.Provider, { value: context, children: children }));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { MotionConfig };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { invariant } from '../../utils/errors.mjs';
|
|
4
|
+
import { forwardRef, useRef, useEffect } from 'react';
|
|
5
|
+
import { ReorderContext } from '../../context/ReorderContext.mjs';
|
|
6
|
+
import { motion } from '../../render/components/motion/proxy.mjs';
|
|
7
|
+
import { useConstant } from '../../utils/use-constant.mjs';
|
|
8
|
+
import { checkReorder } from './utils/check-reorder.mjs';
|
|
9
|
+
|
|
10
|
+
function ReorderGroupComponent({ children, as = "ul", axis = "y", onReorder, values, ...props }, externalRef) {
|
|
11
|
+
const Component = useConstant(() => motion[as]);
|
|
12
|
+
const order = [];
|
|
13
|
+
const isReordering = useRef(false);
|
|
14
|
+
invariant(Boolean(values), "Reorder.Group must be provided a values prop");
|
|
15
|
+
const context = {
|
|
16
|
+
axis,
|
|
17
|
+
registerItem: (value, layout) => {
|
|
18
|
+
// If the entry was already added, update it rather than adding it again
|
|
19
|
+
const idx = order.findIndex((entry) => value === entry.value);
|
|
20
|
+
if (idx !== -1) {
|
|
21
|
+
order[idx].layout = layout[axis];
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
order.push({ value: value, layout: layout[axis] });
|
|
25
|
+
}
|
|
26
|
+
order.sort(compareMin);
|
|
27
|
+
},
|
|
28
|
+
updateOrder: (item, offset, velocity) => {
|
|
29
|
+
if (isReordering.current)
|
|
30
|
+
return;
|
|
31
|
+
const newOrder = checkReorder(order, item, offset, velocity);
|
|
32
|
+
if (order !== newOrder) {
|
|
33
|
+
isReordering.current = true;
|
|
34
|
+
onReorder(newOrder
|
|
35
|
+
.map(getValue)
|
|
36
|
+
.filter((value) => values.indexOf(value) !== -1));
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
isReordering.current = false;
|
|
42
|
+
});
|
|
43
|
+
return (jsx(Component, { ...props, ref: externalRef, ignoreStrict: true, children: jsx(ReorderContext.Provider, { value: context, children: children }) }));
|
|
44
|
+
}
|
|
45
|
+
const ReorderGroup = /*@__PURE__*/ forwardRef(ReorderGroupComponent);
|
|
46
|
+
function getValue(item) {
|
|
47
|
+
return item.value;
|
|
48
|
+
}
|
|
49
|
+
function compareMin(a, b) {
|
|
50
|
+
return a.layout.min - b.layout.min;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { ReorderGroup, ReorderGroupComponent };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { invariant } from '../../utils/errors.mjs';
|
|
4
|
+
import { forwardRef, useContext } from 'react';
|
|
5
|
+
import { ReorderContext } from '../../context/ReorderContext.mjs';
|
|
6
|
+
import { motion } from '../../render/components/motion/proxy.mjs';
|
|
7
|
+
import { useConstant } from '../../utils/use-constant.mjs';
|
|
8
|
+
import { useMotionValue } from '../../value/use-motion-value.mjs';
|
|
9
|
+
import { useTransform } from '../../value/use-transform.mjs';
|
|
10
|
+
import { isMotionValue } from '../../value/utils/is-motion-value.mjs';
|
|
11
|
+
|
|
12
|
+
function useDefaultMotionValue(value, defaultValue = 0) {
|
|
13
|
+
return isMotionValue(value) ? value : useMotionValue(defaultValue);
|
|
14
|
+
}
|
|
15
|
+
function ReorderItemComponent({ children, style = {}, value, as = "li", onDrag, layout = true, ...props }, externalRef) {
|
|
16
|
+
const Component = useConstant(() => motion[as]);
|
|
17
|
+
const context = useContext(ReorderContext);
|
|
18
|
+
const point = {
|
|
19
|
+
x: useDefaultMotionValue(style.x),
|
|
20
|
+
y: useDefaultMotionValue(style.y),
|
|
21
|
+
};
|
|
22
|
+
const zIndex = useTransform([point.x, point.y], ([latestX, latestY]) => latestX || latestY ? 1 : "unset");
|
|
23
|
+
invariant(Boolean(context), "Reorder.Item must be a child of Reorder.Group");
|
|
24
|
+
const { axis, registerItem, updateOrder } = context;
|
|
25
|
+
return (jsx(Component, { drag: axis, ...props, dragSnapToOrigin: true, style: { ...style, x: point.x, y: point.y, zIndex }, layout: layout, onDrag: (event, gesturePoint) => {
|
|
26
|
+
const { velocity } = gesturePoint;
|
|
27
|
+
velocity[axis] &&
|
|
28
|
+
updateOrder(value, point[axis].get(), velocity[axis]);
|
|
29
|
+
onDrag && onDrag(event, gesturePoint);
|
|
30
|
+
}, onLayoutMeasure: (measured) => registerItem(value, measured), ref: externalRef, ignoreStrict: true, children: children }));
|
|
31
|
+
}
|
|
32
|
+
const ReorderItem = /*@__PURE__*/ forwardRef(ReorderItemComponent);
|
|
33
|
+
|
|
34
|
+
export { ReorderItem, ReorderItemComponent };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { moveItem } from '../../../utils/array.mjs';
|
|
2
|
+
import { mixNumber } from '../../../utils/mix/number.mjs';
|
|
3
|
+
|
|
4
|
+
function checkReorder(order, value, offset, velocity) {
|
|
5
|
+
if (!velocity)
|
|
6
|
+
return order;
|
|
7
|
+
const index = order.findIndex((item) => item.value === value);
|
|
8
|
+
if (index === -1)
|
|
9
|
+
return order;
|
|
10
|
+
const nextOffset = velocity > 0 ? 1 : -1;
|
|
11
|
+
const nextItem = order[index + nextOffset];
|
|
12
|
+
if (!nextItem)
|
|
13
|
+
return order;
|
|
14
|
+
const item = order[index];
|
|
15
|
+
const nextLayout = nextItem.layout;
|
|
16
|
+
const nextItemCenter = mixNumber(nextLayout.min, nextLayout.max, 0.5);
|
|
17
|
+
if ((nextOffset === 1 && item.layout.max + offset > nextItemCenter) ||
|
|
18
|
+
(nextOffset === -1 && item.layout.min + offset < nextItemCenter)) {
|
|
19
|
+
return moveItem(order, index, index + nextOffset);
|
|
20
|
+
}
|
|
21
|
+
return order;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { checkReorder };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { addDomEvent } from './add-dom-event.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Attaches an event listener directly to the provided DOM element.
|
|
6
|
+
*
|
|
7
|
+
* Bypassing React's event system can be desirable, for instance when attaching non-passive
|
|
8
|
+
* event handlers.
|
|
9
|
+
*
|
|
10
|
+
* ```jsx
|
|
11
|
+
* const ref = useRef(null)
|
|
12
|
+
*
|
|
13
|
+
* useDomEvent(ref, 'wheel', onWheel, { passive: false })
|
|
14
|
+
*
|
|
15
|
+
* return <div ref={ref} />
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @param ref - React.RefObject that's been provided to the element you want to bind the listener to.
|
|
19
|
+
* @param eventName - Name of the event you want listen for.
|
|
20
|
+
* @param handler - Function to fire when receiving the event.
|
|
21
|
+
* @param options - Options to pass to `Event.addEventListener`.
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
function useDomEvent(ref, eventName, handler, options) {
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const element = ref.current;
|
|
28
|
+
if (handler && element) {
|
|
29
|
+
return addDomEvent(element, eventName, handler, options);
|
|
30
|
+
}
|
|
31
|
+
}, [ref, eventName, handler, options]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { useDomEvent };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MotionGlobalConfig } from '../utils/GlobalConfig.mjs';
|
|
1
2
|
import { createRenderStep } from './render-step.mjs';
|
|
2
3
|
|
|
3
4
|
const stepsOrder = [
|
|
@@ -24,7 +25,9 @@ function createRenderBatcher(scheduleNextBatch, allowKeepAlive) {
|
|
|
24
25
|
}, {});
|
|
25
26
|
const { read, resolveKeyframes, update, preRender, render, postRender } = steps;
|
|
26
27
|
const processBatch = () => {
|
|
27
|
-
const timestamp =
|
|
28
|
+
const timestamp = MotionGlobalConfig.useManualTiming
|
|
29
|
+
? state.timestamp
|
|
30
|
+
: performance.now();
|
|
28
31
|
runNextFrame = false;
|
|
29
32
|
state.delta = useDefaultElapsed
|
|
30
33
|
? 1000 / 60
|