feat: Improved navbar on mobile

This commit is contained in:
Leni Aniva 2025-01-25 14:32:12 -08:00
parent 46ca660a13
commit 6f5933d902
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
1 changed files with 79 additions and 20 deletions

View File

@ -2,41 +2,91 @@
import { page } from '$app/stores'; import { page } from '$app/stores';
import { routes } from "$lib/sitemap.ts" import { routes } from "$lib/sitemap.ts"
import ThemeSwitch from "$lib/components/ThemeSwitch.svelte" import ThemeSwitch from "$lib/components/ThemeSwitch.svelte"
import { Rss } from "lucide-svelte"; import { Rss, Menu } from "lucide-svelte";
function isCurrentLink(pathname, route) { function isCurrentLink(pathname, route) {
return route != "/" && pathname.startsWith(route) return route != "/" && pathname.startsWith(route)
|| route == "/post" && pathname.startsWith("/page"); || route == "/post" && pathname.startsWith("/page");
} }
import { onMount } from 'svelte';
let showDropdown = false;
function onDropdownToggle(event) {
showDropdown = !showDropdown;
}
function onKeyDown(event) {
if (event.key == "Escape") {
showDropdown = !showDropdown;
}
}
</script> </script>
<div id="navbar" class="h-5 space-x-4"> <nav
{#each routes as item} id="navbar"
<a class="h-5 space-x-4"
class="nav-link" class:dropdown-opened={showDropdown}>
class:disabled-link={item.disabled} <div
class:current-link={isCurrentLink($page.url.pathname, item.route)} class="link-container"
href={item.route}>{item.name}</a> on:click={() => { showDropdown = false; }}
{/each} >
<a class="nav-link icon" href="/rss.xml"><Rss /></a> {#each routes as item}
<div><ThemeSwitch /></div> <a
</div> class="nav-link"
class:disabled-link={item.disabled}
class:current-link={isCurrentLink($page.url.pathname, item.route)}
on:click={() => { showDropdown = false; }}
href={item.route}>
{item.name}
</a>
{/each}
<a class="nav-link icon" href="/rss.xml"><Rss /></a>
<div><ThemeSwitch /></div>
</div>
<button
id="dropdown-toggle"
aria-hidden="true"
on:click={onDropdownToggle}>
<Menu />
</button>
</nav>
<svelte:window on:keydown={onKeyDown} />
<style> <style>
#bio-name { .link-container {
font-family: serif;
font-size: 3rem;
font-weight: normal;
color: var(--name);
}
#navbar {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: flex-end; align-items: flex-end;
position: sticky; position: sticky;
top: 0; top: 0;
} }
@media (max-width: 700px) { #dropdown-toggle {
display: none;
}
/* mobile mode */
@media (max-width: 768px) {
.link-container {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 100vh;
justify-content: center;
margin: 0;
padding-left: 5vw;
padding-right: 5vw;
background: hsl(var(--background) / .8);
/* Initial hidden state */
opacity: 0;
transform: translateY(-100%);
transition: transform 0.2s, opacity 0.2s;
}
#navbar { #navbar {
display: inline-block; display: inline-block;
white-space: pre-wrap; white-space: pre-wrap;
@ -44,5 +94,14 @@
#navbar .nav-link { #navbar .nav-link {
display: inline-block; display: inline-block;
} }
#dropdown-toggle {
display: initial;
z-index: 5;
}
.dropdown-opened > .link-container {
opacity: 1;
z-index: 1;
transform: translateY(0);
}
} }
</style> </style>