Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Direction in `framer-motion`

How we can reverse direction of framer-motion implemented animation?

This is My Component, and This change background of box element from #000 to #ff0 :

import React from 'react';
import { motion } from 'framer-motion';
import type { Variants } from 'framer-motion';

const variants: Variants = {
    initial: { background: '#000' },
    animate: { background: '#ff0' },
};

export function Box(props: Props = { isMenuActive: false }) {
    return (
        <motion.div initial='initial' animate='animate' variants={variants}>
            Hi
        </motion.div>
    );
}

type Props = {
    isMenuActive: boolean;
};

BUT

direction of this animation is from initial to animate namespace, but i want if props.isMenuActive was true, then, animation execute from animate to initial namespace, exactly direction of animation reversed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example

In anime.js library, we can use this snippets to reverse direction : documention of anime.js

NOTE : anime.js didn’t support jsx and react

>Solution :

am not sure if framer-motion has a reverse function but you can do it yourself with this logic:

 initial: { background: isMenuActive ? '#000' : '#ff0' },
    animate: { background: isMenuActive ? '#ff0' : '#000' },

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading