Skip to the content

Motion

layout-transition

A shared element that animates from one place to another: render Morph with the same layoutId in two places and it travels between them.

Motion and transitions

When to use it

A tabs or navigation indicator that slides to the active item, a card that grows into its detail view, a selection highlight. Both places must be in the same React tree. Between pages use view-transition; for content that only appears and disappears use presence.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Motion and transitions page.

Reveal, stagger, text reveal

Words arrive one after another when they come into view.

1
2
3
4

Content already on screen when the page loads stays put; only what scrolls in is animated.

Number ticker and presence

128,450

Mounted and unmounted with a transition

Tilt, spotlight, magnetic

Tilt (move the pointer)
Spotlight

Shared element and view transition

1234

Scroll scene

A section that holds still

while the story moves with the scroll.

Then the next beat

Under reduced motion it is a plain stack.

Add it

npx shadcn@latest add @rivier-ui/layout-transition

The registry is named once in components.json: see Installation.

Example

Import it from @/components/motion/layout-transition.

const [active, setActive] = useState("overview");
<MorphGroup id="section-tabs">
  <div className="flex gap-1">
    {["overview", "pricing", "docs"].map((tab) => (
      <button key={tab} onClick={() => setActive(tab)} className="relative h-control rounded-control px-4 text-sm">
        {active === tab ? <Morph layoutId="indicator" className="absolute inset-0 rounded-control bg-accent" /> : null}
        <span className="relative capitalize">{tab}</span>
      </button>
    ))}
  </div>
</MorphGroup>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: motion.

It imports

It is used by

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/motion/layout-transition.tsx
"use client";
/**
 * @name layout-transition
 * @category motion
 * @description A shared element that animates from one place to another: render Morph with the same layoutId in two places and it travels between them.
 * @when A tabs or navigation indicator that slides to the active item, a card that grows into its detail view, a selection highlight. Both places must be in the same React tree. Between pages use view-transition; for content that only appears and disappears use presence.
 * @example
 * const [active, setActive] = useState("overview");
 * <MorphGroup id="section-tabs">
 *   <div className="flex gap-1">
 *     {["overview", "pricing", "docs"].map((tab) => (
 *       <button key={tab} onClick={() => setActive(tab)} className="relative h-control rounded-control px-4 text-sm">
 *         {active === tab ? <Morph layoutId="indicator" className="absolute inset-0 rounded-control bg-accent" /> : null}
 *         <span className="relative capitalize">{tab}</span>
 *       </button>
 *     ))}
 *   </div>
 * </MorphGroup>
 */
import { LayoutGroup, motion, useReducedMotion, type HTMLMotionProps } from "motion/react";
import type { ReactNode } from "react";
import { cn } from "@/lib/cn";
import { DURATION, EASE } from "@/lib/motion";

export type MorphGroupProps = {
  /** Namespaces the layoutIds inside, so two copies of the same component on a page do not animate into each other. */
  id?: string;
  children?: ReactNode;
};

/** Scopes shared elements. Wrap each instance of a component that uses Morph (motion's LayoutGroup). */
export function MorphGroup({ id, children }: MorphGroupProps) {
  return <LayoutGroup id={id}>{children}</LayoutGroup>;
}

export type MorphProps = Omit<HTMLMotionProps<"div">, "layoutId"> & {
  /** The identity shared by the two places. The element animates from wherever this id was last rendered. */
  layoutId: string;
};

/**
 * How it moves: motion measures the element's box before and after the change and animates the difference
 * with `transform` alone (a translate3d and a scale, built in motion-dom's projection code), so layout is
 * computed once and no width, height, top or left is animated. Because the box is scaled while it travels,
 * motion rewrites `border-radius` each frame to cancel the distortion: that is the one non-transform
 * property that changes, and it does not affect layout. Children of a Morph that changes size are scaled
 * with it; make each one a Morph (or a motion element with `layout`) to keep it undistorted.
 *
 * Under reduced motion the layoutId is dropped, so the element is simply in its new place.
 */
export function Morph({ className, layoutId, transition, ...props }: MorphProps) {
  const reduced = useReducedMotion();
  return (
    <motion.div
      data-slot="morph"
      className={cn(className)}
      layoutId={reduced ? undefined : layoutId}
      transition={transition ?? { duration: DURATION.slow, ease: EASE }}
      {...props}
    />
  );
}