Skip to content

Commit

Permalink
feature(navigation): add breadcrumbs #279
Browse files Browse the repository at this point in the history
  • Loading branch information
marikadeveloper committed Jun 13, 2024
1 parent 7894d38 commit fb435c6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script lang="ts" context="module">
<script lang="ts">
import AppRouter from "./routes/AppRouter.svelte";
</script>

<main>
<AppRouter />
</main>


</main>
53 changes: 53 additions & 0 deletions src/components/Breadcrumbs.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<script lang="ts">
import { onMount } from 'svelte';
import { navigate } from 'svelte-routing';
let segments: { name: string; path: string }[] = [];
function updateSegments() {
const path = window.location.pathname;
const pathSegments = path.split('/').filter(Boolean);
segments = pathSegments.map((segment, index) => {
return {
name: decodeURIComponent(segment),
path: '/' + pathSegments.slice(0, index + 1).join('/')
};
});
}
onMount(updateSegments);
function navigateTo(path: string) {
navigate(path);
updateSegments();
}
</script>

<nav aria-label="Breadcrumb">
<ol>
<li>
<a href="/" on:click|preventDefault={() => navigateTo('/')}>Home</a>
</li>
{#each segments as { name, path }, index (name)}
<li>
<a href={path} on:click|preventDefault={() => navigateTo(path)}>{name}</a>
</li>
{/each}
</ol>
</nav>

<style>
ol {
list-style: none;
padding: 0;
display: flex;
align-items: center;
}
li:not(:last-child)::after {
content: '>';
margin: 0 8px;
}
a {
text-decoration: none;
}
</style>
2 changes: 2 additions & 0 deletions src/routes/AppRouter.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import { Route, Router } from "svelte-routing";
import Breadcrumbs from "../components/Breadcrumbs.svelte";
import Header from '../components/Header.svelte';
import Login from "../pages/Login.svelte";
import ProjectPage from "../pages/Project.svelte";
Expand All @@ -10,6 +11,7 @@

<Router>
<Header/>
<Breadcrumbs />
<Route path="/login">
<Login/>
</Route>
Expand Down

0 comments on commit fb435c6

Please sign in to comment.