From 41bfff995598bed7d7ded88b320e75f03264fdf8 Mon Sep 17 00:00:00 2001 From: Dariusz Niemczyk Date: Thu, 13 Feb 2025 02:31:05 +0100 Subject: [PATCH] fix: mobile-nav navigation colors --- src/components/mobile-nav.tsx | 36 ++++++++++++----- src/components/nav.tsx | 67 +++---------------------------- src/hooks/color-sections.tsx | 75 +++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 71 deletions(-) create mode 100644 src/hooks/color-sections.tsx diff --git a/src/components/mobile-nav.tsx b/src/components/mobile-nav.tsx index b4554ed..7486423 100644 --- a/src/components/mobile-nav.tsx +++ b/src/components/mobile-nav.tsx @@ -2,10 +2,33 @@ import { Button } from "@/components/ui/button" import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet" +import { useColorSections } from "@/hooks/color-sections" import type { Sections, translations } from "@/i18n/translations" import { Menu } from "lucide-react" +import { useRef } from "react" import { LanguageSelector } from "./ui/language-selector" +function NavContent({ + t, + linksOrder +}: { + t: typeof translations.pl, + linksOrder: Array +}) { + const parent = useRef(null); + useColorSections(parent); + return ( + + ) +} + export function MobileNav({ t, linksOrder, @@ -14,25 +37,18 @@ export function MobileNav({ linksOrder: Array }) { return ( - + - + {t.mobileNav.menu} - + ) diff --git a/src/components/nav.tsx b/src/components/nav.tsx index 54ac89e..a368f5a 100644 --- a/src/components/nav.tsx +++ b/src/components/nav.tsx @@ -1,9 +1,10 @@ "use client" import { Button } from "@/components/ui/button" -import { Sections, type translations } from "@/i18n/translations" +import { useColorSections } from "@/hooks/color-sections" +import { type translations } from "@/i18n/translations" import { cn } from "@/lib/utils" import { MoonIcon, SunIcon } from "lucide-react" -import { useLayoutEffect, useRef } from "react" +import { useRef } from "react" import { MobileNav } from "./mobile-nav" import { useTheme } from "./providers" import { LanguageSelector } from "./ui/language-selector" @@ -25,64 +26,8 @@ export function Nav({ t: typeof translations.pl }) { const { theme, setTheme } = useTheme() - const previous = useRef(linksOrder[0]) - - - useLayoutEffect(() => { - const options = { - root: null, - rootMargin: "-10px", - threshold: 0.5, // Adjust the visibility threshold as needed - }; - let timeout: NodeJS.Timeout | null = null; - - const sections = linksOrder.map(value => document.getElementById(value)); - const subs = linksOrder.reduce((acc, value) => { - acc[value] = document.querySelector('[data-sub="' + value + '"]')!; - return acc; - }, {} as Record); - const links = linksOrder.reduce((acc, value) => { - acc[value] = document.querySelector('[href="#' + value + '"]')!; - return acc; - }, {} as Record); - - - const observer = new IntersectionObserver((entries) => { - if (timeout) { - clearTimeout(timeout); - } - for (const entry of entries) { - const target = entry.target.id as keyof (typeof translations.pl)["nav"] - if (entry.intersectionRatio > 0) { - // FIXME: This seems to be VERY broken on firefox. - // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1250972 - // It basically spikes up CPU usage to some enormous values just to update the hash, like WTF firefox. - // if (history.replaceState) { - // timeout = setTimeout(() => { - // history.replaceState(null, "", `#${target}`) - // }, 150) - // } - } - subs[previous.current]?.classList.remove('scale-x-100'); - links[previous.current]?.classList.remove('text-primary'); - previous.current = target; - - subs[previous.current]?.classList.add('scale-x-100'); - links[previous.current]?.classList.add('text-primary'); - break; - } - }, options); - - sections.forEach(section => { - if (section) { - observer.observe(section); - } - }); - - return () => { - observer.disconnect() - }; - }, []); + const parent = useRef(null); + useColorSections(parent); return (