@c9up/nebula 0.2.1 → 0.2.3
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 +11 -9
- package/dist/atoms/AccessibleIcon.d.ts +24 -0
- package/dist/atoms/AccessibleIcon.js +27 -0
- package/dist/atoms/Checkbox.d.ts +1 -1
- package/dist/atoms/Checkbox.js +1 -1
- package/dist/atoms/Input.d.ts +5 -0
- package/dist/atoms/Input.js +1 -0
- package/dist/atoms/VisuallyHidden.d.ts +30 -0
- package/dist/atoms/VisuallyHidden.js +35 -0
- package/dist/atoms/index.d.ts +2 -0
- package/dist/atoms/index.js +2 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +7 -4
- package/dist/lib/children.d.ts +13 -8
- package/dist/lib/children.js +5 -0
- package/dist/molecules/Breadcrumb.d.ts +11 -2
- package/dist/molecules/Breadcrumb.js +2 -2
- package/dist/molecules/InputOTP.d.ts +2 -0
- package/dist/molecules/InputOTP.js +3 -1
- package/dist/molecules/Pagination.d.ts +34 -2
- package/dist/molecules/Pagination.js +15 -6
- package/dist/molecules/PieceMap.d.ts +72 -0
- package/dist/molecules/PieceMap.js +78 -0
- package/dist/molecules/Resizable.d.ts +2 -0
- package/dist/molecules/Resizable.js +1 -1
- package/dist/molecules/Toolbar.d.ts +50 -0
- package/dist/molecules/Toolbar.js +70 -0
- package/dist/molecules/index.d.ts +10 -7
- package/dist/molecules/index.js +8 -5
- package/dist/organisms/Calendar.d.ts +13 -0
- package/dist/organisms/Calendar.js +2 -2
- package/dist/organisms/Carousel.d.ts +8 -0
- package/dist/organisms/Carousel.js +8 -5
- package/dist/organisms/DataTable.d.ts +23 -1
- package/dist/organisms/DataTable.js +57 -27
- package/dist/organisms/DatePicker.d.ts +7 -0
- package/dist/organisms/DatePicker.js +2 -1
- package/dist/organisms/DateRangePicker.d.ts +2 -0
- package/dist/organisms/DateRangePicker.js +2 -1
- package/dist/organisms/Dialog.d.ts +13 -3
- package/dist/organisms/Dialog.js +12 -5
- package/dist/organisms/Questionnaire.d.ts +4 -0
- package/dist/organisms/Questionnaire.js +5 -2
- package/dist/organisms/Select.d.ts +23 -4
- package/dist/organisms/Select.js +115 -15
- package/dist/organisms/Sheet.d.ts +2 -0
- package/dist/organisms/Sheet.js +2 -1
- package/dist/organisms/Sidebar.d.ts +6 -0
- package/dist/organisms/Sidebar.js +9 -4
- package/dist/organisms/Toaster.d.ts +80 -10
- package/dist/organisms/Toaster.js +152 -24
- package/dist/organisms/index.d.ts +3 -3
- package/dist/organisms/index.js +1 -1
- package/dist/primitives/direction.d.ts +38 -0
- package/dist/primitives/direction.js +57 -0
- package/dist/primitives/floating.js +25 -13
- package/dist/primitives/focusable.d.ts +10 -0
- package/dist/primitives/focusable.js +10 -1
- package/dist/primitives/index.d.ts +2 -1
- package/dist/primitives/index.js +2 -1
- package/dist/primitives/rovingFocus.js +14 -7
- package/dist/templates/SettingsLayout.d.ts +2 -0
- package/dist/templates/SettingsLayout.js +1 -1
- package/nebula.css +1 -1
- package/package.json +3 -3
- package/registry.json +76 -0
- package/src/atoms/AccessibleIcon.ts +39 -0
- package/src/atoms/Checkbox.ts +1 -1
- package/src/atoms/Input.ts +6 -0
- package/src/atoms/VisuallyHidden.ts +45 -0
- package/src/atoms/index.ts +8 -0
- package/src/index.ts +7 -4
- package/src/lib/children.ts +15 -7
- package/src/molecules/Breadcrumb.ts +15 -4
- package/src/molecules/InputOTP.ts +7 -1
- package/src/molecules/Pagination.ts +50 -8
- package/src/molecules/PieceMap.ts +136 -0
- package/src/molecules/Resizable.ts +3 -1
- package/src/molecules/Toolbar.ts +100 -0
- package/src/molecules/index.ts +28 -5
- package/src/organisms/Calendar.ts +16 -2
- package/src/organisms/Carousel.ts +17 -5
- package/src/organisms/DataTable.ts +95 -30
- package/src/organisms/DatePicker.ts +10 -2
- package/src/organisms/DateRangePicker.ts +4 -2
- package/src/organisms/Dialog.ts +17 -5
- package/src/organisms/Questionnaire.ts +10 -2
- package/src/organisms/Select.ts +137 -16
- package/src/organisms/Sheet.ts +5 -1
- package/src/organisms/Sidebar.ts +15 -4
- package/src/organisms/Toaster.ts +215 -37
- package/src/organisms/index.ts +9 -1
- package/src/primitives/direction.ts +61 -0
- package/src/primitives/floating.ts +29 -13
- package/src/primitives/focusable.ts +10 -1
- package/src/primitives/index.ts +7 -0
- package/src/primitives/rovingFocus.ts +18 -5
- package/src/templates/SettingsLayout.ts +3 -1
package/src/organisms/Toaster.ts
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Toaster — transient notifications.
|
|
3
3
|
*
|
|
4
|
-
* shadcn
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* shadcn ships two: `sonner`, which wraps a third-party library, and `toast`,
|
|
5
|
+
* its own, built on Base UI. nebula keeps it in-house with zero dependencies
|
|
6
|
+
* and follows the SECOND — the shape is the same: one region mounted once near
|
|
7
|
+
* the app root, and a `toast` object callable from anywhere with no reference
|
|
8
|
+
* to it.
|
|
9
|
+
*
|
|
10
|
+
* NAMED DEVIATIONS from Base UI's manager, all deliberate:
|
|
11
|
+
*
|
|
12
|
+
* - `show` / `dismiss` / `variant` / `duration` rather than `add` / `close` /
|
|
13
|
+
* `type` / `timeout`. `variant` is what every other nebula component calls
|
|
14
|
+
* this (Button, Alert, Badge), and renaming it here alone would make the
|
|
15
|
+
* package less consistent with itself than with Base UI.
|
|
16
|
+
* - `variant` keeps `warning`, which Base UI's `type` does not have.
|
|
17
|
+
* - No `priority` (display ordering), `data` (typed payload for a React render
|
|
18
|
+
* prop — a `Child` already covers it) or `positionerProps` (anchored toasts).
|
|
19
|
+
* - No `updateKey`. It exists to make React replay an animation without
|
|
20
|
+
* remounting; `update` here replaces the entry, and aurora re-renders it.
|
|
7
21
|
*
|
|
8
22
|
* That decoupling is the whole design. A queue in module state, a signal the
|
|
9
23
|
* region subscribes to, and `toast.error(…)` works from a route handler's
|
|
@@ -25,50 +39,100 @@
|
|
|
25
39
|
import { component, html, signal } from "@c9up/aurora";
|
|
26
40
|
import type { Child } from "../lib/children.js";
|
|
27
41
|
import { cn } from "../lib/cn.js";
|
|
28
|
-
import { XIcon } from "../lib/icons.js";
|
|
42
|
+
import { LoaderIcon, XIcon } from "../lib/icons.js";
|
|
29
43
|
import { fadeInOut } from "../lib/motion.js";
|
|
30
44
|
import { type Reactive, read } from "../lib/props.js";
|
|
31
45
|
|
|
32
|
-
export type ToastVariant =
|
|
46
|
+
export type ToastVariant =
|
|
47
|
+
| "default"
|
|
48
|
+
| "success"
|
|
49
|
+
| "error"
|
|
50
|
+
| "warning"
|
|
51
|
+
| "loading";
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A toast's identity. A number when `show` minted it, a string when the caller
|
|
55
|
+
* supplied one — which is what makes `update` and deduplication possible, and
|
|
56
|
+
* how {@link toast.promise} turns one toast from loading into its outcome.
|
|
57
|
+
*/
|
|
58
|
+
export type ToastId = number | string;
|
|
33
59
|
|
|
34
60
|
export interface ToastOptions {
|
|
35
61
|
title: Child;
|
|
36
62
|
description?: Child;
|
|
37
63
|
variant?: ToastVariant;
|
|
38
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Milliseconds on screen. `0` keeps it until dismissed.
|
|
66
|
+
*
|
|
67
|
+
* Left out, it comes from the variant: an error stays twice as long because
|
|
68
|
+
* the user has to read it and often act on it, and a `loading` toast does not
|
|
69
|
+
* leave on its own at all — it is waiting for an outcome.
|
|
70
|
+
*/
|
|
39
71
|
duration?: number;
|
|
40
72
|
action?: { label: string; onClick: () => void };
|
|
73
|
+
/**
|
|
74
|
+
* A stable id. Showing a toast under an id that is already on screen UPDATES
|
|
75
|
+
* it instead of stacking another — so "you are offline", raised on every
|
|
76
|
+
* failed request, is one toast rather than nine.
|
|
77
|
+
*/
|
|
78
|
+
id?: string;
|
|
79
|
+
/** Called once the toast leaves, however it left. */
|
|
80
|
+
onClose?: () => void;
|
|
41
81
|
}
|
|
42
82
|
|
|
43
|
-
interface ActiveToast extends ToastOptions {
|
|
44
|
-
readonly id:
|
|
83
|
+
interface ActiveToast extends Omit<ToastOptions, "id"> {
|
|
84
|
+
readonly id: ToastId;
|
|
45
85
|
readonly variant: ToastVariant;
|
|
46
86
|
}
|
|
47
87
|
|
|
48
88
|
const DEFAULT_DURATION_MS = 5000;
|
|
49
89
|
|
|
90
|
+
/** How long a variant stays when the caller did not say. */
|
|
91
|
+
function defaultDuration(variant: ToastVariant): number {
|
|
92
|
+
if (variant === "loading") return 0;
|
|
93
|
+
return variant === "error" ? DEFAULT_DURATION_MS * 2 : DEFAULT_DURATION_MS;
|
|
94
|
+
}
|
|
95
|
+
|
|
50
96
|
const queue = signal<readonly ActiveToast[]>([]);
|
|
51
|
-
const timers = new Map<
|
|
97
|
+
const timers = new Map<ToastId, ReturnType<typeof setTimeout>>();
|
|
52
98
|
/** When each toast is due to disappear, as an epoch timestamp. */
|
|
53
|
-
const deadlines = new Map<
|
|
99
|
+
const deadlines = new Map<ToastId, number>();
|
|
54
100
|
/** Time each toast had left when the pointer entered the region. */
|
|
55
|
-
const remainders = new Map<
|
|
101
|
+
const remainders = new Map<ToastId, number>();
|
|
56
102
|
let nextId = 0;
|
|
57
103
|
|
|
58
|
-
function dismiss(id:
|
|
104
|
+
function dismiss(id: ToastId): void {
|
|
105
|
+
const leaving = queue().find((entry) => entry.id === id);
|
|
59
106
|
clearTimer(id);
|
|
60
107
|
deadlines.delete(id);
|
|
61
108
|
remainders.delete(id);
|
|
62
109
|
queue(queue().filter((entry) => entry.id !== id));
|
|
110
|
+
notifyClosed(leaving);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Run a toast's `onClose`, without letting it take the caller down.
|
|
115
|
+
*
|
|
116
|
+
* The callback belongs to application code and runs from a timer, a click and
|
|
117
|
+
* `clear()` alike. One that throws would otherwise break whichever of those
|
|
118
|
+
* happened to be running — a dismissal that leaves the queue half-updated.
|
|
119
|
+
*/
|
|
120
|
+
function notifyClosed(entry: ActiveToast | undefined): void {
|
|
121
|
+
if (entry?.onClose === undefined) return;
|
|
122
|
+
try {
|
|
123
|
+
entry.onClose();
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error("[nebula] a toast onClose callback threw", error);
|
|
126
|
+
}
|
|
63
127
|
}
|
|
64
128
|
|
|
65
|
-
function clearTimer(id:
|
|
129
|
+
function clearTimer(id: ToastId): void {
|
|
66
130
|
const timer = timers.get(id);
|
|
67
131
|
if (timer !== undefined) clearTimeout(timer);
|
|
68
132
|
timers.delete(id);
|
|
69
133
|
}
|
|
70
134
|
|
|
71
|
-
function schedule(id:
|
|
135
|
+
function schedule(id: ToastId, duration: number): void {
|
|
72
136
|
// A duration of zero means "stays until dismissed", so no timer and no
|
|
73
137
|
// deadline — which is also what keeps pause/resume from resurrecting one.
|
|
74
138
|
if (duration <= 0) return;
|
|
@@ -103,19 +167,79 @@ function resumeAll(minimumMs: number): void {
|
|
|
103
167
|
remainders.clear();
|
|
104
168
|
}
|
|
105
169
|
|
|
106
|
-
function show(options: ToastOptions):
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
170
|
+
function show(options: ToastOptions): ToastId {
|
|
171
|
+
// A supplied id that is already on screen updates rather than stacks.
|
|
172
|
+
if (options.id !== undefined && has(options.id)) {
|
|
173
|
+
update(options.id, options);
|
|
174
|
+
return options.id;
|
|
175
|
+
}
|
|
176
|
+
let id: ToastId;
|
|
177
|
+
if (options.id === undefined) {
|
|
178
|
+
nextId += 1;
|
|
179
|
+
id = nextId;
|
|
180
|
+
} else {
|
|
181
|
+
id = options.id;
|
|
182
|
+
}
|
|
183
|
+
const variant = options.variant ?? "default";
|
|
184
|
+
const entry: ActiveToast = { ...options, id, variant };
|
|
114
185
|
queue([...queue(), entry]);
|
|
115
|
-
schedule(id, options.duration ??
|
|
186
|
+
schedule(id, options.duration ?? defaultDuration(variant));
|
|
116
187
|
return id;
|
|
117
188
|
}
|
|
118
189
|
|
|
190
|
+
function has(id: ToastId): boolean {
|
|
191
|
+
return queue().some((entry) => entry.id === id);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Change a toast that is already on screen. No-op if it has gone.
|
|
196
|
+
*
|
|
197
|
+
* The countdown is recomputed, because that is the part a caller would not
|
|
198
|
+
* think to ask for and always wants: a `loading` toast has no timer at all, so
|
|
199
|
+
* turning it into a success without rescheduling would leave it on screen for
|
|
200
|
+
* good. Which is exactly what {@link toast.promise} does to it.
|
|
201
|
+
*/
|
|
202
|
+
function update(id: ToastId, patch: Partial<ToastOptions>): void {
|
|
203
|
+
const current = queue().find((entry) => entry.id === id);
|
|
204
|
+
if (current === undefined) return;
|
|
205
|
+
const variant = patch.variant ?? current.variant;
|
|
206
|
+
const next: ActiveToast = { ...current, ...patch, id, variant };
|
|
207
|
+
queue(queue().map((entry) => (entry.id === id ? next : entry)));
|
|
208
|
+
clearTimer(id);
|
|
209
|
+
deadlines.delete(id);
|
|
210
|
+
remainders.delete(id);
|
|
211
|
+
schedule(id, patch.duration ?? next.duration ?? defaultDuration(variant));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** What a `promise` stage says: just a title, or the whole toast. */
|
|
215
|
+
export type ToastMessage = Child | Omit<ToastOptions, "id" | "variant">;
|
|
216
|
+
|
|
217
|
+
function optionsOf(message: ToastMessage): Omit<ToastOptions, "id"> {
|
|
218
|
+
// A plain object with a `title` is the options form; anything else — a
|
|
219
|
+
// string, a number, a template — is the title itself. Checked on `title`
|
|
220
|
+
// rather than on the shape in general, because a template result IS an
|
|
221
|
+
// object and would otherwise be mistaken for options.
|
|
222
|
+
if (
|
|
223
|
+
typeof message === "object" &&
|
|
224
|
+
message !== null &&
|
|
225
|
+
"title" in message &&
|
|
226
|
+
!Array.isArray(message)
|
|
227
|
+
) {
|
|
228
|
+
return message;
|
|
229
|
+
}
|
|
230
|
+
return { title: message };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function resolveMessage<T>(
|
|
234
|
+
message: ToastMessage | ((value: T) => ToastMessage),
|
|
235
|
+
value: T,
|
|
236
|
+
): Omit<ToastOptions, "id"> {
|
|
237
|
+
if (typeof message === "function") {
|
|
238
|
+
return optionsOf((message as (v: T) => ToastMessage)(value));
|
|
239
|
+
}
|
|
240
|
+
return optionsOf(message);
|
|
241
|
+
}
|
|
242
|
+
|
|
119
243
|
/**
|
|
120
244
|
* Raise a toast. Returns its id, which `dismiss` accepts.
|
|
121
245
|
*
|
|
@@ -123,31 +247,76 @@ function show(options: ToastOptions): number {
|
|
|
123
247
|
*/
|
|
124
248
|
export const toast = {
|
|
125
249
|
show,
|
|
250
|
+
update,
|
|
126
251
|
dismiss,
|
|
127
|
-
success: (title: Child, description?: Child):
|
|
252
|
+
success: (title: Child, description?: Child): ToastId =>
|
|
128
253
|
show({ title, description, variant: "success" }),
|
|
129
|
-
error: (title: Child, description?: Child):
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
title,
|
|
133
|
-
description,
|
|
134
|
-
variant: "error",
|
|
135
|
-
duration: DEFAULT_DURATION_MS * 2,
|
|
136
|
-
}),
|
|
137
|
-
warning: (title: Child, description?: Child): number =>
|
|
254
|
+
error: (title: Child, description?: Child): ToastId =>
|
|
255
|
+
show({ title, description, variant: "error" }),
|
|
256
|
+
warning: (title: Child, description?: Child): ToastId =>
|
|
138
257
|
show({ title, description, variant: "warning" }),
|
|
258
|
+
loading: (title: Child, description?: Child): ToastId =>
|
|
259
|
+
show({ title, description, variant: "loading" }),
|
|
260
|
+
/**
|
|
261
|
+
* One toast for the whole life of an async call: waiting, then the outcome.
|
|
262
|
+
*
|
|
263
|
+
* ```ts
|
|
264
|
+
* await toast.promise(save(form), {
|
|
265
|
+
* loading: 'Saving…',
|
|
266
|
+
* success: (saved) => `Saved ${saved.name}`,
|
|
267
|
+
* error: (reason) => ({ title: 'Could not save', description: String(reason) }),
|
|
268
|
+
* })
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* Returns the SAME promise it was given, rejection included. It reports, it
|
|
272
|
+
* does not handle: swallowing the rejection here would turn a failed call
|
|
273
|
+
* into a red toast and nothing else, with the caller's own error path never
|
|
274
|
+
* running.
|
|
275
|
+
*/
|
|
276
|
+
promise<T>(
|
|
277
|
+
work: Promise<T>,
|
|
278
|
+
messages: {
|
|
279
|
+
loading: ToastMessage;
|
|
280
|
+
success: ToastMessage | ((value: T) => ToastMessage);
|
|
281
|
+
error: ToastMessage | ((reason: unknown) => ToastMessage);
|
|
282
|
+
},
|
|
283
|
+
): Promise<T> {
|
|
284
|
+
const id = show({ ...optionsOf(messages.loading), variant: "loading" });
|
|
285
|
+
// A separate chain, whose own rejection IS handled — so reporting the
|
|
286
|
+
// failure never adds an unhandled rejection of its own.
|
|
287
|
+
work.then(
|
|
288
|
+
(value) => {
|
|
289
|
+
update(id, {
|
|
290
|
+
...resolveMessage(messages.success, value),
|
|
291
|
+
variant: "success",
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
(reason: unknown) => {
|
|
295
|
+
update(id, {
|
|
296
|
+
...resolveMessage(messages.error, reason),
|
|
297
|
+
variant: "error",
|
|
298
|
+
});
|
|
299
|
+
},
|
|
300
|
+
);
|
|
301
|
+
return work;
|
|
302
|
+
},
|
|
139
303
|
/** Clear everything — on navigation, say. */
|
|
140
304
|
clear(): void {
|
|
141
305
|
for (const timer of timers.values()) clearTimeout(timer);
|
|
142
306
|
timers.clear();
|
|
143
307
|
deadlines.clear();
|
|
144
308
|
remainders.clear();
|
|
309
|
+
const leaving = queue();
|
|
145
310
|
queue([]);
|
|
311
|
+
// After the queue is empty, so a callback that raises a toast of its own
|
|
312
|
+
// is not swept away by the clear that called it.
|
|
313
|
+
for (const entry of leaving) notifyClosed(entry);
|
|
146
314
|
},
|
|
147
315
|
};
|
|
148
316
|
|
|
149
317
|
const variantClasses: Record<ToastVariant, string> = {
|
|
150
318
|
default: "bg-popover text-popover-foreground border",
|
|
319
|
+
loading: "bg-popover text-muted-foreground border",
|
|
151
320
|
success:
|
|
152
321
|
"bg-popover text-popover-foreground border-l-4 border-l-primary border",
|
|
153
322
|
error: "bg-popover text-destructive border-l-4 border-l-destructive border",
|
|
@@ -158,6 +327,8 @@ const variantClasses: Record<ToastVariant, string> = {
|
|
|
158
327
|
export interface ToasterProps {
|
|
159
328
|
/** Which corner. Default `"bottom-right"`. */
|
|
160
329
|
position?: "top-right" | "top-left" | "bottom-right" | "bottom-left";
|
|
330
|
+
/** The dismiss cross's accessible name, as sonner names the prop. */
|
|
331
|
+
closeButtonAriaLabel?: string;
|
|
161
332
|
class?: Reactive<string>;
|
|
162
333
|
}
|
|
163
334
|
|
|
@@ -177,6 +348,8 @@ const positionClasses: Record<NonNullable<ToasterProps["position"]>, string> = {
|
|
|
177
348
|
*/
|
|
178
349
|
export const Toaster = component<ToasterProps>((props) => {
|
|
179
350
|
const position = props.position ?? "bottom-right";
|
|
351
|
+
const closeLabel = props.closeButtonAriaLabel ?? "Dismiss notification";
|
|
352
|
+
const render = (entry: ActiveToast): Child => renderToast(entry, closeLabel);
|
|
180
353
|
|
|
181
354
|
return html`<div
|
|
182
355
|
data-slot="toaster"
|
|
@@ -194,18 +367,18 @@ export const Toaster = component<ToasterProps>((props) => {
|
|
|
194
367
|
${() =>
|
|
195
368
|
queue()
|
|
196
369
|
.filter((entry) => entry.variant !== "error")
|
|
197
|
-
.map(
|
|
370
|
+
.map(render)}
|
|
198
371
|
</div>
|
|
199
372
|
<div role="alert" aria-live="assertive" aria-atomic="false" class="contents">
|
|
200
373
|
${() =>
|
|
201
374
|
queue()
|
|
202
375
|
.filter((entry) => entry.variant === "error")
|
|
203
|
-
.map(
|
|
376
|
+
.map(render)}
|
|
204
377
|
</div>
|
|
205
378
|
</div>`;
|
|
206
379
|
});
|
|
207
380
|
|
|
208
|
-
function renderToast(entry: ActiveToast): Child {
|
|
381
|
+
function renderToast(entry: ActiveToast, closeLabel: string): Child {
|
|
209
382
|
return html`<div
|
|
210
383
|
data-slot="toast"
|
|
211
384
|
data-variant="${entry.variant}"
|
|
@@ -216,6 +389,11 @@ function renderToast(entry: ActiveToast): Child {
|
|
|
216
389
|
fadeInOut,
|
|
217
390
|
)}"
|
|
218
391
|
>
|
|
392
|
+
${
|
|
393
|
+
entry.variant === "loading"
|
|
394
|
+
? LoaderIcon({ class: "size-4 mt-0.5 shrink-0 animate-spin" })
|
|
395
|
+
: null
|
|
396
|
+
}
|
|
219
397
|
<div class="flex-1 text-sm">
|
|
220
398
|
<div class="font-medium">${entry.title}</div>
|
|
221
399
|
${
|
|
@@ -238,7 +416,7 @@ function renderToast(entry: ActiveToast): Child {
|
|
|
238
416
|
}
|
|
239
417
|
<button
|
|
240
418
|
type="button"
|
|
241
|
-
aria-label="
|
|
419
|
+
aria-label="${closeLabel}"
|
|
242
420
|
class="opacity-60 transition-opacity hover:opacity-100"
|
|
243
421
|
@click="${() => dismiss(entry.id)}"
|
|
244
422
|
>${XIcon({ class: "size-4" })}</button>
|
package/src/organisms/index.ts
CHANGED
|
@@ -37,6 +37,7 @@ export {
|
|
|
37
37
|
addDays,
|
|
38
38
|
addMonths,
|
|
39
39
|
Calendar,
|
|
40
|
+
type CalendarLabels,
|
|
40
41
|
type CalendarProps,
|
|
41
42
|
type DateRange,
|
|
42
43
|
isSameDay,
|
|
@@ -120,7 +121,12 @@ export {
|
|
|
120
121
|
type ContextMenuTriggerProps,
|
|
121
122
|
} from "./ContextMenu.js";
|
|
122
123
|
export { type Column, DataTable, type DataTableProps } from "./DataTable.js";
|
|
123
|
-
export {
|
|
124
|
+
export {
|
|
125
|
+
DatePicker,
|
|
126
|
+
type DatePickerLabels,
|
|
127
|
+
type DatePickerProps,
|
|
128
|
+
toISODate,
|
|
129
|
+
} from "./DatePicker.js";
|
|
124
130
|
export {
|
|
125
131
|
DateRangePicker,
|
|
126
132
|
type DateRangePickerProps,
|
|
@@ -388,6 +394,8 @@ export {
|
|
|
388
394
|
export {
|
|
389
395
|
Toaster,
|
|
390
396
|
type ToasterProps,
|
|
397
|
+
type ToastId,
|
|
398
|
+
type ToastMessage,
|
|
391
399
|
type ToastOptions,
|
|
392
400
|
type ToastVariant,
|
|
393
401
|
toast,
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
/**
|
|
3
|
+
* Reading direction — the one place that answers "which way is forward?".
|
|
4
|
+
*
|
|
5
|
+
* Radix ships a `DirectionProvider` and threads the answer through React
|
|
6
|
+
* context. Aurora has a context, but a provider would be the wrong mechanism
|
|
7
|
+
* here: the direction is already on the page. `<html dir="rtl">`, a `dir`
|
|
8
|
+
* attribute on one panel, a stylesheet setting `direction` — all of them are
|
|
9
|
+
* answered by the computed style, and none of them requires the application to
|
|
10
|
+
* remember to wrap anything. A provider would be a second source of truth that
|
|
11
|
+
* silently disagrees with the first.
|
|
12
|
+
*
|
|
13
|
+
* The distinction that matters to callers is PHYSICAL versus LOGICAL. An arrow
|
|
14
|
+
* key is physical: ArrowRight means the key to the right, always. "The next
|
|
15
|
+
* item" is logical, and in a right-to-left list the next item is to the LEFT.
|
|
16
|
+
* `forward()` converts one into the other, and is the whole of this module's
|
|
17
|
+
* job.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export type Direction = "ltr" | "rtl";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The direction this element is laid out in.
|
|
24
|
+
*
|
|
25
|
+
* The computed style rather than the `dir` attribute, so an element inheriting
|
|
26
|
+
* from `<html dir="rtl">` answers correctly without every anchor carrying the
|
|
27
|
+
* attribute itself. `ltr` wherever there is no layout to ask — a server
|
|
28
|
+
* render, a jsdom without styles — which is also the correct default.
|
|
29
|
+
*/
|
|
30
|
+
export function directionOf(element: Element | null | undefined): Direction {
|
|
31
|
+
if (element === null || element === undefined) return "ltr";
|
|
32
|
+
if (typeof getComputedStyle !== "function") return "ltr";
|
|
33
|
+
try {
|
|
34
|
+
return getComputedStyle(element).direction === "rtl" ? "rtl" : "ltr";
|
|
35
|
+
} catch {
|
|
36
|
+
// A detached node in some environments. Not a reason to fail a keypress.
|
|
37
|
+
return "ltr";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Shorthand for the check that reads better at a call site than `=== "rtl"`. */
|
|
42
|
+
export function isRightToLeft(element: Element | null | undefined): boolean {
|
|
43
|
+
return directionOf(element) === "rtl";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Which way a horizontal arrow key actually moves.
|
|
48
|
+
*
|
|
49
|
+
* `+1` for the next item, `-1` for the previous. ArrowRight is `+1` in a
|
|
50
|
+
* left-to-right widget and `-1` in a right-to-left one — that mirroring is the
|
|
51
|
+
* entire reason this exists, and getting it wrong makes every menu, tab list
|
|
52
|
+
* and toolbar navigate backwards for half the world's readers.
|
|
53
|
+
*/
|
|
54
|
+
export function forward(
|
|
55
|
+
key: "ArrowLeft" | "ArrowRight",
|
|
56
|
+
element: Element | null | undefined,
|
|
57
|
+
): -1 | 1 {
|
|
58
|
+
const rightIsForward = !isRightToLeft(element);
|
|
59
|
+
if (key === "ArrowRight") return rightIsForward ? 1 : -1;
|
|
60
|
+
return rightIsForward ? -1 : 1;
|
|
61
|
+
}
|
|
@@ -25,6 +25,8 @@
|
|
|
25
25
|
* has to find an offset parent, and gets it wrong the moment one appears.
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
+
import { isRightToLeft } from "./direction.js";
|
|
29
|
+
|
|
28
30
|
export type Side = "top" | "right" | "bottom" | "left";
|
|
29
31
|
export type Align = "start" | "center" | "end";
|
|
30
32
|
export type Placement = Side | `${Side}-${Align}`;
|
|
@@ -358,6 +360,32 @@ export interface AutoPositionOptions extends PositionOptions {
|
|
|
358
360
|
onPositioned?: (position: Position) => void;
|
|
359
361
|
}
|
|
360
362
|
|
|
363
|
+
/**
|
|
364
|
+
* The surface's size as laid out, before any transform.
|
|
365
|
+
*
|
|
366
|
+
* `getBoundingClientRect()` includes transforms, and every surface opens under
|
|
367
|
+
* `zoom-in-95`: measured on its first frame it is 5% short, and one placed on
|
|
368
|
+
* the `top` side lands that much too low, over the control that opened it. A
|
|
369
|
+
* transform never changes layout, so the `ResizeObserver` would not correct it
|
|
370
|
+
* afterwards either. floating-ui reads the same two sources in the same order
|
|
371
|
+
* (`getCssDimensions`): the computed size, which keeps sub-pixel widths, and
|
|
372
|
+
* the integer offset size when the two disagree.
|
|
373
|
+
*/
|
|
374
|
+
function layoutRect(element: HTMLElement): Rect {
|
|
375
|
+
const box = element.getBoundingClientRect();
|
|
376
|
+
const css = getComputedStyle(element);
|
|
377
|
+
let width = Number.parseFloat(css.width) || 0;
|
|
378
|
+
let height = Number.parseFloat(css.height) || 0;
|
|
379
|
+
if (
|
|
380
|
+
Math.round(width) !== element.offsetWidth ||
|
|
381
|
+
Math.round(height) !== element.offsetHeight
|
|
382
|
+
) {
|
|
383
|
+
width = element.offsetWidth;
|
|
384
|
+
height = element.offsetHeight;
|
|
385
|
+
}
|
|
386
|
+
return { x: box.x, y: box.y, width, height };
|
|
387
|
+
}
|
|
388
|
+
|
|
361
389
|
export interface AutoPosition {
|
|
362
390
|
/** Recompute and rewrite the styles now. */
|
|
363
391
|
update(): void;
|
|
@@ -388,7 +416,7 @@ export function autoPosition(
|
|
|
388
416
|
floating.style.width = `${anchorRect.width}px`;
|
|
389
417
|
}
|
|
390
418
|
|
|
391
|
-
const floatingRect = floating
|
|
419
|
+
const floatingRect = layoutRect(floating);
|
|
392
420
|
const position = resolvePosition(
|
|
393
421
|
anchorRect,
|
|
394
422
|
floatingRect,
|
|
@@ -456,18 +484,6 @@ export function autoPosition(
|
|
|
456
484
|
};
|
|
457
485
|
}
|
|
458
486
|
|
|
459
|
-
/**
|
|
460
|
-
* Is this element laid out right to left?
|
|
461
|
-
*
|
|
462
|
-
* The computed style rather than the `dir` attribute, so an element inheriting
|
|
463
|
-
* the direction from `<html dir="rtl">` is answered correctly without every
|
|
464
|
-
* anchor having to carry the attribute itself.
|
|
465
|
-
*/
|
|
466
|
-
function isRightToLeft(element: HTMLElement): boolean {
|
|
467
|
-
if (typeof getComputedStyle !== "function") return false;
|
|
468
|
-
return getComputedStyle(element).direction === "rtl";
|
|
469
|
-
}
|
|
470
|
-
|
|
471
487
|
/**
|
|
472
488
|
* Watch elements for size changes, where the platform supports it.
|
|
473
489
|
*
|
|
@@ -13,7 +13,16 @@
|
|
|
13
13
|
* styles rather than trusting the markup.
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* The standard tabbable set.
|
|
18
|
+
*
|
|
19
|
+
* Exported because a composite widget that navigates "whatever controls are in
|
|
20
|
+
* here" — a toolbar — needs the same definition, and a second copy next door
|
|
21
|
+
* is how the two drift into disagreeing about `summary` or `contenteditable`.
|
|
22
|
+
* It is a selector, not an answer: `isFocusable` below is still what decides,
|
|
23
|
+
* because a selector cannot see `display: none` or a collapsed `<details>`.
|
|
24
|
+
*/
|
|
25
|
+
export const FOCUSABLE_SELECTOR = [
|
|
17
26
|
"a[href]",
|
|
18
27
|
"area[href]",
|
|
19
28
|
"button:not([disabled])",
|
package/src/primitives/index.ts
CHANGED
|
@@ -18,6 +18,12 @@ export {
|
|
|
18
18
|
type ControllableOptions,
|
|
19
19
|
controllable,
|
|
20
20
|
} from "./controllable.js";
|
|
21
|
+
export {
|
|
22
|
+
type Direction,
|
|
23
|
+
directionOf,
|
|
24
|
+
forward,
|
|
25
|
+
isRightToLeft,
|
|
26
|
+
} from "./direction.js";
|
|
21
27
|
export {
|
|
22
28
|
type DismissableLayer,
|
|
23
29
|
type DismissableOptions,
|
|
@@ -43,6 +49,7 @@ export {
|
|
|
43
49
|
floatingSurface,
|
|
44
50
|
} from "./floatingSurface.js";
|
|
45
51
|
export {
|
|
52
|
+
FOCUSABLE_SELECTOR,
|
|
46
53
|
firstFocusable,
|
|
47
54
|
focusableWithin,
|
|
48
55
|
focusSilently,
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* which is not a budget worth optimising against correctness.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
+
import { forward } from "./direction.js";
|
|
22
23
|
import { focusSilently, isVisible } from "./focusable.js";
|
|
23
24
|
|
|
24
25
|
export type Orientation = "vertical" | "horizontal" | "both";
|
|
@@ -132,7 +133,7 @@ export function rovingFocus(options: RovingFocusOptions): RovingFocus {
|
|
|
132
133
|
// A modifier means the user is talking to the browser, not the widget.
|
|
133
134
|
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
134
135
|
|
|
135
|
-
const direction = directionFor(event.key, orientation);
|
|
136
|
+
const direction = directionFor(event.key, orientation, container);
|
|
136
137
|
if (direction !== 0) {
|
|
137
138
|
event.preventDefault();
|
|
138
139
|
move(direction);
|
|
@@ -177,15 +178,27 @@ export function rovingFocus(options: RovingFocusOptions): RovingFocus {
|
|
|
177
178
|
};
|
|
178
179
|
}
|
|
179
180
|
|
|
180
|
-
/**
|
|
181
|
-
|
|
181
|
+
/**
|
|
182
|
+
* `-1` for previous, `+1` for next, `0` when the key is not ours.
|
|
183
|
+
*
|
|
184
|
+
* The horizontal pair is mirrored in a right-to-left group: ArrowRight means
|
|
185
|
+
* the key to the right, and in an RTL list the item to the right is the
|
|
186
|
+
* PREVIOUS one. Vertical never mirrors — `dir` does not turn a page upside
|
|
187
|
+
* down.
|
|
188
|
+
*/
|
|
189
|
+
function directionFor(
|
|
190
|
+
key: string,
|
|
191
|
+
orientation: Orientation,
|
|
192
|
+
container: HTMLElement,
|
|
193
|
+
): number {
|
|
182
194
|
const vertical = orientation === "vertical" || orientation === "both";
|
|
183
195
|
const horizontal = orientation === "horizontal" || orientation === "both";
|
|
184
196
|
|
|
185
197
|
if (vertical && key === "ArrowDown") return 1;
|
|
186
198
|
if (vertical && key === "ArrowUp") return -1;
|
|
187
|
-
if (horizontal && key === "ArrowRight")
|
|
188
|
-
|
|
199
|
+
if (horizontal && (key === "ArrowRight" || key === "ArrowLeft")) {
|
|
200
|
+
return forward(key, container);
|
|
201
|
+
}
|
|
189
202
|
return 0;
|
|
190
203
|
}
|
|
191
204
|
|
|
@@ -26,6 +26,8 @@ export interface SettingsLayoutProps {
|
|
|
26
26
|
title: Child;
|
|
27
27
|
description?: Child;
|
|
28
28
|
sections: readonly SettingsSection[];
|
|
29
|
+
/** The section list's accessible name. Default `"Settings sections"`. */
|
|
30
|
+
navLabel?: string;
|
|
29
31
|
children?: Slot;
|
|
30
32
|
class?: Reactive<string>;
|
|
31
33
|
}
|
|
@@ -46,7 +48,7 @@ export const SettingsLayout = component<SettingsLayoutProps>((props) => {
|
|
|
46
48
|
<div class="flex flex-col gap-8 md:flex-row md:gap-12">
|
|
47
49
|
<nav
|
|
48
50
|
data-slot="settings-nav"
|
|
49
|
-
aria-label="Settings sections"
|
|
51
|
+
aria-label="${props.navLabel ?? "Settings sections"}"
|
|
50
52
|
class="flex gap-2 overflow-x-auto md:w-48 md:shrink-0 md:flex-col md:overflow-visible"
|
|
51
53
|
>
|
|
52
54
|
${props.sections.map(
|