fix menu not showing active page
All checks were successful
deploy / build-and-deploy (push) Successful in 15s
All checks were successful
deploy / build-and-deploy (push) Successful in 15s
This commit is contained in:
parent
358701309d
commit
ce6325e8dd
@ -12,11 +12,15 @@
|
|||||||
import { navigate } from 'astro:transitions/client';
|
import { navigate } from 'astro:transitions/client';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
|
// html bindings
|
||||||
|
let navElem: HTMLDivElement;
|
||||||
|
|
||||||
|
// states
|
||||||
let navPaths = $state([
|
let navPaths = $state([
|
||||||
{
|
{
|
||||||
name: 'Startseite',
|
name: 'Startseite',
|
||||||
sprite: MenuHome.src,
|
sprite: MenuHome.src,
|
||||||
href: ``,
|
href: '',
|
||||||
active: false
|
active: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -56,20 +60,22 @@
|
|||||||
let isOpen = $state(false);
|
let isOpen = $state(false);
|
||||||
let windowHeight = $state(0);
|
let windowHeight = $state(0);
|
||||||
|
|
||||||
|
// lifecycle
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
localStorage.setItem('showMenuPermanent', `${showMenuPermanent}`);
|
localStorage.setItem('showMenuPermanent', `${showMenuPermanent}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
new MutationObserver(() => {
|
updateActiveNavPath();
|
||||||
for (let i = 0; i < navPaths.length; i++) {
|
new MutationObserver(updateActiveNavPath).observe(document.head, { childList: true });
|
||||||
console.log(navPaths[i].href, window.location.pathname);
|
|
||||||
navPaths[i].active = new URL(navPaths[i].href).pathname === window.location.pathname;
|
|
||||||
}
|
|
||||||
}).observe(document.head, { childList: true });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let navElem: HTMLDivElement;
|
// functions
|
||||||
|
function updateActiveNavPath() {
|
||||||
|
for (let i = 0; i < navPaths.length; i++) {
|
||||||
|
navPaths[i].active = new URL(document.baseURI).pathname + navPaths[i].href === window.location.pathname;
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window bind:innerHeight={windowHeight} />
|
<svelte:window bind:innerHeight={windowHeight} />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user