fix: mobile-nav navigation colors
Some checks failed
/ deploy (push) Failing after 54s

This commit is contained in:
Dariusz Niemczyk 2025-02-13 02:31:05 +01:00
parent 63fca6b5ad
commit 41bfff9955
No known key found for this signature in database
3 changed files with 107 additions and 71 deletions

View file

@ -2,10 +2,33 @@
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet" import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet"
import { useColorSections } from "@/hooks/color-sections"
import type { Sections, translations } from "@/i18n/translations" import type { Sections, translations } from "@/i18n/translations"
import { Menu } from "lucide-react" import { Menu } from "lucide-react"
import { useRef } from "react"
import { LanguageSelector } from "./ui/language-selector" import { LanguageSelector } from "./ui/language-selector"
function NavContent({
t,
linksOrder
}: {
t: typeof translations.pl,
linksOrder: Array<Sections>
}) {
const parent = useRef<HTMLDivElement>(null);
useColorSections(parent);
return (
<nav className="flex flex-col gap-4 mt-8" ref={parent}>
{linksOrder.map((value) => (
<a key={value} href={`#${value}`} className="text-lg hover:text-primary transition-colors">
{t.nav[value]}
</a>
))}
<LanguageSelector />
</nav>
)
}
export function MobileNav({ export function MobileNav({
t, t,
linksOrder, linksOrder,
@ -14,25 +37,18 @@ export function MobileNav({
linksOrder: Array<Sections> linksOrder: Array<Sections>
}) { }) {
return ( return (
<Sheet> <Sheet >
<SheetTrigger asChild> <SheetTrigger asChild>
<Button variant="ghost" size="icon" className="md:hidden"> <Button variant="ghost" size="icon" className="md:hidden">
<Menu className="h-5 w-5" /> <Menu className="h-5 w-5" />
<span className="sr-only">{t.mobileNav.toggleMenu}</span> <span className="sr-only">{t.mobileNav.toggleMenu}</span>
</Button> </Button>
</SheetTrigger> </SheetTrigger>
<SheetContent side="right" className="w-[80vw] sm:w-[385px]"> <SheetContent side="right" className="w-[80vw] sm:w-[385px] z-max">
<SheetHeader> <SheetHeader>
<SheetTitle>{t.mobileNav.menu}</SheetTitle> <SheetTitle>{t.mobileNav.menu}</SheetTitle>
</SheetHeader> </SheetHeader>
<nav className="flex flex-col gap-4 mt-8"> <NavContent t={t} linksOrder={linksOrder} />
{linksOrder.map((value) => (
<a key={value} href={`#${value}`} className="text-lg hover:text-primary transition-colors">
{t.nav[value]}
</a>
))}
<LanguageSelector />
</nav>
</SheetContent> </SheetContent>
</Sheet> </Sheet>
) )

View file

@ -1,9 +1,10 @@
"use client" "use client"
import { Button } from "@/components/ui/button" 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 { cn } from "@/lib/utils"
import { MoonIcon, SunIcon } from "lucide-react" import { MoonIcon, SunIcon } from "lucide-react"
import { useLayoutEffect, useRef } from "react" import { useRef } from "react"
import { MobileNav } from "./mobile-nav" import { MobileNav } from "./mobile-nav"
import { useTheme } from "./providers" import { useTheme } from "./providers"
import { LanguageSelector } from "./ui/language-selector" import { LanguageSelector } from "./ui/language-selector"
@ -25,64 +26,8 @@ export function Nav({
t: typeof translations.pl t: typeof translations.pl
}) { }) {
const { theme, setTheme } = useTheme() const { theme, setTheme } = useTheme()
const previous = useRef<Sections>(linksOrder[0]) const parent = useRef<HTMLDivElement>(null);
useColorSections(parent);
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<Sections, HTMLAnchorElement>);
const links = linksOrder.reduce((acc, value) => {
acc[value] = document.querySelector('[href="#' + value + '"]')!;
return acc;
}, {} as Record<Sections, HTMLAnchorElement>);
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()
};
}, []);
return ( return (
<nav className="fixed top-0 left-0 right-0 backdrop-blur-xs bg-background/40 border-b z-[10000]"> <nav className="fixed top-0 left-0 right-0 backdrop-blur-xs bg-background/40 border-b z-[10000]">
@ -96,7 +41,7 @@ export function Nav({
</div> </div>
<div className="flex items-center"> <div className="flex items-center">
{/* Desktop Navigation */} {/* Desktop Navigation */}
<div className="hidden md:flex md:items-center md:gap-4 lg:gap-8"> <div className="hidden md:flex md:items-center md:gap-4 lg:gap-8" ref={parent}>
{linksOrder.map((value) => ( {linksOrder.map((value) => (
<a <a

View file

@ -0,0 +1,75 @@
import { Sections } from "@/i18n/translations";
import { useLayoutEffect, useRef } from "react";
export const linksOrder: Array<Sections> = [
"hero",
"about",
"where",
"when",
"tickets",
"accommodation",
"food",
"contact",
]
export function useColorSections(parent: React.RefObject<HTMLDivElement | null>) {
const previous = useRef<Sections>(linksOrder[0])
useLayoutEffect(() => {
if (parent.current === null) return;
const options = {
root: null,
rootMargin: "-10px",
threshold: 0.5, // Adjust the visibility threshold as needed
};
const sections = linksOrder.map(value => document.getElementById(value));
const subs = linksOrder.reduce((acc, value) => {
acc[value] = parent.current!.querySelector('[data-sub="' + value + '"]')!;
return acc;
}, {} as Record<Sections, HTMLAnchorElement>);
const links = linksOrder.reduce((acc, value) => {
acc[value] = parent.current!.querySelector('[href="#' + value + '"]')!;
return acc;
}, {} as Record<Sections, HTMLAnchorElement>);
console.log(links)
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
const target = entry.target.id as Sections
if (entry.isIntersecting) {
// 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()
};
}, [parent]);
}