0Pricing
React Academy · Lesson

Variants for Orchestrated Animations

Define named variant objects and propagate them through parent-child component trees.

Variants for Orchestrated Animations is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Variants?

Variants are named animation states defined as objects. Instead of inline values, components reference variant names like 'hidden' and 'visible'. Parent components can orchestrate children automatically.

Defining Variants

Create a variants object with named states. Pass it to the variants prop on a motion component.

const cardVariants = {
  hidden: { opacity: 0, y: 30 },
  visible: { opacity: 1, y: 0 },
};

<motion.div
  variants={cardVariants}
  initial="hidden"
  animate="visible"
  transition={{ duration: 0.4 }}
/>

Staggered Children

Use staggerChildren in a parent's transition to animate children one after another with a delay between each.

const containerVariants = {
  hidden: {},
  visible: {
    transition: {
      staggerChildren: 0.1,
      delayChildren: 0.2,
    },
  },
};

const itemVariants = {
  hidden: { opacity: 0, x: -20 },
  visible: { opacity: 1, x: 0 },
};

<motion.ul variants={containerVariants} initial="hidden" animate="visible">
  {items.map(item => (
    <motion.li key={item.id} variants={itemVariants}>{item.name}</motion.li>
  ))}
</motion.ul>

Variant Propagation

When a parent's variant state changes, all children that share the same variant name automatically animate too — no need to pass animate to each child.

// Children inherit animate="visible" from the parent automatically
const parent = { hidden: {}, visible: { transition: { staggerChildren: 0.05 } } };
const child = { hidden: { opacity: 0 }, visible: { opacity: 1 } };

<motion.div variants={parent} initial="hidden" animate="visible">
  <motion.p variants={child}>First</motion.p>
  <motion.p variants={child}>Second</motion.p>
  <motion.p variants={child}>Third</motion.p>
</motion.div>

Toggling Between States

Control which variant is active with a state variable. Framer Motion transitions smoothly between named states.

const [isOpen, setIsOpen] = useState(false);

const panelVariants = {
  closed: { height: 0, opacity: 0 },
  open: { height: 'auto', opacity: 1 },
};

<motion.div
  variants={panelVariants}
  animate={isOpen ? 'open' : 'closed'}
  initial="closed"
  transition={{ duration: 0.3 }}
/>

Custom Transition per Variant

Add a transition key inside a variant to customize the timing for that specific state only.

const menuVariants = {
  closed: {
    scaleY: 0,
    transition: { duration: 0.2, ease: 'easeIn' },
  },
  open: {
    scaleY: 1,
    transition: { duration: 0.3, ease: 'easeOut', staggerChildren: 0.05 },
  },
};

Exit Variants with AnimatePresence

Add an exit key to your variants object and wrap the component in AnimatePresence to animate on unmount.

const pageVariants = {
  initial: { opacity: 0, x: '100%' },
  enter: { opacity: 1, x: 0 },
  exit: { opacity: 0, x: '-100%' },
};

<AnimatePresence mode="wait">
  <motion.div
    key={router.pathname}
    variants={pageVariants}
    initial="initial"
    animate="enter"
    exit="exit"
    transition={{ duration: 0.3 }}
  />
</AnimatePresence>

Nested Variant Overrides

Children can override the inherited variant state with their own initial or animate props, breaking out of parent orchestration when needed.

useAnimation for Imperative Variant Control

useAnimation() returns an animation controls object. Call controls.start('variantName') programmatically to trigger variants.

const controls = useAnimation();

async function sequence() {
  await controls.start('wiggle');
  await controls.start('bounce');
}

<motion.div animate={controls} variants={{ wiggle: { rotate: 10 }, bounce: { y: -20 } }} />

Orchestration Options

Beyond staggerChildren, you can use when ('beforeChildren' | 'afterChildren') to sequence parent and child animations.

const container = {
  hidden: { opacity: 0 },
  visible: {
    opacity: 1,
    transition: {
      when: 'beforeChildren', // parent fades in before stagger starts
      staggerChildren: 0.1,
    },
  },
};

Reusable Variant Libraries

Define variants in a shared file and import them where needed to keep animation configs consistent across the app.

// lib/variants.ts
export const fadeIn = { hidden: { opacity: 0 }, visible: { opacity: 1 } };
export const slideUp = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0 } };

// Usage:
<motion.div variants={slideUp} initial="hidden" animate="visible" />

Quick Check

What Framer Motion transition option delays child animations one after another?

Recap

Variants are named states defined as objects. Set staggerChildren in a parent's transition to cascade animations through children automatically. Use useAnimation() for imperative control and AnimatePresence with exit variants for unmount animations.

Frequently asked questions

Is the “Variants for Orchestrated Animations” lesson free?

Yes — the full text of “Variants for Orchestrated Animations” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Variants for Orchestrated Animations”?

Define named variant objects and propagate them through parent-child component trees. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Variants for Orchestrated Animations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Motion Components & Basic Animations
  2. Variants for Orchestrated Animations
  3. Gesture Animations: Hover, Tap & Drag
  4. Layout Animations & AnimatePresence
← Back to React Academy