From 6fd24b656cdb038e7a7f93e254fc6d8c147b568f Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Wed, 31 Jan 2024 18:16:24 -0800
Subject: feat(badges): use custom dropdown

---
 src/lib/Dropdown.svelte | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

(limited to 'src/lib/Dropdown.svelte')

diff --git a/src/lib/Dropdown.svelte b/src/lib/Dropdown.svelte
index efbafa1a..bd657f9a 100644
--- a/src/lib/Dropdown.svelte
+++ b/src/lib/Dropdown.svelte
@@ -2,10 +2,13 @@
 	interface Item {
 		name: string;
 		url: string;
+		onClick?: () => void;
 	}
 
 	export let items: Item[] = [];
-	export let title: string;
+	export let title: string | undefined = undefined;
+	export let header = true;
+	export let center = false;
 
 	let open = false;
 
@@ -16,21 +19,31 @@
 
 <svelte:window on:click={handleClickOutside} />
 
-<div class="dropdown" id="dropdown">
+<div
+	class="dropdown"
+	id="dropdown"
+	style={`--dropdown-left: ${center ? '50%' : 'unset'}; --dropdown-transform: ${
+		center ? 'translateX(-50%)' : 'unset'
+	};`}
+>
 	<span
-		class="header-item dropdown-toggle"
+		class={`${header ? 'header-item' : ''} dropdown-toggle`}
 		id="dropdown-toggle"
 		on:click|preventDefault={() => (open = !open)}
 		on:keydown={() => {}}
 		role="button"
 		tabindex="0"
 	>
-		{title}
+		{#if title}
+			{title}
+		{:else}
+			<slot name="title" />
+		{/if}
 	</span>
 
 	<div class={`dropdown-content card card-small ${open ? 'dropdown-open' : ''}`}>
 		{#each items as item}
-			<a href={item.url} class="header-item">
+			<a href={item.url} class="header-item" on:click={item.onClick}>
 				{item.name}
 			</a>
 		{/each}
@@ -69,8 +82,8 @@
 		visibility: hidden;
 		$delay: 0.25s;
 		transition: opacity $delay ease, transform $delay ease, visibility 0s linear $delay;
-		left: 50%;
-		transform: translateX(-50%);
+		left: var(--dropdown-left);
+		transform: var(--dropdown-transform);
 	}
 
 	.dropdown-open {
@@ -78,8 +91,8 @@
 		transform: translateY(0);
 		visibility: visible;
 		transition-delay: 0s, 0s, 0s;
-		left: 50%;
-		transform: translateX(-50%);
+		left: var(--dropdown-left);
+		transform: var(--dropdown-transform);
 	}
 
 	.dropdown:hover .dropdown-content {
@@ -87,8 +100,8 @@
 		transform: translateY(0);
 		visibility: visible;
 		transition-delay: 0s, 0s, 0s;
-		left: 50%;
-		transform: translateX(-50%);
+		left: var(--dropdown-left);
+		transform: var(--dropdown-transform);
 	}
 
 	.dropdown-content a {
-- 
cgit v1.2.3

