@eifi1/ui-kit 0.8.1 → 0.10.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/README.md +25 -11
- package/dist/components/alert-banner.d.ts +32 -5
- package/dist/components/alert-banner.js +57 -14
- package/dist/components/alert-banner.js.map +1 -1
- package/dist/components/amount-input.d.ts +6 -0
- package/dist/components/breadcrumbs.d.ts +60 -0
- package/dist/components/breadcrumbs.js +88 -0
- package/dist/components/breadcrumbs.js.map +1 -0
- package/dist/components/bulk-action-bar.d.ts +66 -0
- package/dist/components/bulk-action-bar.js +149 -0
- package/dist/components/bulk-action-bar.js.map +1 -0
- package/dist/components/calculator.d.ts +6 -0
- package/dist/components/chip.d.ts +58 -10
- package/dist/components/chip.js +91 -7
- package/dist/components/chip.js.map +1 -1
- package/dist/components/copy-button.d.ts +13 -4
- package/dist/components/copy-button.js +17 -4
- package/dist/components/copy-button.js.map +1 -1
- package/dist/components/data-table-filter-popover.d.ts +1 -1
- package/dist/components/data-table-filters.d.ts +1 -1
- package/dist/components/data-table.d.ts +1 -1
- package/dist/components/data-table.js +122 -91
- package/dist/components/data-table.js.map +1 -1
- package/dist/components/date-picker.d.ts +89 -3
- package/dist/components/date-picker.js +163 -68
- package/dist/components/date-picker.js.map +1 -1
- package/dist/components/description-list.d.ts +43 -6
- package/dist/components/description-list.js +91 -11
- package/dist/components/description-list.js.map +1 -1
- package/dist/components/dialog-frame.d.ts +7 -0
- package/dist/components/dialog-frame.js.map +1 -1
- package/dist/components/disclosure.d.ts +68 -4
- package/dist/components/disclosure.js +33 -15
- package/dist/components/disclosure.js.map +1 -1
- package/dist/components/file-dropzone.d.ts +3 -4
- package/dist/components/file-dropzone.js +2 -1
- package/dist/components/file-dropzone.js.map +1 -1
- package/dist/components/floating-panel.d.ts +29 -2
- package/dist/components/floating-panel.js +16 -2
- package/dist/components/floating-panel.js.map +1 -1
- package/dist/components/full-bleed-dialog.d.ts +23 -4
- package/dist/components/full-bleed-dialog.js +11 -2
- package/dist/components/full-bleed-dialog.js.map +1 -1
- package/dist/components/list.d.ts +174 -0
- package/dist/components/list.js +215 -0
- package/dist/components/list.js.map +1 -0
- package/dist/components/menu-item.d.ts +99 -0
- package/dist/components/menu-item.js +83 -0
- package/dist/components/menu-item.js.map +1 -0
- package/dist/components/modal.d.ts +23 -1
- package/dist/components/modal.js +36 -10
- package/dist/components/modal.js.map +1 -1
- package/dist/components/number-field.d.ts +6 -0
- package/dist/components/number-input.d.ts +6 -0
- package/dist/components/numpad-sheet.d.ts +6 -0
- package/dist/components/page-contents.js +1 -2
- package/dist/components/page-contents.js.map +1 -1
- package/dist/components/page-header.d.ts +37 -0
- package/dist/components/page-header.js +36 -0
- package/dist/components/page-header.js.map +1 -0
- package/dist/components/progress-bar.d.ts +16 -2
- package/dist/components/progress-bar.js +4 -2
- package/dist/components/progress-bar.js.map +1 -1
- package/dist/components/series-chart-labels.d.ts +3 -0
- package/dist/components/series-chart-labels.js +2 -1
- package/dist/components/series-chart-labels.js.map +1 -1
- package/dist/components/series-chart.d.ts +16 -0
- package/dist/components/series-chart.js +137 -3
- package/dist/components/series-chart.js.map +1 -1
- package/dist/components/status-dot.d.ts +49 -0
- package/dist/components/status-dot.js +52 -0
- package/dist/components/status-dot.js.map +1 -0
- package/dist/components/table.d.ts +39 -6
- package/dist/components/table.js +43 -4
- package/dist/components/table.js.map +1 -1
- package/dist/components/text.d.ts +63 -0
- package/dist/components/text.js +23 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/toast.d.ts +167 -0
- package/dist/components/toast.js +226 -0
- package/dist/components/toast.js.map +1 -0
- package/dist/components/toggle-group.d.ts +29 -1
- package/dist/components/toggle-group.js +52 -3
- package/dist/components/toggle-group.js.map +1 -1
- package/dist/components/tooltip.d.ts +38 -15
- package/dist/components/tooltip.js +85 -47
- package/dist/components/tooltip.js.map +1 -1
- package/dist/components/ui.d.ts +139 -16
- package/dist/components/ui.js +136 -26
- package/dist/components/ui.js.map +1 -1
- package/dist/components/use-table-state.d.ts +1 -1
- package/dist/{data-table-filters-CF1PXqjQ.d.ts → data-table-filters-Dh9uF_S-.d.ts} +33 -2
- package/dist/data-table.d.ts +1 -1
- package/dist/hooks/use-close-transition.d.ts +7 -1
- package/dist/hooks/use-close-transition.js +5 -2
- package/dist/hooks/use-close-transition.js.map +1 -1
- package/dist/hooks/use-copy-to-clipboard.js +1 -1
- package/dist/hooks/use-copy-to-clipboard.js.map +1 -1
- package/dist/hooks/use-overlay-history.js +84 -4
- package/dist/hooks/use-overlay-history.js.map +1 -1
- package/dist/i18n/defaults.d.ts +6 -0
- package/dist/i18n/defaults.js +11 -1
- package/dist/i18n/defaults.js.map +1 -1
- package/dist/i18n/kit-labels.d.ts +11 -0
- package/dist/i18n/kit-labels.js.map +1 -1
- package/dist/i18n/locales/de-CH-informal.d.ts +6 -0
- package/dist/i18n/locales/de-CH.d.ts +6 -0
- package/dist/i18n/locales/de-informal.d.ts +6 -0
- package/dist/i18n/locales/de.d.ts +6 -0
- package/dist/i18n/locales/de.js +31 -1
- package/dist/i18n/locales/de.js.map +1 -1
- package/dist/i18n/locales/es.d.ts +6 -0
- package/dist/i18n/locales/es.js +31 -1
- package/dist/i18n/locales/es.js.map +1 -1
- package/dist/i18n/locales/fr.d.ts +6 -0
- package/dist/i18n/locales/fr.js +31 -1
- package/dist/i18n/locales/fr.js.map +1 -1
- package/dist/i18n/locales/hu.d.ts +6 -0
- package/dist/i18n/locales/hu.js +31 -1
- package/dist/i18n/locales/hu.js.map +1 -1
- package/dist/i18n/locales/it.d.ts +6 -0
- package/dist/i18n/locales/it.js +31 -1
- package/dist/i18n/locales/it.js.map +1 -1
- package/dist/i18n/locales/zh.d.ts +6 -0
- package/dist/i18n/locales/zh.js +31 -1
- package/dist/i18n/locales/zh.js.map +1 -1
- package/dist/index.d.ts +18 -8
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/clipping.d.ts +9 -0
- package/dist/lib/clipping.js +14 -0
- package/dist/lib/clipping.js.map +1 -0
- package/dist/search/command-palette.d.ts +67 -2
- package/dist/search/command-palette.js +147 -33
- package/dist/search/command-palette.js.map +1 -1
- package/dist/search/global-search.d.ts +166 -0
- package/dist/search/global-search.js +293 -0
- package/dist/search/global-search.js.map +1 -0
- package/dist/search/search-index.d.ts +105 -0
- package/dist/search/search-index.js +142 -0
- package/dist/search/search-index.js.map +1 -0
- package/dist/search.d.ts +3 -1
- package/dist/search.js +2 -0
- package/dist/search.js.map +1 -1
- package/dist/wizard/stepper-nav.d.ts +10 -1
- package/dist/wizard/stepper-nav.js +3 -2
- package/dist/wizard/stepper-nav.js.map +1 -1
- package/dist/wizard/types.d.ts +2 -2
- package/dist/wizard/types.js.map +1 -1
- package/dist/wizard/use-wizard.js +1 -1
- package/dist/wizard/use-wizard.js.map +1 -1
- package/dist/wizard/wizard-summary.js +3 -3
- package/dist/wizard/wizard-summary.js.map +1 -1
- package/package.json +25 -12
- package/src/components/alert-banner.tsx +117 -18
- package/src/components/breadcrumbs.tsx +168 -0
- package/src/components/bulk-action-bar.tsx +256 -0
- package/src/components/chip.tsx +164 -13
- package/src/components/copy-button.tsx +29 -7
- package/src/components/data-table.tsx +81 -5
- package/src/components/date-picker.tsx +377 -118
- package/src/components/description-list.tsx +168 -15
- package/src/components/dialog-frame.tsx +7 -0
- package/src/components/disclosure.tsx +116 -28
- package/src/components/file-dropzone.tsx +9 -10
- package/src/components/floating-panel.tsx +46 -2
- package/src/components/full-bleed-dialog.tsx +42 -5
- package/src/components/list.tsx +417 -0
- package/src/components/menu-item.tsx +214 -0
- package/src/components/modal.tsx +80 -17
- package/src/components/page-contents.tsx +4 -4
- package/src/components/page-header.tsx +68 -0
- package/src/components/progress-bar.tsx +27 -3
- package/src/components/series-chart-labels.ts +4 -0
- package/src/components/series-chart.tsx +253 -3
- package/src/components/status-dot.tsx +99 -0
- package/src/components/table.tsx +91 -6
- package/src/components/text.tsx +89 -0
- package/src/components/toast.tsx +441 -0
- package/src/components/toggle-group.tsx +82 -3
- package/src/components/tooltip.tsx +186 -100
- package/src/components/ui.tsx +321 -41
- package/src/hooks/use-close-transition.ts +14 -5
- package/src/hooks/use-copy-to-clipboard.ts +1 -1
- package/src/hooks/use-overlay-history.ts +181 -4
- package/src/i18n/defaults.ts +10 -0
- package/src/i18n/kit-labels.tsx +10 -0
- package/src/i18n/locales/de.ts +30 -0
- package/src/i18n/locales/es.ts +30 -0
- package/src/i18n/locales/fr.ts +30 -0
- package/src/i18n/locales/hu.ts +30 -0
- package/src/i18n/locales/it.ts +30 -0
- package/src/i18n/locales/zh.ts +30 -0
- package/src/index.ts +30 -0
- package/src/lib/clipping.ts +16 -0
- package/src/search/command-palette.tsx +241 -31
- package/src/search/global-search.tsx +525 -0
- package/src/search/search-index.ts +295 -0
- package/src/search.ts +2 -0
- package/src/wizard/stepper-nav.tsx +13 -2
- package/src/wizard/types.ts +2 -2
- package/src/wizard/use-wizard.ts +3 -3
- package/src/wizard/wizard-summary.tsx +14 -10
- package/tokens.css +97 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { useEffect, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import type { CSSProperties, MouseEvent, ReactElement, ReactNode } from "react";
|
|
3
|
+
import { useKitLabels } from "../i18n/kit-labels";
|
|
4
|
+
import { useMediaQuery } from "../hooks/use-media-query";
|
|
5
|
+
import { PHONE_QUERY } from "./ui";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The kit's toast layer: a `toast` API and a `<Toaster>`, over sonner.
|
|
9
|
+
*
|
|
10
|
+
* WHY A WRAPPER AND NOT "USE SONNER". Both apps mounted sonner by hand and carried the
|
|
11
|
+
* same policy in it — bottom-centre on a phone so the toast covers neither the header
|
|
12
|
+
* nor the bottom nav, a lift above that nav, swipe in every direction, a z-index above
|
|
13
|
+
* every overlay, the theme fed in from the store — and one of them wrote that policy
|
|
14
|
+
* twice. `toast` mirrors sonner's call shape, so migrating is an import swap:
|
|
15
|
+
*
|
|
16
|
+
* import { toast } from "@eifi1/ui-kit"; // was: from "sonner"
|
|
17
|
+
*
|
|
18
|
+
* WHY SONNER IS LOADED LAZILY. It is an OPTIONAL peer, and this module is re-exported by
|
|
19
|
+
* the barrel: a static import here would make every app that imports a Button install
|
|
20
|
+
* it (see optional-peer-imports.test.tsx). So nothing here imports it statically. The
|
|
21
|
+
* `<Toaster>` loads it when it mounts — long before anyone clicks anything — and a call
|
|
22
|
+
* made before that is queued and replayed in order the moment it has loaded. Every call
|
|
23
|
+
* returns its id synchronously anyway: the id is chosen here, not by sonner.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
type Sonner = typeof import("sonner");
|
|
27
|
+
type SonnerData = NonNullable<Parameters<Sonner["toast"]>[1]>;
|
|
28
|
+
|
|
29
|
+
/* ── Loading sonner ──────────────────────────────────────────────────────── */
|
|
30
|
+
|
|
31
|
+
let sonner: Sonner | null = null;
|
|
32
|
+
let loading: Promise<Sonner | null> | null = null;
|
|
33
|
+
const queue: Array<(s: Sonner) => void> = [];
|
|
34
|
+
|
|
35
|
+
function loadSonner(): Promise<Sonner | null> {
|
|
36
|
+
loading ??= import("sonner").then(
|
|
37
|
+
(mod) => {
|
|
38
|
+
sonner = mod;
|
|
39
|
+
// Flushed in the SAME callback that sets `sonner`, so a call arriving between
|
|
40
|
+
// the two cannot overtake one that was queued before it.
|
|
41
|
+
for (const fn of queue.splice(0)) fn(mod);
|
|
42
|
+
return mod;
|
|
43
|
+
},
|
|
44
|
+
() => {
|
|
45
|
+
queue.length = 0;
|
|
46
|
+
console.warn(
|
|
47
|
+
"@eifi1/ui-kit: `toast` needs the optional peer `sonner` — install it (npm i sonner) to show toasts.",
|
|
48
|
+
);
|
|
49
|
+
return null;
|
|
50
|
+
},
|
|
51
|
+
);
|
|
52
|
+
return loading;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function withSonner(fn: (s: Sonner) => void): void {
|
|
56
|
+
if (sonner) {
|
|
57
|
+
fn(sonner);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
queue.push(fn);
|
|
61
|
+
void loadSonner();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let counter = 0;
|
|
65
|
+
const nextId = () => `kit-toast-${++counter}`;
|
|
66
|
+
|
|
67
|
+
/* ── Labels ──────────────────────────────────────────────────────────────── */
|
|
68
|
+
|
|
69
|
+
/** The words the toast layer says. `close` and `notifications` are read by a screen
|
|
70
|
+
* reader (the close button's name, the toast region's name); `undo` and `redo` are
|
|
71
|
+
* the default action labels of {@link toast.undo} / {@link toast.redo}. */
|
|
72
|
+
export interface ToastLabels {
|
|
73
|
+
close: string;
|
|
74
|
+
notifications: string;
|
|
75
|
+
undo: string;
|
|
76
|
+
redo: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const DEFAULT_TOAST_LABELS: ToastLabels = {
|
|
80
|
+
close: "Close notification",
|
|
81
|
+
notifications: "Notifications",
|
|
82
|
+
undo: "Undo",
|
|
83
|
+
redo: "Redo",
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** What the mounted `<Toaster>` resolved from `<UiKitProvider>` — so `toast.undo()`,
|
|
87
|
+
* which is called from mutation callbacks and not from a component, still says
|
|
88
|
+
* "Rückgängig" in a German app. */
|
|
89
|
+
let activeLabels: ToastLabels = DEFAULT_TOAST_LABELS;
|
|
90
|
+
|
|
91
|
+
/* ── The API ─────────────────────────────────────────────────────────────── */
|
|
92
|
+
|
|
93
|
+
export type ToastId = string | number;
|
|
94
|
+
|
|
95
|
+
/** A button on the toast. Clicking it dismisses the toast, unless `onClick` calls
|
|
96
|
+
* `event.preventDefault()`. */
|
|
97
|
+
export interface ToastAction {
|
|
98
|
+
label: ReactNode;
|
|
99
|
+
onClick: (event: MouseEvent<HTMLButtonElement>) => void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface ToastOptions {
|
|
103
|
+
/** Re-using an id REPLACES the toast on screen with that id (in place — it does not
|
|
104
|
+
* stack a second one), which is how a toast is updated. Default: a fresh id. */
|
|
105
|
+
id?: ToastId;
|
|
106
|
+
description?: ReactNode;
|
|
107
|
+
action?: ToastAction;
|
|
108
|
+
/** A second, quieter button (sonner's `cancel`). */
|
|
109
|
+
cancel?: ToastAction;
|
|
110
|
+
/** Milliseconds; `Infinity` keeps it until dismissed. Default 4s — or
|
|
111
|
+
* {@link TOAST_ACTION_DURATION} when there is an `action`, so there is time to
|
|
112
|
+
* read the message AND reach the button. */
|
|
113
|
+
duration?: number;
|
|
114
|
+
/** Called when the toast is closed by the user (close button, swipe, action). */
|
|
115
|
+
onDismiss?: (toast: { id: ToastId }) => void;
|
|
116
|
+
/** Called when the toast times out. */
|
|
117
|
+
onAutoClose?: (toast: { id: ToastId }) => void;
|
|
118
|
+
/** Wrap the message and the description in `data-private`, so an app's demo /
|
|
119
|
+
* privacy mode blurs them like every other sensitive value. */
|
|
120
|
+
redact?: boolean;
|
|
121
|
+
/** Replaces the tone's icon. */
|
|
122
|
+
icon?: ReactNode;
|
|
123
|
+
/** `false`: no close button, no swipe. */
|
|
124
|
+
dismissible?: boolean;
|
|
125
|
+
/** Per toast, over the `<Toaster>`'s `closeButton`. */
|
|
126
|
+
closeButton?: boolean;
|
|
127
|
+
className?: string;
|
|
128
|
+
style?: CSSProperties;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The default duration of a toast that carries an action (8s rather than 4s). */
|
|
132
|
+
export const TOAST_ACTION_DURATION = 8000;
|
|
133
|
+
|
|
134
|
+
type Kind = "default" | "success" | "error" | "warning" | "info" | "loading";
|
|
135
|
+
|
|
136
|
+
const privately = (node: ReactNode) => <span data-private="">{node}</span>;
|
|
137
|
+
|
|
138
|
+
function toSonnerData(opts: ToastOptions | undefined, id: ToastId): SonnerData {
|
|
139
|
+
const { redact, description, duration, action, ...rest } = opts ?? {};
|
|
140
|
+
const data: SonnerData = { ...rest, id };
|
|
141
|
+
if (description !== undefined) data.description = redact ? privately(description) : description;
|
|
142
|
+
if (action) data.action = action;
|
|
143
|
+
const resolved = duration ?? (action ? TOAST_ACTION_DURATION : undefined);
|
|
144
|
+
if (resolved !== undefined) data.duration = resolved;
|
|
145
|
+
return data;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function show(kind: Kind, message: ReactNode, opts?: ToastOptions): ToastId {
|
|
149
|
+
const id = opts?.id ?? nextId();
|
|
150
|
+
const title = opts?.redact ? privately(message) : message;
|
|
151
|
+
const data = toSonnerData(opts, id);
|
|
152
|
+
withSonner((s) => {
|
|
153
|
+
if (kind === "default") s.toast(title, data);
|
|
154
|
+
else s.toast[kind](title, data);
|
|
155
|
+
});
|
|
156
|
+
return id;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface ToastPromiseOptions<T> extends ToastOptions {
|
|
160
|
+
loading: ReactNode;
|
|
161
|
+
/** The success message, or a function of the resolved value. Omitted: the loading
|
|
162
|
+
* toast is dismissed. */
|
|
163
|
+
success?: ReactNode | ((value: T) => ReactNode);
|
|
164
|
+
/** The error message, or a function of the rejection. Omitted: dismissed. */
|
|
165
|
+
error?: ReactNode | ((error: unknown) => ReactNode);
|
|
166
|
+
finally?: () => void;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface ToastUndoOptions extends Omit<ToastOptions, "action"> {
|
|
170
|
+
/** Called when the button is pressed. */
|
|
171
|
+
onUndo: () => void;
|
|
172
|
+
/** Default: `toast.undo` from `<UiKitProvider>` ("Undo"). */
|
|
173
|
+
label?: ReactNode;
|
|
174
|
+
/** Default `success` — the step the toast reports worked. */
|
|
175
|
+
tone?: Exclude<Kind, "loading">;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface ToastRedoOptions extends Omit<ToastUndoOptions, "onUndo"> {
|
|
179
|
+
onRedo: () => void;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function undoable(
|
|
183
|
+
message: ReactNode,
|
|
184
|
+
{ tone = "success", label, run, ...opts }: Omit<ToastUndoOptions, "onUndo"> & { run: () => void },
|
|
185
|
+
fallback: ReactNode,
|
|
186
|
+
): ToastId {
|
|
187
|
+
return show(tone, message, {
|
|
188
|
+
...opts,
|
|
189
|
+
action: { label: label ?? fallback, onClick: () => run() },
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Show a toast. The same shape as sonner's `toast`, so an app migrates by changing the
|
|
195
|
+
* import: `toast(msg, opts)`, `toast.success` / `error` / `warning` / `info` /
|
|
196
|
+
* `loading`, `toast.promise`, `toast.dismiss`, `toast.custom`. Each returns the toast's
|
|
197
|
+
* id; pass it back as `id` to replace that toast in place.
|
|
198
|
+
*
|
|
199
|
+
* Needs a mounted {@link Toaster}. Messages may be any ReactNode.
|
|
200
|
+
*/
|
|
201
|
+
export const toast = Object.assign(
|
|
202
|
+
(message: ReactNode, opts?: ToastOptions): ToastId => show("default", message, opts),
|
|
203
|
+
{
|
|
204
|
+
message: (message: ReactNode, opts?: ToastOptions) => show("default", message, opts),
|
|
205
|
+
success: (message: ReactNode, opts?: ToastOptions) => show("success", message, opts),
|
|
206
|
+
error: (message: ReactNode, opts?: ToastOptions) => show("error", message, opts),
|
|
207
|
+
warning: (message: ReactNode, opts?: ToastOptions) => show("warning", message, opts),
|
|
208
|
+
info: (message: ReactNode, opts?: ToastOptions) => show("info", message, opts),
|
|
209
|
+
/** A spinner that does not time out — replace it (same `id`) or dismiss it. */
|
|
210
|
+
loading: (message: ReactNode, opts?: ToastOptions) => show("loading", message, opts),
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A loading toast that becomes the success or the error toast, in place, when
|
|
214
|
+
* `promise` settles. Returns the toast's id; the promise itself is the caller's.
|
|
215
|
+
*/
|
|
216
|
+
promise<T>(promise: Promise<T> | (() => Promise<T>), options: ToastPromiseOptions<T>): ToastId {
|
|
217
|
+
const { loading: pending, success, error, finally: onFinally, ...opts } = options;
|
|
218
|
+
const id = show("loading", pending, opts);
|
|
219
|
+
const same = { ...opts, id };
|
|
220
|
+
Promise.resolve()
|
|
221
|
+
.then(() => (typeof promise === "function" ? promise() : promise))
|
|
222
|
+
.then(
|
|
223
|
+
(value) => {
|
|
224
|
+
if (success === undefined) toast.dismiss(id);
|
|
225
|
+
else show("success", typeof success === "function" ? success(value) : success, same);
|
|
226
|
+
},
|
|
227
|
+
(reason: unknown) => {
|
|
228
|
+
if (error === undefined) toast.dismiss(id);
|
|
229
|
+
else show("error", typeof error === "function" ? error(reason) : error, same);
|
|
230
|
+
},
|
|
231
|
+
)
|
|
232
|
+
.finally(() => onFinally?.());
|
|
233
|
+
return id;
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
/** Close one toast, or every toast when `id` is omitted. */
|
|
237
|
+
dismiss(id?: ToastId): void {
|
|
238
|
+
withSonner((s) => s.toast.dismiss(id));
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
/** Render your own node in a toast slot — `render` receives the id to dismiss it. */
|
|
242
|
+
custom(render: (id: ToastId) => ReactElement, opts?: Omit<ToastOptions, "redact">): ToastId {
|
|
243
|
+
const id = opts?.id ?? nextId();
|
|
244
|
+
const data = toSonnerData(opts, id);
|
|
245
|
+
withSonner((s) => s.toast.custom(render, data));
|
|
246
|
+
return id;
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* "Deleted — Undo": a toast whose action takes the step back, labelled by the
|
|
251
|
+
* provider's `toast.undo`. The journal stays the app's; this is only its toast.
|
|
252
|
+
* Pair with {@link toast.redo} so the step back can itself be taken back.
|
|
253
|
+
*/
|
|
254
|
+
undo(message: ReactNode, { onUndo, ...opts }: ToastUndoOptions): ToastId {
|
|
255
|
+
return undoable(message, { ...opts, run: onUndo }, activeLabels.undo);
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
/** {@link toast.undo}'s mirror: the action is labelled `toast.redo`. */
|
|
259
|
+
redo(message: ReactNode, { onRedo, ...opts }: ToastRedoOptions): ToastId {
|
|
260
|
+
return undoable(message, { ...opts, run: onRedo }, activeLabels.redo);
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
/* ── The Toaster ─────────────────────────────────────────────────────────── */
|
|
266
|
+
|
|
267
|
+
export type ToastPosition =
|
|
268
|
+
| "top-left"
|
|
269
|
+
| "top-center"
|
|
270
|
+
| "top-right"
|
|
271
|
+
| "bottom-left"
|
|
272
|
+
| "bottom-center"
|
|
273
|
+
| "bottom-right";
|
|
274
|
+
|
|
275
|
+
export type ToastSwipeDirection = "top" | "right" | "bottom" | "left";
|
|
276
|
+
|
|
277
|
+
/** sonner's offset: one value for every edge, or per edge. Numbers are px. */
|
|
278
|
+
export type ToasterOffset =
|
|
279
|
+
| string
|
|
280
|
+
| number
|
|
281
|
+
| { top?: string | number; right?: string | number; bottom?: string | number; left?: string | number };
|
|
282
|
+
|
|
283
|
+
export interface ToasterProps {
|
|
284
|
+
/** Default: `bottom-center` on a phone (so the toast covers neither the header's
|
|
285
|
+
* buttons nor the bottom nav's), `top-center` from `md` up. */
|
|
286
|
+
position?: ToastPosition;
|
|
287
|
+
/** The height to keep clear at the bottom — AppShell's bottom nav. Default: its
|
|
288
|
+
* measured `--app-nav-h`, so nothing has to be passed under an AppShell. The safe
|
|
289
|
+
* area is added either way. Numbers are px. */
|
|
290
|
+
navOffset?: string | number;
|
|
291
|
+
/** Default: follows the `.dark` class the kit's theme store (`useApplyTheme`) puts
|
|
292
|
+
* on `<html>` — whichever store the app built, with nothing passed. */
|
|
293
|
+
theme?: "light" | "dark" | "system";
|
|
294
|
+
labels?: Partial<ToastLabels>;
|
|
295
|
+
/** Default 4s (8s for a toast with an action). */
|
|
296
|
+
duration?: number;
|
|
297
|
+
visibleToasts?: number;
|
|
298
|
+
expand?: boolean;
|
|
299
|
+
/** Default true. */
|
|
300
|
+
closeButton?: boolean;
|
|
301
|
+
/** Tone-coloured toasts (the AlertBanner tokens). Default true. */
|
|
302
|
+
richColors?: boolean;
|
|
303
|
+
/** Default: every direction. */
|
|
304
|
+
swipeDirections?: ToastSwipeDirection[];
|
|
305
|
+
/** Overrides the computed offset outright (above the nav on a phone, below a 48px
|
|
306
|
+
* top bar on desktop). */
|
|
307
|
+
offset?: ToasterOffset;
|
|
308
|
+
/** Below 600px, where sonner switches to its full-width layout. Default: `offset`'s
|
|
309
|
+
* phone value. */
|
|
310
|
+
mobileOffset?: ToasterOffset;
|
|
311
|
+
/** Keyboard shortcut that moves focus into the toasts. Default Alt+T. */
|
|
312
|
+
hotkey?: string[];
|
|
313
|
+
gap?: number;
|
|
314
|
+
dir?: "ltr" | "rtl" | "auto";
|
|
315
|
+
className?: string;
|
|
316
|
+
style?: CSSProperties;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** The kit's palette, mapped onto sonner's variables. Inline on the toaster element,
|
|
320
|
+
* so it wins over sonner's injected sheet with no specificity contest. The tone
|
|
321
|
+
* backgrounds go through `--toast-*` aliases computed at `:root` in tokens.css:
|
|
322
|
+
* sonner redefines `--success-bg` on this same element, and a variable cannot read
|
|
323
|
+
* its own name — `--success-bg: var(--success-bg)` here would be a cycle. */
|
|
324
|
+
const KIT_TOASTER_STYLE = {
|
|
325
|
+
zIndex: "var(--z-toast, 2147483647)",
|
|
326
|
+
fontFamily: "inherit",
|
|
327
|
+
"--normal-bg": "var(--bg-surface)",
|
|
328
|
+
"--normal-bg-hover": "var(--bg-hover)",
|
|
329
|
+
"--normal-border": "var(--border)",
|
|
330
|
+
"--normal-border-hover": "var(--border-strong)",
|
|
331
|
+
"--normal-text": "var(--text-primary)",
|
|
332
|
+
"--gray2": "var(--bg-hover)",
|
|
333
|
+
"--gray5": "var(--border-strong)",
|
|
334
|
+
"--gray11": "var(--text-muted)",
|
|
335
|
+
"--success-bg": "var(--toast-success-bg)",
|
|
336
|
+
"--success-border": "var(--toast-success-border)",
|
|
337
|
+
"--success-text": "var(--success)",
|
|
338
|
+
"--info-bg": "var(--toast-info-bg)",
|
|
339
|
+
"--info-border": "var(--toast-info-border)",
|
|
340
|
+
"--info-text": "var(--info)",
|
|
341
|
+
"--warning-bg": "var(--toast-warning-bg)",
|
|
342
|
+
"--warning-border": "var(--toast-warning-border)",
|
|
343
|
+
"--warning-text": "var(--warning)",
|
|
344
|
+
"--error-bg": "var(--toast-danger-bg)",
|
|
345
|
+
"--error-border": "var(--toast-danger-border)",
|
|
346
|
+
"--error-text": "var(--danger)",
|
|
347
|
+
} as CSSProperties;
|
|
348
|
+
|
|
349
|
+
const ALL_DIRECTIONS: ToastSwipeDirection[] = ["top", "right", "bottom", "left"];
|
|
350
|
+
|
|
351
|
+
/** Light or dark, as the `.dark` class on `<html>` says right now. */
|
|
352
|
+
function readDocumentTheme(): "light" | "dark" {
|
|
353
|
+
return typeof document !== "undefined" && document.documentElement.classList.contains("dark")
|
|
354
|
+
? "dark"
|
|
355
|
+
: "light";
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function subscribeDocumentTheme(onChange: () => void): () => void {
|
|
359
|
+
if (typeof MutationObserver === "undefined") return () => {};
|
|
360
|
+
const mo = new MutationObserver(onChange);
|
|
361
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
|
362
|
+
return () => mo.disconnect();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const px = (v: string | number) => (typeof v === "number" ? `${v}px` : v);
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Where the toasts appear, with the kit's defaults — mount ONE, near the root and
|
|
369
|
+
* inside `<UiKitProvider>` (it reads the `toast` labels) and inside the router if a
|
|
370
|
+
* toast's content navigates.
|
|
371
|
+
*
|
|
372
|
+
* Accessible as sonner makes it: the toasts sit in a polite live region, so a message
|
|
373
|
+
* is announced with its description and its action's label; every toast and every
|
|
374
|
+
* button in it is focusable, the timer pauses while focus or the pointer is inside,
|
|
375
|
+
* and Alt+T ({@link ToasterProps.hotkey}) moves focus to the newest toast. A toast
|
|
376
|
+
* with an action stays up 8s by default, so the button can be reached.
|
|
377
|
+
*/
|
|
378
|
+
export function Toaster({
|
|
379
|
+
position,
|
|
380
|
+
navOffset,
|
|
381
|
+
theme,
|
|
382
|
+
labels: labelsProp,
|
|
383
|
+
closeButton = true,
|
|
384
|
+
richColors = true,
|
|
385
|
+
swipeDirections = ALL_DIRECTIONS,
|
|
386
|
+
offset,
|
|
387
|
+
mobileOffset,
|
|
388
|
+
className,
|
|
389
|
+
style,
|
|
390
|
+
...rest
|
|
391
|
+
}: ToasterProps) {
|
|
392
|
+
const [mod, setMod] = useState<Sonner | null>(sonner);
|
|
393
|
+
useEffect(() => {
|
|
394
|
+
if (mod) return;
|
|
395
|
+
let live = true;
|
|
396
|
+
void loadSonner().then((m) => {
|
|
397
|
+
if (live && m) setMod(m);
|
|
398
|
+
});
|
|
399
|
+
return () => {
|
|
400
|
+
live = false;
|
|
401
|
+
};
|
|
402
|
+
}, [mod]);
|
|
403
|
+
|
|
404
|
+
const labels = useKitLabels("toast", DEFAULT_TOAST_LABELS, labelsProp);
|
|
405
|
+
useEffect(() => {
|
|
406
|
+
activeLabels = labels;
|
|
407
|
+
return () => {
|
|
408
|
+
if (activeLabels === labels) activeLabels = DEFAULT_TOAST_LABELS;
|
|
409
|
+
};
|
|
410
|
+
}, [labels]);
|
|
411
|
+
|
|
412
|
+
const documentTheme = useSyncExternalStore(subscribeDocumentTheme, readDocumentTheme, () => "light" as const);
|
|
413
|
+
const phone = useMediaQuery(PHONE_QUERY, false);
|
|
414
|
+
const resolved: ToastPosition = position ?? (phone ? "bottom-center" : "top-center");
|
|
415
|
+
|
|
416
|
+
// Above the bottom nav and the home indicator: whichever is taller, since the nav
|
|
417
|
+
// already pads itself by the safe area (the same sum BulkActionBar floats on).
|
|
418
|
+
const bottom = `calc(max(${navOffset === undefined ? "var(--app-nav-h, 0px)" : px(navOffset)}, env(safe-area-inset-bottom, 0px)) + 0.75rem)`;
|
|
419
|
+
// Below a 48px TopBar with the same 1rem clearance.
|
|
420
|
+
const top = "calc(env(safe-area-inset-top, 0px) + 4rem)";
|
|
421
|
+
const computed = resolved.startsWith("bottom") ? { bottom } : { top };
|
|
422
|
+
|
|
423
|
+
if (!mod) return null;
|
|
424
|
+
const Sonner = mod.Toaster;
|
|
425
|
+
return (
|
|
426
|
+
<Sonner
|
|
427
|
+
{...rest}
|
|
428
|
+
position={resolved}
|
|
429
|
+
theme={theme ?? documentTheme}
|
|
430
|
+
closeButton={closeButton}
|
|
431
|
+
richColors={richColors}
|
|
432
|
+
swipeDirections={swipeDirections}
|
|
433
|
+
offset={offset ?? computed}
|
|
434
|
+
mobileOffset={mobileOffset ?? offset ?? computed}
|
|
435
|
+
containerAriaLabel={labels.notifications}
|
|
436
|
+
toastOptions={{ closeButtonAriaLabel: labels.close }}
|
|
437
|
+
className={className ? `kit-toaster ${className}` : "kit-toaster"}
|
|
438
|
+
style={{ ...KIT_TOASTER_STYLE, ...style }}
|
|
439
|
+
/>
|
|
440
|
+
);
|
|
441
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useId } from "react";
|
|
2
|
+
import type { ComponentPropsWithoutRef, KeyboardEvent, ReactElement, ReactNode } from "react";
|
|
2
3
|
import { cn } from "../lib/cn";
|
|
3
4
|
import { horizontalStep } from "../lib/direction";
|
|
5
|
+
import { FIELD_INVALID, FloatingField } from "./ui";
|
|
4
6
|
|
|
5
7
|
export interface ToggleOption<T extends string> {
|
|
6
8
|
value: T;
|
|
@@ -44,6 +46,34 @@ export interface ToggleGroupBaseProps<T extends string>
|
|
|
44
46
|
* live and others are not is a menu with holes in it, and no caller here wants one.
|
|
45
47
|
*/
|
|
46
48
|
disabled?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* `sm`: 12px options with `px-2 py-1` — the compact group keksdose's rule editor
|
|
51
|
+
* (rule-editor:204) writes as `optionClassName="px-2 py-1 text-xs"` beside a small
|
|
52
|
+
* caption. `md` (default) is the size every other group has.
|
|
53
|
+
*/
|
|
54
|
+
size?: "sm" | "md";
|
|
55
|
+
/**
|
|
56
|
+
* Stand in a form row as a FIELD: with a label the group wears the field's chrome —
|
|
57
|
+
* border, surface, the top strip with a small static label in it, a labelled
|
|
58
|
+
* {@link Select}'s height — so beside an Input or a Select it reads as one of them
|
|
59
|
+
* rather than as a control with a caption over it. lenkbank builds exactly this by
|
|
60
|
+
* hand as `ToggleField` (features/gear/common.tsx:97, feedback #69), and its notes
|
|
61
|
+
* are why the chrome STRETCHES to its row as well as matching the select's padding:
|
|
62
|
+
* a native select's height is the browser's, so a toggle a few pixels short of it
|
|
63
|
+
* is levelled up by the row rather than by arithmetic.
|
|
64
|
+
*
|
|
65
|
+
* The label names the group (`aria-labelledby`), so `aria-label` is not needed. With
|
|
66
|
+
* a label, `className` styles the field's wrapper — as on {@link Select} — and the
|
|
67
|
+
* group's own box is dropped: two nested borders read as a control in a control.
|
|
68
|
+
*/
|
|
69
|
+
label?: ReactNode;
|
|
70
|
+
/** A {@link FieldHint} on the label line, as on a labelled {@link Select}. Only with
|
|
71
|
+
* `label`. */
|
|
72
|
+
hint?: ReactNode;
|
|
73
|
+
/** The message under the field when it is wrong: paints the field's border with
|
|
74
|
+
* `--danger`, marks the group `aria-invalid` and describes it with the message, as
|
|
75
|
+
* {@link Select}'s `error` does. Only with `label`. */
|
|
76
|
+
error?: ReactNode;
|
|
47
77
|
}
|
|
48
78
|
|
|
49
79
|
/** The group as it has always been: one option is always the answer. */
|
|
@@ -95,9 +125,17 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
95
125
|
optionClassName,
|
|
96
126
|
ariaLabel,
|
|
97
127
|
disabled = false,
|
|
128
|
+
size = "md",
|
|
129
|
+
label,
|
|
130
|
+
hint,
|
|
131
|
+
error,
|
|
98
132
|
"aria-label": ariaLabelAttr,
|
|
99
133
|
...restWithMode
|
|
100
134
|
} = props;
|
|
135
|
+
const labelId = useId();
|
|
136
|
+
const errorId = useId();
|
|
137
|
+
const field = label !== undefined && label !== null && label !== false && label !== "";
|
|
138
|
+
const hasError = field && error !== undefined && error !== null && error !== false && error !== "";
|
|
101
139
|
// Taken off the rest so neither reaches the DOM; `props` keeps them paired, which is
|
|
102
140
|
// what lets the `onChange` below be called with `null` only in the mode that allows it.
|
|
103
141
|
const { allowEmpty: _allowEmpty, onChange: _onChange, ...rest } = restWithMode;
|
|
@@ -127,7 +165,7 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
127
165
|
buttons?.[next]?.focus();
|
|
128
166
|
choose(options[next].value);
|
|
129
167
|
};
|
|
130
|
-
|
|
168
|
+
const group = (
|
|
131
169
|
<div
|
|
132
170
|
// The audit's named example of a closed prop list (§"Public API design"): the
|
|
133
171
|
// tour locates a step by CSS SELECTOR, so a component that drops every attribute
|
|
@@ -144,6 +182,11 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
144
182
|
// The DOM spelling wins; `ariaLabel` is the fallback for the call sites that
|
|
145
183
|
// have not moved yet.
|
|
146
184
|
aria-label={ariaLabelAttr ?? ariaLabel}
|
|
185
|
+
aria-labelledby={field && ariaLabelAttr === undefined && ariaLabel === undefined ? labelId : rest["aria-labelledby"]}
|
|
186
|
+
aria-invalid={hasError || rest["aria-invalid"] || undefined}
|
|
187
|
+
aria-describedby={
|
|
188
|
+
hasError ? (rest["aria-describedby"] ? `${rest["aria-describedby"]} ${errorId}` : errorId) : rest["aria-describedby"]
|
|
189
|
+
}
|
|
147
190
|
// `aria-disabled` on the group as well as `disabled` on each button: a radio
|
|
148
191
|
// group is what the user is being refused, and a screen reader announcing
|
|
149
192
|
// three separately-disabled radios does not say that.
|
|
@@ -166,7 +209,10 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
166
209
|
// package does; `cursor-not-allowed` is on the buttons, which is what a
|
|
167
210
|
// pointer is actually over.
|
|
168
211
|
disabled && "opacity-60",
|
|
169
|
-
|
|
212
|
+
// Inside the field's chrome the group is only a row of segments: no border, no
|
|
213
|
+
// surface, no padding of its own, and the field (not the group) is what dims.
|
|
214
|
+
field && "border-0 bg-transparent p-0 shadow-none opacity-100",
|
|
215
|
+
!field && className,
|
|
170
216
|
)}
|
|
171
217
|
>
|
|
172
218
|
{options.map((opt, index) => {
|
|
@@ -206,6 +252,11 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
206
252
|
// inside the segment it belongs to; focus-visible keeps it for the
|
|
207
253
|
// keyboard, which is the only input that needs it.
|
|
208
254
|
"min-w-0 flex-1 basis-auto truncate rounded px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--border-strong)]",
|
|
255
|
+
size === "sm" && "px-2 py-1 text-xs",
|
|
256
|
+
// In a field: no vertical padding and a 20px line — a `text-sm` line, the
|
|
257
|
+
// same line a labelled Select holds under its label strip — so the field's
|
|
258
|
+
// own `pt-4 pb-1` decides the height, as it does for the select.
|
|
259
|
+
field && "py-0 leading-5",
|
|
209
260
|
active
|
|
210
261
|
? "bg-[var(--bg-inverse)] text-[var(--text-inverse)]"
|
|
211
262
|
: "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]",
|
|
@@ -222,4 +273,32 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
222
273
|
})}
|
|
223
274
|
</div>
|
|
224
275
|
);
|
|
276
|
+
if (!field) return group;
|
|
277
|
+
return (
|
|
278
|
+
// `h-full` + `flex-1`: the chrome fills a grid or stretched flex row, which is what
|
|
279
|
+
// levels it with a select beside it whatever the browser makes of the select.
|
|
280
|
+
<div className={cn("flex h-full flex-col", className)}>
|
|
281
|
+
<FloatingField
|
|
282
|
+
className="flex flex-1 flex-col"
|
|
283
|
+
label={<span id={labelId}>{label}</span>}
|
|
284
|
+
staticLabel
|
|
285
|
+
hint={hint}
|
|
286
|
+
>
|
|
287
|
+
<div
|
|
288
|
+
className={cn(
|
|
289
|
+
"flex flex-1 flex-col justify-center rounded-md border border-[var(--border)] bg-[var(--bg-surface)] px-1 pt-4 pb-1 shadow-sm",
|
|
290
|
+
disabled && "bg-[var(--bg-surface-2)] opacity-60",
|
|
291
|
+
hasError && FIELD_INVALID,
|
|
292
|
+
)}
|
|
293
|
+
>
|
|
294
|
+
{group}
|
|
295
|
+
</div>
|
|
296
|
+
</FloatingField>
|
|
297
|
+
{hasError && (
|
|
298
|
+
<p id={errorId} className="mt-1 text-[11px] leading-tight text-[var(--danger)]">
|
|
299
|
+
{error}
|
|
300
|
+
</p>
|
|
301
|
+
)}
|
|
302
|
+
</div>
|
|
303
|
+
);
|
|
225
304
|
}
|