From 7cd5a22964c2f3472d299a58f4fa78cf21e85c2b Mon Sep 17 00:00:00 2001 From: jessy-david-dev Date: Tue, 27 Jan 2026 18:26:49 +0100 Subject: [PATCH] fix(nav): handle navigation from other pages in SmoothLink --- component/ui/SmoothLink.tsx | 78 +++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/component/ui/SmoothLink.tsx b/component/ui/SmoothLink.tsx index bce28c3..091b925 100644 --- a/component/ui/SmoothLink.tsx +++ b/component/ui/SmoothLink.tsx @@ -1,41 +1,69 @@ "use client"; +import { usePathname, useRouter } from "next/navigation"; import { ReactNode } from "react"; interface SmoothLinkProps { - href: string; - children: ReactNode; - className?: string; - onClick?: () => void; + href: string; + children: ReactNode; + className?: string; + onClick?: () => void; } export default function SmoothLink({ - href, - children, - className, - onClick, + href, + children, + className, + onClick, }: SmoothLinkProps) { - const handleClick = (e: React.MouseEvent) => { - e.preventDefault(); + const pathname = usePathname(); + const router = useRouter(); - const targetId = href.replace("#", ""); + const handleClick = (e: React.MouseEvent) => { + e.preventDefault(); + + // Si on clique sur "/" (Accueil) + if (href === "/") { + if (pathname === "/") { + // On est déjà sur l'accueil, scroll vers le haut + window.scrollTo({ top: 0, behavior: "smooth" }); + } else { + // On est sur une autre page, naviguer vers l'accueil + router.push("/"); + } + onClick?.(); + return; + } + + // Si c'est une ancre (#section) + if (href.startsWith("#")) { + const targetId = href.replace("#", ""); + + if (pathname === "/") { + // On est sur l'accueil, smooth scroll vers la section const element = document.getElementById(targetId); - if (element) { - element.scrollIntoView({ - behavior: "smooth", - block: "start", - }); + element.scrollIntoView({ + behavior: "smooth", + block: "start", + }); } + } else { + // On est sur une autre page, naviguer vers l'accueil + ancre + router.push(`/${href}`); + } + onClick?.(); + return; + } - if (onClick) { - onClick(); - } - }; + // Sinon, navigation normale + router.push(href); + onClick?.(); + }; - return ( - - {children} - - ); + return ( + + {children} + + ); } -- 2.47.3