Add initial shared code packages
This commit is contained in:
commit
838a97d344
54 files changed
+6265
No files matched your search
@@ -0,0 +1,41 @@
|
||||
<script lang="ts">
|
||||
import type { AboutLink } from '../types';
|
||||
import Dialog from './Dialog.svelte';
|
||||
|
||||
let {
|
||||
brand = 'Application',
|
||||
description = '',
|
||||
links = [],
|
||||
open = $bindable(false),
|
||||
version = ''
|
||||
}: {
|
||||
brand?: string;
|
||||
description?: string;
|
||||
links?: AboutLink[];
|
||||
open?: boolean;
|
||||
version?: string;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<Dialog bind:open title={`About ${brand}`}>
|
||||
<div class="sc-auth-brand">
|
||||
<span aria-hidden="true" class="sc-brand-mark">{brand.slice(0, 1).toUpperCase()}</span>
|
||||
<strong>{brand}</strong>
|
||||
</div>
|
||||
|
||||
{#if version}
|
||||
<p class="sc-muted">Version {version}</p>
|
||||
{/if}
|
||||
|
||||
{#if description}
|
||||
<p class="sc-muted">{description}</p>
|
||||
{/if}
|
||||
|
||||
{#if links.length > 0}
|
||||
<div class="sc-about-links">
|
||||
{#each links as link}
|
||||
<a href={link.href} rel="noreferrer" target="_blank">{link.label}</a>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</Dialog>
|
||||
@@ -0,0 +1,51 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
import type { NavItem } from '../types';
|
||||
import Header from './Header.svelte';
|
||||
|
||||
let {
|
||||
brand = 'Application',
|
||||
brandHref = '/',
|
||||
children,
|
||||
class: className = '',
|
||||
currentPath = '',
|
||||
navItems = [],
|
||||
onLogout,
|
||||
onProfile,
|
||||
onSettings,
|
||||
showSettings = true,
|
||||
theme = 'dark',
|
||||
username = ''
|
||||
}: {
|
||||
brand?: string;
|
||||
brandHref?: string;
|
||||
children?: Snippet;
|
||||
class?: string;
|
||||
currentPath?: string;
|
||||
navItems?: NavItem[];
|
||||
onLogout?: () => void | Promise<void>;
|
||||
onProfile?: () => void;
|
||||
onSettings?: () => void;
|
||||
showSettings?: boolean;
|
||||
theme?: 'dark' | 'light' | 'system';
|
||||
username?: string;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<div class={`sc-root sc-app-shell ${className}`} data-theme={theme === 'system' ? undefined : theme}>
|
||||
<Header
|
||||
{brand}
|
||||
{brandHref}
|
||||
{currentPath}
|
||||
{navItems}
|
||||
{onLogout}
|
||||
{onProfile}
|
||||
{onSettings}
|
||||
{showSettings}
|
||||
{username}
|
||||
/>
|
||||
|
||||
<main class="sc-main">
|
||||
{@render children?.()}
|
||||
</main>
|
||||
</div>
|
||||
@@ -0,0 +1,124 @@
|
||||
<script lang="ts">
|
||||
import type { AuthLoginInput, AuthSetupInput } from '../types';
|
||||
import Button from './Button.svelte';
|
||||
|
||||
let {
|
||||
brand = 'Application',
|
||||
error = '',
|
||||
loading = false,
|
||||
onLogin,
|
||||
onSetup,
|
||||
setupRequired = false,
|
||||
tagline = ''
|
||||
}: {
|
||||
brand?: string;
|
||||
error?: string;
|
||||
loading?: boolean;
|
||||
onLogin?: (input: AuthLoginInput) => void | Promise<void>;
|
||||
onSetup?: (input: AuthSetupInput) => void | Promise<void>;
|
||||
setupRequired?: boolean;
|
||||
tagline?: string;
|
||||
} = $props();
|
||||
|
||||
let username = $state('');
|
||||
let email = $state('');
|
||||
let identifier = $state('');
|
||||
let password = $state('');
|
||||
let submitError = $state('');
|
||||
let submitting = $state(false);
|
||||
|
||||
async function submit(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
submitError = '';
|
||||
submitting = true;
|
||||
|
||||
try {
|
||||
if (setupRequired) {
|
||||
if (!onSetup) throw new Error('The application has not configured its setup handler.');
|
||||
await onSetup({ username, email, password });
|
||||
} else {
|
||||
if (!onLogin) throw new Error('The application has not configured its login handler.');
|
||||
await onLogin({ identifier, password });
|
||||
}
|
||||
} catch (caught) {
|
||||
submitError = caught instanceof Error ? caught.message : 'Authentication failed.';
|
||||
} finally {
|
||||
submitting = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="sc-root sc-auth-page">
|
||||
<section aria-labelledby="sc-auth-title" class="sc-auth-card">
|
||||
<div class="sc-auth-brand">
|
||||
<span aria-hidden="true" class="sc-brand-mark">{brand.slice(0, 1).toUpperCase()}</span>
|
||||
<span>{brand}</span>
|
||||
</div>
|
||||
|
||||
<h1 id="sc-auth-title">{setupRequired ? 'Create your account' : 'Welcome back'}</h1>
|
||||
{#if tagline}
|
||||
<p class="sc-auth-subtitle">{tagline}</p>
|
||||
{/if}
|
||||
|
||||
<form class="sc-form sc-auth-form" onsubmit={submit}>
|
||||
{#if setupRequired}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Username</span>
|
||||
<input
|
||||
autocomplete="username"
|
||||
class="sc-input"
|
||||
name="username"
|
||||
required
|
||||
type="text"
|
||||
bind:value={username}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Email</span>
|
||||
<input
|
||||
autocomplete="email"
|
||||
class="sc-input"
|
||||
name="email"
|
||||
required
|
||||
type="email"
|
||||
bind:value={email}
|
||||
/>
|
||||
</label>
|
||||
{:else}
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Username or email</span>
|
||||
<input
|
||||
autocomplete="username"
|
||||
class="sc-input"
|
||||
name="identifier"
|
||||
required
|
||||
type="text"
|
||||
bind:value={identifier}
|
||||
/>
|
||||
</label>
|
||||
{/if}
|
||||
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Password</span>
|
||||
<input
|
||||
autocomplete={setupRequired ? 'new-password' : 'current-password'}
|
||||
class="sc-input"
|
||||
minlength="8"
|
||||
name="password"
|
||||
required
|
||||
type="password"
|
||||
bind:value={password}
|
||||
/>
|
||||
</label>
|
||||
|
||||
{#if error || submitError}
|
||||
<p aria-live="polite" class="sc-error">{submitError || error}</p>
|
||||
{/if}
|
||||
|
||||
<Button disabled={loading || submitting} fullWidth size="lg" type="submit">
|
||||
{loading || submitting ? 'Please wait…' : setupRequired ? 'Create account' : 'Log in'}
|
||||
</Button>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
@@ -0,0 +1,19 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
type BadgeVariant = 'default' | 'success' | 'warning' | 'danger';
|
||||
|
||||
let {
|
||||
children,
|
||||
class: className = '',
|
||||
variant = 'default'
|
||||
}: {
|
||||
children?: Snippet;
|
||||
class?: string;
|
||||
variant?: BadgeVariant;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<span class={`sc-badge ${className}`} data-variant={variant}>
|
||||
{@render children?.()}
|
||||
</span>
|
||||
@@ -0,0 +1,35 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'danger';
|
||||
type ButtonSize = 'sm' | 'md' | 'lg';
|
||||
|
||||
let {
|
||||
children,
|
||||
class: className = '',
|
||||
disabled = false,
|
||||
fullWidth = false,
|
||||
onclick,
|
||||
size = 'md',
|
||||
type = 'button',
|
||||
variant = 'primary'
|
||||
}: {
|
||||
children?: Snippet;
|
||||
class?: string;
|
||||
disabled?: boolean;
|
||||
fullWidth?: boolean;
|
||||
onclick?: (event: MouseEvent) => void;
|
||||
size?: ButtonSize;
|
||||
type?: 'button' | 'submit' | 'reset';
|
||||
variant?: ButtonVariant;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<button
|
||||
class={`sc-button sc-button-${variant} sc-button-${size}${fullWidth ? ' sc-button-full' : ''} ${className}`}
|
||||
{disabled}
|
||||
{onclick}
|
||||
{type}
|
||||
>
|
||||
{@render children?.()}
|
||||
</button>
|
||||
@@ -0,0 +1,15 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
let {
|
||||
children,
|
||||
class: className = ''
|
||||
}: {
|
||||
children?: Snippet;
|
||||
class?: string;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<section class={`sc-card ${className}`}>
|
||||
{@render children?.()}
|
||||
</section>
|
||||
@@ -0,0 +1,54 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
let {
|
||||
children,
|
||||
description = '',
|
||||
footer,
|
||||
open = $bindable(false),
|
||||
title
|
||||
}: {
|
||||
children?: Snippet;
|
||||
description?: string;
|
||||
footer?: Snippet;
|
||||
open?: boolean;
|
||||
title: string;
|
||||
} = $props();
|
||||
|
||||
function close(): void {
|
||||
open = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if open}
|
||||
<div
|
||||
aria-label={title}
|
||||
class="sc-dialog-backdrop"
|
||||
role="presentation"
|
||||
onclick={(event) => {
|
||||
if (event.target === event.currentTarget) close();
|
||||
}}
|
||||
>
|
||||
<section aria-modal="true" class="sc-dialog" role="dialog">
|
||||
<div class="sc-dialog-header">
|
||||
<div>
|
||||
<h2 class="sc-dialog-title">{title}</h2>
|
||||
{#if description}
|
||||
<p class="sc-dialog-description">{description}</p>
|
||||
{/if}
|
||||
</div>
|
||||
<button aria-label="Close" class="sc-dialog-close" type="button" onclick={close}>×</button>
|
||||
</div>
|
||||
|
||||
<div class="sc-dialog-body">
|
||||
{@render children?.()}
|
||||
</div>
|
||||
|
||||
{#if footer}
|
||||
<div class="sc-dialog-footer">
|
||||
{@render footer()}
|
||||
</div>
|
||||
{/if}
|
||||
</section>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -0,0 +1,80 @@
|
||||
<script lang="ts">
|
||||
import type { NavItem } from '../types';
|
||||
|
||||
let {
|
||||
brand = 'Application',
|
||||
brandHref = '/',
|
||||
currentPath = '',
|
||||
navItems = [],
|
||||
onLogout,
|
||||
onProfile,
|
||||
onSettings,
|
||||
showSettings = true,
|
||||
username = ''
|
||||
}: {
|
||||
brand?: string;
|
||||
brandHref?: string;
|
||||
currentPath?: string;
|
||||
navItems?: NavItem[];
|
||||
onLogout?: () => void | Promise<void>;
|
||||
onProfile?: () => void;
|
||||
onSettings?: () => void;
|
||||
showSettings?: boolean;
|
||||
username?: string;
|
||||
} = $props();
|
||||
|
||||
let navOpen = $state(false);
|
||||
|
||||
function isActive(item: NavItem): boolean {
|
||||
return item.href === '/'
|
||||
? currentPath === '/'
|
||||
: currentPath === item.href || currentPath.startsWith(`${item.href}/`);
|
||||
}
|
||||
</script>
|
||||
|
||||
<header class="sc-header">
|
||||
<a class="sc-brand" href={brandHref} onclick={() => (navOpen = false)}>
|
||||
<span aria-hidden="true" class="sc-brand-mark">{brand.slice(0, 1).toUpperCase()}</span>
|
||||
<span>{brand}</span>
|
||||
</a>
|
||||
|
||||
<nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}>
|
||||
{#each navItems as item}
|
||||
<a
|
||||
aria-current={isActive(item) ? 'page' : undefined}
|
||||
class="sc-nav-link"
|
||||
data-active={isActive(item)}
|
||||
href={item.href}
|
||||
onclick={() => (navOpen = false)}
|
||||
>
|
||||
{item.label}
|
||||
</a>
|
||||
{/each}
|
||||
</nav>
|
||||
|
||||
<div class="sc-header-actions">
|
||||
{#if showSettings && onSettings}
|
||||
<button aria-label="Open settings" class="sc-icon-button" type="button" onclick={onSettings}>⚙</button>
|
||||
{/if}
|
||||
|
||||
{#if username && onProfile}
|
||||
<button class="sc-profile-button" type="button" onclick={onProfile}>{username}</button>
|
||||
{/if}
|
||||
|
||||
{#if onLogout}
|
||||
<button class="sc-button sc-button-ghost sc-button-sm" type="button" onclick={() => void onLogout?.()}>
|
||||
Log out
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
<button
|
||||
aria-expanded={navOpen}
|
||||
aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'}
|
||||
class="sc-icon-button sc-mobile-menu-button"
|
||||
type="button"
|
||||
onclick={() => (navOpen = !navOpen)}
|
||||
>
|
||||
{navOpen ? '×' : '☰'}
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
@@ -0,0 +1,31 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
let {
|
||||
actions,
|
||||
children,
|
||||
description = '',
|
||||
title
|
||||
}: {
|
||||
actions?: Snippet;
|
||||
children?: Snippet;
|
||||
description?: string;
|
||||
title: string;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<div class="sc-page-header">
|
||||
<div>
|
||||
<h1>{title}</h1>
|
||||
{#if description}
|
||||
<p>{description}</p>
|
||||
{/if}
|
||||
{@render children?.()}
|
||||
</div>
|
||||
|
||||
{#if actions}
|
||||
<div>
|
||||
{@render actions()}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -0,0 +1,80 @@
|
||||
<script lang="ts">
|
||||
import type { ProfileUpdateInput } from '../types';
|
||||
import Button from './Button.svelte';
|
||||
import Dialog from './Dialog.svelte';
|
||||
|
||||
let {
|
||||
currentEmail = '',
|
||||
currentUsername = '',
|
||||
onSave,
|
||||
open = $bindable(false)
|
||||
}: {
|
||||
currentEmail?: string;
|
||||
currentUsername?: string;
|
||||
onSave?: (input: ProfileUpdateInput) => void | Promise<void>;
|
||||
open?: boolean;
|
||||
} = $props();
|
||||
|
||||
let username = $state('');
|
||||
let email = $state('');
|
||||
let error = $state('');
|
||||
let saving = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
if (open) {
|
||||
username = currentUsername;
|
||||
email = currentEmail;
|
||||
error = '';
|
||||
}
|
||||
});
|
||||
|
||||
async function save(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
error = '';
|
||||
|
||||
if (!onSave) {
|
||||
error = 'The application has not configured its profile update handler.';
|
||||
return;
|
||||
}
|
||||
|
||||
saving = true;
|
||||
try {
|
||||
await onSave({ username, email });
|
||||
open = false;
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Profile update failed.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<Dialog bind:open title="Profile">
|
||||
<div class="sc-profile-summary">
|
||||
<strong>{currentUsername || 'Your profile'}</strong>
|
||||
{#if currentEmail}
|
||||
<span>{currentEmail}</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<form class="sc-form" onsubmit={save}>
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Username</span>
|
||||
<input autocomplete="username" class="sc-input" required type="text" bind:value={username} />
|
||||
</label>
|
||||
|
||||
<label class="sc-field">
|
||||
<span class="sc-label">Email</span>
|
||||
<input autocomplete="email" class="sc-input" required type="email" bind:value={email} />
|
||||
</label>
|
||||
|
||||
{#if error}
|
||||
<p aria-live="polite" class="sc-error">{error}</p>
|
||||
{/if}
|
||||
|
||||
<div class="sc-form-actions">
|
||||
<Button variant="secondary" type="button" onclick={() => (open = false)}>Cancel</Button>
|
||||
<Button disabled={saving} type="submit">{saving ? 'Saving…' : 'Save changes'}</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Dialog>
|
||||
@@ -0,0 +1,24 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
import Dialog from './Dialog.svelte';
|
||||
|
||||
let {
|
||||
children,
|
||||
description = '',
|
||||
open = $bindable(false),
|
||||
title = 'Settings'
|
||||
}: {
|
||||
children?: Snippet;
|
||||
description?: string;
|
||||
open?: boolean;
|
||||
title?: string;
|
||||
} = $props();
|
||||
</script>
|
||||
|
||||
<Dialog bind:open {description} {title}>
|
||||
{#if children}
|
||||
{@render children()}
|
||||
{:else}
|
||||
<p class="sc-muted">No settings have been configured for this application yet.</p>
|
||||
{/if}
|
||||
</Dialog>
|
||||
Reference in new issue
Block a user