Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 51 additions & 49 deletions dev/inc/size-budgets.json
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
{
"animate": 27800,
"animate": 28450,
"mini-animate": 3250,
"animate-sequence": 27800,
"animate-sequence": 28450,
"scroll": 3850,
"scroll+animate": 30450,
"scroll+animate": 31050,
"inView": 500,
"resize": 700,
"hover+press": 2450,
"drag": 27350,
"pinch": 9400,
"motionValue": 1900,
"styleEffect": 4500,
"drag": 27100,
"pinch": 9450,
"motionValue": 1950,
"styleEffect": 4550,
"animateView": 6400,
"stagger": 850,
"frame": 750,
"mix": 2100,
"vanilla-all": 69500,
"motion-div": 34350,
"motion-div (all chunks)": 51850,
"motion-create": 34350,
"motion-create (all chunks)": 51900,
"m-div": 5600,
"m-div(via motion/react)": 5750,
"m+LazyMotion+domAnimation": 32550,
"m+LazyMotion+domMax": 48800,
"m+LazyMotion+domDrag": 37300,
"m+LazyMotion+domLayout": 44900,
"LazyMotion-only": 650,
"domAnimation-only": 29950,
"domMax-only": 46000,
"AnimatePresence": 3150,
"mix": 2150,
"vanilla-all": 70850,
"motion-div": 33900,
"motion-div (all chunks)": 52000,
"motion-create": 33900,
"motion-create (all chunks)": 52000,
"motion-div(sync)": 48950,
"m-div": 5650,
"m-div(via motion/react)": 5800,
"m+LazyMotion+domAnimation": 32700,
"m+LazyMotion+domMax": 49150,
"m+LazyMotion+domDrag": 37400,
"m+LazyMotion+domLayout": 45100,
"LazyMotion-only": 700,
"domAnimation-only": 29700,
"domMax-only": 46100,
"AnimatePresence": 3950,
"MotionConfig": 400,
"LayoutGroup": 1200,
"Reorder": 43500,
"Reorder (all chunks)": 56050,
"Reorder(m)+LazyMotion": 14200,
"Reorder": 38450,
"Reorder (all chunks)": 56550,
"Reorder(m)+LazyMotion": 14400,
"useMotionValue": 2100,
"useTransform": 4800,
"useMotionTemplate": 2250,
"useMotionTemplate": 2300,
"useSpring": 5050,
"useScroll": 6050,
"useScroll": 6100,
"useInView": 650,
"useAnimate": 28100,
"useAnimate": 28750,
"useAnimate(mini)": 3350,
"useMotionValueEvent": 150,
"useReducedMotion": 300,
"useAnimationFrame": 900,
"useVelocity": 2150,
"useAnimationControls": 19900,
"react-all": 86150,
"motion-div+AnimatePresence": 36250,
"motion-div+AnimatePresence (all chunks)": 53800,
"motion-div+useAnimate": 40050,
"motion-div+useAnimate (all chunks)": 57500,
"motion-div+animate": 39950,
"motion-div+animate (all chunks)": 57400,
"motion-div+useInView": 34500,
"motion-div+useInView (all chunks)": 52050,
"motion-div+useScroll": 37700,
"motion-div+useScroll (all chunks)": 55200,
"motion-div+useTransform+useMotionTemplate": 34900,
"motion-div+useTransform+useMotionTemplate (all chunks)": 52400,
"motion-div+useScroll+useTransform+useSpring": 39100,
"motion-div+useScroll+useTransform+useSpring (all chunks)": 56600,
"motion-div+Reorder": 43550,
"motion-div+Reorder (all chunks)": 56100,
"domMax+useScroll": 49350
"useVelocity": 2200,
"useAnimationControls": 19600,
"react-all": 88100,
"react-all (all chunks)": 88250,
"motion-div+AnimatePresence": 36100,
"motion-div+AnimatePresence (all chunks)": 54200,
"motion-div+useAnimate": 40600,
"motion-div+useAnimate (all chunks)": 58700,
"motion-div+animate": 40500,
"motion-div+animate (all chunks)": 58550,
"motion-div+useInView": 34050,
"motion-div+useInView (all chunks)": 52150,
"motion-div+useScroll": 37300,
"motion-div+useScroll (all chunks)": 55350,
"motion-div+useTransform+useMotionTemplate": 34450,
"motion-div+useTransform+useMotionTemplate (all chunks)": 52550,
"motion-div+useScroll+useTransform+useSpring": 38700,
"motion-div+useScroll+useTransform+useSpring (all chunks)": 56800,
"motion-div+Reorder": 38500,
"motion-div+Reorder (all chunks)": 56600,
"domMax+useScroll": 49550
}
7 changes: 6 additions & 1 deletion dev/inc/size-scenarios.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export const scenarios = {
// React components
"motion-div": `import { motion } from "motion/react"; export const D = motion.div`,
"motion-create": `import { motion } from "motion/react"; export const D = motion.create("div")`,
// For builds that write one file: compare the esbuild column with motion-div's
"motion-div(sync)": `import { motion } from "motion/react-sync"; export const D = motion.div`,
"m-div": `export { div } from "motion/react-m"`,
"m-div(via motion/react)": `import { m } from "motion/react"; export const D = m.div`,
"m+LazyMotion+domAnimation": `export { m, LazyMotion, domAnimation } from "motion/react"`,
Expand Down Expand Up @@ -95,7 +97,9 @@ export const scenarios = {
useAnimationFrame: `export { useAnimationFrame } from "motion/react"`,
useVelocity: `export { useVelocity } from "motion/react"`,
useAnimationControls: `export { useAnimationControls } from "motion/react"`,
"react-all": `export * from "motion/react"`,
// Not `export *`: with a fetched chunk that imports from the first load,
// Rollup would emit the entry as a second file that repeats every export.
"react-all": `import * as all from "motion/react"; export { all }`,

// Combinations (marginal cost of mixing APIs in one app)
"motion-div+AnimatePresence": combo("AnimatePresence"),
Expand Down Expand Up @@ -224,6 +228,7 @@ async function warmCache() {
"motion/mini",
"motion/react",
"motion/react-m",
"motion/react-sync",
"motion/react-mini",
]
.map((entry) => `import "${entry}"`)
Expand Down
4 changes: 2 additions & 2 deletions packages/framer-motion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
},
{
"path": "./dist/size-rollup-animate.js",
"maxSize": "26.95 kB"
"maxSize": "27.55 kB"
},
{
"path": "./dist/size-rollup-scroll.js",
Expand All @@ -180,7 +180,7 @@
},
{
"path": "./dist/size-rollup-remix.js",
"maxSize": "52.8 kB"
"maxSize": "53.65 kB"
}
],
"gitHead": "78f3ff51b5508f7e8c1c283d8e80bcd26fec37d7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ const frames = async (count: number) => {
/**
* A value waiting out its delay holds its first keyframe. Once it's set
* there, the frames of the delay don't set it again.
*
* The delays that tests read a value in are far longer than their frames
* take, as a busy machine can spend half a second on five frames.
*/
describe("values in their delay", () => {
test("aren't set again on every frame of the delay", async () => {
const x = motionValue(0)
const set = jest.spyOn(x, "set")
animate(x, [0, 100], { delay: 0.5, duration: 0.1, ease: "linear" })
const animation = animate(x, [0, 100], {
delay: 10,
duration: 0.1,
ease: "linear",
})
await frames(5)

expect(set.mock.calls.length).toBeLessThanOrEqual(1)
expect(x.get()).toBe(0)

animation.stop()
})

test("still start moving when the delay ends", async () => {
Expand All @@ -40,11 +49,13 @@ describe("values in their delay", () => {

test("go back to their first keyframe after an outside write", async () => {
const x = motionValue(0)
animate(x, [0, 100], { delay: 0.5, duration: 0.1 })
const animation = animate(x, [0, 100], { delay: 10, duration: 0.1 })
await frames(2)
x.set(50)
await frames(2)

expect(x.get()).toBe(0)

animation.stop()
})
})
2 changes: 2 additions & 0 deletions packages/framer-motion/src/animation/timeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GroupAnimationWithThen,
MotionValue,
motionValue,
supportPlayback,
supportTimelines,
TimelineWithFallback,
} from "motion-dom"
Expand Down Expand Up @@ -211,6 +212,7 @@ export class Timeline implements AnimationPlaybackControlsWithThen {
private options: TimelineOptions = {},
animations?: AnimationPlaybackControlsWithThen[]
) {
supportPlayback()
this.isPaused = options.autoplay === false
this.newFinished()
items.forEach((item) => this.add(item))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { VisualElement } from "motion-dom"
import { VariantLabels } from "../../motion/types"
import { useConstant } from "../../utils/use-constant"
import { useIsomorphicLayoutEffect } from "../../utils/use-isomorphic-effect"
import { loadExitFeature } from "../../motion/features/animation/load-exit"
import { PopChild } from "./PopChild"

interface PresenceChildProps {
Expand Down Expand Up @@ -37,6 +38,13 @@ export const PresenceChild = ({
anchorY,
root
}: PresenceChildProps) => {
/**
* Motion components only bundle the feature that runs `exit` animations
* when they bundle all their features. The rest get it here, before
* any of them renders inside this component.
*/
loadExitFeature()

const presenceChildren = useConstant(newChildrenMap)
const id = useId()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { act } from "react"
import { AnimatePresence, LazyMotion, m } from "../../.."
import { render } from "../../../jest.setup"
import { gestureAnimations } from "../../../motion/features/gestures"
import { createDomVisualElement } from "../../../render/dom/create-visual-element"

const settle = async () => {
for (let i = 0; i < 5; i++) {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
})
}
}

/**
* AnimatePresence brings the feature that runs `exit` animations, so a
* component can have it without the feature that animates.
*/
describe("exit of a component with no animation feature", () => {
test("is removed at once", async () => {
const features = { renderer: createDomVisualElement, ...gestureAnimations }

const Component = ({ isVisible }: { isVisible: boolean }) => (
<LazyMotion features={features}>
<AnimatePresence>
{isVisible && <m.div id="exits" exit={{ opacity: 0 }} />}
</AnimatePresence>
</LazyMotion>
)

const { container, rerender } = render(<Component isVisible />)
await settle()

rerender(<Component isVisible={false} />)
await settle()

expect(container.querySelector("#exits")).toBeNull()
})
})
16 changes: 16 additions & 0 deletions packages/framer-motion/src/components/AnimatePresence/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PresenceContext as Context } from "../../context/PresenceContext"
import { loadExitFeature } from "../../motion/features/animation/load-exit"

/**
* PresenceContext as apps import it. A component that provides it stands
* in for AnimatePresence, so the feature that runs `exit` animations loads
* with it, as it does with AnimatePresence.
*
* Motion's own code reads the context from context/PresenceContext, which
* loads nothing.
*
* @public
*/
export const PresenceContext = /* @__PURE__ */ (() => (
loadExitFeature(), Context
))()
2 changes: 1 addition & 1 deletion packages/framer-motion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export { startOptimizedAppearAnimation } from "./animation/optimized-appear/star
export { LayoutGroupContext } from "./context/LayoutGroupContext"
export { MotionConfigContext } from "./context/MotionConfigContext"
export { MotionContext } from "./context/MotionContext"
export { PresenceContext } from "./context/PresenceContext"
export { PresenceContext } from "./components/AnimatePresence/context"
export { SwitchLayoutGroupContext } from "./context/SwitchLayoutGroupContext"

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { act, useId, useMemo } from "react"
import { PresenceContext } from "../../components/AnimatePresence/context"
import { render } from "../../jest.setup"
import { motion } from "../../render/components/motion-async/proxy"

const settle = async (ms = 0) => {
for (let i = 0; i < 5; i++) {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, ms))
})
}
}

/**
* This file doesn't import AnimatePresence, as an app with its own
* presence component might not.
*/
describe("a component that provides PresenceContext itself", () => {
test("gets exit animations from motion components that fetch features", async () => {
const onExitComplete = jest.fn()
const register = jest.fn(() => () => {})

const Presence = ({ isPresent }: { isPresent: boolean }) => {
const id = useId()
const context = useMemo(
() => ({
id,
isPresent,
onExitComplete,
register,
}),
[isPresent]
)

return (
<PresenceContext.Provider value={context}>
<motion.div
id="exits"
exit={{ opacity: 0 }}
transition={{ duration: 0.05 }}
/>
</PresenceContext.Provider>
)
}

const { container, rerender } = render(<Presence isPresent />)
await settle()

expect(register).toHaveBeenCalled()

rerender(<Presence isPresent={false} />)
await settle(40)

const element = container.querySelector("#exits") as HTMLElement
expect(element.style.opacity).toBe("0")
expect(onExitComplete).toHaveBeenCalledTimes(1)
})
})
Loading