feat: add notification stack component and integrate into AppShell
This commit is contained in:
1 parent
c4b40a9c36
commit
c5450a72d6
7 files changed
+521
-9
No files matched your search
@@ -2,6 +2,7 @@
|
||||
import type { Snippet } from 'svelte';
|
||||
import type { NavItem } from '../types';
|
||||
import Header from './Header.svelte';
|
||||
import NotificationStack from './NotificationStack.svelte';
|
||||
import PageHeader from './PageHeader.svelte';
|
||||
|
||||
let {
|
||||
@@ -54,6 +55,8 @@
|
||||
</script>
|
||||
|
||||
<div class={`sc-root sc-app-shell ${className}`} data-theme={theme === 'system' ? undefined : theme}>
|
||||
<NotificationStack />
|
||||
|
||||
<Header
|
||||
aboutHref={aboutHref ?? navItems.find((item) => item.label.toLowerCase() === 'about')?.href ?? '/about'}
|
||||
{brand}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<script lang="ts">
|
||||
import { CircleAlert, CircleCheck, Info, OctagonAlert, X } from '@lucide/svelte';
|
||||
import { fly } from 'svelte/transition';
|
||||
import { notifications } from '../notifications.svelte';
|
||||
import type { NotificationVariant } from '../notifications.svelte';
|
||||
|
||||
const icons: Record<NotificationVariant, typeof Info> = {
|
||||
success: CircleCheck,
|
||||
error: OctagonAlert,
|
||||
warning: CircleAlert,
|
||||
info: Info
|
||||
};
|
||||
</script>
|
||||
|
||||
<div aria-live="polite" class="sc-notification-stack" role="status">
|
||||
{#each notifications.entries as entry (entry.id)}
|
||||
{@const Icon = icons[entry.variant]}
|
||||
<div class="sc-notification" data-variant={entry.variant} transition:fly={{ y: -12, duration: 180 }}>
|
||||
<Icon aria-hidden="true" class="sc-notification-icon" size={18} />
|
||||
<p class="sc-notification-message">{entry.message}</p>
|
||||
<button
|
||||
aria-label="Dismiss notification"
|
||||
class="sc-notification-close"
|
||||
type="button"
|
||||
onclick={() => notifications.dismiss(entry.id)}
|
||||
>
|
||||
<X aria-hidden="true" size={16} />
|
||||
</button>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
@@ -7,11 +7,14 @@ export { default as Button } from './components/Button.svelte';
|
||||
export { default as Card } from './components/Card.svelte';
|
||||
export { default as Dialog } from './components/Dialog.svelte';
|
||||
export { default as Header } from './components/Header.svelte';
|
||||
export { default as NotificationStack } from './components/NotificationStack.svelte';
|
||||
export { default as PageHeader } from './components/PageHeader.svelte';
|
||||
export { default as ProfileDialog } from './components/ProfileDialog.svelte';
|
||||
export { default as SettingsDialog } from './components/SettingsDialog.svelte';
|
||||
export { default as TotpSettings } from './components/TotpSettings.svelte';
|
||||
export { default as UserManagement } from './components/UserManagement.svelte';
|
||||
export { notifications, notify } from './notifications.svelte';
|
||||
export type { NotificationEntry, NotificationVariant } from './notifications.svelte';
|
||||
export type {
|
||||
AboutLink,
|
||||
AsyncAction,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
export type NotificationVariant = "success" | "error" | "warning" | "info";
|
||||
|
||||
export interface NotificationEntry {
|
||||
id: number;
|
||||
variant: NotificationVariant;
|
||||
message: string;
|
||||
duration: number;
|
||||
}
|
||||
|
||||
const DEFAULT_DURATION = 5000;
|
||||
|
||||
let entries = $state<NotificationEntry[]>([]);
|
||||
let nextId = 0;
|
||||
const timeouts = new Map<number, ReturnType<typeof setTimeout>>();
|
||||
|
||||
function dismissNotification(id: number): void {
|
||||
const timeout = timeouts.get(id);
|
||||
if (timeout !== undefined) {
|
||||
clearTimeout(timeout);
|
||||
timeouts.delete(id);
|
||||
}
|
||||
entries = entries.filter((entry) => entry.id !== id);
|
||||
}
|
||||
|
||||
function pushNotification(
|
||||
variant: NotificationVariant,
|
||||
message: string,
|
||||
duration: number = DEFAULT_DURATION,
|
||||
): number {
|
||||
const id = ++nextId;
|
||||
entries = [...entries, { id, variant, message, duration }];
|
||||
if (duration > 0) {
|
||||
timeouts.set(
|
||||
id,
|
||||
setTimeout(() => dismissNotification(id), duration),
|
||||
);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
export const notifications = {
|
||||
get entries() {
|
||||
return entries;
|
||||
},
|
||||
dismiss: dismissNotification,
|
||||
};
|
||||
|
||||
export const notify = {
|
||||
success: (message: string, duration?: number) => pushNotification("success", message, duration),
|
||||
error: (message: string, duration?: number) => pushNotification("error", message, duration),
|
||||
warning: (message: string, duration?: number) => pushNotification("warning", message, duration),
|
||||
info: (message: string, duration?: number) => pushNotification("info", message, duration),
|
||||
dismiss: dismissNotification,
|
||||
};
|
||||
@@ -717,6 +717,86 @@
|
||||
color: var(--sc-fg);
|
||||
}
|
||||
|
||||
.sc-notification-stack {
|
||||
position: fixed;
|
||||
z-index: 200;
|
||||
top: 1rem;
|
||||
right: 1rem;
|
||||
display: grid;
|
||||
gap: 0.625rem;
|
||||
width: min(100% - 2rem, 24rem);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.sc-notification {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 0.625rem;
|
||||
padding: 0.875rem 1rem;
|
||||
color: var(--sc-fg);
|
||||
background: var(--sc-surface-raised);
|
||||
border: 1px solid var(--sc-border);
|
||||
border-radius: var(--sc-radius-md);
|
||||
box-shadow: var(--sc-shadow-lg);
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
.sc-notification-icon {
|
||||
flex: 0 0 auto;
|
||||
margin-top: 0.125rem;
|
||||
color: var(--sc-muted-fg);
|
||||
}
|
||||
|
||||
.sc-notification[data-variant="success"] .sc-notification-icon {
|
||||
color: var(--sc-success);
|
||||
}
|
||||
|
||||
.sc-notification[data-variant="error"] .sc-notification-icon {
|
||||
color: var(--sc-danger);
|
||||
}
|
||||
|
||||
.sc-notification[data-variant="warning"] .sc-notification-icon {
|
||||
color: var(--sc-warning);
|
||||
}
|
||||
|
||||
.sc-notification[data-variant="info"] .sc-notification-icon {
|
||||
color: var(--sc-primary);
|
||||
}
|
||||
|
||||
.sc-notification-message {
|
||||
flex: 1 1 auto;
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.45;
|
||||
}
|
||||
|
||||
.sc-notification-close {
|
||||
display: grid;
|
||||
flex: 0 0 auto;
|
||||
width: 1.5rem;
|
||||
height: 1.5rem;
|
||||
place-items: center;
|
||||
color: var(--sc-muted-fg);
|
||||
background: transparent;
|
||||
border: 0;
|
||||
border-radius: var(--sc-radius-sm);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.sc-notification-close:hover {
|
||||
color: var(--sc-fg);
|
||||
background: var(--sc-surface-hover);
|
||||
}
|
||||
|
||||
@media (max-width: 30rem) {
|
||||
.sc-notification-stack {
|
||||
top: 0.75rem;
|
||||
right: 0.75rem;
|
||||
left: 0.75rem;
|
||||
width: auto;
|
||||
}
|
||||
}
|
||||
|
||||
.sc-dialog-backdrop {
|
||||
position: fixed;
|
||||
z-index: 100;
|
||||
|
||||
Reference in new issue
Block a user