Update shared web UI auth and profile menu

This commit is contained in:
Gabriel20xx committed 2026-08-09 10:34:06 +02:00
1 parent e3544617e1
commit d0b18a4902
5 files changed
+183 -14

No files matched your search

+3
View File
@@ -196,11 +196,14 @@ The Node and Python packages intentionally expose the same infrastructure concep
/> />
``` ```
The shared profile menu exposes Profile, Settings, About, and Log out. It uses the `About` navigation item's href automatically; pass `aboutHref` or `onAbout` to `AppShell` when a project uses a custom About destination or dialog.
For authentication, use the same shell with `AuthPage` and connect the callbacks to the application's existing auth service: For authentication, use the same shell with `AuthPage` and connect the callbacks to the application's existing auth service:
```svelte ```svelte
<AuthPage <AuthPage
brand="My application" brand="My application"
faviconHref="/favicon.svg"
onLogin={(credentials) => auth.login(credentials.identifier, credentials.password)} onLogin={(credentials) => auth.login(credentials.identifier, credentials.password)}
onSetup={(credentials) => auth.setup(credentials)} onSetup={(credentials) => auth.setup(credentials)}
onRequestReset={() => auth.requestAdminReset()} onRequestReset={() => auth.requestAdminReset()}
@@ -5,6 +5,7 @@
import PageHeader from './PageHeader.svelte'; import PageHeader from './PageHeader.svelte';
let { let {
aboutHref,
brand = 'Application', brand = 'Application',
brandHref = '/', brandHref = '/',
children, children,
@@ -12,6 +13,7 @@
currentPath = '', currentPath = '',
headerActions, headerActions,
navItems = [], navItems = [],
onAbout,
onLogout, onLogout,
onNavigate, onNavigate,
onProfile, onProfile,
@@ -21,6 +23,7 @@
theme = 'dark', theme = 'dark',
username = '', username = '',
}: { }: {
aboutHref?: string;
brand?: string; brand?: string;
brandHref?: string; brandHref?: string;
children?: Snippet; children?: Snippet;
@@ -28,6 +31,7 @@
currentPath?: string; currentPath?: string;
headerActions?: Snippet; headerActions?: Snippet;
navItems?: NavItem[]; navItems?: NavItem[];
onAbout?: () => void | Promise<void>;
onLogout?: () => void | Promise<void>; onLogout?: () => void | Promise<void>;
onNavigate?: (item: NavItem) => void; onNavigate?: (item: NavItem) => void;
onProfile?: () => void; onProfile?: () => void;
@@ -47,11 +51,13 @@
<div class={`sc-root sc-app-shell ${className}`} data-theme={theme === 'system' ? undefined : theme}> <div class={`sc-root sc-app-shell ${className}`} data-theme={theme === 'system' ? undefined : theme}>
<Header <Header
aboutHref={aboutHref ?? navItems.find((item) => item.label.toLowerCase() === 'about')?.href}
{brand} {brand}
{brandHref} {brandHref}
{currentPath} {currentPath}
actions={headerActions} actions={headerActions}
{navItems} {navItems}
{onAbout}
{onLogout} {onLogout}
{onNavigate} {onNavigate}
{onProfile} {onProfile}
@@ -12,6 +12,7 @@
brand = 'Application', brand = 'Application',
children, children,
error = '', error = '',
faviconHref = '/favicon.svg',
loading = false, loading = false,
onLogin, onLogin,
onConfirmReset, onConfirmReset,
@@ -24,6 +25,7 @@
brand?: string; brand?: string;
children?: Snippet; children?: Snippet;
error?: string; error?: string;
faviconHref?: string;
loading?: boolean; loading?: boolean;
onLogin?: (input: AuthLoginInput) => void | Promise<void>; onLogin?: (input: AuthLoginInput) => void | Promise<void>;
onConfirmReset?: AuthResetConfirm; onConfirmReset?: AuthResetConfirm;
@@ -134,7 +136,7 @@
<div class="sc-root sc-auth-page"> <div class="sc-root sc-auth-page">
<section aria-labelledby="sc-auth-title" class="sc-auth-card"> <section aria-labelledby="sc-auth-title" class="sc-auth-card">
<div class="sc-auth-brand"> <div class="sc-auth-brand">
<span aria-hidden="true" class="sc-brand-mark">{brand.slice(0, 1).toUpperCase()}</span> <img alt="" aria-hidden="true" class="sc-auth-brand-icon" src={faviconHref} />
<span>{brand}</span> <span>{brand}</span>
</div> </div>
+113 -13
View File
@@ -5,6 +5,7 @@
BarChart3, BarChart3,
Bot, Bot,
Box, Box,
ChevronDown,
CircleAlert, CircleAlert,
CircleHelp, CircleHelp,
CircleUserRound, CircleUserRound,
@@ -19,6 +20,7 @@
Info, Info,
LayoutDashboard, LayoutDashboard,
ListVideo, ListVideo,
LogOut,
Logs, Logs,
Magnet, Magnet,
Map, Map,
@@ -91,32 +93,58 @@
}; };
let { let {
aboutHref,
brand = 'Application', brand = 'Application',
brandHref = '/', brandHref = '/',
brandIcon,
currentPath = '', currentPath = '',
navItems = [], navItems = [],
actions, actions,
onAbout,
onLogout, onLogout,
onNavigate, onNavigate,
onProfile, onProfile,
onSettings, onSettings,
showSettings = true, showSettings = true,
username = '' username = '',
}: { }: {
aboutHref?: string;
brand?: string; brand?: string;
brandHref?: string; brandHref?: string;
brandIcon?: Snippet;
currentPath?: string; currentPath?: string;
navItems?: NavItem[]; navItems?: NavItem[];
actions?: Snippet; actions?: Snippet;
onAbout?: () => void | Promise<void>;
onLogout?: () => void | Promise<void>; onLogout?: () => void | Promise<void>;
onNavigate?: (item: NavItem) => void; onNavigate?: (item: NavItem) => void;
onProfile?: () => void; onProfile?: () => void | Promise<void>;
onSettings?: () => void; onSettings?: () => void | Promise<void>;
showSettings?: boolean; showSettings?: boolean;
username?: string; username?: string;
} = $props(); } = $props();
let navOpen = $state(false); let navOpen = $state(false);
let profileOpen = $state(false);
const hasProfileMenu = $derived(
Boolean(username && (onProfile || (showSettings && onSettings) || onAbout || aboutHref || onLogout)),
);
function closeMenus(): void {
navOpen = false;
profileOpen = false;
}
function handleWindowKeydown(event: KeyboardEvent): void {
if (event.key === 'Escape') closeMenus();
}
function handleWindowClick(event: MouseEvent): void {
if (event.target instanceof Element && !event.target.closest('[data-profile-menu]')) {
profileOpen = false;
}
}
function iconFor(item: NavItem) { function iconFor(item: NavItem) {
return icons[item.icon ?? 'layout-dashboard'] ?? LayoutDashboard; return icons[item.icon ?? 'layout-dashboard'] ?? LayoutDashboard;
@@ -129,9 +157,17 @@
} }
</script> </script>
<svelte:window onclick={handleWindowClick} onkeydown={handleWindowKeydown} />
<header class="sc-header"> <header class="sc-header">
<a class="sc-brand" href={brandHref} onclick={() => (navOpen = false)}> <a class="sc-brand" href={brandHref} onclick={closeMenus}>
<span aria-hidden="true" class="sc-brand-mark">{brand.slice(0, 1).toUpperCase()}</span> <span aria-hidden="true" class="sc-brand-mark">
{#if brandIcon}
{@render brandIcon()}
{:else}
{brand.slice(0, 1).toUpperCase()}
{/if}
</span>
<span>{brand}</span> <span>{brand}</span>
</a> </a>
@@ -148,7 +184,7 @@
event.preventDefault(); event.preventDefault();
onNavigate(item); onNavigate(item);
} }
navOpen = false; closeMenus();
}} }}
> >
<Icon aria-hidden="true" class="sc-nav-icon" size={16} strokeWidth={2} /> <Icon aria-hidden="true" class="sc-nav-icon" size={16} strokeWidth={2} />
@@ -160,22 +196,86 @@
<div class="sc-header-actions"> <div class="sc-header-actions">
{#if actions}{@render actions()}{/if} {#if actions}{@render actions()}{/if}
{#if showSettings && onSettings} {#if hasProfileMenu}
<button aria-label="Open settings" class="sc-icon-button" type="button" onclick={onSettings}>⚙</button> <div class="sc-profile-menu" data-profile-menu>
{/if} <button
aria-expanded={profileOpen}
{#if username && onProfile} aria-haspopup="menu"
<button class="sc-profile-button" type="button" onclick={onProfile}> class="sc-profile-button"
type="button"
onclick={() => (profileOpen = !profileOpen)}
>
<CircleUserRound aria-hidden="true" class="sc-profile-icon" size={16} strokeWidth={2} /> <CircleUserRound aria-hidden="true" class="sc-profile-icon" size={16} strokeWidth={2} />
<span>{username}</span> <span>{username}</span>
<ChevronDown aria-hidden="true" class="sc-profile-chevron" size={15} strokeWidth={2} />
</button> </button>
{#if profileOpen}
<div aria-label="Account menu" class="sc-profile-dropdown" role="menu">
{#if onProfile}
<button
class="sc-profile-menu-item"
role="menuitem"
type="button"
onclick={() => {
profileOpen = false;
void onProfile?.();
}}
>
Profile
</button>
{/if}
{#if showSettings && onSettings}
<button
class="sc-profile-menu-item"
role="menuitem"
type="button"
onclick={() => {
profileOpen = false;
void onSettings?.();
}}
>
Settings
</button>
{/if}
{#if onAbout}
<button
class="sc-profile-menu-item"
role="menuitem"
type="button"
onclick={() => {
profileOpen = false;
void onAbout?.();
}}
>
About
</button>
{:else if aboutHref}
<a class="sc-profile-menu-item" href={aboutHref} role="menuitem" onclick={() => (profileOpen = false)}>
About
</a>
{/if} {/if}
{#if onLogout} {#if onLogout}
<button class="sc-button sc-button-ghost sc-button-sm" type="button" onclick={() => void onLogout?.()}> <button
class="sc-profile-menu-item"
role="menuitem"
type="button"
onclick={() => {
profileOpen = false;
void onLogout?.();
}}
>
<LogOut aria-hidden="true" size={15} strokeWidth={2} />
Log out Log out
</button> </button>
{/if} {/if}
</div>
{/if}
</div>
{/if}
<button <button
aria-expanded={navOpen} aria-expanded={navOpen}
+58
View File
@@ -139,6 +139,55 @@
font-size: 0.875rem; font-size: 0.875rem;
} }
.sc-profile-menu {
position: relative;
}
.sc-profile-chevron {
transition: transform 140ms ease;
}
.sc-profile-button[aria-expanded="true"] .sc-profile-chevron {
transform: rotate(180deg);
}
.sc-profile-dropdown {
position: absolute;
top: calc(100% + 0.5rem);
right: 0;
z-index: 20;
display: grid;
min-width: 11rem;
gap: 0.125rem;
padding: 0.375rem;
background: var(--sc-surface);
border: 1px solid var(--sc-border);
border-radius: var(--sc-radius-md);
box-shadow: var(--sc-shadow-lg);
}
.sc-profile-menu-item {
display: flex;
width: 100%;
align-items: center;
gap: 0.5rem;
padding: 0.625rem 0.75rem;
color: var(--sc-muted-fg);
background: transparent;
border: 0;
border-radius: var(--sc-radius-sm);
font: inherit;
font-size: 0.875rem;
text-align: left;
text-decoration: none;
cursor: pointer;
}
.sc-profile-menu-item:hover {
color: var(--sc-fg);
background: var(--sc-surface-hover);
}
.sc-icon-button:hover, .sc-icon-button:hover,
.sc-profile-button:hover { .sc-profile-button:hover {
color: var(--sc-fg); color: var(--sc-fg);
@@ -274,6 +323,7 @@
.sc-button:focus-visible, .sc-button:focus-visible,
.sc-icon-button:focus-visible, .sc-icon-button:focus-visible,
.sc-profile-button:focus-visible, .sc-profile-button:focus-visible,
.sc-profile-menu-item:focus-visible,
.sc-nav-link:focus-visible, .sc-nav-link:focus-visible,
.sc-dialog-close:focus-visible { .sc-dialog-close:focus-visible {
outline: 3px solid color-mix(in srgb, var(--sc-ring) 45%, transparent); outline: 3px solid color-mix(in srgb, var(--sc-ring) 45%, transparent);
@@ -604,6 +654,14 @@
font-weight: 750; font-weight: 750;
} }
.sc-auth-brand-icon {
display: block;
width: 2rem;
height: 2rem;
object-fit: contain;
border-radius: var(--sc-radius-md);
}
.sc-auth-card h1 { .sc-auth-card h1 {
font-size: 1.7rem; font-size: 1.7rem;
} }