### Basic useVelocity Example Source: https://motion.dev/docs/react-use-velocity This example demonstrates how to use useVelocity to get the velocity of a motion value and then use that velocity to transform the scale of a motion component. It requires importing useMotionValue and useVelocity. ```jsx const x = useMotionValue(0) const xVelocity = useVelocity(x) const scale = useTransform( xVelocity, [-3000, 0, 3000], [2, 1, 2], { clamp: false } ) return ``` -------------------------------- ### Install Motion+ Package Source: https://motion.dev/docs/react-scramble-text Install the motion-plus package using npm. Ensure you have your Motion+ private token to access the registry. ```bash npm install "https://api.motion.dev/registry.tgz?package=motion-plus&version=2.6.1&token=YOUR_AUTH_TOKEN" ``` -------------------------------- ### Install Motion with npm Source: https://motion.dev/docs/react-installation Use npm to install the Motion package in your React project. ```bash npm install motion ``` -------------------------------- ### Install Motion with pnpm Source: https://motion.dev/docs/react-installation Use pnpm to install the Motion package in your React project. ```bash pnpm add motion ``` -------------------------------- ### Full Example with State and startTransition Source: https://motion.dev/docs/react-animate-view A complete React component example showing state management with useState and triggering animations using startTransition and AnimateView. ```jsx import { AnimateView } from "motion-plus/animate-view" import { startTransition, useState } from "react" function Example() { const [show, setShow] = useState(true) return ( <> {show && (
)} ) } ``` -------------------------------- ### Install Motion Plus Package Source: https://motion.dev/docs/react-typewriter Install the motion-plus package using npm with your private authentication token. Ensure you are a Motion+ member to obtain a token. ```bash npm install "https://api.motion.dev/registry.tgz?package=motion-plus&version=2.8.0&token=YOUR_AUTH_TOKEN" ``` -------------------------------- ### Install Motion+ Ticker Source: https://motion.dev/docs/react-ticker Install the motion-plus package using npm with your private authentication token. Ensure you are a Motion+ member to obtain a token. ```bash npm install "https://api.motion.dev/registry.tgz?package=motion-plus&version=2.0.2&token=YOUR_AUTH_TOKEN" ``` -------------------------------- ### Install Carousel Component Source: https://motion.dev/docs/react-carousel Install the motion-plus package using npm. Ensure you have a Motion+ membership to generate a private token. ```bash npm install "https://api.motion.dev/registry.tgz?package=motion-plus&version=2.11.0&token=YOUR_AUTH_TOKEN" ``` -------------------------------- ### Install Motion+ Package Source: https://motion.dev/docs/react-animate-number Install the motion-plus package for React using npm. Ensure you replace YOUR_AUTH_TOKEN with your actual Motion+ private token. ```bash npm install "https://api.motion.dev/registry.tgz?package=motion-plus&version=2.10.0&token=YOUR_AUTH_TOKEN" ``` -------------------------------- ### Uninstall framer-motion and install motion Source: https://motion.dev/docs/react-upgrade-guide To upgrade to Motion for React, uninstall the old package and install the new one. ```bash npm uninstall framer-motion npm install motion ``` -------------------------------- ### Install Motion with Yarn Source: https://motion.dev/docs/react-installation Use Yarn to add the Motion package to your React project. ```bash yarn add motion ``` -------------------------------- ### Import Motion components in React Source: https://motion.dev/docs/react-installation After installation, import Motion's components and hooks from 'motion/react'. ```javascript import { motion } from "motion/react" ``` -------------------------------- ### useTransform clamp option example Source: https://motion.dev/docs/react-use-transform Compares the behavior of useTransform with clamp set to true (default) and false, showing how output is constrained or continues to map outside the input range. ```javascript const y = useTransform(x, [0, 1], [0, 2]) const z = useTransform(x, [0, 1], [0, 2], { clamp: false }) useEffect(() => { x.set(2) console.log(y.get()) // 2, input clamped console.log(z.get()) // 4 }) ``` -------------------------------- ### Basic Reduced Motion Check Source: https://motion.dev/docs/react-use-reduced-motion A simple example demonstrating how to get the boolean value from the useReducedMotion hook. ```javascript const shouldReduceMotion = useReducedMotion() ``` -------------------------------- ### Using useVelocity with useMotionValueEvent Source: https://motion.dev/docs/react-use-velocity This example shows how to use useVelocity in conjunction with useMotionValueEvent to log the velocity of a motion value whenever it changes. It requires importing useMotionValue, useVelocity, and useMotionValueEvent. ```jsx import { useMotionValue, useVelocity } from "framer-motion" function Component() { const x = useMotionValue(0) const xVelocity = useVelocity(x) useMotionValueEvent(xVelocity, "change", latest => { console.log("Velocity", latestVelocity) }) return } ``` -------------------------------- ### CSS Transition Example Source: https://motion.dev/docs/react-motion-config This CSS demonstrates a transition for the 'scale' property, with a custom ease curve defined using linear interpolation. It's used in conjunction with Motion for visual editing. ```css 1.card { transition: scale 200ms linear(3 0, 0.009, 0.036, 0.084, 0.157, 0.255, 0.378, 0.522, 0.679, 0.832, 0.954, 1.029, 1.052, 1.038, 1.011, 0.99, 0.984, 0.991, 1.001, 1.005, 1 ); } .card:hover { scale: 1.2; } ``` -------------------------------- ### Layout Animation with Initial Border Radius Source: https://motion.dev/docs/react-upgrade-guide Example of using `layout` prop with `initial` to animate `borderRadius` in Framer Motion 2.0, fixing potential distortions. ```javascript ``` -------------------------------- ### Transform function example with time-based animation Source: https://motion.dev/docs/react-use-transform Creates a motion value 'y' that animates based on the sine wave of time, demonstrating dynamic transformations. ```javascript const distance = 100 const time = useTime() const y = useTransform(() => Math.sin(time.get() / 1000) * distance) ``` -------------------------------- ### Get Motion Value State Source: https://motion.dev/docs/react-motion-value Illustrates how to retrieve the current state of a motion value using the `get` method. ```javascript x.get() // 100 ``` -------------------------------- ### Set Motion Value to Color Source: https://motion.dev/docs/react-motion-value Example of using the `set` method to update a motion value with a string value, such as a color. ```javascript x.set("#f00") ``` -------------------------------- ### useInView with Margin Option Source: https://motion.dev/docs/react-use-in-view This example demonstrates the use of the 'margin' option to expand or shrink the detection area around the viewport. It allows for more precise control over when an element is considered 'in view'. ```jsx const isInView = useInView(ref, { margin: "0px 100px -50px 0px" }) ``` -------------------------------- ### Dynamic Enter Animation with Transition Types Source: https://motion.dev/docs/react-animate-view An example of a dynamic enter animation for AnimateView that resolves based on the provided transition types, allowing for conditional transforms. ```jsx ({ transform: [ `translateX(${types.includes("next") ? 100 : -100}%)`, "none", ], })}> ``` -------------------------------- ### Dynamic Enter and Exit Animations based on Transition Types Source: https://motion.dev/docs/react-animate-view This example shows how to define dynamic enter and exit animations for AnimateView that adapt based on the transition types provided by addTransitionType. ```jsx ({ transform: `translateX(${types.includes("prev") ? 100 : -100}%)`, })} enter={(types) => ({ transform: [ `translateX(${types.includes("next") ? 100 : -100}%)`, "translateX(0%)", ], })}> ``` -------------------------------- ### Mapping single motion value to multiple named output values Source: https://motion.dev/docs/react-use-transform An alternative to the previous example, this maps a single motion value to multiple named output properties (opacity, scale, filter) in one call. ```javascript const { opacity, scale, filter } = useTransform(offset, [100, 600], { opacity: [1, 0.4], scale: [1, 0.6], filter: ["blur(0px)", "blur(10px)"], }) ``` -------------------------------- ### Start Drag from Another Element Source: https://motion.dev/docs/react-use-drag-controls Initiate a drag gesture from a separate element's pointer down event using the controls. ```javascript
controls.start(event)} /> ``` -------------------------------- ### Compose Motion Values with useTransform Source: https://motion.dev/docs/react-motion-value Example of composing motion values where a new motion value `y` is derived from the current value of `x` multiplied by two. ```javascript const y = useTransform(() => x.get() * 2) ``` -------------------------------- ### Motion Value Methods Source: https://motion.dev/docs/react-motion-value Provides methods to interact with motion values, including getting their state and velocity, setting new states, and managing animations. ```APIDOC ## Motion Value API ### `get()` Returns the latest state of the motion value. ### `getVelocity()` Returns the latest velocity of the motion value. Returns `0` if the value is non-numerical. ### `set(state)` Sets the motion value to a new state. This updates the DOM without triggering a React re-render. ```javascript x.set(100) ``` ### `jump(state)` Jumps the motion value to a new state, resetting velocity and ending active animations. ```javascript x.jump(10) ``` ### `isAnimating()` Returns `true` if the value is currently animating. ### `stop()` Stops the active animation of the motion value. ### `on(event, listener)` Subscribes to motion value events. Available events are: `change`, `animationStart`, `animationCancel`, `animationComplete`. Returns a function that unsubscribes the listener. ```javascript const unsubscribe = x.on("change", latest => console.log(latest)) ``` ``` -------------------------------- ### Lightweight Hover Gesture Recognition with hover() Source: https://motion.dev/docs/react-hover-animation For efficient hover gesture handling, import the `hover()` function from 'motion'. This function can be integrated with `useEffect` to manage hover start and end callbacks, returning a cleanup function for proper unsubscription. ```javascript import { hover } from "motion" import { useRef, useEffect } from "react" function Component() { const ref = useRef(null) useEffect(() => { return hover(ref.current, () => { console.log("on hover start") return () => console.log("on hover end") }) }, []) return