Direction-aware transition
Also called directional transition or directional navigation
A transition that slides new content in from the side you are heading toward, so going forward and going back move in opposite directions.
import { useState } from 'react';
import { AnimatePresence, motion, useReducedMotion } from 'motion/react';
const steps = [
{ title: 'Create your account', text: 'Start with your work email.' },
{ title: 'Name your workspace', text: 'Your teammates will see this name.' },
{ title: 'Invite your team', text: 'Add people now or any time later.' }
];
const DISTANCE = 40;
// direction is 1 going forward and -1 going back.
const slide = {
enter: (direction: number) => ({ x: direction * DISTANCE, opacity: 0 }),
center: { x: 0, opacity: 1 },
exit: (direction: number) => ({ x: direction * -DISTANCE, opacity: 0 })
};
export function SignupSteps() {
const [[step, direction], setStep] = useState([0, 1]);
const reduceMotion = useReducedMotion();
// Later steps sit on the left in right-to-left layouts; reduced motion keeps only the fade.
const rtl = document.documentElement.dir === 'rtl';
const custom = reduceMotion ? 0 : rtl ? -direction : direction;
const current = steps[step]!;
const last = step === steps.length - 1;
const go = (next: number) => setStep([next, next > step ? 1 : -1]);
return (
<div className='relative w-80 overflow-hidden rounded-2xl border bg-background p-6'>
{/* The leaving step needs the new direction too, so pass it to AnimatePresence. */}
<AnimatePresence mode='popLayout' initial={false} custom={custom}>
<motion.div
key={step}
custom={custom}
variants={slide}
initial='enter'
animate='center'
exit='exit'
transition={{ duration: 0.25, ease: [0.23, 1, 0.32, 1] }}
>
<h3 className='font-semibold'>{current.title}</h3>
<p className='mt-1 text-sm text-muted-foreground'>{current.text}</p>
</motion.div>
</AnimatePresence>
<div className='mt-6 flex justify-between text-sm font-medium *:rounded-full *:border *:px-4 *:py-2 *:disabled:opacity-40'>
<button
type='button'
disabled={step === 0}
onClick={() => go(step - 1)}
>
Back
</button>
<button type='button' disabled={last} onClick={() => go(step + 1)}>
Next
</button>
</div>
</div>
);
}Add direction-aware transitions to the 3-step signup wizard: moving forward should slide the current step 40px to the left while it fades out and bring the next step in from 40px on the right, and going back should do the reverse. Use 250ms with ease-out, keep the container height stable during the slide, mirror the direction for right-to-left layouts, and use a plain crossfade when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Direction-aware transition?
In a direction-aware transition, the animation depends on where you are going. Tap Next and the current step slides out to the left while the next one enters from the right. Tap Back and everything reverses. The motion matches the map people already have in their heads, with earlier steps on the left and later ones on the right.
Use it for steppers, onboarding flows, carousels, image galleries, tabs, and calendars that page through months. When every change slides the same way, going back feels like moving forward again, and people lose their sense of place.
To build it, compare the new index with the previous one to get a direction of 1 or -1, then use that value in both the entering and the exiting animation. In Motion, pass it to the custom prop of AnimatePresence too, because an element that is leaving can no longer receive new props. In right-to-left languages, flip the direction.
Often confused with
- Slide in
- A slide-in always enters from the same side. A direction-aware transition picks the side based on whether you move forward or back.
Related terms
All termsStepper
A multi-step flow with a row of numbered steps, like Account, Profile, Done, that shows where you are and how many steps are left.
Carousel
A row of slides shown one at a time and moved with arrows, dots, swipes, or a timer, to fit several images or cards in one spot.
Page transition
An animation that plays when you move from one page or screen to another, such as the old page fading out as the new one fades in.
Spatial consistency
Moving elements in ways that match where things live in the interface, so people can tell where an item went and how to find it again.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.