在 Next.js 应用程序路由器中使用 CSS 视图过渡 API。
Use View Transitions API in Next.js App Router.
Demo.
This library is aimed at basic use cases of View Transitions and Next.js App Router. With more complex applications and use cases like concurrent rendering, Suspense and streaming, new primitives and APIs still need to be developed into the core of React and Next.js in the future (more).
Use your favorite package manager to install the next-view-transitions package. For example:
pnpm install next-view-transitionsWrap your content with the <ViewTransitions> component inside the layout file:
import { ViewTransitions } from 'next-view-transitions'
export default function Layout({ children }) {
return (
<ViewTransitions>
<html lang='en'>
<body>
{children}
</body>
</html>
</ViewTransitions>
)
}Then, use the <Link> component for links that need to trigger a view transition:
import { Link } from 'next-view-transitions'
export default function Component() {
return (
)
}Or use the useTransitionRouter hook for programmatic navigation:
import { useTransitionRouter } from 'next-view-transitions'
export default function Component() {
const router = useTransitionRouter()
return (
)
}That's it!
MIT.
暂无开放 Issues,或尚未同步最近议题。