-
Notifications
You must be signed in to change notification settings - Fork 0
Fix nav indicator animation when scrolled #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,26 @@ export function TopNav() { | |
| const pathname = usePathname() | ||
| const router = useRouter() | ||
| const [mobileMenuOpen, setMobileMenuOpen] = React.useState(false) | ||
| const navContainerRef = React.useRef<HTMLDivElement>(null) | ||
| const [indicator, setIndicator] = React.useState({ left: 0, width: 0, opacity: 0 }) | ||
|
|
||
| const isActive = (href: string) => { | ||
| if (href === "/") return pathname === "/" | ||
| return pathname.startsWith(href) | ||
| } | ||
|
|
||
| const activeIndex = navItems.findIndex(item => isActive(item.href)) | ||
|
|
||
| // Measure active tab position (scroll-independent via offsetLeft/offsetWidth) | ||
| React.useLayoutEffect(() => { | ||
| const container = navContainerRef.current | ||
| if (!container || activeIndex === -1) return | ||
| const links = container.querySelectorAll<HTMLAnchorElement>("a") | ||
| const activeLink = links[activeIndex] | ||
| if (activeLink) { | ||
| setIndicator({ left: activeLink.offsetLeft, width: activeLink.offsetWidth, opacity: 1 }) | ||
| } | ||
| }, [activeIndex]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indicator position stale after font swap or resizeLow Severity The |
||
|
|
||
| React.useEffect(() => { | ||
| const handleKeyDown = (e: KeyboardEvent) => { | ||
|
|
@@ -31,6 +51,7 @@ export function TopNav() { | |
|
|
||
| const num = parseInt(e.key) | ||
| if (num >= 1 && num <= navItems.length) { | ||
| window.scrollTo(0, 0) | ||
| router.push(navItems[num - 1].href) | ||
| } | ||
| } | ||
|
|
@@ -39,11 +60,6 @@ export function TopNav() { | |
| return () => window.removeEventListener("keydown", handleKeyDown) | ||
| }, [router]) | ||
|
|
||
| const isActive = (href: string) => { | ||
| if (href === "/") return pathname === "/" | ||
| return pathname.startsWith(href) | ||
| } | ||
|
|
||
| const closeMobileMenu = React.useCallback(() => { | ||
| setMobileMenuOpen(false) | ||
| }, []) | ||
|
|
@@ -55,29 +71,24 @@ export function TopNav() { | |
| <div className="flex h-14 items-center justify-center"> | ||
| {/* Desktop Navigation */} | ||
| <TooltipProvider delayDuration={300}> | ||
| <div className="hidden md:flex items-center gap-1"> | ||
| <div ref={navContainerRef} className="relative hidden md:flex items-center gap-1"> | ||
| <motion.span | ||
| className="absolute top-0 bottom-0 bg-accent/10 rounded-md" | ||
| animate={indicator} | ||
| transition={{ type: "spring", stiffness: 350, damping: 30 }} | ||
| /> | ||
| {navItems.map((item, index) => ( | ||
| <ShortcutTooltip key={item.href} shortcut={String(index + 1)}> | ||
| <Link | ||
| href={item.href} | ||
| onClick={() => window.scrollTo(0, 0)} | ||
| className={cn( | ||
| "relative px-3 py-1.5 text-sm whitespace-nowrap transition-colors rounded-md", | ||
| isActive(item.href) | ||
| ? "text-accent" | ||
| : "text-muted-foreground hover:text-foreground" | ||
| )} | ||
| > | ||
| {isActive(item.href) && ( | ||
| <motion.span | ||
| layoutId="nav-indicator" | ||
| className="absolute inset-0 bg-accent/10 rounded-md" | ||
| transition={{ | ||
| type: "spring", | ||
| stiffness: 350, | ||
| damping: 30, | ||
| }} | ||
| /> | ||
| )} | ||
| <span className="relative z-10">{item.title}</span> | ||
| </Link> | ||
| </ShortcutTooltip> | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indicator persists when no nav item is active
Low Severity
When
activeIndexis -1 (e.g., on a 404 page or a route not innavItems), theuseLayoutEffectreturns early without updating the indicator state. If the indicator was previously visible (opacity: 1), it remains highlighted on the last active tab. The oldlayoutIdapproach correctly removed the indicator by not rendering themotion.spanat all. The early return needs to also setopacity: 0to hide the indicator.