@eifi1/ui-kit 0.9.0 → 0.11.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 +37 -23
- package/dist/components/alert-banner.d.ts +57 -5
- package/dist/components/alert-banner.js +63 -17
- 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 +112 -0
- package/dist/components/bulk-action-bar.js +196 -0
- package/dist/components/bulk-action-bar.js.map +1 -0
- package/dist/components/button-group.d.ts +58 -3
- package/dist/components/button-group.js +56 -5
- package/dist/components/button-group.js.map +1 -1
- package/dist/components/calculator.d.ts +6 -0
- package/dist/components/calendar-heatmap.d.ts +124 -0
- package/dist/components/calendar-heatmap.js +295 -0
- package/dist/components/calendar-heatmap.js.map +1 -0
- package/dist/components/chip.d.ts +31 -5
- package/dist/components/chip.js +85 -5
- package/dist/components/chip.js.map +1 -1
- package/dist/components/choice-card.d.ts +34 -2
- package/dist/components/choice-card.js +49 -0
- package/dist/components/choice-card.js.map +1 -1
- package/dist/components/copy-button.d.ts +14 -4
- package/dist/components/copy-button.js +17 -4
- package/dist/components/copy-button.js.map +1 -1
- package/dist/components/data-table.js +2 -0
- package/dist/components/data-table.js.map +1 -1
- package/dist/components/date-picker.d.ts +22 -2
- package/dist/components/date-picker.js +11 -3
- 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 +57 -2
- package/dist/components/disclosure.js +24 -12
- package/dist/components/disclosure.js.map +1 -1
- package/dist/components/field.d.ts +43 -0
- package/dist/components/field.js +52 -0
- package/dist/components/field.js.map +1 -0
- package/dist/components/file-button.d.ts +1 -0
- package/dist/components/file-dropzone.d.ts +4 -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 +147 -4
- package/dist/components/floating-panel.js +188 -28
- 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 +187 -0
- package/dist/components/list.js +221 -0
- package/dist/components/list.js.map +1 -0
- package/dist/components/menu-item.d.ts +109 -0
- package/dist/components/menu-item.js +88 -0
- package/dist/components/menu-item.js.map +1 -0
- package/dist/components/mini-calendar.d.ts +70 -5
- package/dist/components/mini-calendar.js +166 -62
- package/dist/components/mini-calendar.js.map +1 -1
- 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/nav-pills.d.ts +66 -0
- package/dist/components/nav-pills.js +65 -0
- package/dist/components/nav-pills.js.map +1 -0
- 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 +31 -2
- package/dist/components/progress-bar.js +73 -7
- 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 +32 -0
- package/dist/components/series-chart.js +141 -4
- package/dist/components/series-chart.js.map +1 -1
- package/dist/components/settings-fields.d.ts +1 -0
- package/dist/components/status-dot.d.ts +53 -0
- package/dist/components/status-dot.js +57 -0
- package/dist/components/status-dot.js.map +1 -0
- package/dist/components/table.d.ts +78 -13
- package/dist/components/table.js +84 -11
- package/dist/components/table.js.map +1 -1
- package/dist/components/text.d.ts +70 -0
- package/dist/components/text.js +24 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/time-input.d.ts +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 +44 -1
- package/dist/components/toggle-group.js +73 -4
- package/dist/components/toggle-group.js.map +1 -1
- package/dist/components/tooltip.d.ts +46 -15
- package/dist/components/tooltip.js +87 -47
- package/dist/components/tooltip.js.map +1 -1
- package/dist/components/ui.d.ts +158 -7
- package/dist/components/ui.js +191 -10
- package/dist/components/ui.js.map +1 -1
- package/dist/components/user-avatar.d.ts +25 -3
- package/dist/components/user-avatar.js +32 -3
- package/dist/components/user-avatar.js.map +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-file-drop.d.ts +1 -0
- 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 +30 -2
- package/dist/i18n/locales/de.js.map +1 -1
- package/dist/i18n/locales/es.d.ts +6 -0
- package/dist/i18n/locales/es.js +30 -2
- package/dist/i18n/locales/es.js.map +1 -1
- package/dist/i18n/locales/fr.d.ts +6 -0
- package/dist/i18n/locales/fr.js +30 -2
- package/dist/i18n/locales/fr.js.map +1 -1
- package/dist/i18n/locales/hu.d.ts +6 -0
- package/dist/i18n/locales/hu.js +30 -2
- package/dist/i18n/locales/hu.js.map +1 -1
- package/dist/i18n/locales/it.d.ts +6 -0
- package/dist/i18n/locales/it.js +30 -2
- package/dist/i18n/locales/it.js.map +1 -1
- package/dist/i18n/locales/zh.d.ts +6 -0
- package/dist/i18n/locales/zh.js +30 -2
- package/dist/i18n/locales/zh.js.map +1 -1
- package/dist/index.d.ts +27 -15
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/clipping.d.ts +25 -0
- package/dist/lib/clipping.js +17 -0
- package/dist/lib/clipping.js.map +1 -0
- package/dist/lib/dates.d.ts +17 -1
- package/dist/lib/dates.js +19 -0
- package/dist/lib/dates.js.map +1 -1
- package/dist/rhf/form.d.ts +1 -0
- package/dist/rhf/use-rhf-wizard-step.d.ts +22 -0
- package/dist/rhf/use-rhf-wizard-step.js +38 -0
- package/dist/rhf/use-rhf-wizard-step.js.map +1 -0
- package/dist/rhf.d.ts +2 -0
- package/dist/rhf.js +1 -0
- package/dist/rhf.js.map +1 -1
- package/dist/search/command-palette.d.ts +19 -2
- package/dist/search/command-palette.js +16 -5
- package/dist/search/command-palette.js.map +1 -1
- package/dist/search/global-search.d.ts +26 -1
- package/dist/search/global-search.js +29 -13
- package/dist/search/global-search.js.map +1 -1
- package/dist/search.d.ts +1 -1
- package/dist/shell/topbar-action-menu.d.ts +73 -3
- package/dist/shell/topbar-action-menu.js +100 -27
- package/dist/shell/topbar-action-menu.js.map +1 -1
- package/dist/shell.d.ts +2 -1
- package/dist/wizard/stepper-nav.d.ts +1 -0
- package/dist/wizard/stepper-nav.js +1 -1
- 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-context.d.ts +7 -1
- package/dist/wizard/wizard-context.js +4 -0
- package/dist/wizard/wizard-context.js.map +1 -1
- package/dist/wizard/wizard-summary.js +3 -3
- package/dist/wizard/wizard-summary.js.map +1 -1
- package/dist/wizard.d.ts +2 -1
- package/package.json +16 -12
- package/src/components/alert-banner.tsx +149 -21
- package/src/components/breadcrumbs.tsx +168 -0
- package/src/components/bulk-action-bar.tsx +362 -0
- package/src/components/button-group.tsx +124 -4
- package/src/components/calendar-heatmap.tsx +504 -0
- package/src/components/chip.tsx +128 -7
- package/src/components/choice-card.tsx +97 -1
- package/src/components/copy-button.tsx +29 -7
- package/src/components/data-table.tsx +7 -0
- package/src/components/date-picker.tsx +49 -1
- package/src/components/description-list.tsx +168 -15
- package/src/components/dialog-frame.tsx +7 -0
- package/src/components/disclosure.tsx +93 -21
- package/src/components/field.tsx +137 -0
- package/src/components/file-dropzone.tsx +9 -10
- package/src/components/floating-panel.tsx +366 -15
- package/src/components/full-bleed-dialog.tsx +42 -5
- package/src/components/list.tsx +443 -0
- package/src/components/menu-item.tsx +235 -0
- package/src/components/mini-calendar.tsx +245 -54
- package/src/components/modal.tsx +80 -17
- package/src/components/nav-pills.tsx +145 -0
- package/src/components/page-contents.tsx +4 -4
- package/src/components/page-header.tsx +68 -0
- package/src/components/progress-bar.tsx +129 -11
- package/src/components/series-chart-labels.ts +4 -0
- package/src/components/series-chart.tsx +274 -3
- package/src/components/status-dot.tsx +107 -0
- package/src/components/table.tsx +176 -17
- package/src/components/text.tsx +97 -0
- package/src/components/toast.tsx +441 -0
- package/src/components/toggle-group.tsx +128 -4
- package/src/components/tooltip.tsx +200 -100
- package/src/components/ui.tsx +381 -14
- package/src/components/user-avatar.tsx +57 -3
- package/src/hooks/use-close-transition.ts +14 -5
- package/src/hooks/use-copy-to-clipboard.ts +1 -1
- package/src/i18n/defaults.ts +10 -0
- package/src/i18n/kit-labels.tsx +10 -0
- package/src/i18n/locales/de.ts +29 -0
- package/src/i18n/locales/es.ts +29 -0
- package/src/i18n/locales/fr.ts +29 -0
- package/src/i18n/locales/hu.ts +29 -0
- package/src/i18n/locales/it.ts +29 -0
- package/src/i18n/locales/zh.ts +29 -0
- package/src/index.ts +31 -0
- package/src/lib/clipping.ts +34 -0
- package/src/lib/dates.ts +37 -0
- package/src/rhf/use-rhf-wizard-step.ts +113 -0
- package/src/rhf.ts +2 -0
- package/src/search/command-palette.tsx +42 -5
- package/src/search/global-search.tsx +59 -18
- package/src/shell/topbar-action-menu.tsx +239 -49
- package/src/wizard/stepper-nav.tsx +2 -1
- package/src/wizard/types.ts +2 -2
- package/src/wizard/use-wizard.ts +3 -3
- package/src/wizard/wizard-context.tsx +9 -0
- 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,13 +46,55 @@ 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
|
|
|
79
|
+
/**
|
|
80
|
+
* A line under the group saying what the CHOSEN option means — lenkbank's ToggleField
|
|
81
|
+
* `hint` (features/gear/common.tsx:97): not help behind a "?" but a caption, and one
|
|
82
|
+
* that changes as the choice does. Pass a function of the value for that; it is
|
|
83
|
+
* attached with `aria-describedby`, and a function caption is also a polite live
|
|
84
|
+
* region, because a description is read when the group is entered and not again when
|
|
85
|
+
* an arrow key changes the choice underneath it.
|
|
86
|
+
*
|
|
87
|
+
* In muted 11px text under the field (or the bare group), above an `error`.
|
|
88
|
+
*/
|
|
89
|
+
type ToggleGroupCaption<V> = ReactNode | ((value: V) => ReactNode);
|
|
90
|
+
|
|
49
91
|
/** The group as it has always been: one option is always the answer. */
|
|
50
92
|
export interface ToggleGroupRequiredProps<T extends string> extends ToggleGroupBaseProps<T> {
|
|
51
93
|
allowEmpty?: false;
|
|
52
94
|
value: T;
|
|
53
95
|
onChange: (value: T) => void;
|
|
96
|
+
/** See {@link ToggleGroupCaption}. */
|
|
97
|
+
caption?: ToggleGroupCaption<T>;
|
|
54
98
|
}
|
|
55
99
|
|
|
56
100
|
/**
|
|
@@ -67,6 +111,8 @@ export interface ToggleGroupClearableProps<T extends string> extends ToggleGroup
|
|
|
67
111
|
allowEmpty: true;
|
|
68
112
|
value: T | null;
|
|
69
113
|
onChange: (value: T | null) => void;
|
|
114
|
+
/** See {@link ToggleGroupCaption}. `null` while nothing is chosen. */
|
|
115
|
+
caption?: ToggleGroupCaption<T | null>;
|
|
70
116
|
}
|
|
71
117
|
|
|
72
118
|
/** `allowEmpty` picks the shape, so `onChange` is typed `(T) => void` unless the group
|
|
@@ -95,12 +141,24 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
95
141
|
optionClassName,
|
|
96
142
|
ariaLabel,
|
|
97
143
|
disabled = false,
|
|
144
|
+
size = "md",
|
|
145
|
+
label,
|
|
146
|
+
hint,
|
|
147
|
+
error,
|
|
98
148
|
"aria-label": ariaLabelAttr,
|
|
99
149
|
...restWithMode
|
|
100
150
|
} = props;
|
|
151
|
+
const labelId = useId();
|
|
152
|
+
const errorId = useId();
|
|
153
|
+
const field = label !== undefined && label !== null && label !== false && label !== "";
|
|
154
|
+
const hasError = field && error !== undefined && error !== null && error !== false && error !== "";
|
|
101
155
|
// Taken off the rest so neither reaches the DOM; `props` keeps them paired, which is
|
|
102
156
|
// what lets the `onChange` below be called with `null` only in the mode that allows it.
|
|
103
|
-
const { allowEmpty: _allowEmpty, onChange: _onChange, ...rest } = restWithMode;
|
|
157
|
+
const { allowEmpty: _allowEmpty, onChange: _onChange, caption, ...rest } = restWithMode;
|
|
158
|
+
const captionId = useId();
|
|
159
|
+
const captionIsLive = typeof caption === "function";
|
|
160
|
+
const captionNode = captionIsLive ? (caption as (v: T | null) => ReactNode)(value) : caption;
|
|
161
|
+
const hasCaption = captionNode !== undefined && captionNode !== null && captionNode !== false && captionNode !== "";
|
|
104
162
|
const choose = (next: T) => {
|
|
105
163
|
if (props.allowEmpty) props.onChange(next === value ? null : next);
|
|
106
164
|
else props.onChange(next);
|
|
@@ -127,7 +185,7 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
127
185
|
buttons?.[next]?.focus();
|
|
128
186
|
choose(options[next].value);
|
|
129
187
|
};
|
|
130
|
-
|
|
188
|
+
const group = (
|
|
131
189
|
<div
|
|
132
190
|
// The audit's named example of a closed prop list (§"Public API design"): the
|
|
133
191
|
// tour locates a step by CSS SELECTOR, so a component that drops every attribute
|
|
@@ -144,6 +202,11 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
144
202
|
// The DOM spelling wins; `ariaLabel` is the fallback for the call sites that
|
|
145
203
|
// have not moved yet.
|
|
146
204
|
aria-label={ariaLabelAttr ?? ariaLabel}
|
|
205
|
+
aria-labelledby={field && ariaLabelAttr === undefined && ariaLabel === undefined ? labelId : rest["aria-labelledby"]}
|
|
206
|
+
aria-invalid={hasError || rest["aria-invalid"] || undefined}
|
|
207
|
+
aria-describedby={
|
|
208
|
+
[rest["aria-describedby"], hasCaption && captionId, hasError && errorId].filter(Boolean).join(" ") || undefined
|
|
209
|
+
}
|
|
147
210
|
// `aria-disabled` on the group as well as `disabled` on each button: a radio
|
|
148
211
|
// group is what the user is being refused, and a screen reader announcing
|
|
149
212
|
// three separately-disabled radios does not say that.
|
|
@@ -166,7 +229,10 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
166
229
|
// package does; `cursor-not-allowed` is on the buttons, which is what a
|
|
167
230
|
// pointer is actually over.
|
|
168
231
|
disabled && "opacity-60",
|
|
169
|
-
|
|
232
|
+
// Inside the field's chrome the group is only a row of segments: no border, no
|
|
233
|
+
// surface, no padding of its own, and the field (not the group) is what dims.
|
|
234
|
+
field && "border-0 bg-transparent p-0 shadow-none opacity-100",
|
|
235
|
+
!field && className,
|
|
170
236
|
)}
|
|
171
237
|
>
|
|
172
238
|
{options.map((opt, index) => {
|
|
@@ -206,6 +272,11 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
206
272
|
// inside the segment it belongs to; focus-visible keeps it for the
|
|
207
273
|
// keyboard, which is the only input that needs it.
|
|
208
274
|
"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)]",
|
|
275
|
+
size === "sm" && "px-2 py-1 text-xs",
|
|
276
|
+
// In a field: no vertical padding and a 20px line — a `text-sm` line, the
|
|
277
|
+
// same line a labelled Select holds under its label strip — so the field's
|
|
278
|
+
// own `pt-4 pb-1` decides the height, as it does for the select.
|
|
279
|
+
field && "py-0 leading-5",
|
|
209
280
|
active
|
|
210
281
|
? "bg-[var(--bg-inverse)] text-[var(--text-inverse)]"
|
|
211
282
|
: "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]",
|
|
@@ -222,4 +293,57 @@ export function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): React
|
|
|
222
293
|
})}
|
|
223
294
|
</div>
|
|
224
295
|
);
|
|
296
|
+
// Rendered whenever the caption is — `aria-live` must be on the element BEFORE its
|
|
297
|
+
// text changes, or the change is not announced — but empty (no height, no margin)
|
|
298
|
+
// when there is nothing to say, so a function caption that returns null for some
|
|
299
|
+
// options leaves no gap. Empty rather than `hidden`: some readers do not announce
|
|
300
|
+
// text that appears inside an element coming back from `display: none`.
|
|
301
|
+
const captionEl =
|
|
302
|
+
hasCaption || captionIsLive ? (
|
|
303
|
+
<p
|
|
304
|
+
id={captionId}
|
|
305
|
+
aria-live={captionIsLive ? "polite" : undefined}
|
|
306
|
+
className={cn("text-[11px] leading-snug text-[var(--text-muted)]", hasCaption && "mt-1")}
|
|
307
|
+
>
|
|
308
|
+
{hasCaption ? captionNode : null}
|
|
309
|
+
</p>
|
|
310
|
+
) : null;
|
|
311
|
+
if (!field) {
|
|
312
|
+
if (!captionEl) return group;
|
|
313
|
+
// The group keeps its `className`, as without a caption; the wrapper only stacks.
|
|
314
|
+
return (
|
|
315
|
+
<div className="min-w-0">
|
|
316
|
+
{group}
|
|
317
|
+
{captionEl}
|
|
318
|
+
</div>
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
return (
|
|
322
|
+
// `h-full` + `flex-1`: the chrome fills a grid or stretched flex row, which is what
|
|
323
|
+
// levels it with a select beside it whatever the browser makes of the select.
|
|
324
|
+
<div className={cn("flex h-full flex-col", className)}>
|
|
325
|
+
<FloatingField
|
|
326
|
+
className="flex flex-1 flex-col"
|
|
327
|
+
label={<span id={labelId}>{label}</span>}
|
|
328
|
+
staticLabel
|
|
329
|
+
hint={hint}
|
|
330
|
+
>
|
|
331
|
+
<div
|
|
332
|
+
className={cn(
|
|
333
|
+
"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",
|
|
334
|
+
disabled && "bg-[var(--bg-surface-2)] opacity-60",
|
|
335
|
+
hasError && FIELD_INVALID,
|
|
336
|
+
)}
|
|
337
|
+
>
|
|
338
|
+
{group}
|
|
339
|
+
</div>
|
|
340
|
+
</FloatingField>
|
|
341
|
+
{captionEl}
|
|
342
|
+
{hasError && (
|
|
343
|
+
<p id={errorId} className="mt-1 text-[11px] leading-tight text-[var(--danger)]">
|
|
344
|
+
{error}
|
|
345
|
+
</p>
|
|
346
|
+
)}
|
|
347
|
+
</div>
|
|
348
|
+
);
|
|
225
349
|
}
|