# Patterns and Guidelines Use the official [React `` reference](https://react.dev/reference/react/ViewTransition) for API mechanics. This file collects reusable implementation patterns and failure modes from production apps. ## Searchable Grid with `useDeferredValue` `useDeferredValue` makes filter updates a transition, activating ``: ```tsx 'use client'; import { useDeferredValue, useState, ViewTransition, Suspense } from 'react'; export default function SearchableGrid({ itemsPromise }) { const [search, setSearch] = useState(''); const deferredSearch = useDeferredValue(search); return ( <> setSearch(e.currentTarget.value)} /> }> ); } ``` Per-item `` inside a deferred list triggers cross-fades on every keystroke. Fix with `default="none"`: ```tsx {filteredItems.map(item => ( ))} ``` ## Card Expand/Collapse with `startTransition` Toggle between grid and detail view with shared element morph: ```tsx 'use client'; import { useState, useRef, startTransition, ViewTransition } from 'react'; export default function ItemGrid({ items }) { const [expandedId, setExpandedId] = useState(null); const scrollRef = useRef(0); return expandedId ? ( i.id === expandedId)} onClose={() => { startTransition(() => { setExpandedId(null); setTimeout(() => window.scrollTo({ behavior: 'smooth', top: scrollRef.current }), 100); }); }} /> ) : (
{items.map(item => ( { scrollRef.current = window.scrollY; startTransition(() => setExpandedId(item.id)); }} /> ))}
); } ``` ## Type-Safe Transition Helpers Use `as const` arrays and derived types to prevent ID clashes: ```tsx const transitionTypes = ['default', 'transition-to-detail', 'transition-to-list'] as const; const animationTypes = ['auto', 'none', 'animate-slide-from-left', 'animate-slide-from-right'] as const; type TransitionType = (typeof transitionTypes)[number]; type AnimationType = (typeof animationTypes)[number]; type TransitionMap = { default: AnimationType } & Partial, AnimationType>>; export function HorizontalTransition({ children, enter, exit }: { children: React.ReactNode; enter: TransitionMap; exit: TransitionMap; }) { return {children}; } ``` ## Cross-Fade Without Remount Omit `key` to trigger an update (cross-fade) instead of exit + enter. Avoids Suspense remount/refetch: ```jsx ``` Use `key` when content identity changes (state resets). Omit for cross-fades (tabs, panels, carousel). ## Isolate Elements from Parent Animations Pull an element out of the animated `root` snapshot by giving it its own `view-transition-name`. **`view-transition-name: none` is a no-op** — it's the CSS default, so the element stays in `root` (a common flicker bug). Use a real, unique name, then neutralize with `` (no CSS) or CSS (needed for `z-index`/`display` control — see [css-recipes.md](css-recipes.md#persistent-element-isolation)). - **Persistent chrome** (nav, sidebar, player bar): `