whileInView is not being triggered on soft navigation
I'm making a portfolio site using Next JS and motion, for the projects section I'm animating project cards using whileInView. it is working fine but when I do soft navigation and move to the more projects screen it is not being triggered. It also works when I do a hard refresh.
See this in action here
projects.tsx
import { cn } from "@/lib/utils";
import Wrapper from "@/components/shared/page";
import { ProjectCard } from "./project-card";
import { Button } from "@/components/ui/button";
import { ArrowRight } from "lucide-react";
import Link from "next/link";
import { ROUTES } from "@/lib/constants/routes";
export function Projects({
className,
showMoreButton
}: {
className?: string;
showMoreButton?: boolean;
}) {
return (
<section className={cn("py-24 sm:px-8 bg-accent my-6", className)}>
<Wrapper className="px-4 space-y-20">
...
<ProjectCard
title={"Wall of wonder"}
imgUrl={"/images/project-1.webp"}
skipMargin
/>
... {/* without skipMargin */}
</Wrapper>
</section>
);
}
project-card.tsx
"use client";
import { cn } from "@/lib/utils";
import { motion } from "motion/react";
import Image from "next/image";
import { useRef } from "react";
export const ProjectCard = ({
reverse,
imgUrl,
title,
skipMargin
}: {
title: string;
reverse?: boolean;
imgUrl: string;
skipMargin?: boolean;
}) => {
const scrollRef = useRef<HTMLDivElement>(null);
return (
<article
ref={scrollRef}
className="max-sm:space-y-4 overflow-hidden sm:grid sm:grid-cols-10 gap-4"
>
<motion.div
initial={{
translateX: reverse ? "-20px" : "20px",
opacity: 0
}}
whileInView={{
translateX: "0px",
opacity: 1
}}
viewport={{
root: scrollRef,
margin: skipMargin ? undefined : "-100px",
once: true
}}
transition={{
ease: "easeInOut",
duration: "0.5",
delay: 0.1
}}
...
>
...
</motion.div>
<motion.div
...
initial={{
translateX: reverse ? "20px" : "-20px",
opacity: 0
}}
whileInView={{
translateX: "0px",
opacity: 1
}}
viewport={{
root: scrollRef,
margin: skipMargin ? undefined : "-100px",
once: true
}}
transition={{
ease: "easeInOut",
duration: "0.5",
delay: 0.1
}}
>
<Image
src={imgUrl}
...
/>
</motion.div>
</article>
);
};
Pages:
// Home
const Home = () => ... <Projects /> ...;
const Projects = ()=> <Projects />;
I am also facing this issue and seeing the same behavior.
NextJS version: 15.1.7 Motion version: 12.4.7 (Also tried 12.3.1, 12.1.0, 12.0.6) Motion-dom version: 12.4.5 (Also tried 12.0.0)
After some further testing, it appears to only happen when running NextJS in dev mode (with or without turbopack) If I build the site and run it it appears to work as intended. So annoying during development, but at least the actual build works correctly.
@CaseyBlackburn yes! it is indeed working in production. Thank you!
Same issue with React Router. whileInView is not getting triggered on client-side "soft" navigation.
Also checked if onViewportEnter is triggered, and confirmed it's triggered but the component won't start animation.
And indeed, it was React's strict mode issue, which is dev-only.
After removing <StrictMode> from entry.client.tsx, it started to work.
react-router: 7.2.0
motion 12.4.7
vite: 6.2.0
tailwindcss: 4.0.9
import { motion } from 'motion/react'
export function FadeIn({ children, delay = 0 }) {
return (
<motion.div
initial={{
y: 40,
opacity: 0.1,
}}
whileInView={{
y: 0,
opacity: 1,
transition: {
duration: 0.4,
ease: 'easeInOut',
delay,
},
}}
viewport={{ once: true }}
onViewportEnter={() => console.log('enter')}
onViewportLeave={() => console.log('leave')}
>
{children}
</motion.div>
)
}