@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,226 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useState, useSyncExternalStore } from "react";
|
|
4
|
+
import { useKitLabels } from "../i18n/kit-labels.js";
|
|
5
|
+
import { useMediaQuery } from "../hooks/use-media-query.js";
|
|
6
|
+
import { PHONE_QUERY } from "./ui.js";
|
|
7
|
+
let sonner = null;
|
|
8
|
+
let loading = null;
|
|
9
|
+
const queue = [];
|
|
10
|
+
function loadSonner() {
|
|
11
|
+
loading ??= import("sonner").then(
|
|
12
|
+
(mod) => {
|
|
13
|
+
sonner = mod;
|
|
14
|
+
for (const fn of queue.splice(0)) fn(mod);
|
|
15
|
+
return mod;
|
|
16
|
+
},
|
|
17
|
+
() => {
|
|
18
|
+
queue.length = 0;
|
|
19
|
+
console.warn(
|
|
20
|
+
"@eifi1/ui-kit: `toast` needs the optional peer `sonner` \u2014 install it (npm i sonner) to show toasts."
|
|
21
|
+
);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
return loading;
|
|
26
|
+
}
|
|
27
|
+
function withSonner(fn) {
|
|
28
|
+
if (sonner) {
|
|
29
|
+
fn(sonner);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
queue.push(fn);
|
|
33
|
+
void loadSonner();
|
|
34
|
+
}
|
|
35
|
+
let counter = 0;
|
|
36
|
+
const nextId = () => `kit-toast-${++counter}`;
|
|
37
|
+
const DEFAULT_TOAST_LABELS = {
|
|
38
|
+
close: "Close notification",
|
|
39
|
+
notifications: "Notifications",
|
|
40
|
+
undo: "Undo",
|
|
41
|
+
redo: "Redo"
|
|
42
|
+
};
|
|
43
|
+
let activeLabels = DEFAULT_TOAST_LABELS;
|
|
44
|
+
const TOAST_ACTION_DURATION = 8e3;
|
|
45
|
+
const privately = (node) => /* @__PURE__ */ jsx("span", { "data-private": "", children: node });
|
|
46
|
+
function toSonnerData(opts, id) {
|
|
47
|
+
const { redact, description, duration, action, ...rest } = opts ?? {};
|
|
48
|
+
const data = { ...rest, id };
|
|
49
|
+
if (description !== void 0) data.description = redact ? privately(description) : description;
|
|
50
|
+
if (action) data.action = action;
|
|
51
|
+
const resolved = duration ?? (action ? TOAST_ACTION_DURATION : void 0);
|
|
52
|
+
if (resolved !== void 0) data.duration = resolved;
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
function show(kind, message, opts) {
|
|
56
|
+
const id = opts?.id ?? nextId();
|
|
57
|
+
const title = opts?.redact ? privately(message) : message;
|
|
58
|
+
const data = toSonnerData(opts, id);
|
|
59
|
+
withSonner((s) => {
|
|
60
|
+
if (kind === "default") s.toast(title, data);
|
|
61
|
+
else s.toast[kind](title, data);
|
|
62
|
+
});
|
|
63
|
+
return id;
|
|
64
|
+
}
|
|
65
|
+
function undoable(message, { tone = "success", label, run, ...opts }, fallback) {
|
|
66
|
+
return show(tone, message, {
|
|
67
|
+
...opts,
|
|
68
|
+
action: { label: label ?? fallback, onClick: () => run() }
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const toast = Object.assign(
|
|
72
|
+
(message, opts) => show("default", message, opts),
|
|
73
|
+
{
|
|
74
|
+
message: (message, opts) => show("default", message, opts),
|
|
75
|
+
success: (message, opts) => show("success", message, opts),
|
|
76
|
+
error: (message, opts) => show("error", message, opts),
|
|
77
|
+
warning: (message, opts) => show("warning", message, opts),
|
|
78
|
+
info: (message, opts) => show("info", message, opts),
|
|
79
|
+
/** A spinner that does not time out — replace it (same `id`) or dismiss it. */
|
|
80
|
+
loading: (message, opts) => show("loading", message, opts),
|
|
81
|
+
/**
|
|
82
|
+
* A loading toast that becomes the success or the error toast, in place, when
|
|
83
|
+
* `promise` settles. Returns the toast's id; the promise itself is the caller's.
|
|
84
|
+
*/
|
|
85
|
+
promise(promise, options) {
|
|
86
|
+
const { loading: pending, success, error, finally: onFinally, ...opts } = options;
|
|
87
|
+
const id = show("loading", pending, opts);
|
|
88
|
+
const same = { ...opts, id };
|
|
89
|
+
Promise.resolve().then(() => typeof promise === "function" ? promise() : promise).then(
|
|
90
|
+
(value) => {
|
|
91
|
+
if (success === void 0) toast.dismiss(id);
|
|
92
|
+
else show("success", typeof success === "function" ? success(value) : success, same);
|
|
93
|
+
},
|
|
94
|
+
(reason) => {
|
|
95
|
+
if (error === void 0) toast.dismiss(id);
|
|
96
|
+
else show("error", typeof error === "function" ? error(reason) : error, same);
|
|
97
|
+
}
|
|
98
|
+
).finally(() => onFinally?.());
|
|
99
|
+
return id;
|
|
100
|
+
},
|
|
101
|
+
/** Close one toast, or every toast when `id` is omitted. */
|
|
102
|
+
dismiss(id) {
|
|
103
|
+
withSonner((s) => s.toast.dismiss(id));
|
|
104
|
+
},
|
|
105
|
+
/** Render your own node in a toast slot — `render` receives the id to dismiss it. */
|
|
106
|
+
custom(render, opts) {
|
|
107
|
+
const id = opts?.id ?? nextId();
|
|
108
|
+
const data = toSonnerData(opts, id);
|
|
109
|
+
withSonner((s) => s.toast.custom(render, data));
|
|
110
|
+
return id;
|
|
111
|
+
},
|
|
112
|
+
/**
|
|
113
|
+
* "Deleted — Undo": a toast whose action takes the step back, labelled by the
|
|
114
|
+
* provider's `toast.undo`. The journal stays the app's; this is only its toast.
|
|
115
|
+
* Pair with {@link toast.redo} so the step back can itself be taken back.
|
|
116
|
+
*/
|
|
117
|
+
undo(message, { onUndo, ...opts }) {
|
|
118
|
+
return undoable(message, { ...opts, run: onUndo }, activeLabels.undo);
|
|
119
|
+
},
|
|
120
|
+
/** {@link toast.undo}'s mirror: the action is labelled `toast.redo`. */
|
|
121
|
+
redo(message, { onRedo, ...opts }) {
|
|
122
|
+
return undoable(message, { ...opts, run: onRedo }, activeLabels.redo);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
const KIT_TOASTER_STYLE = {
|
|
127
|
+
zIndex: "var(--z-toast, 2147483647)",
|
|
128
|
+
fontFamily: "inherit",
|
|
129
|
+
"--normal-bg": "var(--bg-surface)",
|
|
130
|
+
"--normal-bg-hover": "var(--bg-hover)",
|
|
131
|
+
"--normal-border": "var(--border)",
|
|
132
|
+
"--normal-border-hover": "var(--border-strong)",
|
|
133
|
+
"--normal-text": "var(--text-primary)",
|
|
134
|
+
"--gray2": "var(--bg-hover)",
|
|
135
|
+
"--gray5": "var(--border-strong)",
|
|
136
|
+
"--gray11": "var(--text-muted)",
|
|
137
|
+
"--success-bg": "var(--toast-success-bg)",
|
|
138
|
+
"--success-border": "var(--toast-success-border)",
|
|
139
|
+
"--success-text": "var(--success)",
|
|
140
|
+
"--info-bg": "var(--toast-info-bg)",
|
|
141
|
+
"--info-border": "var(--toast-info-border)",
|
|
142
|
+
"--info-text": "var(--info)",
|
|
143
|
+
"--warning-bg": "var(--toast-warning-bg)",
|
|
144
|
+
"--warning-border": "var(--toast-warning-border)",
|
|
145
|
+
"--warning-text": "var(--warning)",
|
|
146
|
+
"--error-bg": "var(--toast-danger-bg)",
|
|
147
|
+
"--error-border": "var(--toast-danger-border)",
|
|
148
|
+
"--error-text": "var(--danger)"
|
|
149
|
+
};
|
|
150
|
+
const ALL_DIRECTIONS = ["top", "right", "bottom", "left"];
|
|
151
|
+
function readDocumentTheme() {
|
|
152
|
+
return typeof document !== "undefined" && document.documentElement.classList.contains("dark") ? "dark" : "light";
|
|
153
|
+
}
|
|
154
|
+
function subscribeDocumentTheme(onChange) {
|
|
155
|
+
if (typeof MutationObserver === "undefined") return () => {
|
|
156
|
+
};
|
|
157
|
+
const mo = new MutationObserver(onChange);
|
|
158
|
+
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
|
159
|
+
return () => mo.disconnect();
|
|
160
|
+
}
|
|
161
|
+
const px = (v) => typeof v === "number" ? `${v}px` : v;
|
|
162
|
+
function Toaster({
|
|
163
|
+
position,
|
|
164
|
+
navOffset,
|
|
165
|
+
theme,
|
|
166
|
+
labels: labelsProp,
|
|
167
|
+
closeButton = true,
|
|
168
|
+
richColors = true,
|
|
169
|
+
swipeDirections = ALL_DIRECTIONS,
|
|
170
|
+
offset,
|
|
171
|
+
mobileOffset,
|
|
172
|
+
className,
|
|
173
|
+
style,
|
|
174
|
+
...rest
|
|
175
|
+
}) {
|
|
176
|
+
const [mod, setMod] = useState(sonner);
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
if (mod) return;
|
|
179
|
+
let live = true;
|
|
180
|
+
void loadSonner().then((m) => {
|
|
181
|
+
if (live && m) setMod(m);
|
|
182
|
+
});
|
|
183
|
+
return () => {
|
|
184
|
+
live = false;
|
|
185
|
+
};
|
|
186
|
+
}, [mod]);
|
|
187
|
+
const labels = useKitLabels("toast", DEFAULT_TOAST_LABELS, labelsProp);
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
activeLabels = labels;
|
|
190
|
+
return () => {
|
|
191
|
+
if (activeLabels === labels) activeLabels = DEFAULT_TOAST_LABELS;
|
|
192
|
+
};
|
|
193
|
+
}, [labels]);
|
|
194
|
+
const documentTheme = useSyncExternalStore(subscribeDocumentTheme, readDocumentTheme, () => "light");
|
|
195
|
+
const phone = useMediaQuery(PHONE_QUERY, false);
|
|
196
|
+
const resolved = position ?? (phone ? "bottom-center" : "top-center");
|
|
197
|
+
const bottom = `calc(max(${navOffset === void 0 ? "var(--app-nav-h, 0px)" : px(navOffset)}, env(safe-area-inset-bottom, 0px)) + 0.75rem)`;
|
|
198
|
+
const top = "calc(env(safe-area-inset-top, 0px) + 4rem)";
|
|
199
|
+
const computed = resolved.startsWith("bottom") ? { bottom } : { top };
|
|
200
|
+
if (!mod) return null;
|
|
201
|
+
const Sonner = mod.Toaster;
|
|
202
|
+
return /* @__PURE__ */ jsx(
|
|
203
|
+
Sonner,
|
|
204
|
+
{
|
|
205
|
+
...rest,
|
|
206
|
+
position: resolved,
|
|
207
|
+
theme: theme ?? documentTheme,
|
|
208
|
+
closeButton,
|
|
209
|
+
richColors,
|
|
210
|
+
swipeDirections,
|
|
211
|
+
offset: offset ?? computed,
|
|
212
|
+
mobileOffset: mobileOffset ?? offset ?? computed,
|
|
213
|
+
containerAriaLabel: labels.notifications,
|
|
214
|
+
toastOptions: { closeButtonAriaLabel: labels.close },
|
|
215
|
+
className: className ? `kit-toaster ${className}` : "kit-toaster",
|
|
216
|
+
style: { ...KIT_TOASTER_STYLE, ...style }
|
|
217
|
+
}
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
export {
|
|
221
|
+
DEFAULT_TOAST_LABELS,
|
|
222
|
+
TOAST_ACTION_DURATION,
|
|
223
|
+
Toaster,
|
|
224
|
+
toast
|
|
225
|
+
};
|
|
226
|
+
//# sourceMappingURL=toast.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/toast.tsx"],"sourcesContent":["import { useEffect, useState, useSyncExternalStore } from \"react\";\nimport type { CSSProperties, MouseEvent, ReactElement, ReactNode } from \"react\";\nimport { useKitLabels } from \"../i18n/kit-labels\";\nimport { useMediaQuery } from \"../hooks/use-media-query\";\nimport { PHONE_QUERY } from \"./ui\";\n\n/**\n * The kit's toast layer: a `toast` API and a `<Toaster>`, over sonner.\n *\n * WHY A WRAPPER AND NOT \"USE SONNER\". Both apps mounted sonner by hand and carried the\n * same policy in it — bottom-centre on a phone so the toast covers neither the header\n * nor the bottom nav, a lift above that nav, swipe in every direction, a z-index above\n * every overlay, the theme fed in from the store — and one of them wrote that policy\n * twice. `toast` mirrors sonner's call shape, so migrating is an import swap:\n *\n * import { toast } from \"@eifi1/ui-kit\"; // was: from \"sonner\"\n *\n * WHY SONNER IS LOADED LAZILY. It is an OPTIONAL peer, and this module is re-exported by\n * the barrel: a static import here would make every app that imports a Button install\n * it (see optional-peer-imports.test.tsx). So nothing here imports it statically. The\n * `<Toaster>` loads it when it mounts — long before anyone clicks anything — and a call\n * made before that is queued and replayed in order the moment it has loaded. Every call\n * returns its id synchronously anyway: the id is chosen here, not by sonner.\n */\n\ntype Sonner = typeof import(\"sonner\");\ntype SonnerData = NonNullable<Parameters<Sonner[\"toast\"]>[1]>;\n\n/* ── Loading sonner ──────────────────────────────────────────────────────── */\n\nlet sonner: Sonner | null = null;\nlet loading: Promise<Sonner | null> | null = null;\nconst queue: Array<(s: Sonner) => void> = [];\n\nfunction loadSonner(): Promise<Sonner | null> {\n loading ??= import(\"sonner\").then(\n (mod) => {\n sonner = mod;\n // Flushed in the SAME callback that sets `sonner`, so a call arriving between\n // the two cannot overtake one that was queued before it.\n for (const fn of queue.splice(0)) fn(mod);\n return mod;\n },\n () => {\n queue.length = 0;\n console.warn(\n \"@eifi1/ui-kit: `toast` needs the optional peer `sonner` — install it (npm i sonner) to show toasts.\",\n );\n return null;\n },\n );\n return loading;\n}\n\nfunction withSonner(fn: (s: Sonner) => void): void {\n if (sonner) {\n fn(sonner);\n return;\n }\n queue.push(fn);\n void loadSonner();\n}\n\nlet counter = 0;\nconst nextId = () => `kit-toast-${++counter}`;\n\n/* ── Labels ──────────────────────────────────────────────────────────────── */\n\n/** The words the toast layer says. `close` and `notifications` are read by a screen\n * reader (the close button's name, the toast region's name); `undo` and `redo` are\n * the default action labels of {@link toast.undo} / {@link toast.redo}. */\nexport interface ToastLabels {\n close: string;\n notifications: string;\n undo: string;\n redo: string;\n}\n\nexport const DEFAULT_TOAST_LABELS: ToastLabels = {\n close: \"Close notification\",\n notifications: \"Notifications\",\n undo: \"Undo\",\n redo: \"Redo\",\n};\n\n/** What the mounted `<Toaster>` resolved from `<UiKitProvider>` — so `toast.undo()`,\n * which is called from mutation callbacks and not from a component, still says\n * \"Rückgängig\" in a German app. */\nlet activeLabels: ToastLabels = DEFAULT_TOAST_LABELS;\n\n/* ── The API ─────────────────────────────────────────────────────────────── */\n\nexport type ToastId = string | number;\n\n/** A button on the toast. Clicking it dismisses the toast, unless `onClick` calls\n * `event.preventDefault()`. */\nexport interface ToastAction {\n label: ReactNode;\n onClick: (event: MouseEvent<HTMLButtonElement>) => void;\n}\n\nexport interface ToastOptions {\n /** Re-using an id REPLACES the toast on screen with that id (in place — it does not\n * stack a second one), which is how a toast is updated. Default: a fresh id. */\n id?: ToastId;\n description?: ReactNode;\n action?: ToastAction;\n /** A second, quieter button (sonner's `cancel`). */\n cancel?: ToastAction;\n /** Milliseconds; `Infinity` keeps it until dismissed. Default 4s — or\n * {@link TOAST_ACTION_DURATION} when there is an `action`, so there is time to\n * read the message AND reach the button. */\n duration?: number;\n /** Called when the toast is closed by the user (close button, swipe, action). */\n onDismiss?: (toast: { id: ToastId }) => void;\n /** Called when the toast times out. */\n onAutoClose?: (toast: { id: ToastId }) => void;\n /** Wrap the message and the description in `data-private`, so an app's demo /\n * privacy mode blurs them like every other sensitive value. */\n redact?: boolean;\n /** Replaces the tone's icon. */\n icon?: ReactNode;\n /** `false`: no close button, no swipe. */\n dismissible?: boolean;\n /** Per toast, over the `<Toaster>`'s `closeButton`. */\n closeButton?: boolean;\n className?: string;\n style?: CSSProperties;\n}\n\n/** The default duration of a toast that carries an action (8s rather than 4s). */\nexport const TOAST_ACTION_DURATION = 8000;\n\ntype Kind = \"default\" | \"success\" | \"error\" | \"warning\" | \"info\" | \"loading\";\n\nconst privately = (node: ReactNode) => <span data-private=\"\">{node}</span>;\n\nfunction toSonnerData(opts: ToastOptions | undefined, id: ToastId): SonnerData {\n const { redact, description, duration, action, ...rest } = opts ?? {};\n const data: SonnerData = { ...rest, id };\n if (description !== undefined) data.description = redact ? privately(description) : description;\n if (action) data.action = action;\n const resolved = duration ?? (action ? TOAST_ACTION_DURATION : undefined);\n if (resolved !== undefined) data.duration = resolved;\n return data;\n}\n\nfunction show(kind: Kind, message: ReactNode, opts?: ToastOptions): ToastId {\n const id = opts?.id ?? nextId();\n const title = opts?.redact ? privately(message) : message;\n const data = toSonnerData(opts, id);\n withSonner((s) => {\n if (kind === \"default\") s.toast(title, data);\n else s.toast[kind](title, data);\n });\n return id;\n}\n\nexport interface ToastPromiseOptions<T> extends ToastOptions {\n loading: ReactNode;\n /** The success message, or a function of the resolved value. Omitted: the loading\n * toast is dismissed. */\n success?: ReactNode | ((value: T) => ReactNode);\n /** The error message, or a function of the rejection. Omitted: dismissed. */\n error?: ReactNode | ((error: unknown) => ReactNode);\n finally?: () => void;\n}\n\nexport interface ToastUndoOptions extends Omit<ToastOptions, \"action\"> {\n /** Called when the button is pressed. */\n onUndo: () => void;\n /** Default: `toast.undo` from `<UiKitProvider>` (\"Undo\"). */\n label?: ReactNode;\n /** Default `success` — the step the toast reports worked. */\n tone?: Exclude<Kind, \"loading\">;\n}\n\nexport interface ToastRedoOptions extends Omit<ToastUndoOptions, \"onUndo\"> {\n onRedo: () => void;\n}\n\nfunction undoable(\n message: ReactNode,\n { tone = \"success\", label, run, ...opts }: Omit<ToastUndoOptions, \"onUndo\"> & { run: () => void },\n fallback: ReactNode,\n): ToastId {\n return show(tone, message, {\n ...opts,\n action: { label: label ?? fallback, onClick: () => run() },\n });\n}\n\n/**\n * Show a toast. The same shape as sonner's `toast`, so an app migrates by changing the\n * import: `toast(msg, opts)`, `toast.success` / `error` / `warning` / `info` /\n * `loading`, `toast.promise`, `toast.dismiss`, `toast.custom`. Each returns the toast's\n * id; pass it back as `id` to replace that toast in place.\n *\n * Needs a mounted {@link Toaster}. Messages may be any ReactNode.\n */\nexport const toast = Object.assign(\n (message: ReactNode, opts?: ToastOptions): ToastId => show(\"default\", message, opts),\n {\n message: (message: ReactNode, opts?: ToastOptions) => show(\"default\", message, opts),\n success: (message: ReactNode, opts?: ToastOptions) => show(\"success\", message, opts),\n error: (message: ReactNode, opts?: ToastOptions) => show(\"error\", message, opts),\n warning: (message: ReactNode, opts?: ToastOptions) => show(\"warning\", message, opts),\n info: (message: ReactNode, opts?: ToastOptions) => show(\"info\", message, opts),\n /** A spinner that does not time out — replace it (same `id`) or dismiss it. */\n loading: (message: ReactNode, opts?: ToastOptions) => show(\"loading\", message, opts),\n\n /**\n * A loading toast that becomes the success or the error toast, in place, when\n * `promise` settles. Returns the toast's id; the promise itself is the caller's.\n */\n promise<T>(promise: Promise<T> | (() => Promise<T>), options: ToastPromiseOptions<T>): ToastId {\n const { loading: pending, success, error, finally: onFinally, ...opts } = options;\n const id = show(\"loading\", pending, opts);\n const same = { ...opts, id };\n Promise.resolve()\n .then(() => (typeof promise === \"function\" ? promise() : promise))\n .then(\n (value) => {\n if (success === undefined) toast.dismiss(id);\n else show(\"success\", typeof success === \"function\" ? success(value) : success, same);\n },\n (reason: unknown) => {\n if (error === undefined) toast.dismiss(id);\n else show(\"error\", typeof error === \"function\" ? error(reason) : error, same);\n },\n )\n .finally(() => onFinally?.());\n return id;\n },\n\n /** Close one toast, or every toast when `id` is omitted. */\n dismiss(id?: ToastId): void {\n withSonner((s) => s.toast.dismiss(id));\n },\n\n /** Render your own node in a toast slot — `render` receives the id to dismiss it. */\n custom(render: (id: ToastId) => ReactElement, opts?: Omit<ToastOptions, \"redact\">): ToastId {\n const id = opts?.id ?? nextId();\n const data = toSonnerData(opts, id);\n withSonner((s) => s.toast.custom(render, data));\n return id;\n },\n\n /**\n * \"Deleted — Undo\": a toast whose action takes the step back, labelled by the\n * provider's `toast.undo`. The journal stays the app's; this is only its toast.\n * Pair with {@link toast.redo} so the step back can itself be taken back.\n */\n undo(message: ReactNode, { onUndo, ...opts }: ToastUndoOptions): ToastId {\n return undoable(message, { ...opts, run: onUndo }, activeLabels.undo);\n },\n\n /** {@link toast.undo}'s mirror: the action is labelled `toast.redo`. */\n redo(message: ReactNode, { onRedo, ...opts }: ToastRedoOptions): ToastId {\n return undoable(message, { ...opts, run: onRedo }, activeLabels.redo);\n },\n },\n);\n\n/* ── The Toaster ─────────────────────────────────────────────────────────── */\n\nexport type ToastPosition =\n | \"top-left\"\n | \"top-center\"\n | \"top-right\"\n | \"bottom-left\"\n | \"bottom-center\"\n | \"bottom-right\";\n\nexport type ToastSwipeDirection = \"top\" | \"right\" | \"bottom\" | \"left\";\n\n/** sonner's offset: one value for every edge, or per edge. Numbers are px. */\nexport type ToasterOffset =\n | string\n | number\n | { top?: string | number; right?: string | number; bottom?: string | number; left?: string | number };\n\nexport interface ToasterProps {\n /** Default: `bottom-center` on a phone (so the toast covers neither the header's\n * buttons nor the bottom nav's), `top-center` from `md` up. */\n position?: ToastPosition;\n /** The height to keep clear at the bottom — AppShell's bottom nav. Default: its\n * measured `--app-nav-h`, so nothing has to be passed under an AppShell. The safe\n * area is added either way. Numbers are px. */\n navOffset?: string | number;\n /** Default: follows the `.dark` class the kit's theme store (`useApplyTheme`) puts\n * on `<html>` — whichever store the app built, with nothing passed. */\n theme?: \"light\" | \"dark\" | \"system\";\n labels?: Partial<ToastLabels>;\n /** Default 4s (8s for a toast with an action). */\n duration?: number;\n visibleToasts?: number;\n expand?: boolean;\n /** Default true. */\n closeButton?: boolean;\n /** Tone-coloured toasts (the AlertBanner tokens). Default true. */\n richColors?: boolean;\n /** Default: every direction. */\n swipeDirections?: ToastSwipeDirection[];\n /** Overrides the computed offset outright (above the nav on a phone, below a 48px\n * top bar on desktop). */\n offset?: ToasterOffset;\n /** Below 600px, where sonner switches to its full-width layout. Default: `offset`'s\n * phone value. */\n mobileOffset?: ToasterOffset;\n /** Keyboard shortcut that moves focus into the toasts. Default Alt+T. */\n hotkey?: string[];\n gap?: number;\n dir?: \"ltr\" | \"rtl\" | \"auto\";\n className?: string;\n style?: CSSProperties;\n}\n\n/** The kit's palette, mapped onto sonner's variables. Inline on the toaster element,\n * so it wins over sonner's injected sheet with no specificity contest. The tone\n * backgrounds go through `--toast-*` aliases computed at `:root` in tokens.css:\n * sonner redefines `--success-bg` on this same element, and a variable cannot read\n * its own name — `--success-bg: var(--success-bg)` here would be a cycle. */\nconst KIT_TOASTER_STYLE = {\n zIndex: \"var(--z-toast, 2147483647)\",\n fontFamily: \"inherit\",\n \"--normal-bg\": \"var(--bg-surface)\",\n \"--normal-bg-hover\": \"var(--bg-hover)\",\n \"--normal-border\": \"var(--border)\",\n \"--normal-border-hover\": \"var(--border-strong)\",\n \"--normal-text\": \"var(--text-primary)\",\n \"--gray2\": \"var(--bg-hover)\",\n \"--gray5\": \"var(--border-strong)\",\n \"--gray11\": \"var(--text-muted)\",\n \"--success-bg\": \"var(--toast-success-bg)\",\n \"--success-border\": \"var(--toast-success-border)\",\n \"--success-text\": \"var(--success)\",\n \"--info-bg\": \"var(--toast-info-bg)\",\n \"--info-border\": \"var(--toast-info-border)\",\n \"--info-text\": \"var(--info)\",\n \"--warning-bg\": \"var(--toast-warning-bg)\",\n \"--warning-border\": \"var(--toast-warning-border)\",\n \"--warning-text\": \"var(--warning)\",\n \"--error-bg\": \"var(--toast-danger-bg)\",\n \"--error-border\": \"var(--toast-danger-border)\",\n \"--error-text\": \"var(--danger)\",\n} as CSSProperties;\n\nconst ALL_DIRECTIONS: ToastSwipeDirection[] = [\"top\", \"right\", \"bottom\", \"left\"];\n\n/** Light or dark, as the `.dark` class on `<html>` says right now. */\nfunction readDocumentTheme(): \"light\" | \"dark\" {\n return typeof document !== \"undefined\" && document.documentElement.classList.contains(\"dark\")\n ? \"dark\"\n : \"light\";\n}\n\nfunction subscribeDocumentTheme(onChange: () => void): () => void {\n if (typeof MutationObserver === \"undefined\") return () => {};\n const mo = new MutationObserver(onChange);\n mo.observe(document.documentElement, { attributes: true, attributeFilter: [\"class\"] });\n return () => mo.disconnect();\n}\n\nconst px = (v: string | number) => (typeof v === \"number\" ? `${v}px` : v);\n\n/**\n * Where the toasts appear, with the kit's defaults — mount ONE, near the root and\n * inside `<UiKitProvider>` (it reads the `toast` labels) and inside the router if a\n * toast's content navigates.\n *\n * Accessible as sonner makes it: the toasts sit in a polite live region, so a message\n * is announced with its description and its action's label; every toast and every\n * button in it is focusable, the timer pauses while focus or the pointer is inside,\n * and Alt+T ({@link ToasterProps.hotkey}) moves focus to the newest toast. A toast\n * with an action stays up 8s by default, so the button can be reached.\n */\nexport function Toaster({\n position,\n navOffset,\n theme,\n labels: labelsProp,\n closeButton = true,\n richColors = true,\n swipeDirections = ALL_DIRECTIONS,\n offset,\n mobileOffset,\n className,\n style,\n ...rest\n}: ToasterProps) {\n const [mod, setMod] = useState<Sonner | null>(sonner);\n useEffect(() => {\n if (mod) return;\n let live = true;\n void loadSonner().then((m) => {\n if (live && m) setMod(m);\n });\n return () => {\n live = false;\n };\n }, [mod]);\n\n const labels = useKitLabels(\"toast\", DEFAULT_TOAST_LABELS, labelsProp);\n useEffect(() => {\n activeLabels = labels;\n return () => {\n if (activeLabels === labels) activeLabels = DEFAULT_TOAST_LABELS;\n };\n }, [labels]);\n\n const documentTheme = useSyncExternalStore(subscribeDocumentTheme, readDocumentTheme, () => \"light\" as const);\n const phone = useMediaQuery(PHONE_QUERY, false);\n const resolved: ToastPosition = position ?? (phone ? \"bottom-center\" : \"top-center\");\n\n // Above the bottom nav and the home indicator: whichever is taller, since the nav\n // already pads itself by the safe area (the same sum BulkActionBar floats on).\n const bottom = `calc(max(${navOffset === undefined ? \"var(--app-nav-h, 0px)\" : px(navOffset)}, env(safe-area-inset-bottom, 0px)) + 0.75rem)`;\n // Below a 48px TopBar with the same 1rem clearance.\n const top = \"calc(env(safe-area-inset-top, 0px) + 4rem)\";\n const computed = resolved.startsWith(\"bottom\") ? { bottom } : { top };\n\n if (!mod) return null;\n const Sonner = mod.Toaster;\n return (\n <Sonner\n {...rest}\n position={resolved}\n theme={theme ?? documentTheme}\n closeButton={closeButton}\n richColors={richColors}\n swipeDirections={swipeDirections}\n offset={offset ?? computed}\n mobileOffset={mobileOffset ?? offset ?? computed}\n containerAriaLabel={labels.notifications}\n toastOptions={{ closeButtonAriaLabel: labels.close }}\n className={className ? `kit-toaster ${className}` : \"kit-toaster\"}\n style={{ ...KIT_TOASTER_STYLE, ...style }}\n />\n );\n}\n"],"mappings":";AAuIuC;AAvIvC,SAAS,WAAW,UAAU,4BAA4B;AAE1D,SAAS,oBAAoB;AAC7B,SAAS,qBAAqB;AAC9B,SAAS,mBAAmB;AA0B5B,IAAI,SAAwB;AAC5B,IAAI,UAAyC;AAC7C,MAAM,QAAoC,CAAC;AAE3C,SAAS,aAAqC;AAC5C,cAAY,OAAO,QAAQ,EAAE;AAAA,IAC3B,CAAC,QAAQ;AACP,eAAS;AAGT,iBAAW,MAAM,MAAM,OAAO,CAAC,EAAG,IAAG,GAAG;AACxC,aAAO;AAAA,IACT;AAAA,IACA,MAAM;AACJ,YAAM,SAAS;AACf,cAAQ;AAAA,QACN;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,WAAW,IAA+B;AACjD,MAAI,QAAQ;AACV,OAAG,MAAM;AACT;AAAA,EACF;AACA,QAAM,KAAK,EAAE;AACb,OAAK,WAAW;AAClB;AAEA,IAAI,UAAU;AACd,MAAM,SAAS,MAAM,aAAa,EAAE,OAAO;AAcpC,MAAM,uBAAoC;AAAA,EAC/C,OAAO;AAAA,EACP,eAAe;AAAA,EACf,MAAM;AAAA,EACN,MAAM;AACR;AAKA,IAAI,eAA4B;AA2CzB,MAAM,wBAAwB;A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D,OAAO,EAAE,GAAG,mBAAmB,GAAG,MAAM;AAAA;AAAA,EAC1C;AAEJ;","names":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentPropsWithoutRef, ReactElement } from 'react';
|
|
1
|
+
import { ComponentPropsWithoutRef, ReactNode, ReactElement } from 'react';
|
|
2
2
|
|
|
3
3
|
interface ToggleOption<T extends string> {
|
|
4
4
|
value: T;
|
|
@@ -40,6 +40,34 @@ interface ToggleGroupBaseProps<T extends string> extends Omit<ComponentPropsWith
|
|
|
40
40
|
* live and others are not is a menu with holes in it, and no caller here wants one.
|
|
41
41
|
*/
|
|
42
42
|
disabled?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* `sm`: 12px options with `px-2 py-1` — the compact group keksdose's rule editor
|
|
45
|
+
* (rule-editor:204) writes as `optionClassName="px-2 py-1 text-xs"` beside a small
|
|
46
|
+
* caption. `md` (default) is the size every other group has.
|
|
47
|
+
*/
|
|
48
|
+
size?: "sm" | "md";
|
|
49
|
+
/**
|
|
50
|
+
* Stand in a form row as a FIELD: with a label the group wears the field's chrome —
|
|
51
|
+
* border, surface, the top strip with a small static label in it, a labelled
|
|
52
|
+
* {@link Select}'s height — so beside an Input or a Select it reads as one of them
|
|
53
|
+
* rather than as a control with a caption over it. lenkbank builds exactly this by
|
|
54
|
+
* hand as `ToggleField` (features/gear/common.tsx:97, feedback #69), and its notes
|
|
55
|
+
* are why the chrome STRETCHES to its row as well as matching the select's padding:
|
|
56
|
+
* a native select's height is the browser's, so a toggle a few pixels short of it
|
|
57
|
+
* is levelled up by the row rather than by arithmetic.
|
|
58
|
+
*
|
|
59
|
+
* The label names the group (`aria-labelledby`), so `aria-label` is not needed. With
|
|
60
|
+
* a label, `className` styles the field's wrapper — as on {@link Select} — and the
|
|
61
|
+
* group's own box is dropped: two nested borders read as a control in a control.
|
|
62
|
+
*/
|
|
63
|
+
label?: ReactNode;
|
|
64
|
+
/** A {@link FieldHint} on the label line, as on a labelled {@link Select}. Only with
|
|
65
|
+
* `label`. */
|
|
66
|
+
hint?: ReactNode;
|
|
67
|
+
/** The message under the field when it is wrong: paints the field's border with
|
|
68
|
+
* `--danger`, marks the group `aria-invalid` and describes it with the message, as
|
|
69
|
+
* {@link Select}'s `error` does. Only with `label`. */
|
|
70
|
+
error?: ReactNode;
|
|
43
71
|
}
|
|
44
72
|
/** The group as it has always been: one option is always the answer. */
|
|
45
73
|
interface ToggleGroupRequiredProps<T extends string> extends ToggleGroupBaseProps<T> {
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useId } from "react";
|
|
3
4
|
import { cn } from "../lib/cn.js";
|
|
4
5
|
import { horizontalStep } from "../lib/direction.js";
|
|
6
|
+
import { FIELD_INVALID, FloatingField } from "./ui.js";
|
|
5
7
|
function ToggleGroup(props) {
|
|
6
8
|
const {
|
|
7
9
|
value,
|
|
@@ -10,9 +12,17 @@ function ToggleGroup(props) {
|
|
|
10
12
|
optionClassName,
|
|
11
13
|
ariaLabel,
|
|
12
14
|
disabled = false,
|
|
15
|
+
size = "md",
|
|
16
|
+
label,
|
|
17
|
+
hint,
|
|
18
|
+
error,
|
|
13
19
|
"aria-label": ariaLabelAttr,
|
|
14
20
|
...restWithMode
|
|
15
21
|
} = props;
|
|
22
|
+
const labelId = useId();
|
|
23
|
+
const errorId = useId();
|
|
24
|
+
const field = label !== void 0 && label !== null && label !== false && label !== "";
|
|
25
|
+
const hasError = field && error !== void 0 && error !== null && error !== false && error !== "";
|
|
16
26
|
const { allowEmpty: _allowEmpty, onChange: _onChange, ...rest } = restWithMode;
|
|
17
27
|
const choose = (next) => {
|
|
18
28
|
if (props.allowEmpty) props.onChange(next === value ? null : next);
|
|
@@ -36,12 +46,15 @@ function ToggleGroup(props) {
|
|
|
36
46
|
buttons?.[next]?.focus();
|
|
37
47
|
choose(options[next].value);
|
|
38
48
|
};
|
|
39
|
-
|
|
49
|
+
const group = /* @__PURE__ */ jsx(
|
|
40
50
|
"div",
|
|
41
51
|
{
|
|
42
52
|
...rest,
|
|
43
53
|
role: clearable ? "group" : "radiogroup",
|
|
44
54
|
"aria-label": ariaLabelAttr ?? ariaLabel,
|
|
55
|
+
"aria-labelledby": field && ariaLabelAttr === void 0 && ariaLabel === void 0 ? labelId : rest["aria-labelledby"],
|
|
56
|
+
"aria-invalid": hasError || rest["aria-invalid"] || void 0,
|
|
57
|
+
"aria-describedby": hasError ? rest["aria-describedby"] ? `${rest["aria-describedby"]} ${errorId}` : errorId : rest["aria-describedby"],
|
|
45
58
|
"aria-disabled": disabled || void 0,
|
|
46
59
|
className: cn(
|
|
47
60
|
// `gap-0.5` — the same 2px as the container's own padding, so EVERY segment
|
|
@@ -61,7 +74,10 @@ function ToggleGroup(props) {
|
|
|
61
74
|
// package does; `cursor-not-allowed` is on the buttons, which is what a
|
|
62
75
|
// pointer is actually over.
|
|
63
76
|
disabled && "opacity-60",
|
|
64
|
-
|
|
77
|
+
// Inside the field's chrome the group is only a row of segments: no border, no
|
|
78
|
+
// surface, no padding of its own, and the field (not the group) is what dims.
|
|
79
|
+
field && "border-0 bg-transparent p-0 shadow-none opacity-100",
|
|
80
|
+
!field && className
|
|
65
81
|
),
|
|
66
82
|
children: options.map((opt, index) => {
|
|
67
83
|
const active = opt.value === value;
|
|
@@ -100,6 +116,11 @@ function ToggleGroup(props) {
|
|
|
100
116
|
// inside the segment it belongs to; focus-visible keeps it for the
|
|
101
117
|
// keyboard, which is the only input that needs it.
|
|
102
118
|
"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)]",
|
|
119
|
+
size === "sm" && "px-2 py-1 text-xs",
|
|
120
|
+
// In a field: no vertical padding and a 20px line — a `text-sm` line, the
|
|
121
|
+
// same line a labelled Select holds under its label strip — so the field's
|
|
122
|
+
// own `pt-4 pb-1` decides the height, as it does for the select.
|
|
123
|
+
field && "py-0 leading-5",
|
|
103
124
|
active ? "bg-[var(--bg-inverse)] text-[var(--text-inverse)]" : "text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]",
|
|
104
125
|
// No hover fill on a group that cannot be changed — a segment that
|
|
105
126
|
// lights up under the pointer is an offer, and there is none here.
|
|
@@ -114,6 +135,34 @@ function ToggleGroup(props) {
|
|
|
114
135
|
})
|
|
115
136
|
}
|
|
116
137
|
);
|
|
138
|
+
if (!field) return group;
|
|
139
|
+
return (
|
|
140
|
+
// `h-full` + `flex-1`: the chrome fills a grid or stretched flex row, which is what
|
|
141
|
+
// levels it with a select beside it whatever the browser makes of the select.
|
|
142
|
+
/* @__PURE__ */ jsxs("div", { className: cn("flex h-full flex-col", className), children: [
|
|
143
|
+
/* @__PURE__ */ jsx(
|
|
144
|
+
FloatingField,
|
|
145
|
+
{
|
|
146
|
+
className: "flex flex-1 flex-col",
|
|
147
|
+
label: /* @__PURE__ */ jsx("span", { id: labelId, children: label }),
|
|
148
|
+
staticLabel: true,
|
|
149
|
+
hint,
|
|
150
|
+
children: /* @__PURE__ */ jsx(
|
|
151
|
+
"div",
|
|
152
|
+
{
|
|
153
|
+
className: cn(
|
|
154
|
+
"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",
|
|
155
|
+
disabled && "bg-[var(--bg-surface-2)] opacity-60",
|
|
156
|
+
hasError && FIELD_INVALID
|
|
157
|
+
),
|
|
158
|
+
children: group
|
|
159
|
+
}
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
),
|
|
163
|
+
hasError && /* @__PURE__ */ jsx("p", { id: errorId, className: "mt-1 text-[11px] leading-tight text-[var(--danger)]", children: error })
|
|
164
|
+
] })
|
|
165
|
+
);
|
|
117
166
|
}
|
|
118
167
|
export {
|
|
119
168
|
ToggleGroup
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/components/toggle-group.tsx"],"sourcesContent":["import type { ComponentPropsWithoutRef, KeyboardEvent, ReactElement } from \"react\";\nimport { cn } from \"../lib/cn\";\nimport { horizontalStep } from \"../lib/direction\";\n\nexport interface ToggleOption<T extends string> {\n value: T;\n label: string;\n className?: string;\n}\n\n/**\n * `onChange` is the group's own — the chosen VALUE, not a DOM event — so the div's\n * `onChange` is omitted rather than shadowed: leaving both in scope would give the\n * prop two incompatible meanings depending on which overload TypeScript picked.\n *\n * `children` is omitted too: this renders its `options` and nothing else, so a `children` the type\n * accepted and the component ignored would be a prop that silently does nothing —\n * worse than one that does not compile.\n */\nexport interface ToggleGroupBaseProps<T extends string>\n extends Omit<ComponentPropsWithoutRef<\"div\">, \"onChange\" | \"children\"> {\n options: ToggleOption<T>[];\n className?: string;\n /** Applied to every option button (e.g. to tune height/rounding to match\n * adjacent fields). Per-option `className` still wins over this. */\n optionClassName?: string;\n /**\n * @deprecated Pass `aria-label` instead — the DOM spelling, which every other\n * control in this kit now answers to. Kept working because three applications ship\n * this one today; it names the group only when `aria-label` is absent.\n */\n ariaLabel?: string;\n /**\n * Show, refuse the change (Keksdose live #288: a payment dated in the future has no\n * state to set).\n *\n * Whatever `value` says stays pressed and keeps its own fill rather than going grey\n * with the rest — a reader who cannot see WHICH option is chosen has been told less\n * than before it was disabled. A caller with nothing to show passes no value, and\n * the group renders dimmed with nothing pressed, which is the shape Keksdose's\n * status picker uses for a row whose status does not exist yet.\n *\n * On the whole GROUP, not per option: a segmented control where some segments are\n * live and others are not is a menu with holes in it, and no caller here wants one.\n */\n disabled?: boolean;\n}\n\n/** The group as it has always been: one option is always the answer. */\nexport interface ToggleGroupRequiredProps<T extends string> extends ToggleGroupBaseProps<T> {\n allowEmpty?: false;\n value: T;\n onChange: (value: T) => void;\n}\n\n/**\n * A group that can be emptied: clicking the active option clears it, and `onChange`\n * receives `null` (Keksdose's support-panel filters, where \"no filter\" is reached by\n * clicking the filter that is on).\n *\n * The options become TOGGLE BUTTONS (`aria-pressed`, in a `role=\"group\"`) rather than\n * radios. A radio cannot be unchecked by activating it — no screen reader user expects\n * a second press on \"Open, radio, checked\" to leave nothing checked, and nothing would\n * tell them it had. \"Open, toggle button, pressed\" says exactly what a press will do.\n */\nexport interface ToggleGroupClearableProps<T extends string> extends ToggleGroupBaseProps<T> {\n allowEmpty: true;\n value: T | null;\n onChange: (value: T | null) => void;\n}\n\n/** `allowEmpty` picks the shape, so `onChange` is typed `(T) => void` unless the group\n * can actually emit `null` — no existing caller has a `null` to handle. */\nexport type ToggleGroupProps<T extends string> =\n | ToggleGroupRequiredProps<T>\n | ToggleGroupClearableProps<T>;\n\n/**\n * Overloaded rather than typed by the union alone (keksdose, \"Gaps found adopting\n * 0.6.0\" #7). Inferring `T` through a union of prop shapes let TypeScript settle on\n * `string`, so `<ToggleGroup allowEmpty value={filter} onChange={setFilter} />` over a\n * `useState<Status | null>` did not compile unless the caller spelled\n * `<ToggleGroup<Status>>`. One signature per mode lets each infer `T` from its own\n * `value`/`onChange`/`options`; the third keeps a caller that forwards a\n * {@link ToggleGroupProps} union (a wrapper component) compiling.\n */\nexport function ToggleGroup<T extends string>(props: ToggleGroupClearableProps<T>): ReactElement;\nexport function ToggleGroup<T extends string>(props: ToggleGroupRequiredProps<T>): ReactElement;\nexport function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): ReactElement;\nexport function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): ReactElement {\n const {\n value,\n options,\n className,\n optionClassName,\n ariaLabel,\n disabled = false,\n \"aria-label\": ariaLabelAttr,\n ...restWithMode\n } = props;\n // Taken off the rest so neither reaches the DOM; `props` keeps them paired, which is\n // what lets the `onChange` below be called with `null` only in the mode that allows it.\n const { allowEmpty: _allowEmpty, onChange: _onChange, ...rest } = restWithMode;\n const choose = (next: T) => {\n if (props.allowEmpty) props.onChange(next === value ? null : next);\n else props.onChange(next);\n };\n const clearable = props.allowEmpty === true;\n // A radio group is ONE tab stop (the checked radio, else the first) and arrows move\n // the choice — the pattern `role=\"radiogroup\"` promises a screen-reader user. Before\n // 0.7.0 each segment was its own tab stop with no arrow keys. The clearable mode is a\n // row of toggle buttons, where separate tab stops are the pattern.\n const tabStop = options.some((o) => o.value === value) ? value : options[0]?.value;\n const onRadioKey = (e: KeyboardEvent<HTMLButtonElement>, index: number) => {\n const last = options.length - 1;\n let next: number | null = null;\n const step = horizontalStep(e.key, e.currentTarget);\n if (step !== 0) next = index + step;\n else if (e.key === \"ArrowDown\") next = index + 1;\n else if (e.key === \"ArrowUp\") next = index - 1;\n else if (e.key === \"Home\") next = 0;\n else if (e.key === \"End\") next = last;\n if (next === null) return;\n e.preventDefault();\n next = next < 0 ? last : next > last ? 0 : next;\n const buttons = e.currentTarget.parentElement?.querySelectorAll<HTMLButtonElement>(\":scope > button\");\n buttons?.[next]?.focus();\n choose(options[next].value);\n };\n return (\n <div\n // The audit's named example of a closed prop list (§\"Public API design\"): the\n // tour locates a step by CSS SELECTOR, so a component that drops every attribute\n // it was not expecting cannot be spotlighted at all — and Keksdose's rule editor\n // carries a comment explaining that it wraps this group in a bare <div> for\n // exactly that reason.\n //\n // `...rest` first, then the attributes the group cannot do without: a caller\n // hanging an anchor or a test id on the group must not be able to overwrite the\n // radiogroup role or the disabled state by accident. `className` is destructured\n // out entirely and merged through `cn`, so it is never in here.\n {...rest}\n role={clearable ? \"group\" : \"radiogroup\"}\n // The DOM spelling wins; `ariaLabel` is the fallback for the call sites that\n // have not moved yet.\n aria-label={ariaLabelAttr ?? ariaLabel}\n // `aria-disabled` on the group as well as `disabled` on each button: a radio\n // group is what the user is being refused, and a screen reader announcing\n // three separately-disabled radios does not say that.\n aria-disabled={disabled || undefined}\n className={cn(\n // `gap-0.5` — the same 2px as the container's own padding, so EVERY segment\n // sits in a uniform 2px moat and no two fills ever touch. Flush segments were\n // Keksdose live #268's rework: the pressed segment wears a saturated fill and\n // an unpressed neighbour wears a pale hover fill, and with a shared edge the\n // two rectangles read as one smeared shape — *\"the boundary of the selected\n // option and hovering next to it overlays the boundary of the selected\n // button\"*. A gap is what makes each segment its own chip; it cannot be\n // undone by a caller's per-option colour, which a hover-only fix could.\n //\n // (The hover fill is the DESKTOP half of that report: Tailwind v4 wraps every\n // `hover:` in `@media (hover: hover)`, so a phone never paints it. The half a\n // phone does see is the focus ring — see the segment's own note below.)\n \"inline-flex w-full gap-0.5 rounded-md border border-[var(--border-strong)] bg-[var(--bg-surface)] p-0.5 shadow-sm\",\n // The whole group fades, the way every other disabled control in this\n // package does; `cursor-not-allowed` is on the buttons, which is what a\n // pointer is actually over.\n disabled && \"opacity-60\",\n className,\n )}\n >\n {options.map((opt, index) => {\n const active = opt.value === value;\n return (\n <button\n key={opt.value}\n type=\"button\"\n role={clearable ? undefined : \"radio\"}\n aria-checked={clearable ? undefined : active}\n aria-pressed={clearable ? active : undefined}\n disabled={disabled}\n tabIndex={clearable ? undefined : opt.value === tabStop ? 0 : -1}\n onKeyDown={clearable ? undefined : (e) => onRadioKey(e, index)}\n onClick={() => choose(opt.value)}\n className={cn(\n // `truncate` (which carries whitespace-nowrap) rather than letting a\n // label wrap: a segmented control sizes its whole row to the tallest\n // option, so one two-word option — Keksdose feedback #147's \"Where I\n // am\" — silently doubles the height of every segment beside it.\n //\n // `basis-auto` is what keeps that ellipsis a LAST resort rather than\n // the normal state (Keksdose dev#475). With flex-1's `basis-0`, a\n // shrink-to-fit group (`w-auto`) still resolves to the sum of the\n // labels' widths — and then splits it EQUALLY, so the short option\n // got 66px it did not need and \"Where I am\" got 66 of the 81 it did:\n // truncated at 1778px of free screen. Basing each segment on its own\n // content and sharing only the LEFTOVER space keeps a full-width\n // group's segments near-equal and an auto-width group's exact.\n //\n // `focus-visible` + `ring-inset`, not `focus` + an outset ring. A ring\n // is a box-shadow that spreads OUTWARD, so on a flush group it painted\n // 2px of ring over both neighbours and over the container's own border\n // — and on a phone it appeared on every TAP, because a tap focuses the\n // button. That is the other half of what live #268's rework saw\n // overlaying the selected segment's boundary. Inset keeps the ring\n // inside the segment it belongs to; focus-visible keeps it for the\n // keyboard, which is the only input that needs it.\n \"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)]\",\n active\n ? \"bg-[var(--bg-inverse)] text-[var(--text-inverse)]\"\n : \"text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]\",\n // No hover fill on a group that cannot be changed — a segment that\n // lights up under the pointer is an offer, and there is none here.\n disabled && \"cursor-not-allowed hover:bg-transparent dark:hover:bg-transparent\",\n optionClassName,\n opt.className,\n )}\n >\n {opt.label}\n </button>\n );\n })}\n </div>\n );\n}\n"],"mappings":";AA8KU;AA7KV,SAAS,UAAU;AACnB,SAAS,sBAAsB;AAuFxB,SAAS,YAA8B,OAA0C;AACtF,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,cAAc;AAAA,IACd,GAAG;AAAA,EACL,IAAI;AAGJ,QAAM,EAAE,YAAY,aAAa,UAAU,WAAW,GAAG,KAAK,IAAI;AAClE,QAAM,SAAS,CAAC,SAAY;AAC1B,QAAI,MAAM,WAAY,OAAM,SAAS,SAAS,QAAQ,OAAO,IAAI;AAAA,QAC5D,OAAM,SAAS,IAAI;AAAA,EAC1B;AACA,QAAM,YAAY,MAAM,eAAe;AAKvC,QAAM,UAAU,QAAQ,KAAK,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,QAAQ,QAAQ,CAAC,GAAG;AAC7E,QAAM,aAAa,CAAC,GAAqC,UAAkB;AACzE,UAAM,OAAO,QAAQ,SAAS;AAC9B,QAAI,OAAsB;AAC1B,UAAM,OAAO,eAAe,EAAE,KAAK,EAAE,aAAa;AAClD,QAAI,SAAS,EAAG,QAAO,QAAQ;AAAA,aACtB,EAAE,QAAQ,YAAa,QAAO,Q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A,cAGJ,YAAY;AAAA,cACZ;AAAA,cACA,IAAI;AAAA,YACN;AAAA,YAEC,cAAI;AAAA;AAAA,UA3CA,IAAI;AAAA,QA4CX;AAAA,MAEJ,CAAC;AAAA;AAAA,EACH;AAEJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/components/toggle-group.tsx"],"sourcesContent":["import { useId } from \"react\";\nimport type { ComponentPropsWithoutRef, KeyboardEvent, ReactElement, ReactNode } from \"react\";\nimport { cn } from \"../lib/cn\";\nimport { horizontalStep } from \"../lib/direction\";\nimport { FIELD_INVALID, FloatingField } from \"./ui\";\n\nexport interface ToggleOption<T extends string> {\n value: T;\n label: string;\n className?: string;\n}\n\n/**\n * `onChange` is the group's own — the chosen VALUE, not a DOM event — so the div's\n * `onChange` is omitted rather than shadowed: leaving both in scope would give the\n * prop two incompatible meanings depending on which overload TypeScript picked.\n *\n * `children` is omitted too: this renders its `options` and nothing else, so a `children` the type\n * accepted and the component ignored would be a prop that silently does nothing —\n * worse than one that does not compile.\n */\nexport interface ToggleGroupBaseProps<T extends string>\n extends Omit<ComponentPropsWithoutRef<\"div\">, \"onChange\" | \"children\"> {\n options: ToggleOption<T>[];\n className?: string;\n /** Applied to every option button (e.g. to tune height/rounding to match\n * adjacent fields). Per-option `className` still wins over this. */\n optionClassName?: string;\n /**\n * @deprecated Pass `aria-label` instead — the DOM spelling, which every other\n * control in this kit now answers to. Kept working because three applications ship\n * this one today; it names the group only when `aria-label` is absent.\n */\n ariaLabel?: string;\n /**\n * Show, refuse the change (Keksdose live #288: a payment dated in the future has no\n * state to set).\n *\n * Whatever `value` says stays pressed and keeps its own fill rather than going grey\n * with the rest — a reader who cannot see WHICH option is chosen has been told less\n * than before it was disabled. A caller with nothing to show passes no value, and\n * the group renders dimmed with nothing pressed, which is the shape Keksdose's\n * status picker uses for a row whose status does not exist yet.\n *\n * On the whole GROUP, not per option: a segmented control where some segments are\n * live and others are not is a menu with holes in it, and no caller here wants one.\n */\n disabled?: boolean;\n /**\n * `sm`: 12px options with `px-2 py-1` — the compact group keksdose's rule editor\n * (rule-editor:204) writes as `optionClassName=\"px-2 py-1 text-xs\"` beside a small\n * caption. `md` (default) is the size every other group has.\n */\n size?: \"sm\" | \"md\";\n /**\n * Stand in a form row as a FIELD: with a label the group wears the field's chrome —\n * border, surface, the top strip with a small static label in it, a labelled\n * {@link Select}'s height — so beside an Input or a Select it reads as one of them\n * rather than as a control with a caption over it. lenkbank builds exactly this by\n * hand as `ToggleField` (features/gear/common.tsx:97, feedback #69), and its notes\n * are why the chrome STRETCHES to its row as well as matching the select's padding:\n * a native select's height is the browser's, so a toggle a few pixels short of it\n * is levelled up by the row rather than by arithmetic.\n *\n * The label names the group (`aria-labelledby`), so `aria-label` is not needed. With\n * a label, `className` styles the field's wrapper — as on {@link Select} — and the\n * group's own box is dropped: two nested borders read as a control in a control.\n */\n label?: ReactNode;\n /** A {@link FieldHint} on the label line, as on a labelled {@link Select}. Only with\n * `label`. */\n hint?: ReactNode;\n /** The message under the field when it is wrong: paints the field's border with\n * `--danger`, marks the group `aria-invalid` and describes it with the message, as\n * {@link Select}'s `error` does. Only with `label`. */\n error?: ReactNode;\n}\n\n/** The group as it has always been: one option is always the answer. */\nexport interface ToggleGroupRequiredProps<T extends string> extends ToggleGroupBaseProps<T> {\n allowEmpty?: false;\n value: T;\n onChange: (value: T) => void;\n}\n\n/**\n * A group that can be emptied: clicking the active option clears it, and `onChange`\n * receives `null` (Keksdose's support-panel filters, where \"no filter\" is reached by\n * clicking the filter that is on).\n *\n * The options become TOGGLE BUTTONS (`aria-pressed`, in a `role=\"group\"`) rather than\n * radios. A radio cannot be unchecked by activating it — no screen reader user expects\n * a second press on \"Open, radio, checked\" to leave nothing checked, and nothing would\n * tell them it had. \"Open, toggle button, pressed\" says exactly what a press will do.\n */\nexport interface ToggleGroupClearableProps<T extends string> extends ToggleGroupBaseProps<T> {\n allowEmpty: true;\n value: T | null;\n onChange: (value: T | null) => void;\n}\n\n/** `allowEmpty` picks the shape, so `onChange` is typed `(T) => void` unless the group\n * can actually emit `null` — no existing caller has a `null` to handle. */\nexport type ToggleGroupProps<T extends string> =\n | ToggleGroupRequiredProps<T>\n | ToggleGroupClearableProps<T>;\n\n/**\n * Overloaded rather than typed by the union alone (keksdose, \"Gaps found adopting\n * 0.6.0\" #7). Inferring `T` through a union of prop shapes let TypeScript settle on\n * `string`, so `<ToggleGroup allowEmpty value={filter} onChange={setFilter} />` over a\n * `useState<Status | null>` did not compile unless the caller spelled\n * `<ToggleGroup<Status>>`. One signature per mode lets each infer `T` from its own\n * `value`/`onChange`/`options`; the third keeps a caller that forwards a\n * {@link ToggleGroupProps} union (a wrapper component) compiling.\n */\nexport function ToggleGroup<T extends string>(props: ToggleGroupClearableProps<T>): ReactElement;\nexport function ToggleGroup<T extends string>(props: ToggleGroupRequiredProps<T>): ReactElement;\nexport function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): ReactElement;\nexport function ToggleGroup<T extends string>(props: ToggleGroupProps<T>): ReactElement {\n const {\n value,\n options,\n className,\n optionClassName,\n ariaLabel,\n disabled = false,\n size = \"md\",\n label,\n hint,\n error,\n \"aria-label\": ariaLabelAttr,\n ...restWithMode\n } = props;\n const labelId = useId();\n const errorId = useId();\n const field = label !== undefined && label !== null && label !== false && label !== \"\";\n const hasError = field && error !== undefined && error !== null && error !== false && error !== \"\";\n // Taken off the rest so neither reaches the DOM; `props` keeps them paired, which is\n // what lets the `onChange` below be called with `null` only in the mode that allows it.\n const { allowEmpty: _allowEmpty, onChange: _onChange, ...rest } = restWithMode;\n const choose = (next: T) => {\n if (props.allowEmpty) props.onChange(next === value ? null : next);\n else props.onChange(next);\n };\n const clearable = props.allowEmpty === true;\n // A radio group is ONE tab stop (the checked radio, else the first) and arrows move\n // the choice — the pattern `role=\"radiogroup\"` promises a screen-reader user. Before\n // 0.7.0 each segment was its own tab stop with no arrow keys. The clearable mode is a\n // row of toggle buttons, where separate tab stops are the pattern.\n const tabStop = options.some((o) => o.value === value) ? value : options[0]?.value;\n const onRadioKey = (e: KeyboardEvent<HTMLButtonElement>, index: number) => {\n const last = options.length - 1;\n let next: number | null = null;\n const step = horizontalStep(e.key, e.currentTarget);\n if (step !== 0) next = index + step;\n else if (e.key === \"ArrowDown\") next = index + 1;\n else if (e.key === \"ArrowUp\") next = index - 1;\n else if (e.key === \"Home\") next = 0;\n else if (e.key === \"End\") next = last;\n if (next === null) return;\n e.preventDefault();\n next = next < 0 ? last : next > last ? 0 : next;\n const buttons = e.currentTarget.parentElement?.querySelectorAll<HTMLButtonElement>(\":scope > button\");\n buttons?.[next]?.focus();\n choose(options[next].value);\n };\n const group = (\n <div\n // The audit's named example of a closed prop list (§\"Public API design\"): the\n // tour locates a step by CSS SELECTOR, so a component that drops every attribute\n // it was not expecting cannot be spotlighted at all — and Keksdose's rule editor\n // carries a comment explaining that it wraps this group in a bare <div> for\n // exactly that reason.\n //\n // `...rest` first, then the attributes the group cannot do without: a caller\n // hanging an anchor or a test id on the group must not be able to overwrite the\n // radiogroup role or the disabled state by accident. `className` is destructured\n // out entirely and merged through `cn`, so it is never in here.\n {...rest}\n role={clearable ? \"group\" : \"radiogroup\"}\n // The DOM spelling wins; `ariaLabel` is the fallback for the call sites that\n // have not moved yet.\n aria-label={ariaLabelAttr ?? ariaLabel}\n aria-labelledby={field && ariaLabelAttr === undefined && ariaLabel === undefined ? labelId : rest[\"aria-labelledby\"]}\n aria-invalid={hasError || rest[\"aria-invalid\"] || undefined}\n aria-describedby={\n hasError ? (rest[\"aria-describedby\"] ? `${rest[\"aria-describedby\"]} ${errorId}` : errorId) : rest[\"aria-describedby\"]\n }\n // `aria-disabled` on the group as well as `disabled` on each button: a radio\n // group is what the user is being refused, and a screen reader announcing\n // three separately-disabled radios does not say that.\n aria-disabled={disabled || undefined}\n className={cn(\n // `gap-0.5` — the same 2px as the container's own padding, so EVERY segment\n // sits in a uniform 2px moat and no two fills ever touch. Flush segments were\n // Keksdose live #268's rework: the pressed segment wears a saturated fill and\n // an unpressed neighbour wears a pale hover fill, and with a shared edge the\n // two rectangles read as one smeared shape — *\"the boundary of the selected\n // option and hovering next to it overlays the boundary of the selected\n // button\"*. A gap is what makes each segment its own chip; it cannot be\n // undone by a caller's per-option colour, which a hover-only fix could.\n //\n // (The hover fill is the DESKTOP half of that report: Tailwind v4 wraps every\n // `hover:` in `@media (hover: hover)`, so a phone never paints it. The half a\n // phone does see is the focus ring — see the segment's own note below.)\n \"inline-flex w-full gap-0.5 rounded-md border border-[var(--border-strong)] bg-[var(--bg-surface)] p-0.5 shadow-sm\",\n // The whole group fades, the way every other disabled control in this\n // package does; `cursor-not-allowed` is on the buttons, which is what a\n // pointer is actually over.\n disabled && \"opacity-60\",\n // Inside the field's chrome the group is only a row of segments: no border, no\n // surface, no padding of its own, and the field (not the group) is what dims.\n field && \"border-0 bg-transparent p-0 shadow-none opacity-100\",\n !field && className,\n )}\n >\n {options.map((opt, index) => {\n const active = opt.value === value;\n return (\n <button\n key={opt.value}\n type=\"button\"\n role={clearable ? undefined : \"radio\"}\n aria-checked={clearable ? undefined : active}\n aria-pressed={clearable ? active : undefined}\n disabled={disabled}\n tabIndex={clearable ? undefined : opt.value === tabStop ? 0 : -1}\n onKeyDown={clearable ? undefined : (e) => onRadioKey(e, index)}\n onClick={() => choose(opt.value)}\n className={cn(\n // `truncate` (which carries whitespace-nowrap) rather than letting a\n // label wrap: a segmented control sizes its whole row to the tallest\n // option, so one two-word option — Keksdose feedback #147's \"Where I\n // am\" — silently doubles the height of every segment beside it.\n //\n // `basis-auto` is what keeps that ellipsis a LAST resort rather than\n // the normal state (Keksdose dev#475). With flex-1's `basis-0`, a\n // shrink-to-fit group (`w-auto`) still resolves to the sum of the\n // labels' widths — and then splits it EQUALLY, so the short option\n // got 66px it did not need and \"Where I am\" got 66 of the 81 it did:\n // truncated at 1778px of free screen. Basing each segment on its own\n // content and sharing only the LEFTOVER space keeps a full-width\n // group's segments near-equal and an auto-width group's exact.\n //\n // `focus-visible` + `ring-inset`, not `focus` + an outset ring. A ring\n // is a box-shadow that spreads OUTWARD, so on a flush group it painted\n // 2px of ring over both neighbours and over the container's own border\n // — and on a phone it appeared on every TAP, because a tap focuses the\n // button. That is the other half of what live #268's rework saw\n // overlaying the selected segment's boundary. Inset keeps the ring\n // inside the segment it belongs to; focus-visible keeps it for the\n // keyboard, which is the only input that needs it.\n \"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)]\",\n size === \"sm\" && \"px-2 py-1 text-xs\",\n // In a field: no vertical padding and a 20px line — a `text-sm` line, the\n // same line a labelled Select holds under its label strip — so the field's\n // own `pt-4 pb-1` decides the height, as it does for the select.\n field && \"py-0 leading-5\",\n active\n ? \"bg-[var(--bg-inverse)] text-[var(--text-inverse)]\"\n : \"text-[var(--text-secondary)] hover:bg-[var(--bg-hover)]\",\n // No hover fill on a group that cannot be changed — a segment that\n // lights up under the pointer is an offer, and there is none here.\n disabled && \"cursor-not-allowed hover:bg-transparent dark:hover:bg-transparent\",\n optionClassName,\n opt.className,\n )}\n >\n {opt.label}\n </button>\n );\n })}\n </div>\n );\n if (!field) return group;\n return (\n // `h-full` + `flex-1`: the chrome fills a grid or stretched flex row, which is what\n // levels it with a select beside it whatever the browser makes of the select.\n <div className={cn(\"flex h-full flex-col\", className)}>\n <FloatingField\n className=\"flex flex-1 flex-col\"\n label={<span id={labelId}>{label}</span>}\n staticLabel\n hint={hint}\n >\n <div\n className={cn(\n \"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\",\n disabled && \"bg-[var(--bg-surface-2)] opacity-60\",\n hasError && FIELD_INVALID,\n )}\n >\n {group}\n </div>\n </FloatingField>\n {hasError && (\n <p id={errorId} className=\"mt-1 text-[11px] leading-tight text-[var(--danger)]\">\n {error}\n </p>\n )}\n </div>\n );\n}\n"],"mappings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AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,cAuBT;AAAA,cACA,SAAS,QAAQ;AAAA;AAAA;AAAA;AAAA,cAIjB,SAAS;AAAA,cACT,SACI,sDACA;AAAA;AAAA;AAAA,cAGJ,YAAY;AAAA,cACZ;AAAA,cACA,IAAI;AAAA,YACN;AAAA,YAEC,cAAI;AAAA;AAAA,UAhDA,IAAI;AAAA,QAiDX;AAAA,MAEJ,CAAC;AAAA;AAAA,EACH;AAEF,MAAI,CAAC,MAAO,QAAO;AACnB;AAAA;AAAA;AAAA,IAGE,qBAAC,SAAI,WAAW,GAAG,wBAAwB,SAAS,GAClD;AAAA;AAAA,QAAC;AAAA;AAAA,UACC,WAAU;AAAA,UACV,OAAO,oBAAC,UAAK,IAAI,SAAU,iBAAM;AAAA,UACjC,aAAW;AAAA,UACX;AAAA,UAEA;AAAA,YAAC;AAAA;AAAA,cACC,WAAW;AAAA,gBACT;AAAA,gBACA,YAAY;AAAA,gBACZ,YAAY;AAAA,cACd;AAAA,cAEC;AAAA;AAAA,UACH;AAAA;AAAA,MACF;AAAA,MACC,YACC,oBAAC,OAAE,IAAI,SAAS,WAAU,uDACvB,iBACH;AAAA,OAEJ;AAAA;AAEJ;","names":[]}
|
|
@@ -22,6 +22,13 @@ interface TooltipProps extends ComponentPropsWithoutRef<"span"> {
|
|
|
22
22
|
label: ReactNode;
|
|
23
23
|
side?: TooltipSide;
|
|
24
24
|
className?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Where the bubble lives. Left out (the default since 0.10.0), the tooltip decides for
|
|
27
|
+
* itself: the bubble stays next to the trigger unless an ancestor clips or scrolls
|
|
28
|
+
* (`overflow` other than `visible`), in which case it is portalled — see "Inside a
|
|
29
|
+
* scroll container" below. `true` always portals, `false` never does; both are exactly
|
|
30
|
+
* what they were before the default existed.
|
|
31
|
+
*/
|
|
25
32
|
portal?: boolean;
|
|
26
33
|
/** Tag the bubble `data-private`, for a label that repeats the user's own data. */
|
|
27
34
|
redact?: boolean;
|
|
@@ -30,11 +37,10 @@ interface TooltipProps extends ComponentPropsWithoutRef<"span"> {
|
|
|
30
37
|
/**
|
|
31
38
|
* Hover/focus label for a control.
|
|
32
39
|
*
|
|
33
|
-
* Two
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* measurement.
|
|
40
|
+
* Two placements, and the choice matters more than it looks. In place, the bubble is
|
|
41
|
+
* always mounted next to the trigger and fades in on `:hover`, which costs no state and
|
|
42
|
+
* works in a plain render test. Portalled, the bubble is mounted in `document.body`
|
|
43
|
+
* only while it is up, positioned by measurement.
|
|
38
44
|
*
|
|
39
45
|
* ⚠️ **A bubble that repeats a value has to be redactable.** The consuming app blurs
|
|
40
46
|
* `[data-private]` under a `demo-mode` class on `<html>` — and the portalled bubble is
|
|
@@ -66,18 +72,35 @@ interface TooltipProps extends ComponentPropsWithoutRef<"span"> {
|
|
|
66
72
|
* point somewhere else — which is precisely what you cannot do when what you need to
|
|
67
73
|
* read is underneath it. The listener is the document's rather than the wrapper's
|
|
68
74
|
* because the pointer opens this with the keyboard focus somewhere else entirely, and
|
|
69
|
-
* it is subscribed only while a bubble is actually up: the
|
|
75
|
+
* it is subscribed only while a bubble is actually up: the in-place bubble is always
|
|
70
76
|
* mounted, and a table of forty tooltips must not mean forty keydown listeners.
|
|
71
77
|
*
|
|
72
|
-
* ⚠️ **Inside a scroll container,
|
|
73
|
-
* absolutely positioned, but an absolutely
|
|
74
|
-
* towards its scroll-container ancestor's
|
|
75
|
-
* bubble on a control near the right edge makes
|
|
76
|
-
* with nothing to reveal. That is what Keksdose feedback
|
|
77
|
-
* admin roster: 66px of horizontal scroll on a table that fit,
|
|
78
|
-
* tooltips nobody could see.
|
|
79
|
-
*
|
|
80
|
-
* cannot be clipped
|
|
78
|
+
* ⚠️ **Inside a scroll container, the bubble has to be portalled — and by default it
|
|
79
|
+
* now is.** An always-mounted bubble is absolutely positioned, but an absolutely
|
|
80
|
+
* positioned descendant still counts towards its scroll-container ancestor's
|
|
81
|
+
* scrollable overflow — so an invisible bubble on a control near the right edge makes
|
|
82
|
+
* the container scroll sideways with nothing to reveal. That is what Keksdose feedback
|
|
83
|
+
* dev#488 reported on the admin roster: 66px of horizontal scroll on a table that fit,
|
|
84
|
+
* 44px of it owed to tooltips nobody could see. And a visible one is clipped by the
|
|
85
|
+
* container's edge. The portalled bubble is `position: fixed` and absent until hovered,
|
|
86
|
+
* so it adds no width and cannot be clipped.
|
|
87
|
+
*
|
|
88
|
+
* Until 0.10.0 the cure was `portal` at the call site, and kastlan asked for it to stop
|
|
89
|
+
* being one: every tooltip in a table, a drawer or a scrolling card had to remember it,
|
|
90
|
+
* and the ones that forgot were only found by someone scrolling sideways. So with
|
|
91
|
+
* `portal` left out, the tooltip looks for a clipping ancestor itself — any element
|
|
92
|
+
* between it and `<body>` whose computed `overflow-x` / `overflow-y` is not `visible`.
|
|
93
|
+
* It looks at MOUNT, not only on open: dev#488's phantom scroll is caused by a bubble
|
|
94
|
+
* nobody opened, so a check that waited for the hover would find the damage already
|
|
95
|
+
* done. It looks again on every open, for a container that started scrolling after
|
|
96
|
+
* the tooltip mounted (a table that grew). Only the bubble changes place; the trigger
|
|
97
|
+
* and the caller's child stay mounted, so a switch never costs a focused button its
|
|
98
|
+
* focus. Outside any such container the in-place bubble is kept, which is still the
|
|
99
|
+
* cheaper one and the one a plain render test can find without a hover.
|
|
100
|
+
*
|
|
101
|
+
* Why not simply portal everything? Because the in-place bubble is the one existing
|
|
102
|
+
* app tests rely on (it is in the DOM without a hover), and because it follows its
|
|
103
|
+
* trigger through a scroll or an animation for free — the portalled one re-measures.
|
|
81
104
|
*/
|
|
82
105
|
declare function Tooltip({ label, side, className, portal, redact, children, ...rest }: TooltipProps): react.JSX.Element;
|
|
83
106
|
interface TooltipSize {
|