Skip to content

Commit

Permalink
fix: navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mokto committed Apr 30, 2024
1 parent 286ef36 commit b8bdc50
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lib/menu/menu-item.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script lang="ts">
import { page } from '$app/stores';
import { urlStore } from '$lib/utils/url-store';
export let href: string;
export let label: string;
$: active = href === $page.url.pathname;
$: pathname = $urlStore || $page.url.pathname;
$: active = href === pathname;
</script>

<a
Expand Down
3 changes: 3 additions & 0 deletions src/lib/utils/url-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { writable } from 'svelte/store';

export const urlStore = writable('');
4 changes: 4 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import type { PageData } from './$types';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { urlStore } from '$lib/utils/url-store';
/** @type {import('./$types').LayoutData} */
export let data: PageData;
page.subscribe((val) => urlStore.set(val.url.pathname));
</script>

<div class="h-16 bg-stripe-200 fixed inset-x-0 sm:hidden">
Expand Down
7 changes: 6 additions & 1 deletion src/routes/[...page]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Markdown from '$lib/components/markdown/markdown.svelte';
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { urlStore } from '$lib/utils/url-store';
/** @type {import('./$types').PageData} */
export let data: PageData;
Expand Down Expand Up @@ -36,7 +37,11 @@
const id = menuItems[values.findIndex((y) => y === Math.min(...values))].id;
window.history.pushState({}, '', window.location.origin + (id ? '/topics/' + id : ''));
const newPathname = id ? '/topics/' + id : '/';
if (newPathname !== window.location.pathname) {
window.history.pushState({}, '', window.location.origin + newPathname);
urlStore.set(newPathname);
}
};
onMount(() => {
Expand Down

0 comments on commit b8bdc50

Please sign in to comment.