framer-motion
Version:
A simple and powerful JavaScript animation library
1,678 lines (1,660 loc) • 95.3 kB
TypeScript
/// <reference types="react" />
import { Easing, TransformProperties, SVGPathProperties, VariableKeyframesDefinition, MotionValue, AnimationPlaybackControls, ValueAnimationOptions, Transition as Transition$1, EasingFunction, ProgressTimeline, Batcher } from 'motion-dom';
import * as React$1 from 'react';
import { SVGAttributes, CSSProperties, PropsWithoutRef, RefAttributes, JSX } from 'react';
import * as react_jsx_runtime from 'react/jsx-runtime';
interface Point {
x: number;
y: number;
}
interface Axis {
min: number;
max: number;
}
interface Box {
x: Axis;
y: Axis;
}
interface BoundingBox {
top: number;
right: number;
bottom: number;
left: number;
}
interface AxisDelta {
translate: number;
scale: number;
origin: number;
originPoint: number;
}
interface Delta {
x: AxisDelta;
y: AxisDelta;
}
type TransformPoint = (point: Point) => Point;
type GenericKeyframesTarget<V> = V[] | Array<null | V>;
/**
* @public
*/
type ResolvedKeyframesTarget = GenericKeyframesTarget<number> | GenericKeyframesTarget<string>;
/**
* @public
*/
type KeyframesTarget = ResolvedKeyframesTarget | GenericKeyframesTarget<CustomValueType>;
/**
* @public
*/
type ResolvedSingleTarget = string | number;
/**
* @public
*/
type SingleTarget = ResolvedSingleTarget | CustomValueType;
/**
* @public
*/
type ResolvedValueTarget = ResolvedSingleTarget | ResolvedKeyframesTarget;
/**
* @public
*/
type ValueTarget = SingleTarget | KeyframesTarget;
/**
* Options for orchestrating the timing of animations.
*
* @public
*/
interface Orchestration {
/**
* Delay the animation by this duration (in seconds). Defaults to `0`.
*
* @remarks
* ```javascript
* const transition = {
* delay: 0.2
* }
* ```
*
* @public
*/
delay?: number;
/**
* Describes the relationship between the transition and its children. Set
* to `false` by default.
*
* @remarks
* When using variants, the transition can be scheduled in relation to its
* children with either `"beforeChildren"` to finish this transition before
* starting children transitions, `"afterChildren"` to finish children
* transitions before starting this transition.
*
* ```jsx
* const list = {
* hidden: {
* opacity: 0,
* transition: { when: "afterChildren" }
* }
* }
*
* const item = {
* hidden: {
* opacity: 0,
* transition: { duration: 2 }
* }
* }
*
* return (
* <motion.ul variants={list} animate="hidden">
* <motion.li variants={item} />
* <motion.li variants={item} />
* </motion.ul>
* )
* ```
*
* @public
*/
when?: false | "beforeChildren" | "afterChildren" | string;
/**
* When using variants, children animations will start after this duration
* (in seconds). You can add the `transition` property to both the `Frame` and the `variant` directly. Adding it to the `variant` generally offers more flexibility, as it allows you to customize the delay per visual state.
*
* ```jsx
* const container = {
* hidden: { opacity: 0 },
* show: {
* opacity: 1,
* transition: {
* delayChildren: 0.5
* }
* }
* }
*
* const item = {
* hidden: { opacity: 0 },
* show: { opacity: 1 }
* }
*
* return (
* <motion.ul
* variants={container}
* initial="hidden"
* animate="show"
* >
* <motion.li variants={item} />
* <motion.li variants={item} />
* </motion.ul>
* )
* ```
*
* @public
*/
delayChildren?: number;
/**
* When using variants, animations of child components can be staggered by this
* duration (in seconds).
*
* For instance, if `staggerChildren` is `0.01`, the first child will be
* delayed by `0` seconds, the second by `0.01`, the third by `0.02` and so
* on.
*
* The calculated stagger delay will be added to `delayChildren`.
*
* ```jsx
* const container = {
* hidden: { opacity: 0 },
* show: {
* opacity: 1,
* transition: {
* staggerChildren: 0.5
* }
* }
* }
*
* const item = {
* hidden: { opacity: 0 },
* show: { opacity: 1 }
* }
*
* return (
* <motion.ol
* variants={container}
* initial="hidden"
* animate="show"
* >
* <motion.li variants={item} />
* <motion.li variants={item} />
* </motion.ol>
* )
* ```
*
* @public
*/
staggerChildren?: number;
/**
* The direction in which to stagger children.
*
* A value of `1` staggers from the first to the last while `-1`
* staggers from the last to the first.
*
* ```jsx
* const container = {
* hidden: { opacity: 0 },
* show: {
* opacity: 1,
* transition: {
* staggerChildren: 0.5,
* staggerDirection: -1
* }
* }
* }
*
* const item = {
* hidden: { opacity: 0 },
* show: { opacity: 1 }
* }
*
* return (
* <motion.ul
* variants={container}
* initial="hidden"
* animate="show"
* >
* <motion.li variants={item} size={50} />
* <motion.li variants={item} size={50} />
* </motion.ul>
* )
* ```
*
* @public
*/
staggerDirection?: number;
}
interface Repeat {
/**
* The number of times to repeat the transition. Set to `Infinity` for perpetual repeating.
*
* Without setting `repeatType`, this will loop the animation.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ repeat: Infinity, duration: 2 }}
* />
* ```
*
* @public
*/
repeat?: number;
/**
* How to repeat the animation. This can be either:
*
* "loop": Repeats the animation from the start
*
* "reverse": Alternates between forward and backwards playback
*
* "mirror": Switches `from` and `to` alternately
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{
* repeat: 1,
* repeatType: "reverse",
* duration: 2
* }}
* />
* ```
*
* @public
*/
repeatType?: "loop" | "reverse" | "mirror";
/**
* When repeating an animation, `repeatDelay` will set the
* duration of the time to wait, in seconds, between each repetition.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ repeat: Infinity, repeatDelay: 1 }}
* />
* ```
*
* @public
*/
repeatDelay?: number;
}
/**
* An animation that animates between two or more values over a specific duration of time.
* This is the default animation for non-physical values like `color` and `opacity`.
*
* @public
*/
interface Tween extends Repeat {
/**
* Set `type` to `"tween"` to use a duration-based tween animation.
* If any non-orchestration `transition` values are set without a `type` property,
* this is used as the default animation.
*
* ```jsx
* <motion.path
* animate={{ pathLength: 1 }}
* transition={{ duration: 2, type: "tween" }}
* />
* ```
*
* @public
*/
type?: "tween";
/**
* The duration of the tween animation. Set to `0.3` by default, 0r `0.8` if animating a series of keyframes.
*
* ```jsx
* const variants = {
* visible: {
* opacity: 1,
* transition: { duration: 2 }
* }
* }
* ```
*
* @public
*/
duration?: number;
/**
* The easing function to use. Set as one of the below.
*
* - The name of an existing easing function.
*
* - An array of four numbers to define a cubic bezier curve.
*
* - An easing function, that accepts and returns a value `0-1`.
*
* If the animating value is set as an array of multiple values for a keyframes
* animation, `ease` can be set as an array of easing functions to set different easings between
* each of those values.
*
*
* ```jsx
* <motion.div
* animate={{ opacity: 0 }}
* transition={{ ease: [0.17, 0.67, 0.83, 0.67] }}
* />
* ```
*
* @public
*/
ease?: Easing | Easing[];
/**
* When animating keyframes, `times` can be used to determine where in the animation each keyframe is reached.
* Each value in `times` is a value between `0` and `1`, representing `duration`.
*
* There must be the same number of `times` as there are keyframes.
* Defaults to an array of evenly-spread durations.
*
* ```jsx
* <motion.div
* animate={{ scale: [0, 1, 0.5, 1] }}
* transition={{ times: [0, 0.1, 0.9, 1] }}
* />
* ```
*
* @public
*/
times?: number[];
/**
* When animating keyframes, `easings` can be used to define easing functions between each keyframe. This array should be one item fewer than the number of keyframes, as these easings apply to the transitions between the keyframes.
*
* ```jsx
* <motion.div
* animate={{ backgroundColor: ["#0f0", "#00f", "#f00"] }}
* transition={{ easings: ["easeIn", "easeOut"] }}
* />
* ```
*
* @public
*/
easings?: Easing[];
/**
* The value to animate from.
* By default, this is the current state of the animating value.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ from: 90, duration: 2 }}
* />
* ```
*
* @public
*/
from?: number | string;
}
/**
* An animation that simulates spring physics for realistic motion.
* This is the default animation for physical values like `x`, `y`, `scale` and `rotate`.
*
* @public
*/
interface Spring extends Repeat {
/**
* Set `type` to `"spring"` to animate using spring physics for natural
* movement. Type is set to `"spring"` by default.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: 'spring' }}
* />
* ```
*
* @public
*/
type: "spring";
/**
* Stiffness of the spring. Higher values will create more sudden movement.
* Set to `100` by default.
*
* ```jsx
* <motion.section
* animate={{ rotate: 180 }}
* transition={{ type: 'spring', stiffness: 50 }}
* />
* ```
*
* @public
*/
stiffness?: number;
/**
* Strength of opposing force. If set to 0, spring will oscillate
* indefinitely. Set to `10` by default.
*
* ```jsx
* <motion.a
* animate={{ rotate: 180 }}
* transition={{ type: 'spring', damping: 300 }}
* />
* ```
*
* @public
*/
damping?: number;
/**
* Mass of the moving object. Higher values will result in more lethargic
* movement. Set to `1` by default.
*
* ```jsx
* <motion.feTurbulence
* animate={{ baseFrequency: 0.5 } as any}
* transition={{ type: "spring", mass: 0.5 }}
* />
* ```
*
* @public
*/
mass?: number;
/**
* The duration of the animation, defined in seconds. Spring animations can be a maximum of 10 seconds.
*
* If `bounce` is set, this defaults to `0.8`.
*
* Note: `duration` and `bounce` will be overridden if `stiffness`, `damping` or `mass` are set.
*
* ```jsx
* <motion.div
* animate={{ x: 100 }}
* transition={{ type: "spring", duration: 0.8 }}
* />
* ```
*
* @public
*/
duration?: number;
/**
* If visualDuration is set, this will override duration.
*
* The visual duration is a time, set in seconds, that the animation will take to visually appear to reach its target.
*
* In other words, the bulk of the transition will occur before this time, and the "bouncy bit" will mostly happen after.
*
* This makes it easier to edit a spring, as well as visually coordinate it with other time-based animations.
*
* ```jsx
* <motion.div
* animate={{ x: 100 }}
* transition={{ type: "spring", visualDuration: 0.5 }}
* />
* ```
*
* @public
*/
visualDuration?: number;
/**
* `bounce` determines the "bounciness" of a spring animation.
*
* `0` is no bounce, and `1` is extremely bouncy.
*
* If `duration` is set, this defaults to `0.25`.
*
* Note: `bounce` and `duration` will be overridden if `stiffness`, `damping` or `mass` are set.
*
* ```jsx
* <motion.div
* animate={{ x: 100 }}
* transition={{ type: "spring", bounce: 0.25 }}
* />
* ```
*
* @public
*/
bounce?: number;
/**
* End animation if absolute speed (in units per second) drops below this
* value and delta is smaller than `restDelta`. Set to `0.01` by default.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: 'spring', restSpeed: 0.5 }}
* />
* ```
*
* @public
*/
restSpeed?: number;
/**
* End animation if distance is below this value and speed is below
* `restSpeed`. When animation ends, spring gets “snapped” to. Set to
* `0.01` by default.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: 'spring', restDelta: 0.5 }}
* />
* ```
*
* @public
*/
restDelta?: number;
/**
* The value to animate from.
* By default, this is the initial state of the animating value.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: 'spring', from: 90 }}
* />
* ```
*
* @public
*/
from?: number | string;
/**
* The initial velocity of the spring. By default this is the current velocity of the component.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: 'spring', velocity: 2 }}
* />
* ```
*
* @public
*/
velocity?: number;
}
/**
* An animation that decelerates a value based on its initial velocity,
* usually used to implement inertial scrolling.
*
* Optionally, `min` and `max` boundaries can be defined, and inertia
* will snap to these with a spring animation.
*
* This animation will automatically precalculate a target value,
* which can be modified with the `modifyTarget` property.
*
* This allows you to add snap-to-grid or similar functionality.
*
* Inertia is also the animation used for `dragTransition`, and can be configured via that prop.
*
* @public
*/
interface Inertia {
/**
* Set `type` to animate using the inertia animation. Set to `"tween"` by
* default. This can be used for natural deceleration, like momentum scrolling.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: "inertia", velocity: 50 }}
* />
* ```
*
* @public
*/
type: "inertia";
/**
* A function that receives the automatically-calculated target and returns a new one. Useful for snapping the target to a grid.
*
* ```jsx
* <motion.div
* drag
* dragTransition={{
* power: 0,
* // Snap calculated target to nearest 50 pixels
* modifyTarget: target => Math.round(target / 50) * 50
* }}
* />
* ```
*
* @public
*/
modifyTarget?(v: number): number;
/**
* If `min` or `max` is set, this affects the stiffness of the bounce
* spring. Higher values will create more sudden movement. Set to `500` by
* default.
*
* ```jsx
* <motion.div
* drag
* dragTransition={{
* min: 0,
* max: 100,
* bounceStiffness: 100
* }}
* />
* ```
*
* @public
*/
bounceStiffness?: number;
/**
* If `min` or `max` is set, this affects the damping of the bounce spring.
* If set to `0`, spring will oscillate indefinitely. Set to `10` by
* default.
*
* ```jsx
* <motion.div
* drag
* dragTransition={{
* min: 0,
* max: 100,
* bounceDamping: 8
* }}
* />
* ```
*
* @public
*/
bounceDamping?: number;
/**
* A higher power value equals a further target. Set to `0.8` by default.
*
* ```jsx
* <motion.div
* drag
* dragTransition={{ power: 0.2 }}
* />
* ```
*
* @public
*/
power?: number;
/**
* Adjusting the time constant will change the duration of the
* deceleration, thereby affecting its feel. Set to `700` by default.
*
* ```jsx
* <motion.div
* drag
* dragTransition={{ timeConstant: 200 }}
* />
* ```
*
* @public
*/
timeConstant?: number;
/**
* End the animation if the distance to the animation target is below this value, and the absolute speed is below `restSpeed`.
* When the animation ends, the value gets snapped to the animation target. Set to `0.01` by default.
* Generally the default values provide smooth animation endings, only in rare cases should you need to customize these.
*
* ```jsx
* <motion.div
* drag
* dragTransition={{ restDelta: 10 }}
* />
* ```
*
* @public
*/
restDelta?: number;
/**
* Minimum constraint. If set, the value will "bump" against this value (or immediately spring to it if the animation starts as less than this value).
*
* ```jsx
* <motion.div
* drag
* dragTransition={{ min: 0, max: 100 }}
* />
* ```
*
* @public
*/
min?: number;
/**
* Maximum constraint. If set, the value will "bump" against this value (or immediately snap to it, if the initial animation value exceeds this value).
*
* ```jsx
* <motion.div
* drag
* dragTransition={{ min: 0, max: 100 }}
* />
* ```
*
* @public
*/
max?: number;
/**
* The value to animate from. By default, this is the current state of the animating value.
*
* ```jsx
* <Frame
* drag
* dragTransition={{ from: 50 }}
* />
* ```
*
* @public
*/
from?: number | string;
/**
* The initial velocity of the animation.
* By default this is the current velocity of the component.
*
* ```jsx
* <motion.div
* animate={{ rotate: 180 }}
* transition={{ type: 'inertia', velocity: 200 }}
* />
* ```
*
* @public
*/
velocity?: number;
}
/**
* Keyframes tweens between multiple `values`.
*
* These tweens can be arranged using the `duration`, `easings`, and `times` properties.
*/
interface Keyframes {
/**
* Set `type` to `"keyframes"` to animate using the keyframes animation.
* Set to `"tween"` by default. This can be used to animate between a series of values.
*
* @public
*/
type: "keyframes";
/**
* An array of numbers between 0 and 1, where `1` represents the `total` duration.
*
* Each value represents at which point during the animation each item in the animation target should be hit, so the array should be the same length as `values`.
*
* Defaults to an array of evenly-spread durations.
*
* @public
*/
times?: number[];
/**
* An array of easing functions for each generated tween, or a single easing function applied to all tweens.
*
* This array should be one item less than `values`, as these easings apply to the transitions *between* the `values`.
*
* ```jsx
* const transition = {
* backgroundColor: {
* type: 'keyframes',
* easings: ['circIn', 'circOut']
* }
* }
* ```
*
* @public
*/
ease?: Easing | Easing[];
/**
* The total duration of the animation. Set to `0.3` by default.
*
* ```jsx
* const transition = {
* type: "keyframes",
* duration: 2
* }
*
* <Frame
* animate={{ opacity: 0 }}
* transition={transition}
* />
* ```
*
* @public
*/
duration?: number;
/**
* @public
*/
repeatDelay?: number;
}
/**
* @public
*/
interface None {
/**
* Set `type` to `false` for an instant transition.
*
* @public
*/
type: false;
}
/**
* @public
*/
type PermissiveTransitionDefinition = {
[key: string]: any;
};
/**
* @public
*/
type TransitionDefinition = Tween | Spring | Keyframes | Inertia | None | PermissiveTransitionDefinition;
type TransitionMap = Orchestration & TransitionDefinition & {
[key: string]: TransitionDefinition;
};
/**
* Transition props
*
* @public
*/
type Transition = (Orchestration & Repeat & TransitionDefinition) | (Orchestration & Repeat & TransitionMap);
type Omit$1<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type CSSPropertiesWithoutTransitionOrSingleTransforms = Omit$1<CSSProperties, "transition" | "rotate" | "scale" | "perspective">;
type SVGTransformAttributes = {
attrX?: number;
attrY?: number;
attrScale?: number;
};
type TargetProperties = CSSPropertiesWithoutTransitionOrSingleTransforms & SVGAttributes<SVGElement> & SVGTransformAttributes & TransformProperties & CustomStyles & SVGPathProperties & VariableKeyframesDefinition;
/**
* @public
*/
type MakeCustomValueType<T> = {
[K in keyof T]: T[K] | CustomValueType;
};
/**
* @public
*/
type Target = MakeCustomValueType<TargetProperties>;
/**
* @public
*/
type MakeKeyframes<T> = {
[K in keyof T]: T[K] | T[K][] | [null, ...T[K][]];
};
/**
* @public
*/
type TargetWithKeyframes = MakeKeyframes<Target>;
/**
* An object that specifies values to animate to. Each value may be set either as
* a single value, or an array of values.
*
* It may also option contain these properties:
*
* - `transition`: Specifies transitions for all or individual values.
* - `transitionEnd`: Specifies values to set when the animation finishes.
*
* ```jsx
* const target = {
* x: "0%",
* opacity: 0,
* transition: { duration: 1 },
* transitionEnd: { display: "none" }
* }
* ```
*
* @public
*/
type TargetAndTransition = TargetWithKeyframes & {
transition?: Transition;
transitionEnd?: Target;
};
type TargetResolver = (custom: any, current: Target, velocity: Target) => TargetAndTransition | string;
/**
* @public
*/
type Variant = TargetAndTransition | TargetResolver;
/**
* @public
*/
type Variants = {
[key: string]: Variant;
};
/**
* @public
*/
interface CustomValueType {
mix: (from: any, to: any) => (p: number) => number | string;
toValue: () => number | string;
}
type RefObject<T> = {
current: T | null;
};
/**
* Passed in to pan event handlers like `onPan` the `PanInfo` object contains
* information about the current state of the tap gesture such as its
* `point`, `delta`, `offset` and `velocity`.
*
* ```jsx
* <motion.div onPan={(event, info) => {
* console.log(info.point.x, info.point.y)
* }} />
* ```
*
* @public
*/
interface PanInfo {
/**
* Contains `x` and `y` values for the current pan position relative
* to the device or page.
*
* ```jsx
* function onPan(event, info) {
* console.log(info.point.x, info.point.y)
* }
*
* <motion.div onPan={onPan} />
* ```
*
* @public
*/
point: Point;
/**
* Contains `x` and `y` values for the distance moved since
* the last event.
*
* ```jsx
* function onPan(event, info) {
* console.log(info.delta.x, info.delta.y)
* }
*
* <motion.div onPan={onPan} />
* ```
*
* @public
*/
delta: Point;
/**
* Contains `x` and `y` values for the distance moved from
* the first pan event.
*
* ```jsx
* function onPan(event, info) {
* console.log(info.offset.x, info.offset.y)
* }
*
* <motion.div onPan={onPan} />
* ```
*
* @public
*/
offset: Point;
/**
* Contains `x` and `y` values for the current velocity of the pointer, in px/ms.
*
* ```jsx
* function onPan(event, info) {
* console.log(info.velocity.x, info.velocity.y)
* }
*
* <motion.div onPan={onPan} />
* ```
*
* @public
*/
velocity: Point;
}
type ReducedMotionConfig = "always" | "never" | "user";
/**
* @public
*/
interface MotionConfigContext {
/**
* Internal, exported only for usage in Framer
*/
transformPagePoint: TransformPoint;
/**
* Internal. Determines whether this is a static context ie the Framer canvas. If so,
* it'll disable all dynamic functionality.
*/
isStatic: boolean;
/**
* Defines a new default transition for the entire tree.
*
* @public
*/
transition?: Transition;
/**
* If true, will respect the device prefersReducedMotion setting by switching
* transform animations off.
*
* @public
*/
reducedMotion?: ReducedMotionConfig;
/**
* A custom `nonce` attribute used when wanting to enforce a Content Security Policy (CSP).
* For more details see:
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src#unsafe_inline_styles
*
* @public
*/
nonce?: string;
}
/**
* @public
*/
declare const MotionConfigContext: React$1.Context<MotionConfigContext>;
/**
* @public
*/
interface PresenceContextProps {
id: string;
isPresent: boolean;
register: (id: string | number) => () => void;
onExitComplete?: (id: string | number) => void;
initial?: false | VariantLabels;
custom?: any;
}
/**
* @public
*/
declare const PresenceContext: React$1.Context<PresenceContextProps | null>;
interface SwitchLayoutGroup {
register?: (member: IProjectionNode) => void;
deregister?: (member: IProjectionNode) => void;
}
type InitialPromotionConfig = {
/**
* The initial transition to use when the elements in this group mount (and automatically promoted).
* Subsequent updates should provide a transition in the promote method.
*/
transition?: Transition;
/**
* If the follow tree should preserve its opacity when the lead is promoted on mount
*/
shouldPreserveFollowOpacity?: (member: IProjectionNode) => boolean;
};
type SwitchLayoutGroupContext = SwitchLayoutGroup & InitialPromotionConfig;
/**
* Internal, exported only for usage in Framer
*/
declare const SwitchLayoutGroupContext: React$1.Context<SwitchLayoutGroupContext>;
interface OnUpdateSettings<Instance, RenderState> {
props: MotionProps;
prevProps?: MotionProps;
current: Instance | null;
renderState: RenderState;
latestValues: ResolvedValues;
}
interface VisualState<Instance, RenderState> {
renderState: RenderState;
latestValues: ResolvedValues;
onUpdate?: (settings: OnUpdateSettings<Instance, RenderState>) => void;
onMount?: (instance: Instance) => void;
}
type UseVisualState<Instance, RenderState> = (props: MotionProps, isStatic: boolean) => VisualState<Instance, RenderState>;
interface UseVisualStateConfig<Instance, RenderState> {
scrapeMotionValuesFromProps: ScrapeMotionValuesFromProps;
createRenderState: () => RenderState;
onUpdate?: (settings: OnUpdateSettings<Instance, RenderState>) => void;
}
declare const makeUseVisualState: <I, RS>(config: UseVisualStateConfig<I, RS>) => UseVisualState<I, RS>;
type ScrapeMotionValuesFromProps = (props: MotionProps, prevProps: MotionProps, visualElement?: VisualElement) => {
[key: string]: MotionValue | string | number;
};
type VisualElementOptions<Instance, RenderState = any> = {
visualState: VisualState<Instance, RenderState>;
parent?: VisualElement<unknown>;
variantParent?: VisualElement<unknown>;
presenceContext: PresenceContextProps | null;
props: MotionProps;
blockInitialAnimation?: boolean;
reducedMotionConfig?: ReducedMotionConfig;
};
/**
* A generic set of string/number values
*/
interface ResolvedValues {
[key: string]: string | number;
}
interface VisualElementEventCallbacks {
BeforeLayoutMeasure: () => void;
LayoutMeasure: (layout: Box, prevLayout?: Box) => void;
LayoutUpdate: (layout: Axis, prevLayout: Axis) => void;
Update: (latest: ResolvedValues) => void;
AnimationStart: (definition: AnimationDefinition) => void;
AnimationComplete: (definition: AnimationDefinition) => void;
LayoutAnimationStart: () => void;
LayoutAnimationComplete: () => void;
SetAxisTarget: () => void;
Unmount: () => void;
}
interface LayoutLifecycles {
onBeforeLayoutMeasure?(box: Box): void;
onLayoutMeasure?(box: Box, prevBox: Box): void;
}
interface AnimationLifecycles {
/**
* Callback with latest motion values, fired max once per frame.
*
* ```jsx
* function onUpdate(latest) {
* console.log(latest.x, latest.opacity)
* }
*
* <motion.div animate={{ x: 100, opacity: 0 }} onUpdate={onUpdate} />
* ```
*/
onUpdate?(latest: ResolvedValues): void;
/**
* Callback when animation defined in `animate` begins.
*
* The provided callback will be called with the triggering animation definition.
* If this is a variant, it'll be the variant name, and if a target object
* then it'll be the target object.
*
* This way, it's possible to figure out which animation has started.
*
* ```jsx
* function onStart() {
* console.log("Animation started")
* }
*
* <motion.div animate={{ x: 100 }} onAnimationStart={onStart} />
* ```
*/
onAnimationStart?(definition: AnimationDefinition): void;
/**
* Callback when animation defined in `animate` is complete.
*
* The provided callback will be called with the triggering animation definition.
* If this is a variant, it'll be the variant name, and if a target object
* then it'll be the target object.
*
* This way, it's possible to figure out which animation has completed.
*
* ```jsx
* function onComplete() {
* console.log("Animation completed")
* }
*
* <motion.div
* animate={{ x: 100 }}
* onAnimationComplete={definition => {
* console.log('Completed animating', definition)
* }}
* />
* ```
*/
onAnimationComplete?(definition: AnimationDefinition): void;
}
type EventProps = LayoutLifecycles & AnimationLifecycles;
type CreateVisualElement<Instance> = (Component: string | React.ComponentType<React.PropsWithChildren<unknown>>, options: VisualElementOptions<Instance>) => VisualElement<Instance>;
interface WithDepth {
depth: number;
}
declare class FlatTree {
private children;
private isDirty;
add(child: WithDepth): void;
remove(child: WithDepth): void;
forEach(callback: (child: WithDepth) => void): void;
}
declare class NodeStack {
lead?: IProjectionNode;
prevLead?: IProjectionNode;
members: IProjectionNode[];
add(node: IProjectionNode): void;
remove(node: IProjectionNode): void;
relegate(node: IProjectionNode): boolean;
promote(node: IProjectionNode, preserveFollowOpacity?: boolean): void;
exitAnimationComplete(): void;
scheduleRender(): void;
/**
* Clear any leads that have been removed this render to prevent them from being
* used in future animations and to prevent memory leaks
*/
removeLeadSnapshot(): void;
}
interface Measurements {
animationId: number;
measuredBox: Box;
layoutBox: Box;
latestValues: ResolvedValues;
source: number;
}
type Phase = "snapshot" | "measure";
interface ScrollMeasurements {
animationId: number;
phase: Phase;
offset: Point;
isRoot: boolean;
wasRoot: boolean;
}
type LayoutEvents = "willUpdate" | "didUpdate" | "beforeMeasure" | "measure" | "projectionUpdate" | "animationStart" | "animationComplete";
interface IProjectionNode<I = unknown> {
id: number;
animationId: number;
parent?: IProjectionNode;
relativeParent?: IProjectionNode;
root?: IProjectionNode;
children: Set<IProjectionNode>;
path: IProjectionNode[];
nodes?: FlatTree;
depth: number;
instance: I;
mount: (node: I, isLayoutDirty?: boolean) => void;
unmount: () => void;
options: ProjectionNodeOptions;
setOptions(options: ProjectionNodeOptions): void;
layout?: Measurements;
snapshot?: Measurements;
target?: Box;
relativeTarget?: Box;
relativeTargetOrigin?: Box;
targetDelta?: Delta;
targetWithTransforms?: Box;
scroll?: ScrollMeasurements;
treeScale?: Point;
projectionDelta?: Delta;
projectionDeltaWithTransform?: Delta;
latestValues: ResolvedValues;
isLayoutDirty: boolean;
isProjectionDirty: boolean;
isSharedProjectionDirty: boolean;
isTransformDirty: boolean;
resolvedRelativeTargetAt?: number;
shouldResetTransform: boolean;
prevTransformTemplateValue: string | undefined;
isUpdateBlocked(): boolean;
updateManuallyBlocked: boolean;
updateBlockedByResize: boolean;
blockUpdate(): void;
unblockUpdate(): void;
isUpdating: boolean;
needsReset: boolean;
startUpdate(): void;
willUpdate(notifyListeners?: boolean): void;
didUpdate(): void;
measure(removeTransform?: boolean): Measurements;
measurePageBox(): Box;
updateLayout(): void;
updateSnapshot(): void;
clearSnapshot(): void;
updateScroll(phase?: Phase): void;
scheduleUpdateProjection(): void;
scheduleCheckAfterUnmount(): void;
checkUpdateFailed(): void;
sharedNodes: Map<string, NodeStack>;
registerSharedNode(id: string, node: IProjectionNode): void;
getStack(): NodeStack | undefined;
isVisible: boolean;
hide(): void;
show(): void;
scheduleRender(notifyAll?: boolean): void;
getClosestProjectingParent(): IProjectionNode | undefined;
setTargetDelta(delta: Delta): void;
resetTransform(): void;
resetSkewAndRotation(): void;
applyTransform(box: Box, transformOnly?: boolean): Box;
resolveTargetDelta(force?: boolean): void;
calcProjection(): void;
getProjectionStyles(styleProp?: MotionStyle): MotionStyle | undefined;
clearMeasurements(): void;
resetTree(): void;
isProjecting(): boolean;
animationValues?: ResolvedValues;
currentAnimation?: AnimationPlaybackControls;
isTreeAnimating?: boolean;
isAnimationBlocked?: boolean;
isTreeAnimationBlocked: () => boolean;
setAnimationOrigin(delta: Delta): void;
startAnimation(transition: Transition): void;
finishAnimation(): void;
hasCheckedOptimisedAppear: boolean;
isLead(): boolean;
promote(options?: {
needsReset?: boolean;
transition?: Transition;
preserveFollowOpacity?: boolean;
}): void;
relegate(): boolean;
resumeFrom?: IProjectionNode;
resumingFrom?: IProjectionNode;
isPresent?: boolean;
addEventListener(name: LayoutEvents, handler: any): VoidFunction;
notifyListeners(name: LayoutEvents, ...args: any): void;
hasListeners(name: LayoutEvents): boolean;
hasTreeAnimated: boolean;
preserveOpacity?: boolean;
}
interface ProjectionNodeOptions {
animate?: boolean;
layoutScroll?: boolean;
layoutRoot?: boolean;
alwaysMeasureLayout?: boolean;
onExitComplete?: VoidFunction;
animationType?: "size" | "position" | "both" | "preserve-aspect";
layoutId?: string;
layout?: boolean | string;
visualElement?: VisualElement;
crossfade?: boolean;
transition?: Transition;
initialPromotionConfig?: InitialPromotionConfig;
}
type AnimationType = "animate" | "whileHover" | "whileTap" | "whileDrag" | "whileFocus" | "whileInView" | "exit";
type VisualElementAnimationOptions = {
delay?: number;
transitionOverride?: Transition;
custom?: any;
type?: AnimationType;
};
interface AnimationState {
animateChanges: (type?: AnimationType) => Promise<any>;
setActive: (type: AnimationType, isActive: boolean, options?: VisualElementAnimationOptions) => Promise<any>;
setAnimateFunction: (fn: any) => void;
getState: () => {
[key: string]: AnimationTypeState;
};
reset: () => void;
}
interface AnimationTypeState {
isActive: boolean;
protectedKeys: {
[key: string]: true;
};
needsAnimating: {
[key: string]: boolean;
};
prevResolvedValues: {
[key: string]: any;
};
prevProp?: VariantLabels | TargetAndTransition;
}
type UnresolvedKeyframes<T extends string | number> = Array<T | null>;
type ResolvedKeyframes<T extends string | number> = Array<T>;
type OnKeyframesResolved<T extends string | number> = (resolvedKeyframes: ResolvedKeyframes<T>, finalKeyframe: T) => void;
declare class KeyframeResolver<T extends string | number = any> {
name?: string;
element?: VisualElement<any>;
finalKeyframe?: T;
suspendedScrollY?: number;
protected unresolvedKeyframes: UnresolvedKeyframes<string | number>;
private motionValue?;
private onComplete;
/**
* Track whether this resolver has completed. Once complete, it never
* needs to attempt keyframe resolution again.
*/
private isComplete;
/**
* Track whether this resolver is async. If it is, it'll be added to the
* resolver queue and flushed in the next frame. Resolvers that aren't going
* to trigger read/write thrashing don't need to be async.
*/
private isAsync;
/**
* Track whether this resolver needs to perform a measurement
* to resolve its keyframes.
*/
needsMeasurement: boolean;
/**
* Track whether this resolver is currently scheduled to resolve
* to allow it to be cancelled and resumed externally.
*/
isScheduled: boolean;
constructor(unresolvedKeyframes: UnresolvedKeyframes<string | number>, onComplete: OnKeyframesResolved<T>, name?: string, motionValue?: MotionValue<T>, element?: VisualElement<any>, isAsync?: boolean);
scheduleResolve(): void;
readKeyframes(): void;
setFinalKeyframe(): void;
measureInitialState(): void;
renderEndStyles(): void;
measureEndState(): void;
complete(): void;
cancel(): void;
resume(): void;
}
/**
* A VisualElement is an imperative abstraction around UI elements such as
* HTMLElement, SVGElement, Three.Object3D etc.
*/
declare abstract class VisualElement<Instance = unknown, RenderState = unknown, Options extends {} = {}> {
/**
* VisualElements are arranged in trees mirroring that of the React tree.
* Each type of VisualElement has a unique name, to detect when we're crossing
* type boundaries within that tree.
*/
abstract type: string;
/**
* An `Array.sort` compatible function that will compare two Instances and
* compare their respective positions within the tree.
*/
abstract sortInstanceNodePosition(a: Instance, b: Instance): number;
/**
* Measure the viewport-relative bounding box of the Instance.
*/
abstract measureInstanceViewportBox(instance: Instance, props: MotionProps & Partial<MotionConfigContext>): Box;
/**
* When a value has been removed from all animation props we need to
* pick a target to animate back to. For instance, for HTMLElements
* we can look in the style prop.
*/
abstract getBaseTargetFromProps(props: MotionProps, key: string): string | number | undefined | MotionValue;
/**
* When we first animate to a value we need to animate it *from* a value.
* Often this have been specified via the initial prop but it might be
* that the value needs to be read from the Instance.
*/
abstract readValueFromInstance(instance: Instance, key: string, options: Options): string | number | null | undefined;
/**
* When a value has been removed from the VisualElement we use this to remove
* it from the inherting class' unique render state.
*/
abstract removeValueFromRenderState(key: string, renderState: RenderState): void;
/**
* Run before a React or VisualElement render, builds the latest motion
* values into an Instance-specific format. For example, HTMLVisualElement
* will use this step to build `style` and `var` values.
*/
abstract build(renderState: RenderState, latestValues: ResolvedValues, props: MotionProps): void;
/**
* Apply the built values to the Instance. For example, HTMLElements will have
* styles applied via `setProperty` and the style attribute, whereas SVGElements
* will have values applied to attributes.
*/
abstract renderInstance(instance: Instance, renderState: RenderState, styleProp?: MotionStyle, projection?: IProjectionNode): void;
/**
* This method is called when a transform property is bound to a motion value.
* It's currently used to measure SVG elements when a new transform property is bound.
*/
onBindTransform?(): void;
/**
* If the component child is provided as a motion value, handle subscriptions
* with the renderer-specific VisualElement.
*/
handleChildMotionValue?(): void;
/**
* This method takes React props and returns found MotionValues. For example, HTML
* MotionValues will be found within the style prop, whereas for Three.js within attribute arrays.
*
* This isn't an abstract method as it needs calling in the constructor, but it is
* intended to be one.
*/
scrapeMotionValuesFromProps(_props: MotionProps, _prevProps: MotionProps, _visualElement: VisualElement): {
[key: string]: MotionValue | string | number;
};
/**
* A reference to the current underlying Instance, e.g. a HTMLElement
* or Three.Mesh etc.
*/
current: Instance | null;
/**
* A reference to the parent VisualElement (if exists).
*/
parent: VisualElement | undefined;
/**
* A set containing references to this VisualElement's children.
*/
children: Set<VisualElement<unknown, unknown, {}>>;
/**
* The depth of this VisualElement within the overall VisualElement tree.
*/
depth: number;
/**
* The current render state of this VisualElement. Defined by inherting VisualElements.
*/
renderState: RenderState;
/**
* An object containing the latest static values for each of this VisualElement's
* MotionValues.
*/
latestValues: ResolvedValues;
/**
* Determine what role this visual element should take in the variant tree.
*/
isVariantNode: boolean;
isControllingVariants: boolean;
/**
* If this component is part of the variant tree, it should track
* any children that are also part of the tree. This is essentially
* a shadow tree to simplify logic around how to stagger over children.
*/
variantChildren?: Set<VisualElement>;
/**
* Decides whether this VisualElement should animate in reduced motion
* mode.
*
* TODO: This is currently set on every individual VisualElement but feels
* like it could be set globally.
*/
shouldReduceMotion: boolean | null;
/**
* Normally, if a component is controlled by a parent's variants, it can
* rely on that ancestor to trigger animations further down the tree.
* However, if a component is created after its parent is mounted, the parent
* won't trigger that mount animation so the child needs to.
*
* TODO: This might be better replaced with a method isParentMounted
*/
manuallyAnimateOnMount: boolean;
/**
* This can be set by AnimatePresence to force components that mount
* at the same time as it to mount as if they have initial={false} set.
*/
blockInitialAnimation: boolean;
/**
* A reference to this VisualElement's projection node, used in layout animations.
*/
projection?: IProjectionNode;
/**
* A map of all motion values attached to this visual element. Motion
* values are source of truth for any given animated value. A motion
* value might be provided externally by the component via props.
*/
values: Map<string, MotionValue<any>>;
/**
* The AnimationState, this is hydrated by the animation Feature.
*/
animationState?: AnimationState;
KeyframeResolver: typeof KeyframeResolver;
/**
* The options used to create this VisualElement. The Options type is defined
* by the inheriting VisualElement and is passed straight through to the render functions.
*/
readonly options: Options;
/**
* A reference to the latest props provided to the VisualElement's host React component.
*/
props: MotionProps;
prevProps?: MotionProps;
presenceContext: PresenceContextProps | null;
prevPresenceContext?: PresenceContextProps | null;
/**
* Cleanup functions for active features (hover/tap/exit etc)
*/
private features;
/**
* A map of every subscription that binds the provided or generated
* motion values onChange listeners to this visual element.
*/
private valueSubscriptions;
/**
* A reference to the ReducedMotionConfig passed to the VisualElement's host React component.
*/
private reducedMotionConfig;
/**
* On mount, this will be hydrated with a callback to disconnect
* this visual element from its parent on unmount.
*/
private removeFromVariantTree;
/**
* A reference to the previously-provided motion values as returned
* from scrapeMotionValuesFromProps. We use the keys in here to determine
* if any motion values need to be removed after props are updated.
*/
private prevMotionValues;
/**
* When values are removed from all animation props we need to search
* for a fallback value to animate to. These values are tracked in baseTarget.
*/
private baseTarget;
/**
* Create an object of the values we initially animated from (if initial prop present).
*/
private initialValues;
/**
* An object containing a SubscriptionManager for each active event.
*/
private events;
/**
* An object containing an unsubscribe function for each prop event subscription.
* For example, every "Update" event can have multiple subscribers via
* VisualElement.on(), but only one of those can be defined via the onUpdate prop.
*/
private propEventSubscriptions;
private onUpdate?;
constructor({ parent, props, presenceContext, reducedMotionConfig, blockInitialAnimation, visualState, }: VisualElementOptions<Instance, RenderState>, options?: Options);
mount(instance: Instance): void;
unmount(): void;
private bindToMotionValue;
sortNodePosition(other: VisualElement<Instance>): number;
updateFeatures(): void;
notifyUpdate: () => void;
triggerBuild(): void;
render: () => void;
private renderScheduledAt;
scheduleRender: () => void;
/**
* Measure the current viewport box with or without transforms.
* Only measures axis-aligned boxes, rotate and skew must be manually
* removed with a re-render to work.
*/
measureViewportBox(): Box;
getStaticValue(key: string): string | number;
setStaticValue(key: string, value: string | number): void;
/**
* Update the provided props. Ensure any newly-added motion values are
* added to our map, old ones removed, and listeners updated.
*/
update(props: MotionProps, presenceContext: PresenceContextProps | null): void;
getProps(): MotionProps;
/**
* Returns the variant definition with a given name.
*/
getVariant(name: string): Variant | undefined;
/**
* Returns the defined default transition on this component.
*/
getDefaultTransition(): Transition | undefined;
getTransformPagePoint(): any;
getClosestVariantNode(): VisualElement | undefined;
/**
* Add a child visual element to our set of children.
*/
addVariantChild(child: VisualElement): (() => boolean) | undefined;
/**
* Add a motion value and bind it to this visual element.
*/
addValue(key: string, value: MotionValue): void;
/**
* Remove a motion value and unbind any active subscriptions.
*/
removeValue(key: string): void;
/**
* Check whether we have a motion value for this key
*/
hasValue(key: string): boolean;
/**
* Get a motion value for this key. If called with a default
* value, we'll create one if none exists.
*/
getValue(key: string): MotionValue | undefined;
getValue(key: string, defaultValue: string | number | null): MotionValue;
/**
* If we're trying to animate to a previously unencountered value,
* we need to check for it in our state and as a last resort read it
* directly from the instance (which might have performance implications).
*/
readValue(key: string, target?: string | number | null): any;
/**
* Set the base target to later animate back to. Thi