What is your developer-dependent CMS actually costing you? Calculate your costs and get a full report
What is your developer-dependent CMS actually costing you? Calculate your costs and get a full report
Fully Featured Starter
Morph a blog card's image into the post's hero image with React's stable ViewTransition component in Next.js, using a name built from the Agility contentID.
When a visitor clicks a blog card, the card's image can grow into the post's hero image instead of the page just swapping. This article shows how the Advanced Next.js Demo Site does it with React's ViewTransition component, and what to copy into your own site.
Open the blog listing and click any post. The card image moves and resizes into the hero image at the top of the post. Go back and it reverses. If your browser doesn't support view transitions, the navigation works as normal, with no animation.
The code lives in these files:
src/components/agility-components/post-listing/PostCard.tsx, the card on the listingsrc/components/agility-components/post-details/PostImage.tsx, the hero image on the postsrc/lib/hooks/useViewTransition.ts, the function that builds the shared namesrc/styles/view-transitions.css, the timingThe demo runs Next.js 16.3.5 and React 19.3.0 (package.json). React 19.3 made ViewTransition a stable export, so you import it from react:
import { ViewTransition } from 'react'
The demo's next.config.mjs used to set experimental.viewTransition. That flag is gone: Next 16.3 no longer recognizes it and warns about it on boot, so the config just carries a comment explaining the removal. You don't need any config for this feature.
Next.js route navigations are React transitions, so a <ViewTransition> animates during navigation with nothing extra to wire up. React finds elements that have the same name on the old and new page and animates between their size and position. The Next.js guide on view transitions covers the full set of patterns.
Both pages need the same name, and it has to be unique per post. The demo builds it from the post's Agility contentID:
// src/lib/hooks/useViewTransition.ts
export function createPostImageTransitionName(contentID: string | number) {
return `post-image-${contentID}`
}
Keeping this in one function means the card and the hero can't drift apart.
PostCard wraps its image in ViewTransition and passes the post's contentID:
// src/components/agility-components/post-listing/PostCard.tsx
<ViewTransition name={createPostImageTransitionName(post.contentID)}>
<AgilityPic
image={post.image}
fallbackWidth={400}
className="absolute inset-0 w-full h-full rounded-2xl ..."
sources={[/* per-breakpoint crops */]}
/>
</ViewTransition>
PostImage does the same on the post page. PostDetails renders it with the post's contentID:
// src/components/agility-components/post-details/PostImage.tsx
<ViewTransition name={createPostImageTransitionName(contentID)}>
<AgilityPic
data-agility-field="image"
image={image}
priority
fallbackWidth={672}
className={className}
sources={[/* retina and mobile variants */]}
/>
</ViewTransition>
Both components are client components ('use client'), and both wrap an AgilityPic. The two images are cropped differently (a square on large screens in the card, a 3:2 box in the post), and the morph animates between those shapes.
view-transitions.css is imported once in src/app/layout.tsx. It sets the durations:
/* src/styles/view-transitions.css */
@view-transition {
navigation: auto;
}
@supports (view-transition-name: none) {
::view-transition-old(*),
::view-transition-new(*) {
animation-duration: 400ms;
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 300ms;
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
}
Named elements, like the post image, animate over 400ms. The rest of the page (root) cross-fades over 300ms. The @view-transition { navigation: auto } rule is the declaration for cross-document navigations (see MDN's View Transition API page). The blog morph itself is driven by the ViewTransition component.
The Next.js guide notes that React's integration uses newer View Transition features, available in Chromium 125+ and recent Safari and Firefox versions, and that some animations may behave differently in Safari. Where the browser doesn't support them, the app works normally and the transitions don't animate.
The demo also contains three older pieces that nothing imports:
| File | What it does | Used? |
|---|---|---|
src/components/ViewTransitionLink.tsx | A Link that calls document.startViewTransition around router.push on click | No |
src/components/ViewTransitionProvider.tsx | Wraps popstate and internal link clicks in document.startViewTransition | No |
useViewTransition() in useViewTransition.ts | Feature-detects startViewTransition and calls it | No |
They come from an earlier approach that triggered transitions by hand. With React's ViewTransition, they're redundant, and ViewTransitionProvider would also turn internal links into full page loads (window.location.href), so don't copy it. Only createPostImageTransitionName from that file is in use.
experimental.viewTransition from your config if it's there.contentID works.<ViewTransition name={...}>, and wrap the matching element on the detail page with the same name.animation-duration on ::view-transition-old(*) and ::view-transition-new(*).Two things to check if the morph doesn't play. The Next.js guide says the morph plays when the destination renders in the same commit as the navigation, which is the case for prefetched pages, and that if the destination suspends into a fallback first, no pair forms. And give each card its own name, as the demo does with the contentID.