How to make Page Transitions In Nexjs14 App Dir in the layout.tsx. #59349
Replies: 6 comments 13 replies
-
Absolutely crazy how this hasnt been answered still and everybody on the web is looking for an answer. |
Beta Was this translation helpful? Give feedback.
-
Can't you just manage framer motion on a wrapper client component inside layout.tsx? Thant's how I manage all of my global context and states. // layout.tsx server component
export default async function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<FramerMotionWrapper>{children}</FramerMotionWrapper>
</body>
</html>
);
}
// FramerMotionWrapper.tsx client component
'use client';
export const FramerMotionWrapper: React.FC<{children}:{children: React.ReactNode}> = ({children}){
// manage states etc.
return {
<AnimatePresence>{children}</AnimatePresence>
}
} |
Beta Was this translation helpful? Give feedback.
-
Use Template Component to wrap a children import type { Metadata } from "next";
import { Inter } from "next/font/google";
import Template from "./Template";
const inter = Inter({ subsets: ["latin"] });
export const metadata: Metadata = {
title: "Store",
description: "Store",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={inter.className}>
// Wrapper component
<Template>{children}</Template>
</body>
</html>
);
} Template Component with FrozenRouter which makes exit animation working: "use client"
import React from "react";
import { motion, AnimatePresence } from "framer-motion";
import { usePathname } from "next/navigation";
import { LayoutRouterContext } from "next/dist/shared/lib/app-router-context.shared-runtime";
import { useContext, useRef } from "react";
// Prevents instant page opening
function FrozenRouter(props: { children: React.ReactNode }) {
const context = useContext(LayoutRouterContext ?? {});
const frozen = useRef(context).current;
return (
<LayoutRouterContext.Provider value={frozen}>
{props.children}
</LayoutRouterContext.Provider>
);
}
export default function Template({children}: {children: React.ReactNode}){
let pathname = usePathname();
return(
<>
<AnimatePresence mode={'wait'} initial={false}>
<motion.div
key={pathname}
initial={{opacity: 0}}
animate={{opacity: 1}}
exit={{opacity: 0}}
transition={{duration: 0.8, ease: [0.27,0.94,0.48,1.0]}}
>
// Completing page exit animation and load new page
<FrozenRouter>{children}</FrozenRouter>
</motion.div>
</AnimatePresence>
</>
)
} |
Beta Was this translation helpful? Give feedback.
-
Frozen router has it's own problems eg. browser back and forward. There's no real solution yet, it's an open bug #49279 |
Beta Was this translation helpful? Give feedback.
-
I found this answer which works without any error : https://stackoverflow.com/a/78299318/7186622 If answer deleted, the original content was:
|
Beta Was this translation helpful? Give feedback.
-
Any update on this guys? Is NextJs working on something to implement about page transitions? |
Beta Was this translation helpful? Give feedback.
-
How to make Page Transitions In Nexjs14 App Dir in the layout.tsx. Exit animation does not seem to work with framer motion when you animate presence. Because this can only work client slide. SO i have to initialize "use client" but layout.tsx is a server component
Beta Was this translation helpful? Give feedback.
All reactions