@paul-portfolio/react 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Toaster.d.ts +28 -0
- package/dist/Toaster.js +62 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ToastVariant } from './Toast';
|
|
2
|
+
export type ToastInput = {
|
|
3
|
+
title: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
variant?: ToastVariant;
|
|
6
|
+
/** Auto-dismiss delay in ms. 0 keeps it until dismissed. */
|
|
7
|
+
duration?: number;
|
|
8
|
+
};
|
|
9
|
+
declare function dismiss(id: number): void;
|
|
10
|
+
/**
|
|
11
|
+
* Raise a toast from anywhere — a component, an event handler, or a plain
|
|
12
|
+
* module like a query-client error handler. `<Toaster />` renders whatever is in
|
|
13
|
+
* the queue, so the call site needs no context or hook.
|
|
14
|
+
*/
|
|
15
|
+
export declare const toast: ((input: ToastInput) => number) & {
|
|
16
|
+
success: (title: string, description?: string) => number;
|
|
17
|
+
error: (title: string, description?: string) => number;
|
|
18
|
+
warning: (title: string, description?: string) => number;
|
|
19
|
+
info: (title: string, description?: string) => number;
|
|
20
|
+
dismiss: typeof dismiss;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Mount once, near the app root. Renders the toast queue in a fixed live region
|
|
24
|
+
* — errors announced assertively, everything else politely — and portals to the
|
|
25
|
+
* body so it's never clipped. Returns nothing on the server (no document there).
|
|
26
|
+
*/
|
|
27
|
+
export declare function Toaster(): import("react").ReactPortal | null;
|
|
28
|
+
export {};
|
package/dist/Toaster.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useSyncExternalStore } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
import { cx } from './cx';
|
|
5
|
+
const DEFAULT_DURATION = 5000;
|
|
6
|
+
// A module-level store, so `toast(...)` can be called from anywhere — including
|
|
7
|
+
// outside React, which is the whole point: a global fetch/query error handler
|
|
8
|
+
// isn't a component and can't call a hook. `<Toaster />` subscribes to it.
|
|
9
|
+
const listeners = new Set();
|
|
10
|
+
const timers = new Map();
|
|
11
|
+
let records = [];
|
|
12
|
+
let nextId = 0;
|
|
13
|
+
function emit() {
|
|
14
|
+
for (const listener of listeners)
|
|
15
|
+
listener();
|
|
16
|
+
}
|
|
17
|
+
function subscribe(listener) {
|
|
18
|
+
listeners.add(listener);
|
|
19
|
+
return () => listeners.delete(listener);
|
|
20
|
+
}
|
|
21
|
+
function dismiss(id) {
|
|
22
|
+
records = records.filter((r) => r.id !== id);
|
|
23
|
+
const timer = timers.get(id);
|
|
24
|
+
if (timer) {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
timers.delete(id);
|
|
27
|
+
}
|
|
28
|
+
emit();
|
|
29
|
+
}
|
|
30
|
+
function show(input) {
|
|
31
|
+
const id = nextId++;
|
|
32
|
+
records = [...records, { ...input, id }];
|
|
33
|
+
emit();
|
|
34
|
+
const duration = input.duration ?? DEFAULT_DURATION;
|
|
35
|
+
if (duration > 0 && typeof setTimeout !== 'undefined') {
|
|
36
|
+
timers.set(id, setTimeout(() => dismiss(id), duration));
|
|
37
|
+
}
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Raise a toast from anywhere — a component, an event handler, or a plain
|
|
42
|
+
* module like a query-client error handler. `<Toaster />` renders whatever is in
|
|
43
|
+
* the queue, so the call site needs no context or hook.
|
|
44
|
+
*/
|
|
45
|
+
export const toast = Object.assign((input) => show(input), {
|
|
46
|
+
success: (title, description) => show({ title, description, variant: 'success' }),
|
|
47
|
+
error: (title, description) => show({ title, description, variant: 'error' }),
|
|
48
|
+
warning: (title, description) => show({ title, description, variant: 'warning' }),
|
|
49
|
+
info: (title, description) => show({ title, description, variant: 'default' }),
|
|
50
|
+
dismiss,
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Mount once, near the app root. Renders the toast queue in a fixed live region
|
|
54
|
+
* — errors announced assertively, everything else politely — and portals to the
|
|
55
|
+
* body so it's never clipped. Returns nothing on the server (no document there).
|
|
56
|
+
*/
|
|
57
|
+
export function Toaster() {
|
|
58
|
+
const items = useSyncExternalStore(subscribe, () => records, () => []);
|
|
59
|
+
if (typeof document === 'undefined')
|
|
60
|
+
return null;
|
|
61
|
+
return createPortal(_jsx("div", { className: "toast-region", role: "region", "aria-label": "Notifications", children: items.map((t) => (_jsxs("div", { role: t.variant === 'error' ? 'alert' : 'status', "aria-live": t.variant === 'error' ? 'assertive' : 'polite', className: cx('toast', `toast--${t.variant ?? 'default'}`), children: [_jsxs("div", { className: "toast__body", children: [_jsx("p", { className: "toast__title", children: t.title }), t.description && (_jsx("p", { className: "toast__description", children: t.description }))] }), _jsx("button", { type: "button", className: "toast__dismiss", "aria-label": "Dismiss notification", onClick: () => dismiss(t.id), children: _jsx("span", { "aria-hidden": "true", children: "\u2715" }) })] }, t.id))) }), document.body);
|
|
62
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export { CodeBlock } from './CodeBlock';
|
|
|
40
40
|
export { CommandPalette, type Command } from './CommandPalette';
|
|
41
41
|
export { Combobox, type ComboboxOption } from './Combobox';
|
|
42
42
|
export { ToastProvider, useToast, type ToastOptions, type ToastVariant, } from './Toast';
|
|
43
|
+
export { Toaster, toast, type ToastInput } from './Toaster';
|
|
43
44
|
export { TokenUsageMeter } from './TokenUsageMeter';
|
|
44
45
|
export { VisuallyHidden } from './VisuallyHidden';
|
|
45
46
|
export { GuidedTour, type GuidedTourStep } from './GuidedTour';
|
package/dist/index.js
CHANGED
|
@@ -40,6 +40,7 @@ export { CodeBlock } from './CodeBlock';
|
|
|
40
40
|
export { CommandPalette } from './CommandPalette';
|
|
41
41
|
export { Combobox } from './Combobox';
|
|
42
42
|
export { ToastProvider, useToast, } from './Toast';
|
|
43
|
+
export { Toaster, toast } from './Toaster';
|
|
43
44
|
export { TokenUsageMeter } from './TokenUsageMeter';
|
|
44
45
|
export { VisuallyHidden } from './VisuallyHidden';
|
|
45
46
|
export { GuidedTour } from './GuidedTour';
|