feat(ui): implement dropdown menu component with items and separators
Build & Push Docker Image / Docker Build & Push (push) Successful in 4m32s
Lint / Lint (push) Successful in 17s
Tests / Test (push) Successful in 11s

This commit is contained in:
Gabriel20xx committed 2026-08-02 15:54:34 +02:00
1 parent cd72558db8
commit 9af1e0ce73
7 files changed
+100 -36

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="%sveltekit.assets%/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>CollabTable Server</title>
<title>CollabTable</title>
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover" class="h-full">
@@ -15,11 +15,20 @@
<script>
import { page } from '$app/state';
import { resolve } from '$app/paths';
import { Menu, Table2, Settings, X } from '@lucide/svelte';
import { LogOut, Menu, Table2, Settings, UserRound, X } from '@lucide/svelte';
import { cn } from '$lib/utils/cn.js';
import Button from '$lib/components/ui/button.svelte';
import HealthWidget from '$lib/features/health/HealthWidget.svelte';
import SettingsDialog from '$lib/features/layout/SettingsDialog.svelte';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '$lib/components/ui/dropdown-menu';
import { useAuth } from '$lib/features/auth/auth.svelte.js';
import CredentialsDialog from '$lib/features/auth/CredentialsDialog.svelte';
const navLinkClass = (active) =>
cn(
@@ -27,8 +36,11 @@
active && 'bg-surface-hover text-primary',
);
const auth = useAuth();
let navOpen = $state(false);
let settingsOpen = $state(false);
let credentialsOpen = $state(false);
</script>
<header class="sticky top-0 z-40 flex h-14 items-center gap-2 border-b border-border bg-bg px-3">
@@ -82,6 +94,29 @@
<Button variant="ghost" size="icon" aria-label="Settings" onclick={() => (settingsOpen = true)}>
<Settings class="size-5" />
</Button>
<DropdownMenu>
<DropdownMenuTrigger>
{#snippet child({ props })}
<Button variant="ghost" size="sm" class="max-[639px]:size-9 max-[639px]:p-0" aria-label="Account" {...props}>
<UserRound class="size-5" />
<span class="hidden min-[640px]:inline">{auth.status?.username ?? ''}</span>
</Button>
{/snippet}
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem onSelect={() => (credentialsOpen = true)}>
<UserRound class="size-4" />
Profile
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem onSelect={() => void auth.logout()}>
<LogOut class="size-4" />
Sign out
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</header>
<CredentialsDialog bind:open={credentialsOpen} currentUsername={auth.status?.username ?? ''} />
<SettingsDialog bind:open={settingsOpen} />
@@ -0,0 +1,28 @@
<script>
import { DropdownMenu } from 'bits-ui';
import { cn } from '$lib/utils/cn.js';
let {
ref = $bindable(null),
class: className,
sideOffset = 6,
align = 'end',
children,
...restProps
} = $props();
</script>
<DropdownMenu.Portal>
<DropdownMenu.Content
bind:ref
{sideOffset}
{align}
class={cn(
'z-50 min-w-[10rem] overflow-hidden rounded-md border border-border bg-surface p-1 text-fg shadow-lg animate-in fade-in-0 zoom-in-95',
className,
)}
{...restProps}
>
{@render children?.()}
</DropdownMenu.Content>
</DropdownMenu.Portal>
@@ -0,0 +1,8 @@
import { DropdownMenu as DropdownMenuPrimitive } from 'bits-ui';
export const DropdownMenu = DropdownMenuPrimitive.Root;
export const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger;
export { default as DropdownMenuContent } from './content.svelte';
export { default as DropdownMenuItem } from './item.svelte';
export { default as DropdownMenuSeparator } from './separator.svelte';
@@ -0,0 +1,18 @@
<script>
import { DropdownMenu } from 'bits-ui';
import { cn } from '$lib/utils/cn.js';
let { ref = $bindable(null), class: className, active = false, children, ...restProps } = $props();
</script>
<DropdownMenu.Item
bind:ref
class={cn(
'flex cursor-pointer select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none data-highlighted:bg-surface-hover',
active && 'text-primary font-medium',
className,
)}
{...restProps}
>
{@render children?.()}
</DropdownMenu.Item>
@@ -0,0 +1,8 @@
<script>
import { DropdownMenu } from 'bits-ui';
import { cn } from '$lib/utils/cn.js';
let { ref = $bindable(null), class: className, ...restProps } = $props();
</script>
<DropdownMenu.Separator bind:ref class={cn('-mx-1 my-1 h-px bg-border', className)} {...restProps} />
@@ -1,28 +1,10 @@
<script>
import { LogOut, Moon, Sun, SunMoon, UserCog } from '@lucide/svelte';
import { Moon, Sun, SunMoon } from '@lucide/svelte';
import Button from '$lib/components/ui/button.svelte';
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '$lib/components/ui/dialog';
import { themeState } from '$lib/theme.svelte.js';
import { useAuth } from '$lib/features/auth/auth.svelte.js';
import CredentialsDialog from '$lib/features/auth/CredentialsDialog.svelte';
let { open = $bindable(false) } = $props();
const auth = useAuth();
let credentialsOpen = $state(false);
// bits-ui dialogs don't nest cleanly (focus-trap/z-index conflicts), so close this one
// before opening the next, matching the pattern used elsewhere in this project series.
function openCredentials() {
open = false;
credentialsOpen = true;
}
async function handleLogout() {
open = false;
await auth.logout();
}
</script>
<Dialog bind:open>
@@ -65,23 +47,8 @@
</Button>
</div>
</div>
<div>
<span class="mb-2 block text-sm font-medium">Account</span>
<div class="flex flex-col gap-2">
<Button type="button" variant="secondary" class="justify-start gap-2" onclick={openCredentials}>
<UserCog class="size-4" />
Change username / password
</Button>
<Button type="button" variant="secondary" class="justify-start gap-2" onclick={handleLogout}>
<LogOut class="size-4" />
Log out
</Button>
</div>
</div>
<DialogFooter>
<Button type="button" variant="secondary" onclick={() => (open = false)}>Dismiss</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<CredentialsDialog bind:open={credentialsOpen} currentUsername={auth.status?.username ?? ''} />