← Public packages
@kentcdodds/package-app-kit
Design tokens, PWA install/update, About/version, cache helpers, and optional realtime notes sync for Kody package apps.
src/ui.ts
98 lines · 2.9 KB · TypeScript/**
* Vanilla UI helpers for package apps, including a sonner-inspired toaster.
* Toaster CSS/HTML for package apps. Browser boot lives in committed `src/client/toast.js` (served via `./client`), not string emitters.
*/
export type ToastTone = 'default' | 'success' | 'warning' | 'error'
export type ToastAction = {
label: string
onClick: () => void
}
export type ShowToastInput = {
title: string
description?: string
tone?: ToastTone
/** Milliseconds; omit or 0 for sticky (update toasts). */
durationMs?: number
action?: ToastAction
id?: string
}
/**
* CSS for the floating toast host (sonner-like stack).
* Include once in the app shell after theme CSS.
*
* @returns Toast host CSS
*
* @example
* import { toastCss } from 'kody:@kentcdodds/package-app-kit/ui'
* const css = toastCss()
*/
export function toastCss(): string {
return `
.pak-toast-host {
position: fixed;
z-index: 80;
left: 50%;
bottom: calc(16px + env(safe-area-inset-bottom));
transform: translateX(-50%);
width: min(420px, calc(100% - 24px));
display: grid;
gap: 8px;
pointer-events: none;
}
.pak-toast {
pointer-events: auto;
display: grid;
grid-template-columns: 1fr auto;
gap: 8px 10px;
align-items: center;
padding: 12px 12px 12px 14px;
border-radius: var(--radius, 12px);
border: 1px solid var(--line);
background: color-mix(in srgb, var(--surface) 92%, transparent);
backdrop-filter: blur(10px);
box-shadow: var(--pak-shadow-out, 0 10px 24px rgba(0,0,0,0.18));
animation: pak-toast-in 160ms ease-out;
}
.pak-toast[data-tone="success"] { border-color: color-mix(in srgb, var(--good) 45%, var(--line)); }
.pak-toast[data-tone="warning"] { border-color: color-mix(in srgb, var(--warn) 45%, var(--line)); }
.pak-toast[data-tone="error"] { border-color: color-mix(in srgb, var(--bad) 45%, var(--line)); }
.pak-toast strong { display: block; font-size: 0.95rem; }
.pak-toast p { margin: 2px 0 0; color: var(--muted); font-size: 0.85rem; }
.pak-toast-actions { display: flex; gap: 6px; align-items: center; }
.pak-toast button {
border: 0; border-radius: 999px; min-height: 36px; padding: 0 12px;
font: inherit; font-weight: 650; cursor: pointer;
background: linear-gradient(180deg, var(--accent), var(--accent-2));
color: var(--accent-ink);
}
.pak-toast [data-toast-dismiss] {
background: transparent; color: var(--muted); min-width: 36px; padding: 0 8px;
}
@keyframes pak-toast-in {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: translateY(0); }
}
`.trim()
}
/**
* HTML fragment for the toast host. Place near end of `<body>`.
*
* @returns Toast host markup
*
* @example
* import { toastHostHtml } from 'kody:@kentcdodds/package-app-kit/ui'
* const html = toastHostHtml()
*/
export function toastHostHtml(): string {
return `<div class="pak-toast-host" data-toast-host aria-live="polite"></div>`
}
/** Primary callable export for this subpath. */
export default toastCss