From df4c009ac8495249656cf854be2c4b26739678ec Mon Sep 17 00:00:00 2001 From: Realmy <163438634+RealmyTheMan@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:15:59 +0100 Subject: [PATCH] Separate menu into custom component --- .../components/functional/FancyMenu.svelte | 76 ++++++++++++++ src/routes/+layout.svelte | 99 ++++++------------- 2 files changed, 108 insertions(+), 67 deletions(-) create mode 100644 src/lib/components/functional/FancyMenu.svelte diff --git a/src/lib/components/functional/FancyMenu.svelte b/src/lib/components/functional/FancyMenu.svelte new file mode 100644 index 0000000..bfff5e0 --- /dev/null +++ b/src/lib/components/functional/FancyMenu.svelte @@ -0,0 +1,76 @@ + + +
+ {#if activeLinkIndex !== -1} +
+ {/if} + {#each links as { name, url } (url)} + { + if (shouldGoBack) { + const currentIndex = links.findIndex((i) => + i.activeMatch($page.url.pathname), + ); + const nextIndex = links.findIndex((i) => + i.activeMatch(url), + ); + $shouldGoBack = nextIndex < currentIndex; + } + }} + > +
+ {#key name} + + {name} + + {/key} +
+
+ {/each} +
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 32ffd0b..dc85a70 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -5,28 +5,40 @@ import { quintOut } from "svelte/easing"; import { files } from "$lib/store/index.svelte"; import Logo from "$lib/components/visual/svg/Logo.svelte"; - import { fly } from "svelte/transition"; import featuredImage from "$lib/assets/VERT_Feature.webp"; import { PUB_HOSTNAME, PUB_PLAUSIBLE_URL } from "$env/static/public"; + import FancyMenu from "$lib/components/functional/FancyMenu.svelte"; + import { writable } from "svelte/store"; let { children, data } = $props(); - let navWidth = $state(1); - let shouldGoBack = $state(false); + let shouldGoBack = writable(false); - const links = $derived<{ - [key: string]: string; - }>({ - Upload: "/", - [files.files.length > 0 - ? `Convert (${files.files.length})` - : `Convert`]: "/convert", - About: "/about", - }); - - const linkCount = $derived(Object.keys(links).length); - const linkIndex = $derived( - Object.keys(links).findIndex((link) => links[link] === data.pathname), - ); + const links = $derived< + { + name: string; + url: string; + activeMatch: (pathname: string) => boolean; + }[] + >([ + { + name: "Upload", + url: "/", + activeMatch: (pathname) => pathname === "/", + }, + { + name: + files.files.length > 0 + ? `Convert (${files.files.length})` + : `Convert`, + url: "/convert", + activeMatch: (pathname) => pathname === "/convert", + }, + { + name: "About", + url: "/about", + activeMatch: (pathname) => pathname.startsWith("/about"), + }, + ]); const maybeNavToHome = (e: DragEvent) => { if (e.dataTransfer?.types.includes("Files")) { @@ -78,54 +90,7 @@ -
-
- {#each Object.entries(links) as [name, link] (link)} - { - const keys = Object.keys(links); - const currentIndex = keys.findIndex( - (key) => links[key] === data.pathname, - ); - const nextIndex = keys.findIndex( - (key) => links[key] === link, - ); - shouldGoBack = nextIndex < currentIndex; - }} - > -
- {#key name} - - {name} - - {/key} -
-
- {/each} -
+
{#key data.pathname} @@ -137,7 +102,7 @@ easing: quintOut, blurMultiplier: 12, x: { - start: !shouldGoBack ? 250 : -250, + start: !$shouldGoBack ? 250 : -250, end: 0, }, scale: { @@ -151,7 +116,7 @@ blurMultiplier: 12, x: { start: 0, - end: !shouldGoBack ? -250 : 250, + end: !$shouldGoBack ? -250 : 250, }, scale: { start: 1,