Compare commits
No commits in common. "975fd2f52b226ee3033f163bcc8025cf8d253372" and "3b1bbdf67cb7716d4dc214e3b4e12a9bf4783381" have entirely different histories.
975fd2f52b
...
3b1bbdf67c
|
|
@ -3,7 +3,7 @@ import { Button } from "@/components/ui/button"
|
||||||
import { Sections, type translations } from "@/i18n/translations"
|
import { Sections, 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 { useEffect, useState } from "react"
|
import { useDeferredValue, useEffect, useState } 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,8 +25,8 @@ export function Nav({
|
||||||
t: typeof translations.pl
|
t: typeof translations.pl
|
||||||
}) {
|
}) {
|
||||||
const { theme, setTheme } = useTheme()
|
const { theme, setTheme } = useTheme()
|
||||||
const [activeSection,] = useState<Sections>("about")
|
const [activeSection, setActiveSection] = useState<Sections>("about")
|
||||||
// const deferedActiveSection = useDeferredValue(activeSection)
|
const deferedActiveSection = useDeferredValue(activeSection)
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -35,25 +35,24 @@ export function Nav({
|
||||||
rootMargin: "-10px",
|
rootMargin: "-10px",
|
||||||
threshold: 0.5, // Adjust the visibility threshold as needed
|
threshold: 0.5, // Adjust the visibility threshold as needed
|
||||||
};
|
};
|
||||||
// let timeout: NodeJS.Timeout | null = null;
|
let timeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
|
||||||
const observer = new IntersectionObserver((entries) => {
|
const observer = new IntersectionObserver((entries) => {
|
||||||
// if (timeout) {
|
if (timeout) {
|
||||||
// clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
// }
|
|
||||||
for (const entry of entries) {
|
|
||||||
// const target = entry.target.id as keyof (typeof translations.pl)["nav"]
|
|
||||||
// if (activeSection !== target && entry.intersectionRatio > 0) {
|
|
||||||
// // setActiveSection(target);
|
|
||||||
// if (window.location.hash !== `#${target}` && history.replaceState) {
|
|
||||||
// timeout = setTimeout(() => {
|
|
||||||
// history.replaceState(null, "", `#${target}`)
|
|
||||||
// }, 150)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
}
|
}
|
||||||
|
entries.forEach(entry => {
|
||||||
|
const target = entry.target.id as keyof (typeof translations.pl)["nav"]
|
||||||
|
if (entry.isIntersecting) {
|
||||||
|
setActiveSection(target);
|
||||||
|
if (window.location.hash !== `#${target}` && history.replaceState) {
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
history.replaceState(null, "", `#${target}`)
|
||||||
|
}, 150)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
const sections = linksOrder.map(value => document.getElementById(value));
|
const sections = linksOrder.map(value => document.getElementById(value));
|
||||||
|
|
@ -64,7 +63,11 @@ export function Nav({
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
observer.disconnect()
|
sections.forEach(section => {
|
||||||
|
if (section) {
|
||||||
|
observer.unobserve(section);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
@ -86,13 +89,13 @@ export function Nav({
|
||||||
<a
|
<a
|
||||||
key={value}
|
key={value}
|
||||||
href={`#${value}`}
|
href={`#${value}`}
|
||||||
className={cn("text-sm md:text-md hover:text-primary transition-colors relative group will-change-[color]", {
|
className={cn("text-sm md:text-md hover:text-primary transition-colors relative group", {
|
||||||
'text-primary': activeSection === value
|
'text-primary': deferedActiveSection === value
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
{t.nav[value]}
|
{t.nav[value]}
|
||||||
<span className={cn("absolute inset-x-0 -bottom-1 h-0.5 bg-primary transform scale-x-0 group-hover:scale-x-100 transition-transform will-change-transform", {
|
<span className={cn("absolute inset-x-0 -bottom-1 h-0.5 bg-primary transform scale-x-0 group-hover:scale-x-100 transition-transform", {
|
||||||
'scale-x-100': activeSection === value
|
'scale-x-100': deferedActiveSection === value
|
||||||
})} />
|
})} />
|
||||||
</a>
|
</a>
|
||||||
))}
|
))}
|
||||||
|
|
@ -111,7 +114,7 @@ export function Nav({
|
||||||
|
|
||||||
{/* Mobile Navigation */}
|
{/* Mobile Navigation */}
|
||||||
<div className="md:hidden ml-2">
|
<div className="md:hidden ml-2">
|
||||||
<MobileNav t={t} linksOrder={linksOrder} activeSection={activeSection} />
|
<MobileNav t={t} linksOrder={linksOrder} activeSection={deferedActiveSection} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue