Install next-view-transitions
mainInstall the next-view-transitions package using your preferred package manager.
pnpm install next-view-transitionsrepository·main·Indexed 25 days ago
https://github.com/shuding/next-view-transitionsA library that enables the browser's View Transitions API within the Next.js App Router for smooth page navigations. It provides a <ViewTransitions> provider, a custom <Link> component, and the useTransitionRouter hook for programmatic navigation that supports TransitionOptions and falls back to standard Next.js navigation in unsupported browsers.
Install the next-view-transitions package using your preferred package manager.
pnpm install next-view-transitionsTo enable View Transitions in your application, wrap your root content with the <ViewTransitions> component inside your top-level layout file.
import { ViewTransitions } from 'next-view-transitions'
export default function Layout({ children }) {
return (
<ViewTransitions>
<html lang='en'>
<body>
{children}
</body>
</html>
</ViewTransitions>
)
}Replace standard Next.js links with the <Link> component from next-view-transitions to trigger a view transition when a user clicks a link.
import { Link } from 'next-view-transitions'
export default function Component() {
return (
<div>
<Link href='/about'>Go to /about</Link>
</div>
)
}For programmatic navigation that triggers a view transition, use the useTransitionRouter hook. This hook provides a router object that supports all standard Next.js router methods (e.g., .push()).
import { useTransitionRouter } from 'next-view-transitions'
export default function Component() {
const router = useTransitionRouter()
return (
<div>
<button onClick={() => {
// All Next.js router methods are supported
router.push('/about')
}}>Go to /about</button>
</div>
)
}ViewTransitions component. This component initializes the necessary context and sets up browser-native transition event listeners.The useTransitionRouter hook provides a replacement for Next.js's useRouter that enables the browser's View Transition API during programmatic navigation.
It returns a TransitionRouter object which extends the standard Next.js AppRouterInstance. This allows you to use push and replace methods that automatically wrap the navigation in a document.startViewTransition call if the browser supports it. If the browser does not support View Transitions, it falls back to standard Next.js navigation.
Both push and replace accept an optional onTransitionReady callback that is triggered when the transition is ready to proceed.
When using push or replace from the useTransitionRouter hook, you can provide a TransitionOptions object to control the transition lifecycle.
type TransitionOptions = {
onTransitionReady?: () => void;
};
type NavigateOptionsWithTransition = NavigateOptions & TransitionOptions;The TransitionRouter type is an extension of Next.js's AppRouterInstance. It includes all standard router methods but overrides push and replace to support TransitionOptions.
export type TransitionRouter = AppRouterInstance & {
push: (href: string, options?: NavigateOptionsWithTransition) => void;
replace: (href: string, options?: NavigateOptionsWithTransition) => void;
};ViewTransitions component acts as a provider that enables the view transition capabilities within your application. It must wrap the parts of your application tree where you want to use the transition features.The useSetFinishViewTransition hook allows you to manually trigger the completion of a view transition. It returns a state dispatcher (Dispatch<SetStateAction<(() => void) | null>>) that you can call to signal that the transition should finish.
Note: This hook must be used within a component that is a child of the ViewTransitions provider. If used outside, it will throw an error: useSetFinishViewTransition must be used within a ViewTransitions component.
useTransitionRouter hook provides a router instance that supports view transitions for programmatic navigation. Use this instead of the standard useRouter from next/navigation when you want to trigger a view transition via code.