React

Verified against Claude Code · 2026-07-27

Implement a smooth transition between two UI states in React

Implements an enter/exit or shared-element transition using the lightest mechanism that actually produces the described motion — CSS, startTransition, or the View Transition API — instead of defaulting to a heavy animation library.

Claude CodeCursorChatGPTClaude4 fillable variables

The prompt

Ready to copy — highlighted parts are example details you can swap.

You are implementing a specific UI transition in React. You pick the lightest mechanism that actually produces the described motion, not a default reach for a full animation library because it's the most familiar tool.

TRANSITION
When a card in a grid is clicked, it should visually expand into a full detail view that appears to grow from the card's position, not just appear instantly in a new location.

CURRENT CODE
A CardGrid component that renders CardPreview items, and a separate CardDetail route that currently just replaces the whole page instantly on click.

PREFERRED ANIMATION LIBRARY
Motion (formerly Framer Motion), already a dependency elsewhere in the app.

BROWSER SUPPORT REQUIREMENT
Must degrade gracefully on Safari versions that do not support the View Transition API — an instant swap is an acceptable fallback there.

SELECTION RULES
If the transition is a simple property change — opacity, transform, color — tied to a class or data-attribute toggle, implement it with a plain CSS transition and let React only toggle the class or state; do not reach for a JavaScript animation library for something CSS already animates natively and more cheaply. If the transition needs to happen while a state update or navigation would otherwise cause a jarring instant swap, wrap the state update in startTransition, or useTransition's returned function, so React can keep showing the old UI, marked as pending, until the new content is actually ready — this addresses the timing of the swap, not the visual motion itself, and should not be confused with an animation mechanism. If the transition is a genuine shared-element or cross-DOM-tree animation — an item visually morphing from a list into a detail view — and the target platform supports it per the stated browser requirement, use the View Transition API: React's ViewTransition integration if the project's React version has it, or the native browser API directly otherwise, since this is the one case plain CSS transitions and startTransition genuinely cannot produce on their own. Only bring in an external animation library when the transition involves physics-based motion — spring, drag, gesture-driven interaction — that neither CSS nor the View Transition API can model. Respect prefers-reduced-motion for any transition longer than a subtle micro-interaction, either skipping it entirely or replacing it with an instant or cross-fade equivalent for users who have opted out of motion. Prefer animating transform and opacity over properties like width, height, or top and left that trigger layout recalculation on every frame — a transition on a layout-affecting property forces the browser to recompute surrounding elements' positions on every frame, which is measurably more expensive than compositing a transform, and the visual difference is rarely worth that cost when translate or scale achieves the same motion. When combining startTransition with a CSS transition — for instance, fading in newly-committed content — attach the transition to the element that actually persists across the state change, not to a freshly-mounted element with no prior state, since a transition needs an initial and a final value on the same element to have anything to animate between.

OUTPUT FORMAT
1. Which mechanism you chose and the one-sentence reason it is the lightest fit for this specific transition, not a generic justification that would apply to any transition.
2. The implementation, as real code.
3. The prefers-reduced-motion fallback, implemented, not just described.

Customize

Optional — swap in your own details for the highlighted parts above.

Why this works

This is a decision-tree prompt before it's a code-generation prompt, and the actual failure mode it prevents is the default reach for a full animation library on a transition a plain CSS transition already handles for free — a real, avoidable bundle-size and complexity cost for zero additional visual benefit, and the most common outcome of asking an AI assistant to "add an animation" without constraints. Separating startTransition from the View Transition API matters because they solve genuinely different problems that get conflated constantly in practice: startTransition changes when React commits new content and keeps the old UI interactive while marked pending, but it does not animate anything by itself, while the View Transition API is the one mechanism that can actually morph an element's position and size across a DOM change — treating them as interchangeable, which a less constrained prompt reliably produces, is a specific and common mistake this prompt heads off by naming both mechanisms and their distinct jobs explicitly. The prefers-reduced-motion rule is included because it is a real, testable accessibility requirement with a documented user population that has explicitly opted out of motion for vestibular or attention reasons, not a nice-to-have, and it is the exact detail a plain "make it animate" request reliably skips because nothing about that request signals it matters. The transform-and-opacity preference reflects a real, measurable browser rendering cost, not a stylistic one: animating width, height, or an offset property forces the browser's layout engine to recompute affected elements' geometry on every frame, a cost that scales with how much of the page is affected, while transform and opacity changes can be handled by compositing alone on modern browsers, skipping layout and paint entirely for the animated element — the practical result is a transition that stays smooth under load with transform where the equivalent width or top animation visibly stutters. The requirement that a CSS transition attach to a persisting element, not a freshly-mounted one, addresses a specific and common mistake: a CSS transition has nothing to interpolate between if the element it's attached to has no prior state on the page to transition from, so a transition class on a brand-new element that just mounted produces no visible animation at all, and the fix is either mounting the element in its "before" state and toggling a class on the next tick, or using the View Transition API, which is specifically designed to capture a before/after snapshot across exactly the mount and unmount boundary plain CSS transitions cannot bridge.

What you get back

Chosen mechanism: the View Transition API — this is a genuine shared-element case (the card needs to visually morph into the detail view), which CSS transitions and startTransition alone cannot produce; a graceful instant-swap fallback covers the stated Safari support gap. function CardGrid({ cards }) { const navigate = useNavigate(); const openCard = (id) => { if (document.startViewTransition) { document.startViewTransition(() => navigate('/card/' + id)); } else { navigate('/card/' + id); } }; // each CardPreview gets style={{ viewTransitionName: 'card-' + card.id }} // and CardDetail's root element gets the matching viewTransitionName } Reduced-motion fallback: check window.matchMedia('(prefers-reduced-motion: reduce)').matches before calling startViewTransition — if true, navigate directly with no transition, since the API's default cross-fade is still enough visual motion to be worth skipping for users who have opted out.

Verified against

Claude Code Sonnet 4.6 · 2026-07-27

Claude Sonnet 4.6 · 2026-08-01

Changelog

  • 2026-07-27 Initial publish, verified against Claude Code and Claude on Sonnet 4.6.

Need this built into your business?

If a prompt isn't enough — custom software, built and maintained for you — that's Scult's day job.

EXPLORE CUSTOM SOFTWARE
All React prompts

Check your AI visibility

One URL in, a 0–100 score and the exact fixes out.

RUN THE CHECK

Browse all the tools

15 tools across six categories
13 of them never send your data anywhere

Free · No signup · No trial clock

SEE THE DIRECTORY