@ac-kit/ui-common 0.1.0 → 0.2.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/CHANGELOG.md +155 -0
- package/README.md +327 -9
- package/dist/anchored/anchored.d.ts +19 -0
- package/dist/anchored/anchored.d.ts.map +1 -0
- package/dist/anchored/anchored.js +2 -0
- package/dist/anchored/anchored.js.map +1 -0
- package/dist/focus/typeahead.d.ts +112 -0
- package/dist/focus/typeahead.d.ts.map +1 -0
- package/dist/focus/typeahead.js +178 -0
- package/dist/focus/typeahead.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/menu/menu.d.ts +157 -0
- package/dist/menu/menu.d.ts.map +1 -0
- package/dist/menu/menu.js +162 -0
- package/dist/menu/menu.js.map +1 -0
- package/dist/preact/_anchor.d.ts +13 -0
- package/dist/preact/_anchor.d.ts.map +1 -0
- package/dist/preact/_anchor.js +39 -0
- package/dist/preact/_anchor.js.map +1 -0
- package/dist/preact/_dom.d.ts +14 -0
- package/dist/preact/_dom.d.ts.map +1 -0
- package/dist/preact/_dom.js +31 -0
- package/dist/preact/_dom.js.map +1 -0
- package/dist/preact/_live-announcer.d.ts +26 -0
- package/dist/preact/_live-announcer.d.ts.map +1 -0
- package/dist/preact/_live-announcer.js +101 -0
- package/dist/preact/_live-announcer.js.map +1 -0
- package/dist/preact/index.d.ts +7 -0
- package/dist/preact/index.d.ts.map +1 -1
- package/dist/preact/index.js +7 -0
- package/dist/preact/index.js.map +1 -1
- package/dist/preact/knob.d.ts.map +1 -1
- package/dist/preact/knob.js +5 -13
- package/dist/preact/knob.js.map +1 -1
- package/dist/preact/menu-button.d.ts +117 -0
- package/dist/preact/menu-button.d.ts.map +1 -0
- package/dist/preact/menu-button.js +330 -0
- package/dist/preact/menu-button.js.map +1 -0
- package/dist/preact/progress-ring.d.ts +42 -0
- package/dist/preact/progress-ring.d.ts.map +1 -0
- package/dist/preact/progress-ring.js +26 -0
- package/dist/preact/progress-ring.js.map +1 -0
- package/dist/preact/slider.d.ts.map +1 -1
- package/dist/preact/slider.js +6 -4
- package/dist/preact/slider.js.map +1 -1
- package/dist/preact/spin-button.d.ts +67 -0
- package/dist/preact/spin-button.d.ts.map +1 -0
- package/dist/preact/spin-button.js +132 -0
- package/dist/preact/spin-button.js.map +1 -0
- package/dist/preact/tabs.d.ts +97 -0
- package/dist/preact/tabs.d.ts.map +1 -0
- package/dist/preact/tabs.js +264 -0
- package/dist/preact/tabs.js.map +1 -0
- package/dist/preact/toaster.d.ts +43 -0
- package/dist/preact/toaster.d.ts.map +1 -0
- package/dist/preact/toaster.js +356 -0
- package/dist/preact/toaster.js.map +1 -0
- package/dist/preact/toolbar.d.ts.map +1 -1
- package/dist/preact/toolbar.js +2 -8
- package/dist/preact/toolbar.js.map +1 -1
- package/dist/preact/tooltip.d.ts +67 -0
- package/dist/preact/tooltip.d.ts.map +1 -0
- package/dist/preact/tooltip.js +165 -0
- package/dist/preact/tooltip.js.map +1 -0
- package/dist/preact/tree.d.ts +93 -0
- package/dist/preact/tree.d.ts.map +1 -0
- package/dist/preact/tree.js +399 -0
- package/dist/preact/tree.js.map +1 -0
- package/dist/progress/progress.d.ts +51 -0
- package/dist/progress/progress.d.ts.map +1 -0
- package/dist/progress/progress.js +43 -0
- package/dist/progress/progress.js.map +1 -0
- package/dist/spin-button/spin-button.d.ts +110 -0
- package/dist/spin-button/spin-button.d.ts.map +1 -0
- package/dist/spin-button/spin-button.js +179 -0
- package/dist/spin-button/spin-button.js.map +1 -0
- package/dist/tabs/tabs.d.ts +157 -0
- package/dist/tabs/tabs.d.ts.map +1 -0
- package/dist/tabs/tabs.js +120 -0
- package/dist/tabs/tabs.js.map +1 -0
- package/dist/toast/toast-queue.d.ts +212 -0
- package/dist/toast/toast-queue.d.ts.map +1 -0
- package/dist/toast/toast-queue.js +290 -0
- package/dist/toast/toast-queue.js.map +1 -0
- package/dist/toast/toasts-obscure.d.ts +25 -0
- package/dist/toast/toasts-obscure.d.ts.map +1 -0
- package/dist/toast/toasts-obscure.js +30 -0
- package/dist/toast/toasts-obscure.js.map +1 -0
- package/dist/tooltip/tooltip.d.ts +183 -0
- package/dist/tooltip/tooltip.d.ts.map +1 -0
- package/dist/tooltip/tooltip.js +185 -0
- package/dist/tooltip/tooltip.js.map +1 -0
- package/dist/tree/tree.d.ts +302 -0
- package/dist/tree/tree.d.ts.map +1 -0
- package/dist/tree/tree.js +439 -0
- package/dist/tree/tree.js.map +1 -0
- package/package.json +22 -12
- package/styles.css +894 -3
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { describeValue, isIntegerInRange } from "@ac-kit/core";
|
|
2
|
+
import { rovingFocusIndex } from "../focus/roving-focus.js";
|
|
3
|
+
/**
|
|
4
|
+
* The attributes of a tab list (WAI-ARIA tabs pattern): a named row or column
|
|
5
|
+
* of tabs with one tab stop.
|
|
6
|
+
*
|
|
7
|
+
* Time complexity: O(1). Memory: O(1).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* tabListAttributes({ label: "Settings" });
|
|
11
|
+
* // { role: "tablist", "aria-label": "Settings" }
|
|
12
|
+
*/
|
|
13
|
+
export function tabListAttributes(input) {
|
|
14
|
+
const { label, labelledBy, orientation = "horizontal" } = input;
|
|
15
|
+
const attributes = { role: "tablist" };
|
|
16
|
+
if (label !== undefined) {
|
|
17
|
+
attributes["aria-label"] = label;
|
|
18
|
+
}
|
|
19
|
+
if (labelledBy !== undefined) {
|
|
20
|
+
attributes["aria-labelledby"] = labelledBy;
|
|
21
|
+
}
|
|
22
|
+
if (orientation === "vertical") {
|
|
23
|
+
attributes["aria-orientation"] = "vertical";
|
|
24
|
+
}
|
|
25
|
+
return attributes;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The attributes of a tab: selected or not, the panel it controls, and in the
|
|
29
|
+
* tab order only as the tab list's single stop. A disabled tab stays focusable,
|
|
30
|
+
* as the WAI-ARIA practices advise for the items of a composite widget, so it
|
|
31
|
+
* can be found and read; it is never selected.
|
|
32
|
+
*
|
|
33
|
+
* Time complexity: O(1). Memory: O(1).
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* tabAttributes({ panelId: "p", isSelected: true, isTabStop: true });
|
|
37
|
+
* // { role: "tab", "aria-selected": "true", "aria-controls": "p",
|
|
38
|
+
* // tabindex: 0 }
|
|
39
|
+
*/
|
|
40
|
+
export function tabAttributes(input) {
|
|
41
|
+
const attributes = {
|
|
42
|
+
role: "tab",
|
|
43
|
+
"aria-selected": input.isSelected ? "true" : "false",
|
|
44
|
+
"aria-controls": input.panelId,
|
|
45
|
+
tabindex: input.isTabStop ? 0 : -1,
|
|
46
|
+
};
|
|
47
|
+
if (input.isDisabled === true) {
|
|
48
|
+
attributes["aria-disabled"] = "true";
|
|
49
|
+
}
|
|
50
|
+
return attributes;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The attributes of a tab panel: named by its tab, and in the tab order itself
|
|
54
|
+
* when nothing inside it is.
|
|
55
|
+
*
|
|
56
|
+
* Time complexity: O(1). Memory: O(1).
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* tabPanelAttributes({ tabId: "t", isFocusable: true });
|
|
60
|
+
* // { role: "tabpanel", "aria-labelledby": "t", tabindex: 0 }
|
|
61
|
+
*/
|
|
62
|
+
export function tabPanelAttributes(input) {
|
|
63
|
+
const attributes = {
|
|
64
|
+
role: "tabpanel",
|
|
65
|
+
"aria-labelledby": input.tabId,
|
|
66
|
+
};
|
|
67
|
+
if (input.isFocusable) {
|
|
68
|
+
attributes.tabindex = 0;
|
|
69
|
+
}
|
|
70
|
+
return attributes;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The action of a key on a focused tab, {@link TabsKeyAction}. `undefined` for a
|
|
74
|
+
* key the list does not use, or with Ctrl, Alt or Meta held (a shortcut).
|
|
75
|
+
*
|
|
76
|
+
* Time complexity: O(count) at worst. Memory: O(1).
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* tabsKeyAction({ key: "ArrowRight", current: 2, count: 3 });
|
|
80
|
+
* // { kind: "focus", index: 0, selects: true }
|
|
81
|
+
*
|
|
82
|
+
* @throws {RangeError} When `count` is not a positive safe integer or `current`
|
|
83
|
+
* is outside [0, count).
|
|
84
|
+
*/
|
|
85
|
+
export function tabsKeyAction(input) {
|
|
86
|
+
const { key, current, count, orientation = "horizontal", direction = "ltr", activation = "automatic", isDisabled, isDeletable = false, } = input;
|
|
87
|
+
if (!isIntegerInRange(count, 1, Number.MAX_SAFE_INTEGER + 1)) {
|
|
88
|
+
throw new RangeError(`count must be a positive safe integer, got ${describeValue(count)}`);
|
|
89
|
+
}
|
|
90
|
+
if (!isIntegerInRange(current, 0, count)) {
|
|
91
|
+
throw new RangeError(`current must be an integer in [0, ${String(count)}), got ${describeValue(current)}`);
|
|
92
|
+
}
|
|
93
|
+
if (input.ctrlKey === true ||
|
|
94
|
+
input.altKey === true ||
|
|
95
|
+
input.metaKey === true) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
switch (key) {
|
|
99
|
+
case "Enter":
|
|
100
|
+
case " ":
|
|
101
|
+
return activation === "manual" ? { kind: "select" } : undefined;
|
|
102
|
+
case "Delete":
|
|
103
|
+
return isDeletable ? { kind: "delete" } : undefined;
|
|
104
|
+
default: {
|
|
105
|
+
const index = rovingFocusIndex(current, key, count, {
|
|
106
|
+
orientation,
|
|
107
|
+
direction,
|
|
108
|
+
});
|
|
109
|
+
if (index === undefined) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
kind: "focus",
|
|
114
|
+
index,
|
|
115
|
+
selects: activation === "automatic" && isDisabled?.(index) !== true,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//#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AK,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QACjE,KAAK,QAAQ;YACZ,OAAO,WAAW,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QACrD,SAAS,CAAC;YACT,MAAM,KAAK,GAAG,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE;gBACnD,WAAW;gBACX,SAAS;aACT,CAAC,CAAC;YACH,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACzB,OAAO,SAAS,CAAC;YAClB,CAAC;YAED,OAAO;gBACN,IAAI,EAAE,OAAO;gBACb,KAAK;gBACL,OAAO,EAAE,UAAU,KAAK,WAAW,IAAI,UAAU,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI;aACnE,CAAC;QACH,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { type CallableNoArgs } from "@ac-kit/core";
|
|
2
|
+
import type { ControlTone } from "../tone.js";
|
|
3
|
+
/**
|
|
4
|
+
* How a toast is announced: `polite` waits for the screen reader to finish
|
|
5
|
+
* (`status`), `assertive` interrupts it (`alert`), for what needs attention
|
|
6
|
+
* now, such as a failure.
|
|
7
|
+
*/
|
|
8
|
+
export type ToastUrgency = "polite" | "assertive";
|
|
9
|
+
/** The one action a toast offers, such as Undo. */
|
|
10
|
+
export type ToastAction = {
|
|
11
|
+
readonly label: string;
|
|
12
|
+
/** Called when it is pressed; the toast is dismissed then. */
|
|
13
|
+
readonly onAction: CallableNoArgs;
|
|
14
|
+
};
|
|
15
|
+
/** What a toast says. Every text is the caller's: the library ships none. */
|
|
16
|
+
export type ToastContent = {
|
|
17
|
+
readonly title: string;
|
|
18
|
+
readonly description?: string;
|
|
19
|
+
readonly tone?: ControlTone;
|
|
20
|
+
/**
|
|
21
|
+
* A toast with an action stays until dismissed, unless given a duration: a
|
|
22
|
+
* person who needs longer to reach it, through the keyboard or a screen
|
|
23
|
+
* reader, still can.
|
|
24
|
+
*/
|
|
25
|
+
readonly action?: ToastAction;
|
|
26
|
+
/** Defaults to `polite`. */
|
|
27
|
+
readonly urgency?: ToastUrgency;
|
|
28
|
+
};
|
|
29
|
+
/** A toast in a {@link ToastQueue}. */
|
|
30
|
+
export type Toast = ToastContent & {
|
|
31
|
+
readonly id: string;
|
|
32
|
+
/** How long it shows once visible, clocks paused excepted, in ms. */
|
|
33
|
+
readonly durationMs: number;
|
|
34
|
+
/**
|
|
35
|
+
* Grows each time the toast is shown again with its id, so an adapter
|
|
36
|
+
* announces the new content.
|
|
37
|
+
*/
|
|
38
|
+
readonly revision: number;
|
|
39
|
+
};
|
|
40
|
+
/** Options of {@link ToastQueue.show}. */
|
|
41
|
+
export type ToastShowOptions = {
|
|
42
|
+
/**
|
|
43
|
+
* Shows it in place of the toast with this id, if there is one, with its time
|
|
44
|
+
* started again: "Saving…" becoming "Saved". Defaults to a new id.
|
|
45
|
+
*/
|
|
46
|
+
readonly id?: string;
|
|
47
|
+
/**
|
|
48
|
+
* How long it shows once visible, in ms; `Infinity` until dismissed. Defaults
|
|
49
|
+
* to the queue's, or `Infinity` for a toast with an action.
|
|
50
|
+
*/
|
|
51
|
+
readonly durationMs?: number;
|
|
52
|
+
};
|
|
53
|
+
/** Options of {@link ToastQueue}. */
|
|
54
|
+
export type ToastQueueOptions = {
|
|
55
|
+
/** How many toasts show at once; the others wait their turn. Defaults to 3. */
|
|
56
|
+
readonly limit?: number;
|
|
57
|
+
/**
|
|
58
|
+
* How long a toast shows by default, in ms; `Infinity` until dismissed.
|
|
59
|
+
* Defaults to 6000. Let people lengthen it (WCAG 2.2.1, timing adjustable).
|
|
60
|
+
*/
|
|
61
|
+
readonly durationMs?: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Brief messages about what an application did — saved, sent, failed — shown a
|
|
65
|
+
* few at a time, each until its duration has run or it is dismissed. The
|
|
66
|
+
* framework-free state of a toaster: an adapter renders `visible`, announces
|
|
67
|
+
* each toast as it becomes visible, runs a timer to `deadline` and calls
|
|
68
|
+
* `advance` then. Every time is passed in, in ms of one clock.
|
|
69
|
+
*
|
|
70
|
+
* Only visible toasts' clocks run, and none while a reason pauses the queue:
|
|
71
|
+
* the pointer over the toasts, focus among them, the page hidden. So a person
|
|
72
|
+
* reading or reaching a toast does not see it go.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* const toasts = new ToastQueue();
|
|
76
|
+
* const id = toasts.show({ title: "Saving…" }, performance.now(), {
|
|
77
|
+
* durationMs: Infinity,
|
|
78
|
+
* });
|
|
79
|
+
* toasts.show({ title: "Saved", tone: "positive" }, performance.now(), {
|
|
80
|
+
* id,
|
|
81
|
+
* });
|
|
82
|
+
*/
|
|
83
|
+
export declare class ToastQueue {
|
|
84
|
+
private readonly limit_;
|
|
85
|
+
private readonly durationMs_;
|
|
86
|
+
private readonly entries_;
|
|
87
|
+
private readonly pauses_;
|
|
88
|
+
private readonly listeners_;
|
|
89
|
+
private nextId_;
|
|
90
|
+
private revision_;
|
|
91
|
+
private toasts_;
|
|
92
|
+
/**
|
|
93
|
+
* @throws {RangeError} When `limit` is not a positive safe integer, or
|
|
94
|
+
* `durationMs` is negative or `NaN`.
|
|
95
|
+
*/
|
|
96
|
+
constructor(options?: ToastQueueOptions);
|
|
97
|
+
/** Every toast, in the order shown: the visible ones first. */
|
|
98
|
+
get toasts(): readonly Toast[];
|
|
99
|
+
/** The toasts on screen: the first `limit`. */
|
|
100
|
+
get visible(): readonly Toast[];
|
|
101
|
+
/** Whether a reason pauses every clock. */
|
|
102
|
+
get isPaused(): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* When the next visible toast's time runs out, in the caller's clock;
|
|
105
|
+
* `undefined` while paused, or when no visible toast has a finite duration.
|
|
106
|
+
*/
|
|
107
|
+
get deadline(): number | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* Shows `content` at `time`, after the toasts already queued, or in place of
|
|
110
|
+
* the toast with `options.id`. Returns its id.
|
|
111
|
+
*
|
|
112
|
+
* @throws {RangeError} When `time` is not finite, `durationMs` is negative or
|
|
113
|
+
* `NaN`, or `id` is empty.
|
|
114
|
+
*/
|
|
115
|
+
show(content: ToastContent, time: number, options?: ToastShowOptions): string;
|
|
116
|
+
/**
|
|
117
|
+
* Removes the toast with `id` at `time`; the next waiting one shows then.
|
|
118
|
+
* Returns whether the queue had it.
|
|
119
|
+
*
|
|
120
|
+
* @throws {RangeError} When `time` is not finite.
|
|
121
|
+
*/
|
|
122
|
+
dismiss(id: string, time: number): boolean;
|
|
123
|
+
/** Removes every toast. No clock is left to restart, so it takes no time. */
|
|
124
|
+
clear(): void;
|
|
125
|
+
/**
|
|
126
|
+
* Pauses every clock for `reason` at `time`, or lifts that reason; the clocks
|
|
127
|
+
* run while no reason is left. Reasons are the caller's names, such as
|
|
128
|
+
* `"hover"`, `"focus"` and `"hidden"`.
|
|
129
|
+
*
|
|
130
|
+
* @throws {RangeError} When `time` is not finite.
|
|
131
|
+
*/
|
|
132
|
+
setPaused(reason: string, isPaused: boolean, time: number): void;
|
|
133
|
+
/**
|
|
134
|
+
* Removes, at `time`, the visible toasts whose time has run out, and starts
|
|
135
|
+
* the clocks of those that show in their place.
|
|
136
|
+
*
|
|
137
|
+
* @throws {RangeError} When `time` is not finite.
|
|
138
|
+
*/
|
|
139
|
+
advance(time: number): void;
|
|
140
|
+
/**
|
|
141
|
+
* Calls `listener` after each change of the toasts or their clocks, until the
|
|
142
|
+
* returned subscription is disposed or `options.signal` aborts.
|
|
143
|
+
*/
|
|
144
|
+
subscribe(listener: CallableNoArgs, options?: {
|
|
145
|
+
readonly signal?: AbortSignal;
|
|
146
|
+
}): Disposable;
|
|
147
|
+
private generateId_;
|
|
148
|
+
/** Stops or starts each clock as visibility and pauses say. */
|
|
149
|
+
private startClocks_;
|
|
150
|
+
private settle_;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Where the toasts gather on the screen: its top or bottom, at the start,
|
|
154
|
+
* centre or end of a line.
|
|
155
|
+
*/
|
|
156
|
+
export type ToasterPosition = "top-start" | "top-center" | "top-end" | "bottom-start" | "bottom-center" | "bottom-end";
|
|
157
|
+
/** The attributes of a toaster, the region holding the toasts. */
|
|
158
|
+
export type ToasterAttributes = {
|
|
159
|
+
role: "region";
|
|
160
|
+
"aria-label": string;
|
|
161
|
+
tabindex: -1;
|
|
162
|
+
"data-position": ToasterPosition;
|
|
163
|
+
};
|
|
164
|
+
/** Input of {@link toasterAttributes}. */
|
|
165
|
+
export type ToasterAttributesInput = {
|
|
166
|
+
/** Its accessible name, which may say its shortcut: "Notifications (F8)". */
|
|
167
|
+
label: string;
|
|
168
|
+
/** Defaults to `bottom-end`. */
|
|
169
|
+
position?: ToasterPosition;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* The attributes of a toaster: a landmark region named by `label`, so a screen
|
|
173
|
+
* reader user finds the toasts, and focusable by script, so a shortcut can take
|
|
174
|
+
* focus to them.
|
|
175
|
+
*
|
|
176
|
+
* Time complexity: O(1). Memory: O(1).
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* toasterAttributes({ label: "Notifications" });
|
|
180
|
+
* // { role: "region", "aria-label": "Notifications", tabindex: -1,
|
|
181
|
+
* // "data-position": "bottom-end" }
|
|
182
|
+
*/
|
|
183
|
+
export declare function toasterAttributes(input: ToasterAttributesInput): ToasterAttributes;
|
|
184
|
+
/** The attributes of one toast, keyed by their HTML attribute names. */
|
|
185
|
+
export type ToastAttributes = {
|
|
186
|
+
"data-urgency": ToastUrgency;
|
|
187
|
+
"data-tone"?: string;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* The attributes of one toast: its urgency and its tone, for the stylesheet.
|
|
191
|
+
*
|
|
192
|
+
* Time complexity: O(1). Memory: O(1).
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* toastAttributes({ title: "Failed", tone: "negative", urgency: "assertive" });
|
|
196
|
+
* // { "data-urgency": "assertive", "data-tone": "negative" }
|
|
197
|
+
*/
|
|
198
|
+
export declare function toastAttributes(content: ToastContent): ToastAttributes;
|
|
199
|
+
/**
|
|
200
|
+
* What a screen reader announces of a toast, one line each: its title, its
|
|
201
|
+
* description, its action's label (so a person knows there is one to reach). A
|
|
202
|
+
* live region reads them as they appear; the toast itself is not one, so it is
|
|
203
|
+
* not read twice.
|
|
204
|
+
*
|
|
205
|
+
* Time complexity: O(1). Memory: O(1).
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* toastAnnouncement({ title: "Deleted", description: "3 files" });
|
|
209
|
+
* // ["Deleted", "3 files"]
|
|
210
|
+
*/
|
|
211
|
+
export declare function toastAnnouncement(content: ToastContent): readonly string[];
|
|
212
|
+
//# sourceMappingURL=toast-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toast-queue.d.ts","sourceRoot":"","sources":["../../src/toast/toast-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,KAAK,cAAc,EACnB,MAAM,cAAc,CAAC;AAEtB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,WAAW,CAAC;AAElD,mDAAmD;AACnD,MAAM,MAAM,WAAW,GAAG;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,8DAA8D;IAC9D,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;CAClC,CAAC;AAEF,6EAA6E;AAC7E,MAAM,MAAM,YAAY,GAAG;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC;IAC5B;;;;OAIG;I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,MAAM,CAAC;IACd,gCAAgC;IAChC,QAAQ,CAAC,EAAE,eAAe,CAAC;CAC3B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAChC,KAAK,EAAE,sBAAsB,GAC3B,iBAAiB,CAOnB;AAED,wEAAwE;AACxE,MAAM,MAAM,eAAe,GAAG;IAC7B,cAAc,EAAE,YAAY,CAAC;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,eAAe,CAStE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,YAAY,GAAG,SAAS,MAAM,EAAE,CAU1E"}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { assertFinite, assertInteger, assertNonNegative, } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Brief messages about what an application did — saved, sent, failed — shown a
|
|
4
|
+
* few at a time, each until its duration has run or it is dismissed. The
|
|
5
|
+
* framework-free state of a toaster: an adapter renders `visible`, announces
|
|
6
|
+
* each toast as it becomes visible, runs a timer to `deadline` and calls
|
|
7
|
+
* `advance` then. Every time is passed in, in ms of one clock.
|
|
8
|
+
*
|
|
9
|
+
* Only visible toasts' clocks run, and none while a reason pauses the queue:
|
|
10
|
+
* the pointer over the toasts, focus among them, the page hidden. So a person
|
|
11
|
+
* reading or reaching a toast does not see it go.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const toasts = new ToastQueue();
|
|
15
|
+
* const id = toasts.show({ title: "Saving…" }, performance.now(), {
|
|
16
|
+
* durationMs: Infinity,
|
|
17
|
+
* });
|
|
18
|
+
* toasts.show({ title: "Saved", tone: "positive" }, performance.now(), {
|
|
19
|
+
* id,
|
|
20
|
+
* });
|
|
21
|
+
*/
|
|
22
|
+
export class ToastQueue {
|
|
23
|
+
limit_;
|
|
24
|
+
durationMs_;
|
|
25
|
+
entries_ = [];
|
|
26
|
+
pauses_ = new Set();
|
|
27
|
+
listeners_ = new Set();
|
|
28
|
+
nextId_ = 1;
|
|
29
|
+
revision_ = 0;
|
|
30
|
+
toasts_ = [];
|
|
31
|
+
/**
|
|
32
|
+
* @throws {RangeError} When `limit` is not a positive safe integer, or
|
|
33
|
+
* `durationMs` is negative or `NaN`.
|
|
34
|
+
*/
|
|
35
|
+
constructor(options = {}) {
|
|
36
|
+
const { limit = 3, durationMs = 6000 } = options;
|
|
37
|
+
assertInteger(limit, "limit", 1);
|
|
38
|
+
assertNonNegative(durationMs, "durationMs", { isInfinityAllowed: true });
|
|
39
|
+
this.limit_ = limit;
|
|
40
|
+
this.durationMs_ = durationMs;
|
|
41
|
+
}
|
|
42
|
+
/** Every toast, in the order shown: the visible ones first. */
|
|
43
|
+
get toasts() {
|
|
44
|
+
return this.toasts_;
|
|
45
|
+
}
|
|
46
|
+
/** The toasts on screen: the first `limit`. */
|
|
47
|
+
get visible() {
|
|
48
|
+
return this.toasts_.slice(0, this.limit_);
|
|
49
|
+
}
|
|
50
|
+
/** Whether a reason pauses every clock. */
|
|
51
|
+
get isPaused() {
|
|
52
|
+
return this.pauses_.size > 0;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* When the next visible toast's time runs out, in the caller's clock;
|
|
56
|
+
* `undefined` while paused, or when no visible toast has a finite duration.
|
|
57
|
+
*/
|
|
58
|
+
get deadline() {
|
|
59
|
+
let deadline;
|
|
60
|
+
for (const { remainingMs, runningSince } of this.entries_) {
|
|
61
|
+
if (runningSince !== undefined && remainingMs < Infinity) {
|
|
62
|
+
const end = runningSince + remainingMs;
|
|
63
|
+
if (deadline === undefined || end < deadline) {
|
|
64
|
+
deadline = end;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return deadline;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Shows `content` at `time`, after the toasts already queued, or in place of
|
|
72
|
+
* the toast with `options.id`. Returns its id.
|
|
73
|
+
*
|
|
74
|
+
* @throws {RangeError} When `time` is not finite, `durationMs` is negative or
|
|
75
|
+
* `NaN`, or `id` is empty.
|
|
76
|
+
*/
|
|
77
|
+
show(content, time, options = {}) {
|
|
78
|
+
assertFinite(time, "time");
|
|
79
|
+
const { id: requestedId, durationMs: requested } = options;
|
|
80
|
+
if (requestedId === "") {
|
|
81
|
+
throw new RangeError("id must not be empty");
|
|
82
|
+
}
|
|
83
|
+
const durationMs = requested ?? (content.action === undefined ? this.durationMs_ : Infinity);
|
|
84
|
+
assertNonNegative(durationMs, "durationMs", { isInfinityAllowed: true });
|
|
85
|
+
this.revision_ += 1;
|
|
86
|
+
const existing = requestedId === undefined
|
|
87
|
+
? undefined
|
|
88
|
+
: this.entries_.find((entry) => entry.toast.id === requestedId);
|
|
89
|
+
const id = requestedId ?? this.generateId_();
|
|
90
|
+
const toast = {
|
|
91
|
+
...content,
|
|
92
|
+
id,
|
|
93
|
+
durationMs,
|
|
94
|
+
revision: this.revision_,
|
|
95
|
+
};
|
|
96
|
+
if (existing === undefined) {
|
|
97
|
+
this.entries_.push({
|
|
98
|
+
toast,
|
|
99
|
+
remainingMs: durationMs,
|
|
100
|
+
runningSince: undefined,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
existing.toast = toast;
|
|
105
|
+
existing.remainingMs = durationMs;
|
|
106
|
+
existing.runningSince = undefined;
|
|
107
|
+
}
|
|
108
|
+
this.settle_(time);
|
|
109
|
+
return id;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Removes the toast with `id` at `time`; the next waiting one shows then.
|
|
113
|
+
* Returns whether the queue had it.
|
|
114
|
+
*
|
|
115
|
+
* @throws {RangeError} When `time` is not finite.
|
|
116
|
+
*/
|
|
117
|
+
dismiss(id, time) {
|
|
118
|
+
assertFinite(time, "time");
|
|
119
|
+
const index = this.entries_.findIndex((entry) => entry.toast.id === id);
|
|
120
|
+
if (index === -1) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
this.entries_.splice(index, 1);
|
|
124
|
+
this.settle_(time);
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
/** Removes every toast. No clock is left to restart, so it takes no time. */
|
|
128
|
+
clear() {
|
|
129
|
+
if (this.entries_.length === 0) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.entries_.length = 0;
|
|
133
|
+
this.settle_(0);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Pauses every clock for `reason` at `time`, or lifts that reason; the clocks
|
|
137
|
+
* run while no reason is left. Reasons are the caller's names, such as
|
|
138
|
+
* `"hover"`, `"focus"` and `"hidden"`.
|
|
139
|
+
*
|
|
140
|
+
* @throws {RangeError} When `time` is not finite.
|
|
141
|
+
*/
|
|
142
|
+
setPaused(reason, isPaused, time) {
|
|
143
|
+
assertFinite(time, "time");
|
|
144
|
+
const wasPaused = this.isPaused;
|
|
145
|
+
if (isPaused) {
|
|
146
|
+
this.pauses_.add(reason);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
this.pauses_.delete(reason);
|
|
150
|
+
}
|
|
151
|
+
if (wasPaused !== this.isPaused) {
|
|
152
|
+
this.settle_(time);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Removes, at `time`, the visible toasts whose time has run out, and starts
|
|
157
|
+
* the clocks of those that show in their place.
|
|
158
|
+
*
|
|
159
|
+
* @throws {RangeError} When `time` is not finite.
|
|
160
|
+
*/
|
|
161
|
+
advance(time) {
|
|
162
|
+
assertFinite(time, "time");
|
|
163
|
+
let isChanged = false;
|
|
164
|
+
for (;;) {
|
|
165
|
+
const index = this.entries_.findIndex(({ remainingMs, runningSince }) => runningSince !== undefined && runningSince + remainingMs <= time);
|
|
166
|
+
if (index === -1) {
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
this.entries_.splice(index, 1);
|
|
170
|
+
this.startClocks_(time);
|
|
171
|
+
isChanged = true;
|
|
172
|
+
}
|
|
173
|
+
if (isChanged) {
|
|
174
|
+
this.settle_(time);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Calls `listener` after each change of the toasts or their clocks, until the
|
|
179
|
+
* returned subscription is disposed or `options.signal` aborts.
|
|
180
|
+
*/
|
|
181
|
+
subscribe(listener, options = {}) {
|
|
182
|
+
const { signal } = options;
|
|
183
|
+
const unsubscribe = () => {
|
|
184
|
+
this.listeners_.delete(listener);
|
|
185
|
+
};
|
|
186
|
+
if (signal?.aborted === true) {
|
|
187
|
+
return { [Symbol.dispose]: unsubscribe };
|
|
188
|
+
}
|
|
189
|
+
this.listeners_.add(listener);
|
|
190
|
+
signal?.addEventListener("abort", unsubscribe, { once: true });
|
|
191
|
+
return {
|
|
192
|
+
[Symbol.dispose]() {
|
|
193
|
+
signal?.removeEventListener("abort", unsubscribe);
|
|
194
|
+
unsubscribe();
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
generateId_() {
|
|
199
|
+
for (;;) {
|
|
200
|
+
const id = `toast-${String(this.nextId_)}`;
|
|
201
|
+
this.nextId_ += 1;
|
|
202
|
+
if (!this.entries_.some((entry) => entry.toast.id === id)) {
|
|
203
|
+
return id;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** Stops or starts each clock as visibility and pauses say. */
|
|
208
|
+
startClocks_(time) {
|
|
209
|
+
const isRunning = !this.isPaused;
|
|
210
|
+
for (const [index, entry] of this.entries_.entries()) {
|
|
211
|
+
const shouldRun = isRunning && index < this.limit_;
|
|
212
|
+
if (shouldRun && entry.runningSince === undefined) {
|
|
213
|
+
entry.runningSince = time;
|
|
214
|
+
}
|
|
215
|
+
else if (!shouldRun && entry.runningSince !== undefined) {
|
|
216
|
+
entry.remainingMs = Math.max(0, entry.remainingMs - (time - entry.runningSince));
|
|
217
|
+
entry.runningSince = undefined;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
settle_(time) {
|
|
222
|
+
this.startClocks_(time);
|
|
223
|
+
this.toasts_ = this.entries_.map((entry) => entry.toast);
|
|
224
|
+
// A copy: a listener may unsubscribe while they are called.
|
|
225
|
+
for (const listener of Array.from(this.listeners_)) {
|
|
226
|
+
listener();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The attributes of a toaster: a landmark region named by `label`, so a screen
|
|
232
|
+
* reader user finds the toasts, and focusable by script, so a shortcut can take
|
|
233
|
+
* focus to them.
|
|
234
|
+
*
|
|
235
|
+
* Time complexity: O(1). Memory: O(1).
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* toasterAttributes({ label: "Notifications" });
|
|
239
|
+
* // { role: "region", "aria-label": "Notifications", tabindex: -1,
|
|
240
|
+
* // "data-position": "bottom-end" }
|
|
241
|
+
*/
|
|
242
|
+
export function toasterAttributes(input) {
|
|
243
|
+
return {
|
|
244
|
+
role: "region",
|
|
245
|
+
"aria-label": input.label,
|
|
246
|
+
tabindex: -1,
|
|
247
|
+
"data-position": input.position ?? "bottom-end",
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* The attributes of one toast: its urgency and its tone, for the stylesheet.
|
|
252
|
+
*
|
|
253
|
+
* Time complexity: O(1). Memory: O(1).
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* toastAttributes({ title: "Failed", tone: "negative", urgency: "assertive" });
|
|
257
|
+
* // { "data-urgency": "assertive", "data-tone": "negative" }
|
|
258
|
+
*/
|
|
259
|
+
export function toastAttributes(content) {
|
|
260
|
+
const attributes = {
|
|
261
|
+
"data-urgency": content.urgency ?? "polite",
|
|
262
|
+
};
|
|
263
|
+
if (content.tone !== undefined) {
|
|
264
|
+
attributes["data-tone"] = content.tone;
|
|
265
|
+
}
|
|
266
|
+
return attributes;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* What a screen reader announces of a toast, one line each: its title, its
|
|
270
|
+
* description, its action's label (so a person knows there is one to reach). A
|
|
271
|
+
* live region reads them as they appear; the toast itself is not one, so it is
|
|
272
|
+
* not read twice.
|
|
273
|
+
*
|
|
274
|
+
* Time complexity: O(1). Memory: O(1).
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* toastAnnouncement({ title: "Deleted", description: "3 files" });
|
|
278
|
+
* // ["Deleted", "3 files"]
|
|
279
|
+
*/
|
|
280
|
+
export function toastAnnouncement(content) {
|
|
281
|
+
const lines = [content.title];
|
|
282
|
+
if (content.description !== undefined) {
|
|
283
|
+
lines.push(content.description);
|
|
284
|
+
}
|
|
285
|
+
if (content.action !== undefined) {
|
|
286
|
+
lines.push(content.action.label);
|
|
287
|
+
}
|
|
288
|
+
return lines;
|
|
289
|
+
}
|
|
290
|
+
//# sourceMappingURL=toast-queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toast-queue.js","sourceRoot":"","sources":["../../src/toast/toast-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,aAAa,EACb,iBAAiB,GAEjB,MAAM,cAAc,CAAC;AA6EtB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,UAAU;IACL,MAAM,CAAS;IACf,WAAW,CAAS;IACpB,QAAQ,GAAa,EAAE,CAAC;IACxB,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAC5B,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAChD,OAAO,GAAG,CAAC,CAAC;IACZ,SAAS,GAAG,CAAC,CAAC;IACd,OAAO,GAAqB,EAAE,CAAC;IAEvC;;;OAGG;IACH,YAAY,OAAO,GAAsB,EAAE;QAC1C,MAAM,EAAE,KAAK,GAAG,CAAC,EAAE,UAAU,GAAG,IAAI,EAAE,GAAG,O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OMRectReadOnly` from `getBoundingClientRect()` fits,
|
|
3
|
+
* or any record of the same numbers.
|
|
4
|
+
*/
|
|
5
|
+
export type ScreenRect = {
|
|
6
|
+
readonly x: number;
|
|
7
|
+
readonly y: number;
|
|
8
|
+
readonly width: number;
|
|
9
|
+
readonly height: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Whether any of the `toasts` covers part of `target`, the focused control: the
|
|
13
|
+
* toaster then fades its toasts, so a focused control is never hidden by them
|
|
14
|
+
* (WCAG 2.2 success criterion 2.4.11, focus not obscured). Edges that only
|
|
15
|
+
* touch do not cover; an empty box covers nothing.
|
|
16
|
+
*
|
|
17
|
+
* Time complexity: O(n) for n toasts. Memory: O(1).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* toastsObscure({ x: 0, y: 0, width: 10, height: 10 }, [
|
|
21
|
+
* { x: 5, y: 5, width: 10, height: 10 },
|
|
22
|
+
* ]); // true
|
|
23
|
+
*/
|
|
24
|
+
export declare function toastsObscure(target: ScreenRect, toasts: Iterable<ScreenRect>): boolean;
|
|
25
|
+
//# sourceMappingURL=toasts-obscure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toasts-obscure.d.ts","sourceRoot":"","sources":["../../src/toast/toasts-obscure.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IACxB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAOF;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAC5B,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,GAC1B,OAAO,CAWT"}
|