mobile sidebar - collapsable

This commit is contained in:
2025-08-13 22:13:46 +02:00
parent ea9cdb7692
commit 6d4da254cd
3 changed files with 147 additions and 10 deletions

View File

@@ -27,8 +27,10 @@
<div class="header"> <div class="header">
<div class="left"> <div class="left">
<a href="/">
<!-- <img src="/logo.png" /> --> <!-- <img src="/logo.png" /> -->
<h1>schleppe.cloud</h1> <h1>schleppe.cloud</h1>
</a>
</div> </div>
<div class="middle crumbs"> <div class="middle crumbs">
@@ -40,7 +42,6 @@
</div> </div>
<div class="right"> <div class="right">
<span>User profile</span>
<User /> <User />
</div> </div>
</div> </div>
@@ -52,7 +53,7 @@
left: 0; left: 0;
display: grid; display: grid;
grid-template-columns: 240px 1fr auto; grid-template-columns: 200px 1fr auto;
grid-template-areas: 'logoSection siteAndEnvironment profileAndHelp'; grid-template-areas: 'logoSection siteAndEnvironment profileAndHelp';
align-items: center; align-items: center;
background: #1c1819; background: #1c1819;
@@ -64,7 +65,7 @@
font-size: 1rem; font-size: 1rem;
z-index: 100; z-index: 100;
&::after { &::before {
content: ''; content: '';
position: absolute; position: absolute;
width: 100%; width: 100%;
@@ -102,10 +103,11 @@
} }
.crumbs { .crumbs {
margin-left: 0.6rem; margin-left: 2rem;
li { li {
display: block; display: block;
cursor: pointer;
} }
.seperator { .seperator {
@@ -113,6 +115,15 @@
padding: 0 0.75rem; padding: 0 0.75rem;
} }
} }
@media screen and (max-width: 750px) {
top: -0.25rem;
overflow: scroll;
.crumbs {
margin-left: 0;
}
}
} }
:global(.right svg) { :global(.right svg) {

View File

@@ -2,6 +2,7 @@
import { page } from '$app/stores'; import { page } from '$app/stores';
import { derived } from 'svelte/store'; import { derived } from 'svelte/store';
let mobileNavOpen = $state(false);
const pages = [ const pages = [
{ {
name: 'Home', name: 'Home',
@@ -34,16 +35,37 @@
]; ];
const activePage = derived(page, ($page) => $page.url.pathname); const activePage = derived(page, ($page) => $page.url.pathname);
const toggle = () => {
mobileNavOpen = !mobileNavOpen;
// nav opens
if (mobileNavOpen) {
const index = pages.findIndex((page) => $activePage === page.path);
const el: HTMLElement = document.getElementsByTagName('nav')[0].getElementsByTagName('a')[
index
];
if (!el) return;
setTimeout(() => {
el?.scrollIntoViewIfNeeded();
}, 300);
}
};
</script> </script>
<div class="nav-wrapper"> <div class={`nav-wrapper ${mobileNavOpen ? 'open' : ''}`}>
<nav> <nav>
{#each pages as page, i (page.name)} {#each pages as page, i (page.name)}
{#if i === 0} {#if i === 0}
<a class={$activePage === page.path ? 'highlight' : ''} href={page.path}>{page.name}</a> <a
class={$activePage === page.path ? 'highlight' : ''}
on:click={() => (mobileNavOpen = false)}
href={page.path}>{page.name}</a
>
{:else} {:else}
<a <a
class={`${$activePage !== page.path && $activePage.startsWith(page.path) ? 'child' : ''} ${$activePage.startsWith(page.path) ? 'highlight' : ''}`} class={`${$activePage !== page.path && $activePage.startsWith(page.path) ? 'child' : ''} ${$activePage.startsWith(page.path) ? 'highlight' : ''}`}
on:click={() => (mobileNavOpen = false)}
href={page.path}>{page.name}</a href={page.path}>{page.name}</a
> >
{/if} {/if}
@@ -51,13 +73,89 @@
</nav> </nav>
</div> </div>
<div id="mobile-nav-toggle" class={mobileNavOpen ? 'open' : ''} on:click={toggle}>
<span></span>
<span></span>
<span></span>
</div>
<style lang="scss"> <style lang="scss">
#mobile-nav-toggle {
--size: 3rem;
--padding: 2rem;
position: fixed;
z-index: 99;
width: var(--size);
height: var(--size);
border-radius: 50%;
right: 1rem;
bottom: var(--padding);
background-color: var(--color);
display: flex;
flex-direction: column;
gap: 0.4rem;
justify-content: center;
align-items: center;
cursor: pointer;
transition: all 0.2s ease-in-out;
@media screen and (min-width: 750px) {
display: none;
}
span {
width: 50%;
border-radius: 4px;
height: 3px;
background-color: var(--highlight);
transition: all 0.2s ease-in-out;
opacity: 100%;
}
&.open,
&:hover {
height: calc(var(--size) + 4px);
width: calc(var(--size) + 4px);
margin-bottom: -2px;
margin-right: -2px;
}
&.open {
span {
margin-left: 10px;
}
span:nth-child(1) {
transform: rotate(45deg);
transform-origin: top left;
}
span:nth-child(2) {
opacity: 0%;
height: 1px;
}
span:nth-child(3) {
transform: rotate(-45deg);
transform-origin: bottom left;
}
}
}
.nav-wrapper { .nav-wrapper {
--nav-width: 240px; --nav-width: 240px;
top: 72px; top: 72px;
left: 0; right: 0;
min-width: var(--nav-width); min-width: var(--nav-width);
margin-right: 1rem; margin-right: 1rem;
transition: all 0.4s ease-in-out;
z-index: 99;
border-radius: var(--border-radius);
border-bottom-right-radius: 0;
border-top-right-radius: 0;
max-height: 66vh;
height: fit-content;
background-color: var(--bg);
@media screen and (max-width: 1460px) { @media screen and (max-width: 1460px) {
--nav-width: 220px; --nav-width: 220px;
@@ -68,12 +166,39 @@
margin-left: 0.5rem; margin-left: 0.5rem;
margin-right: 0; margin-right: 0;
} }
/* mobile */
@media screen and (max-width: 750px) {
position: fixed;
right: -100%;
padding-top: 0;
padding: 0.51rem;
top: calc(72px + 1rem);
&.open {
right: 0.5rem;
right: -1rem;
background-color: var(--color);
overflow-y: scroll;
nav a {
color: white;
&:hover,
&.highlight {
color: black;
}
}
}
}
} }
nav { nav {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
position: fixed; /* position: fixed; */
width: var(--nav-width); width: var(--nav-width);
gap: 4px; gap: 4px;
margin-top: 1rem; margin-top: 1rem;

View File

@@ -59,5 +59,6 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
margin-bottom: 1.5rem; margin-bottom: 1.5rem;
z-index: 100;
} }
</style> </style>