motion-start 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +39 -0
- package/package.json +64 -0
- package/src/animation/UseAnimatedState.svelte +86 -0
- package/src/animation/UseAnimation.svelte +61 -0
- package/src/animation/animate.ts +78 -0
- package/src/animation/animation-controls.ts +101 -0
- package/src/animation/types.ts +83 -0
- package/src/animation/use-animated-state.ts +1 -0
- package/src/animation/use-animation.ts +74 -0
- package/src/animation/utils/default-transitions.ts +70 -0
- package/src/animation/utils/easing.ts +55 -0
- package/src/animation/utils/is-animatable.ts +42 -0
- package/src/animation/utils/is-animation-controls.ts +17 -0
- package/src/animation/utils/is-keyframes-target.ts +17 -0
- package/src/animation/utils/transitions.ts +218 -0
- package/src/animation/utils/variant-resolvers.ts +15 -0
- package/src/components/AnimatePresence/AnimatePresence.svelte +180 -0
- package/src/components/AnimatePresence/PresenceChild/PresenceChild.svelte +78 -0
- package/src/components/AnimatePresence/PresenceChild/index.ts +7 -0
- package/src/components/AnimatePresence/PresenceChild/types.ts +10 -0
- package/src/components/AnimatePresence/index.ts +46 -0
- package/src/components/AnimatePresence/types.ts +79 -0
- package/src/components/AnimatePresence/use-presence.ts +90 -0
- package/src/components/AnimateSharedLayout/AnimateSharedLayout.svelte +239 -0
- package/src/components/AnimateSharedLayout/index.ts +11 -0
- package/src/components/AnimateSharedLayout/types.ts +111 -0
- package/src/components/AnimateSharedLayout/utils/batcher.ts +96 -0
- package/src/components/AnimateSharedLayout/utils/crossfader.ts +260 -0
- package/src/components/AnimateSharedLayout/utils/rotate.ts +48 -0
- package/src/components/AnimateSharedLayout/utils/stack.ts +160 -0
- package/src/components/LazyMotion/LazyMotion.svelte +82 -0
- package/src/components/LazyMotion/index.ts +42 -0
- package/src/components/LazyMotion/types.ts +58 -0
- package/src/components/MotionConfig/MotionConfig.svelte +56 -0
- package/src/components/MotionConfig/MotionConfigScaleCorrection.ts +47 -0
- package/src/components/MotionConfig/index.ts +20 -0
- package/src/components/MotionDiv.svelte +8 -0
- package/src/context/DOMcontext.ts +21 -0
- package/src/context/LayoutGroupContext.ts +13 -0
- package/src/context/LazyContext.ts +18 -0
- package/src/context/MotionConfigContext.ts +48 -0
- package/src/context/MotionContext/MotionContext.svelte +27 -0
- package/src/context/MotionContext/MotionContextProvider.svelte +22 -0
- package/src/context/MotionContext/UseCreateMotionContext.svelte +34 -0
- package/src/context/MotionContext/create.ts +1 -0
- package/src/context/MotionContext/index.ts +14 -0
- package/src/context/MotionContext/utils.ts +29 -0
- package/src/context/PresenceContext.ts +26 -0
- package/src/context/ScaleCorrectionProvider.svelte +27 -0
- package/src/context/SharedLayoutContext.ts +29 -0
- package/src/events/UseDomEvent.svelte +67 -0
- package/src/events/UsePointerEvent.svelte +76 -0
- package/src/events/event-info.ts +69 -0
- package/src/events/types.ts +15 -0
- package/src/events/use-dom-event.ts +48 -0
- package/src/events/use-pointer-event.ts +29 -0
- package/src/events/utils.ts +25 -0
- package/src/gestures/PanSession.ts +298 -0
- package/src/gestures/UseFocusGesture.svelte +31 -0
- package/src/gestures/UseGestures.svelte +17 -0
- package/src/gestures/UseHoverGesture.svelte +40 -0
- package/src/gestures/UsePanGesture.svelte +58 -0
- package/src/gestures/UseTapGesture.svelte +77 -0
- package/src/gestures/drag/UseDrag.svelte +55 -0
- package/src/gestures/drag/UseDragControls.svelte +145 -0
- package/src/gestures/drag/VisualElementDragControls.ts +632 -0
- package/src/gestures/drag/types.ts +307 -0
- package/src/gestures/drag/use-drag-controls.ts +148 -0
- package/src/gestures/drag/use-drag.ts +15 -0
- package/src/gestures/drag/utils/constraints.ts +157 -0
- package/src/gestures/drag/utils/lock.ts +69 -0
- package/src/gestures/types.ts +257 -0
- package/src/gestures/use-focus-gesture.ts +16 -0
- package/src/gestures/use-gestures.ts +2 -0
- package/src/gestures/use-hover-gesture.ts +10 -0
- package/src/gestures/use-pan-gesture.ts +22 -0
- package/src/gestures/use-tap-gesture.ts +14 -0
- package/src/gestures/utils/event-type.ts +24 -0
- package/src/gestures/utils/is-node-or-child.ts +31 -0
- package/src/index.ts +104 -0
- package/src/motion/Motion.svelte +246 -0
- package/src/motion/MotionSSR.svelte +244 -0
- package/src/motion/features/AnimationState.svelte +29 -0
- package/src/motion/features/Exit.svelte +32 -0
- package/src/motion/features/UseFeatures.svelte +39 -0
- package/src/motion/features/animations.ts +22 -0
- package/src/motion/features/definitions.ts +49 -0
- package/src/motion/features/drag.ts +24 -0
- package/src/motion/features/gestures.ts +24 -0
- package/src/motion/features/layout/Animate.svelte +314 -0
- package/src/motion/features/layout/Animate.ts +9 -0
- package/src/motion/features/layout/AnimateLayoutContextProvider.svelte +14 -0
- package/src/motion/features/layout/Measure.svelte +98 -0
- package/src/motion/features/layout/Measure.ts +9 -0
- package/src/motion/features/layout/MeasureContextProvider.svelte +32 -0
- package/src/motion/features/layout/index.ts +20 -0
- package/src/motion/features/layout/types.ts +71 -0
- package/src/motion/features/layout/utils.ts +40 -0
- package/src/motion/features/types.ts +53 -0
- package/src/motion/features/use-features.ts +16 -0
- package/src/motion/index.ts +64 -0
- package/src/motion/types.ts +278 -0
- package/src/motion/utils/UseLayoutId.svelte +18 -0
- package/src/motion/utils/UseVisualElement.svelte +104 -0
- package/src/motion/utils/UseVisualState.svelte +137 -0
- package/src/motion/utils/is-forced-motion-value.ts +23 -0
- package/src/motion/utils/make-renderless-component.ts +17 -0
- package/src/motion/utils/should-inhert-variant.ts +6 -0
- package/src/motion/utils/use-motion-ref.ts +41 -0
- package/src/motion/utils/use-visual-element.ts +13 -0
- package/src/motion/utils/use-visual-state.ts +24 -0
- package/src/motion/utils/valid-prop.ts +80 -0
- package/src/render/dom/M.svelte +16 -0
- package/src/render/dom/UseRender.svelte +37 -0
- package/src/render/dom/create-motion-class.ts +12 -0
- package/src/render/dom/create-visual-element.ts +22 -0
- package/src/render/dom/featureBundle.ts +22 -0
- package/src/render/dom/motion-minimal.ts +22 -0
- package/src/render/dom/motion-proxy.ts +107 -0
- package/src/render/dom/motion.ts +62 -0
- package/src/render/dom/projection/convert-to-relative.ts +40 -0
- package/src/render/dom/projection/default-scale-correctors.ts +138 -0
- package/src/render/dom/projection/measure.ts +28 -0
- package/src/render/dom/projection/relative-set.ts +27 -0
- package/src/render/dom/projection/scale-correction.ts +22 -0
- package/src/render/dom/projection/types.ts +13 -0
- package/src/render/dom/projection/utils.ts +69 -0
- package/src/render/dom/svg-visual-element.ts +114 -0
- package/src/render/dom/types.ts +32 -0
- package/src/render/dom/use-render.ts +11 -0
- package/src/render/dom/utils/UseInitialMotionProps.svelte +26 -0
- package/src/render/dom/utils/batch-layout.ts +77 -0
- package/src/render/dom/utils/camel-to-dash.ts +20 -0
- package/src/render/dom/utils/create-config.ts +33 -0
- package/src/render/dom/utils/css-variables-conversion.ts +121 -0
- package/src/render/dom/utils/filter-props.ts +55 -0
- package/src/render/dom/utils/is-css-variable.ts +18 -0
- package/src/render/dom/utils/is-svg-component.ts +41 -0
- package/src/render/dom/utils/parse-dom-variant.ts +26 -0
- package/src/render/dom/utils/unit-conversion.ts +258 -0
- package/src/render/dom/utils/use-html-props.ts +2 -0
- package/src/render/dom/utils/use-svg-props.ts +1 -0
- package/src/render/dom/value-types/animatable-none.ts +24 -0
- package/src/render/dom/value-types/defaults.ts +30 -0
- package/src/render/dom/value-types/dimensions.ts +27 -0
- package/src/render/dom/value-types/find.ts +31 -0
- package/src/render/dom/value-types/get-as-type.ts +21 -0
- package/src/render/dom/value-types/number.ts +83 -0
- package/src/render/dom/value-types/test.ts +17 -0
- package/src/render/dom/value-types/type-auto.ts +21 -0
- package/src/render/dom/value-types/type-int.ts +23 -0
- package/src/render/dom/value-types/types.ts +8 -0
- package/src/render/html/UseHTMLProps.svelte +33 -0
- package/src/render/html/UseInitialMotionValues.svelte +27 -0
- package/src/render/html/UseStyle.svelte +47 -0
- package/src/render/html/config-motion.ts +23 -0
- package/src/render/html/supported-elements.ts +10 -0
- package/src/render/html/types.ts +64 -0
- package/src/render/html/use-props.ts +14 -0
- package/src/render/html/utils/build-projection-transform.ts +53 -0
- package/src/render/html/utils/build-styles.ts +121 -0
- package/src/render/html/utils/build-transform.ts +79 -0
- package/src/render/html/utils/create-render-state.ts +18 -0
- package/src/render/html/utils/render.ts +22 -0
- package/src/render/html/utils/scrape-motion-values.ts +26 -0
- package/src/render/html/utils/transform.ts +51 -0
- package/src/render/html/visual-element.ts +129 -0
- package/src/render/index.ts +703 -0
- package/src/render/svg/UseSVGProps.svelte +34 -0
- package/src/render/svg/config-motion.ts +51 -0
- package/src/render/svg/lowercase-elements.ts +35 -0
- package/src/render/svg/supported-elements.ts +10 -0
- package/src/render/svg/types.ts +51 -0
- package/src/render/svg/use-props.ts +14 -0
- package/src/render/svg/utils/build-attrs.ts +58 -0
- package/src/render/svg/utils/camel-case-attrs.ts +27 -0
- package/src/render/svg/utils/create-render-state.ts +17 -0
- package/src/render/svg/utils/path.ts +49 -0
- package/src/render/svg/utils/render.ts +22 -0
- package/src/render/svg/utils/scrape-motion-values.ts +26 -0
- package/src/render/svg/utils/transform-origin.ts +30 -0
- package/src/render/svg/visual-element.ts +44 -0
- package/src/render/types.ts +148 -0
- package/src/render/utils/animation-state.ts +375 -0
- package/src/render/utils/animation.ts +167 -0
- package/src/render/utils/compare-by-depth.ts +18 -0
- package/src/render/utils/flat-tree.ts +35 -0
- package/src/render/utils/is-draggable.ts +17 -0
- package/src/render/utils/lifecycles.ts +172 -0
- package/src/render/utils/motion-values.ts +59 -0
- package/src/render/utils/projection.ts +38 -0
- package/src/render/utils/setters.ts +910 -0
- package/src/render/utils/state.ts +113 -0
- package/src/render/utils/types.ts +12 -0
- package/src/render/utils/variants.ts +76 -0
- package/src/types/geometry.ts +91 -0
- package/src/types.ts +1088 -0
- package/src/utils/UseUnmountEffect.svelte +11 -0
- package/src/utils/array.ts +18 -0
- package/src/utils/each-axis.ts +15 -0
- package/src/utils/errors.ts +22 -0
- package/src/utils/fix-process-env.ts +22 -0
- package/src/utils/geometry/delta-apply.ts +162 -0
- package/src/utils/geometry/delta-calc.ts +89 -0
- package/src/utils/geometry/index.ts +83 -0
- package/src/utils/is-browser.ts +12 -0
- package/src/utils/is-numerical-string.ts +15 -0
- package/src/utils/is-ref-object.ts +16 -0
- package/src/utils/noop.ts +15 -0
- package/src/utils/resolve-value.ts +23 -0
- package/src/utils/shallow-compare.ts +23 -0
- package/src/utils/subscription-manager.ts +49 -0
- package/src/utils/time-conversion.ts +18 -0
- package/src/utils/transform.ts +120 -0
- package/src/utils/use-constant.ts +23 -0
- package/src/utils/use-cycle.ts +78 -0
- package/src/utils/use-force-update.ts +7 -0
- package/src/utils/use-isomorphic-effect.ts +8 -0
- package/src/utils/use-reduced-motion.ts +70 -0
- package/src/utils/use-unmount-effect.ts +8 -0
- package/src/value/index.ts +409 -0
- package/src/value/scroll/use-element-scroll.ts +73 -0
- package/src/value/scroll/use-viewport-scroll.ts +81 -0
- package/src/value/scroll/utils.ts +76 -0
- package/src/value/use-combine-values.ts +53 -0
- package/src/value/use-motion-template.ts +57 -0
- package/src/value/use-motion-value.ts +27 -0
- package/src/value/use-spring.ts +84 -0
- package/src/value/use-transform.ts +216 -0
- package/src/value/use-velocity.ts +44 -0
- package/src/value/utils/is-motion-value.ts +15 -0
- package/src/value/utils/resolve-motion-value.ts +29 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
based on framer-motion@4.1.17,
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
*/
|
|
5
|
+
import { MotionValue } from ".";
|
|
6
|
+
|
|
7
|
+
import { useCombineMotionValues } from "./use-combine-values"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Combine multiple motion values into a new one using a string template literal.
|
|
11
|
+
*
|
|
12
|
+
* ```jsx
|
|
13
|
+
* import {
|
|
14
|
+
* motion,
|
|
15
|
+
* useSpring,
|
|
16
|
+
* useMotionValue,
|
|
17
|
+
* useMotionTemplate
|
|
18
|
+
* } from "framer-motion"
|
|
19
|
+
*
|
|
20
|
+
* function Component() {
|
|
21
|
+
* const shadowX = useSpring(0)
|
|
22
|
+
* const shadowY = useMotionValue(0)
|
|
23
|
+
* const shadow = useMotionTemplate`drop-shadow(${shadowX}px ${shadowY}px 20px rgba(0,0,0,0.3))`
|
|
24
|
+
*
|
|
25
|
+
* return <MotionDiv style={{ filter: shadow }} />
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export const useMotionTemplate = (fragments: TemplateStringsArray, ...values: MotionValue[]) => {
|
|
33
|
+
/**
|
|
34
|
+
* Create a function that will build a string from the latest motion values.
|
|
35
|
+
*/
|
|
36
|
+
let numFragments = fragments.length;
|
|
37
|
+
const buildValue = () => {
|
|
38
|
+
let output = ``
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < numFragments; i++) {
|
|
41
|
+
output += fragments[i]
|
|
42
|
+
const value = values[i]
|
|
43
|
+
if (value) output += values[i].get()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return output
|
|
47
|
+
}
|
|
48
|
+
const value = useCombineMotionValues(values, buildValue) as any;
|
|
49
|
+
value.resetInner = value.reset;
|
|
50
|
+
|
|
51
|
+
value.reset = (f: TemplateStringsArray, ...vs: MotionValue[]) => {
|
|
52
|
+
numFragments = f.length;
|
|
53
|
+
value.resetInner(vs,buildValue)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return value as MotionValue<string> & { reset: (fragments: TemplateStringsArray, ...values: MotionValue[]) => void };
|
|
57
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
based on framer-motion@4.1.17,
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates a `MotionValue` to track the state and velocity of a value.
|
|
7
|
+
*
|
|
8
|
+
* Usually, these are created automatically. For advanced use-cases, like use with `useTransform`, you can create `MotionValue`s externally and pass them into the animated component via the `style` prop.
|
|
9
|
+
*
|
|
10
|
+
* @motion
|
|
11
|
+
*
|
|
12
|
+
* ```jsx
|
|
13
|
+
* <script>
|
|
14
|
+
* const scale = useMotionValue(1)
|
|
15
|
+
* </script
|
|
16
|
+
*
|
|
17
|
+
* <Motion let:motion>
|
|
18
|
+
* <div style={{scale}} use:motion/>
|
|
19
|
+
* </Motion>
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param initial - The initial state.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export { motionValue as useMotionValue } from './index.js';
|
|
27
|
+
// export { default as UseMotionValue } from './UseMotionValue.svelte';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
based on framer-motion@4.1.17,
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
*/
|
|
5
|
+
import type { SpringOptions } from "popmotion";
|
|
6
|
+
import { MotionValue } from "../value";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
based on framer-motion@4.1.16,
|
|
10
|
+
Copyright (c) 2018 Framer B.V.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { fixed } from '../utils/fix-process-env';
|
|
14
|
+
import { getContext } from "svelte"
|
|
15
|
+
import { MotionConfigContext, type MotionConfigContextObject } from "../context/MotionConfigContext"
|
|
16
|
+
import { get, type Writable } from 'svelte/store';
|
|
17
|
+
import { useMotionValue } from "./use-motion-value";
|
|
18
|
+
import { isMotionValue } from "./utils/is-motion-value";
|
|
19
|
+
import { animate } from "popmotion"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a `MotionValue` that, when `set`, will use a spring animation to animate to its new state.
|
|
23
|
+
*
|
|
24
|
+
* It can either work as a stand-alone `MotionValue` by initialising it with a value, or as a subscriber
|
|
25
|
+
* to another `MotionValue`.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
*
|
|
29
|
+
* ```jsx
|
|
30
|
+
* const x = useSpring(0, { stiffness: 300 })
|
|
31
|
+
* const y = useSpring(x, { damping: 10 })
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param inputValue - `MotionValue` or number. If provided a `MotionValue`, when the input `MotionValue` changes, the created `MotionValue` will spring towards that value.
|
|
35
|
+
* @param springConfig - Configuration options for the spring.
|
|
36
|
+
* @returns `MotionValue`
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export const useSpring = (source: MotionValue | number, config: SpringOptions = {}, isCustom = false) => {
|
|
41
|
+
|
|
42
|
+
const mcc: Writable<MotionConfigContextObject> = getContext(MotionConfigContext) || MotionConfigContext(isCustom);
|
|
43
|
+
|
|
44
|
+
let activeSpringAnimation: { stop: () => void } | null = null;
|
|
45
|
+
|
|
46
|
+
let value = useMotionValue(isMotionValue(source) ? source.get() : source) as MotionValue<any> & { reset: (_: any, config: SpringOptions) => void };
|
|
47
|
+
|
|
48
|
+
let cleanup: () => void;
|
|
49
|
+
const update = (_source: typeof source, _config: typeof config) => {
|
|
50
|
+
value.attach((v, set) => {
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
const { isStatic } = get(mcc);
|
|
54
|
+
|
|
55
|
+
if (isStatic) {
|
|
56
|
+
return set(v);
|
|
57
|
+
}
|
|
58
|
+
if (activeSpringAnimation) {
|
|
59
|
+
activeSpringAnimation.stop();
|
|
60
|
+
}
|
|
61
|
+
activeSpringAnimation = animate({
|
|
62
|
+
from: value.get(),
|
|
63
|
+
to: v,
|
|
64
|
+
velocity: value.getVelocity(),
|
|
65
|
+
..._config,
|
|
66
|
+
onUpdate: set,
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
return value.get();
|
|
70
|
+
})
|
|
71
|
+
cleanup?.()
|
|
72
|
+
return isMotionValue(_source) ?
|
|
73
|
+
_source.onChange(v => value.set(parseFloat(v))) :
|
|
74
|
+
undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
update(source, config);
|
|
78
|
+
|
|
79
|
+
value.reset = update;
|
|
80
|
+
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//export { default as UseSpring } from './UseSpring.svelte';
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
based on framer-motion@4.1.17,
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
*/
|
|
5
|
+
import { MotionValue } from "../value";
|
|
6
|
+
import type { TransformOptions } from "../utils/transform";
|
|
7
|
+
|
|
8
|
+
import { transform } from "../utils/transform"
|
|
9
|
+
import { useCombineMotionValues } from "./use-combine-values"
|
|
10
|
+
|
|
11
|
+
export type InputRange = number[];
|
|
12
|
+
type SingleTransformer<I, O> = (input: I) => O;
|
|
13
|
+
type MultiTransformer<I, O> = (input: I[]) => O;
|
|
14
|
+
type Transformer<I, O> =
|
|
15
|
+
| SingleTransformer<I, O>
|
|
16
|
+
/**
|
|
17
|
+
* Ideally, this would be typed <I, O> in all instances, but to type this
|
|
18
|
+
* more accurately requires the tuple support in TypeScript 4:
|
|
19
|
+
* https://gist.github.com/InventingWithMonster/c4d23752a0fae7888596c4ff6d92733a
|
|
20
|
+
*/
|
|
21
|
+
| MultiTransformer<string | number, O>
|
|
22
|
+
/**
|
|
23
|
+
* Create a `MotionValue` that transforms the output of another `MotionValue` by mapping it from one range of values into another.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
*
|
|
27
|
+
* Given an input range of `[-200, -100, 100, 200]` and an output range of
|
|
28
|
+
* `[0, 1, 1, 0]`, the returned `MotionValue` will:
|
|
29
|
+
*
|
|
30
|
+
* - When provided a value between `-200` and `-100`, will return a value between `0` and `1`.
|
|
31
|
+
* - When provided a value between `-100` and `100`, will return `1`.
|
|
32
|
+
* - When provided a value between `100` and `200`, will return a value between `1` and `0`
|
|
33
|
+
*
|
|
34
|
+
*
|
|
35
|
+
* The input range must be a linear series of numbers. The output range
|
|
36
|
+
* can be any value type supported by Framer Motion: numbers, colors, shadows, etc.
|
|
37
|
+
*
|
|
38
|
+
* Every value in the output range must be of the same type and in the same format.
|
|
39
|
+
*
|
|
40
|
+
* @motion
|
|
41
|
+
*
|
|
42
|
+
* ```jsx
|
|
43
|
+
* <script>
|
|
44
|
+
* const x = useMotionValue(0)
|
|
45
|
+
* const xRange = [-200, -100, 100, 200]
|
|
46
|
+
* const opacityRange = [0, 1, 1, 0]
|
|
47
|
+
* const opacity = useTransform(x, xRange, opacityRange)
|
|
48
|
+
* </script>
|
|
49
|
+
*
|
|
50
|
+
* <Motion let:motion
|
|
51
|
+
* animate={{ x: 200 }}
|
|
52
|
+
* style={{ opacity, x }}
|
|
53
|
+
* >
|
|
54
|
+
* <div use:motion/>
|
|
55
|
+
* </Motion>
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @param inputValue - `MotionValue`
|
|
59
|
+
* @param inputRange - A linear series of numbers (either all increasing or decreasing)
|
|
60
|
+
* @param outputRange - A series of numbers, colors or strings. Must be the same length as `inputRange`.
|
|
61
|
+
* @param options -
|
|
62
|
+
*
|
|
63
|
+
* - clamp: boolean. Clamp values to within the given range. Defaults to `true`
|
|
64
|
+
* - ease: EasingFunction[]. Easing functions to use on the interpolations between each value in the input and output ranges. If provided as an array, the array must be one item shorter than the input and output ranges, as the easings apply to the transition between each.
|
|
65
|
+
*
|
|
66
|
+
* @returns `MotionValue & { reset: (value: MotionValue<number>, inputRange: InputRange, outputRange: O[], options?: TransformOptions<O>) => void }`
|
|
67
|
+
*
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
70
|
+
export function useTransform<I, O>(value: MotionValue<number>, inputRange: InputRange, outputRange: O[], options?: TransformOptions<O>):
|
|
71
|
+
MotionValue<O> & { reset: (value: MotionValue<number>, inputRange: InputRange, outputRange: O[], options?: TransformOptions<O>) => void };
|
|
72
|
+
/**
|
|
73
|
+
* Create a `MotionValue` that transforms the output of another `MotionValue` through a function.
|
|
74
|
+
* In this example, `y` will always be double `x`.
|
|
75
|
+
*
|
|
76
|
+
* @motion
|
|
77
|
+
*
|
|
78
|
+
* ```jsx
|
|
79
|
+
* <script>
|
|
80
|
+
* const x = useMotionValue(10)
|
|
81
|
+
* const y = useTransform(x, value => value * 2)
|
|
82
|
+
* </script>
|
|
83
|
+
*
|
|
84
|
+
* <Motion let:motion style={{ x, y }}>
|
|
85
|
+
* <div use:motion/>
|
|
86
|
+
* </Motion>
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @param input - A `MotionValue` that will pass its latest value through `transform` to update the returned `MotionValue`.
|
|
90
|
+
* @param transform - A function that accepts the latest value from `input` and returns a new value.
|
|
91
|
+
* @returns `MotionValue`
|
|
92
|
+
*
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
export function useTransform<I, O>(input: MotionValue<I>, transformer: SingleTransformer<I, O>):
|
|
96
|
+
MotionValue<O> & { reset: (input: MotionValue<I>, transformer: SingleTransformer<I, O>) => void};
|
|
97
|
+
/**
|
|
98
|
+
* Pass an array of `MotionValue`s and a function to combine them. In this example, `z` will be the `x` multiplied by `y`.
|
|
99
|
+
*
|
|
100
|
+
* @motion
|
|
101
|
+
*
|
|
102
|
+
* ```jsx
|
|
103
|
+
* <script>
|
|
104
|
+
* const x = useMotionValue(0)
|
|
105
|
+
* const y = useMotionValue(0)
|
|
106
|
+
* const z = useTransform([x, y], [latestX, latestY] => latestX * latestY)
|
|
107
|
+
* </script>
|
|
108
|
+
*
|
|
109
|
+
* <Motion let:motion style={{ x, y, z }}>
|
|
110
|
+
* return <div use:motion/>
|
|
111
|
+
* </Motion>
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @param input - An array of `MotionValue`s that will pass their latest values through `transform` to update the returned `MotionValue`.
|
|
115
|
+
* @param transform - A function that accepts the latest values from `input` and returns a new value.
|
|
116
|
+
* @returns `MotionValue`
|
|
117
|
+
*
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
// export function useTransform<I, O>(input: MotionValue<string | number>[], transformer: MultiTransformer<I, O>):
|
|
121
|
+
// MotionValue<O> & { reset: (input: MotionValue<string | number>[], transformer: MultiTransformer<I, O>) => void };
|
|
122
|
+
// export function useTransform<I, O>(
|
|
123
|
+
// input: MotionValue<string>[] | MotionValue<number>[] | MotionValue<string | number>[],
|
|
124
|
+
// transformer: MultiTransformer<I, O>
|
|
125
|
+
// ): MotionValue<O> & { reset: (input: MotionValue<string>[] | MotionValue<number>[] | MotionValue<string | number>[], transformer: MultiTransformer<I, O>) => void };
|
|
126
|
+
// export function useTransform<I, O>(transformer: () => O): MotionValue<O>;
|
|
127
|
+
/**
|
|
128
|
+
* Pass an array of `MotionValue`s and a function to combine them. In this example, `z` will be the `x` multiplied by `y`.
|
|
129
|
+
*
|
|
130
|
+
* ```jsx
|
|
131
|
+
* export const MyComponent = () => {
|
|
132
|
+
* const x = useMotionValue(0)
|
|
133
|
+
* const y = useMotionValue(0)
|
|
134
|
+
* const z = useTransform([x, y], ([latestX, latestY]) => latestX * latestY)
|
|
135
|
+
*
|
|
136
|
+
* return <motion.div style={{ x, y, z }} />
|
|
137
|
+
* }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @param input - An array of `MotionValue`s that will pass their latest values through `transform` to update the returned `MotionValue`.
|
|
141
|
+
* @param transform - A function that accepts the latest values from `input` and returns a new value.
|
|
142
|
+
* @returns `MotionValue`
|
|
143
|
+
*
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
export function useTransform<I, O>(
|
|
147
|
+
input:
|
|
148
|
+
| MotionValue<I>
|
|
149
|
+
| MotionValue<string>[]
|
|
150
|
+
| MotionValue<number>[]
|
|
151
|
+
| MotionValue<string | number>[]
|
|
152
|
+
| (() => O),
|
|
153
|
+
inputRangeOrTransformer?: InputRange | Transformer<I, O>,
|
|
154
|
+
outputRange?: O[],
|
|
155
|
+
options?: TransformOptions<O>
|
|
156
|
+
): any {
|
|
157
|
+
|
|
158
|
+
const latest = <I[]>[];
|
|
159
|
+
|
|
160
|
+
const update = (
|
|
161
|
+
input:
|
|
162
|
+
| MotionValue<I>
|
|
163
|
+
| MotionValue<string>[]
|
|
164
|
+
| MotionValue<number>[]
|
|
165
|
+
| MotionValue<string | number>[]
|
|
166
|
+
| (() => O),
|
|
167
|
+
inputRangeOrTransformer?: InputRange | Transformer<I,O>,
|
|
168
|
+
outputRange?: O[],
|
|
169
|
+
options?: TransformOptions<O>
|
|
170
|
+
) => {
|
|
171
|
+
const transformer = typeof inputRangeOrTransformer === "function"
|
|
172
|
+
? inputRangeOrTransformer
|
|
173
|
+
: transform(inputRangeOrTransformer!, outputRange!, options);
|
|
174
|
+
const values = Array.isArray(input) ? input : [input];
|
|
175
|
+
const _transformer = Array.isArray(input) ? transformer :
|
|
176
|
+
([latest]) =>
|
|
177
|
+
transformer(latest);
|
|
178
|
+
return [values, () => {
|
|
179
|
+
latest.length = 0
|
|
180
|
+
const numValues = values.length
|
|
181
|
+
for (let i = 0; i < numValues; i++) {
|
|
182
|
+
latest[i] = values[i].get()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return _transformer(latest)
|
|
186
|
+
}]
|
|
187
|
+
|
|
188
|
+
}
|
|
189
|
+
const comb = useCombineMotionValues(...update(input,
|
|
190
|
+
inputRangeOrTransformer,
|
|
191
|
+
outputRange,
|
|
192
|
+
options)) as any;
|
|
193
|
+
|
|
194
|
+
comb.updateInner = comb.reset;
|
|
195
|
+
|
|
196
|
+
comb.reset = (
|
|
197
|
+
input:
|
|
198
|
+
| MotionValue<I>
|
|
199
|
+
| MotionValue<string>[]
|
|
200
|
+
| MotionValue<number>[]
|
|
201
|
+
| MotionValue<string | number>[]
|
|
202
|
+
| (() => O),
|
|
203
|
+
inputRangeOrTransformer?: InputRange | Transformer<I,O>,
|
|
204
|
+
outputRange?: O[],
|
|
205
|
+
options?: TransformOptions<O>
|
|
206
|
+
) => comb.updateInner(
|
|
207
|
+
...update(
|
|
208
|
+
input,
|
|
209
|
+
inputRangeOrTransformer,
|
|
210
|
+
outputRange,
|
|
211
|
+
options
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
return comb;
|
|
215
|
+
}
|
|
216
|
+
// export { default as UseTransform } from './UseTransform.svelte';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
based on framer-motion@4.1.17,
|
|
4
|
+
Copyright (c) 2018 Framer B.V.
|
|
5
|
+
*/
|
|
6
|
+
import { MotionValue } from "./index.js";
|
|
7
|
+
import { useMotionValue } from "./use-motion-value.js"
|
|
8
|
+
/**
|
|
9
|
+
* Creates a `MotionValue` that updates when the velocity of the provided `MotionValue` changes.
|
|
10
|
+
*
|
|
11
|
+
* ```javascript
|
|
12
|
+
* const x = useMotionValue(0)
|
|
13
|
+
* const xVelocity = useVelocity(x)
|
|
14
|
+
* const xAcceleration = useVelocity(xVelocity)
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export const useVelocity = (value : MotionValue<number>) => {
|
|
20
|
+
let val = value;
|
|
21
|
+
let cleanup: () => void;
|
|
22
|
+
|
|
23
|
+
const reset = (value: MotionValue<number>) => {
|
|
24
|
+
cleanup?.();
|
|
25
|
+
val = value
|
|
26
|
+
cleanup = val.velocityUpdateSubscribers.add((newVelocity) => {
|
|
27
|
+
velocity.set(newVelocity);
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const velocity = useMotionValue(value.getVelocity(), () => {
|
|
32
|
+
cleanup?.();
|
|
33
|
+
cleanup = val.velocityUpdateSubscribers.add((newVelocity) => {
|
|
34
|
+
velocity.set(newVelocity);
|
|
35
|
+
})
|
|
36
|
+
return () => {
|
|
37
|
+
cleanup?.()
|
|
38
|
+
}
|
|
39
|
+
}) as MotionValue<number> & { reset: typeof reset };
|
|
40
|
+
|
|
41
|
+
velocity.reset = reset;
|
|
42
|
+
|
|
43
|
+
return velocity;
|
|
44
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
based on framer-motion@4.1.17,
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
*/
|
|
5
|
+
import type { MotionValue } from "../";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
based on framer-motion@4.0.3,
|
|
9
|
+
Copyright (c) 2018 Framer B.V.
|
|
10
|
+
*/
|
|
11
|
+
var isMotionValue = function (value: any): value is MotionValue {
|
|
12
|
+
return value !== null && typeof value === "object" && value.getVelocity;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { isMotionValue };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
based on framer-motion@4.1.17,
|
|
3
|
+
Copyright (c) 2018 Framer B.V.
|
|
4
|
+
*/
|
|
5
|
+
import { MotionValue } from "..";
|
|
6
|
+
import type { CustomValueType } from "../../types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
based on framer-motion@4.0.3,
|
|
10
|
+
Copyright (c) 2018 Framer B.V.
|
|
11
|
+
*/
|
|
12
|
+
import { isCustomValue } from '../../utils/resolve-value.js';
|
|
13
|
+
import { isMotionValue } from './is-motion-value.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* If the provided value is a MotionValue, this returns the actual value, otherwise just the value itself
|
|
17
|
+
*
|
|
18
|
+
* TODO: Remove and move to library
|
|
19
|
+
*
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
function resolveMotionValue(value?: string | number | CustomValueType | MotionValue): string | number {
|
|
23
|
+
var unwrappedValue = isMotionValue(value) ? value.get() : value;
|
|
24
|
+
return isCustomValue(unwrappedValue)
|
|
25
|
+
? unwrappedValue.toValue()
|
|
26
|
+
: unwrappedValue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { resolveMotionValue };
|