Skip to content

Commit f229b88

Browse files
committed
Improve breadcrumbs
1 parent 9825ff1 commit f229b88

6 files changed

Lines changed: 56 additions & 30 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Pathname } from '$app/types';
2+
import { onMount } from 'svelte';
3+
4+
export interface BreadCrumbEntry {
5+
text: string;
6+
href: Pathname | null;
7+
}
8+
9+
let state = $state<BreadCrumbEntry[]>([]);
10+
11+
export const breadcrumbs = {
12+
get State() {
13+
return state;
14+
},
15+
push: (text: string, href: Pathname | null = null) => {
16+
onMount(() => {
17+
const entry = { text, href };
18+
state = [...state, entry];
19+
return () => {
20+
state = state.filter((e) => e.text !== entry.text || e.href !== entry.href);
21+
};
22+
});
23+
},
24+
clear: () => state = [],
25+
};

src/lib/stores/BreadCrumbStore.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script lang="ts">
22
import Container from '$lib/components/Container.svelte';
3+
import { breadcrumbs } from '$lib/state/Breadcrumbs.svelte';
4+
breadcrumbs.push('Home', '/home');
35
</script>
46

57
<Container>Home</Container>

src/routes/(authenticated)/hubs/+page.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import { type Hub, columns } from './columns';
1414
import DataTableActions from './data-table-actions.svelte';
1515
import HubCreateDialog from './dialog-hub-create.svelte';
16+
import { breadcrumbs } from '$lib/state/Breadcrumbs.svelte';
1617
1718
const isMobile = new IsMobile();
1819
@@ -46,6 +47,7 @@
4647
4748
let showAddHubModal = $state<boolean>(false);
4849
50+
breadcrumbs.push('Hubs', '/hubs');
4951
onMount(refreshOwnHubs);
5052
</script>
5153

src/routes/Breadcrumb.svelte

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script lang="ts">
2+
import * as Breadcrumb from "$lib/components/ui/breadcrumb";
3+
import { breadcrumbs } from "$lib/state/Breadcrumbs.svelte";
4+
</script>
5+
6+
{#if breadcrumbs.State.length > 0}
7+
<Breadcrumb.Root>
8+
<Breadcrumb.List>
9+
{#each breadcrumbs.State as crumb, index}
10+
<Breadcrumb.Item>
11+
{#if index < breadcrumbs.State.length - 1}
12+
<Breadcrumb.Link href={crumb.href} class="text-gray-600 dark:text-gray-300">
13+
{crumb.text}
14+
</Breadcrumb.Link>
15+
<Breadcrumb.Separator class="text-gray-600 dark:text-gray-300" />
16+
{:else}
17+
<Breadcrumb.Page class="text-gray-900 dark:text-gray-100">
18+
{crumb.text}
19+
</Breadcrumb.Page>
20+
{/if}
21+
</Breadcrumb.Item>
22+
{/each}
23+
</Breadcrumb.List>
24+
</Breadcrumb.Root>
25+
{/if}

src/routes/Header.svelte

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import LightSwitch from '$lib/components/LightSwitch.svelte';
66
import DiscordIcon from '$lib/components/svg/DiscordIcon.svelte';
77
import GithubIcon from '$lib/components/svg/GithubIcon.svelte';
8-
import * as Breadcrumb from '$lib/components/ui/breadcrumb';
98
import { Button } from '$lib/components/ui/button';
109
import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
1110
import { useSidebar } from '$lib/components/ui/sidebar';
12-
import { BreadCrumbStore } from '$lib/stores/BreadCrumbStore';
1311
import { UserStore } from '$lib/stores/UserStore';
1412
import { cn } from '$lib/utils';
13+
import Breadcrumb from './Breadcrumb.svelte';
1514
import type { Pathname } from '$app/types';
1615
1716
let sidebar = useSidebar();
@@ -32,26 +31,7 @@
3231
<Button variant="ghost" class="size-8" title="Open Sidebar" onclick={() => sidebar.toggle()}>
3332
<PanelLeft size={24} class="m-0 text-gray-600 dark:text-gray-300" />
3433
</Button>
35-
{#if $BreadCrumbStore.length > 0}
36-
<Breadcrumb.Root>
37-
<Breadcrumb.List>
38-
{#each $BreadCrumbStore as crumb, index}
39-
<Breadcrumb.Item>
40-
{#if index < $BreadCrumbStore.length - 1}
41-
<Breadcrumb.Link href={crumb.href} class="text-gray-600 dark:text-gray-300">
42-
{crumb.text}
43-
</Breadcrumb.Link>
44-
<Breadcrumb.Separator class="text-gray-600 dark:text-gray-300" />
45-
{:else}
46-
<Breadcrumb.Page class="text-gray-900 dark:text-gray-100">
47-
{crumb.text}
48-
</Breadcrumb.Page>
49-
{/if}
50-
</Breadcrumb.Item>
51-
{/each}
52-
</Breadcrumb.List>
53-
</Breadcrumb.Root>
54-
{/if}
34+
<Breadcrumb />
5535
<div
5636
class={cn(
5737
'flex flex-1 flex-row items-center justify-between space-x-2 py-2',

0 commit comments

Comments
 (0)