question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Introduce useTransition for custom animations

See original GitHub issue

I would like to create a custom animation with my own transition setup that is not a spring transition (e.g. tween or keyframes based).

Currently useSpring exposes a way of creating a MotionValue that can transition to a new value when set; however, there is no way to create a MotionValue that uses a different transition configuration. The use cases are the same for the useSpring hook (just using a different transition).

I could be wrong but I don’t think useMotionValue does this right now, it’s more of a tracking variable for animations that are occurring.

I’d imagine this would work like useSpring except it would allow for any transition to be specified – useSpring could then just be derived from useTransition. An alternative could be to expose useTween and useKeyframes, but that feels like it would get annoying. Something like:

// Simple transition
const x = useTransition(0, { duration: 1, delay: 0.5 });
x.set(500); // x transitions to 500 and takes 1s after a 0.5s delay

// Spring transition
const x = useTransition(0, { type: 'spring', ...}); 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

4reactions
mattgperrycommented, Jul 22, 2019

Hi @BenMagyar. I sort-of see the use-case. useSpring is uniquely useful in how it integrates existing velocities ie https://codesandbox.io/s/framer-motion-usespring-6s2gr.

The problem with the useSpring API in general is you’re bound to React’s render lifecycle when you want to change the transition. It might be more likely that we offer an imperative animate function ie

const x = useMotionValue(0)

animate(x, 100, transition)

That could conceivably animate one or more values outside of React’s lifecycle.

But as we already have ways of declaratively and imperatively animating components I probably won’t press on this until there’s a way of using this with something other than the DOM, ie a hook that could be used with the canvas.

0reactions
RuudBurgercommented, Jan 23, 2021

I was searching for a similar options, so I created a hook that works like useSpring does, but for all transitions:

const useTransition = <T>(source: MotionValue<T>, config?: {}) => {
  const animatedMotionValue = useMotionValue(source.get());

  useEffect(() => {
    let animation: undefined | ReturnType<typeof animate>;

    return source.onChange((newValue: T) => {
      animation?.stop(); // Don't know if this is needed
      animation = animate(animatedMotionValue, newValue, config);
    });
  }, [animatedMotionValue, config, source]);

  return animatedMotionValue;
};

const MyComponent = () => {
  const x = useMotionValue(0);
  const animatedX = useMotionValue(x, {});

  useEffect(() => {
    setTimeout(() => {
      x.set(100);
    }, 1000)
  },[x]);

  return <motion.div style={{ x: animatedX }} />
}

I assume you can use spring aswel, because it just uses the animate function from framer-motion

Read more comments on GitHub >

github_iconTop Results From Across the Web

Concurrent UI Patterns (Experimental)
React offers a new built-in useTransition() Hook to help with this. We can use it in three steps. First, we'll make sure that...
Read more >
ReactJS startTransition and useTransition (React ... - YouTube
React 18 comes with new features called useTransition and startTransition which give you fine control over specifying a low priority update.
Read more >
Create a custom transition animation
A custom transition lets you create an animation that is not available from any of the built-in transition classes. For example, you can...
Read more >
Using React Spring for animation: Context and examples
useTransition — For mount/unmount transitions (lists where items are added/removed/updated); useChain — To queue or chain multiple animations ...
Read more >
Animation transitions and triggers
Predefined states and wildcard matchinglink. In Angular, transition states can be defined explicitly through the state() function, or using the predefined * ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found