undo-toast 0.0.0-stage → 0.4.1
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/AGENTS.md +9 -0
- package/LICENSE +21 -0
- package/README.md +120 -2
- package/dist/chunk-6IIGXUXK.js +259 -0
- package/dist/chunk-JP44LPGK.js +355 -0
- package/dist/cli/main.js +64 -0
- package/dist/dom/index.d.ts +24 -0
- package/dist/dom/index.js +129 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +2 -0
- package/dist/offers-c-kCY6ff.d.ts +426 -0
- package/dist/react/index.d.ts +57 -0
- package/dist/react/index.js +140 -0
- package/dist/styles.css +118 -0
- package/dist/undo-toast.global.js +1 -0
- package/docs/adr/0001-undo-toast-package.md +48 -0
- package/docs/research/bulk-confirm.md +80 -0
- package/docs/research/undo-toast-standard.md +193 -0
- package/package.json +88 -4
- package/skills/undo-toast/SKILL.md +75 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision vocabulary: what an agent (or a person) says about an action, and the named presets
|
|
3
|
+
* for common actions. This table is the single source of truth: the TypeScript types, the
|
|
4
|
+
* validation in `advise`, the CLI's `fields` and `presets` commands and the shipped skill's tables
|
|
5
|
+
* are all derived from it.
|
|
6
|
+
*/
|
|
7
|
+
declare const CONTEXT_FIELDS: {
|
|
8
|
+
readonly reversal: {
|
|
9
|
+
readonly values: readonly ["deferred", "compensating", "manual", "none"];
|
|
10
|
+
readonly default: "compensating";
|
|
11
|
+
readonly describe: "How the action can be taken back. 'deferred': nothing is committed until the undo window ends (Gmail's undo send, zodal's undoable mutation). 'compensating': it is already done, and one Undo applies the inverse of the whole operation, in place (re-insert, restore; for a bulk action, one undo step for the whole selection). 'manual': recoverable, but only by hand, item by item, somewhere else (finding 30 songs in a playlist to remove them, restoring 12 items one by one from a trash without a one-step restore); recoverable is not reversible. 'none': it cannot be taken back (a payment, an email already delivered, Delete permanently, Empty Trash).";
|
|
12
|
+
};
|
|
13
|
+
readonly cost: {
|
|
14
|
+
readonly values: readonly ["low", "high"];
|
|
15
|
+
readonly default: "low";
|
|
16
|
+
readonly describe: "What a mistake costs the user. 'high': their work is lost (a document, hours of edits), many items at once, or something hard to recreate (a key, a playlist they built). 'low': it is easy to redo by hand.";
|
|
17
|
+
};
|
|
18
|
+
readonly frequency: {
|
|
19
|
+
readonly values: readonly ["rare", "occasional", "frequent"];
|
|
20
|
+
readonly default: "occasional";
|
|
21
|
+
readonly describe: "How often the user does this. 'frequent' (toggle, check, reorder, many times a session) makes a toast noise.";
|
|
22
|
+
};
|
|
23
|
+
readonly resultStaysInView: {
|
|
24
|
+
readonly values: readonly [true, false];
|
|
25
|
+
readonly default: false;
|
|
26
|
+
readonly describe: "True when the changed thing stays on screen showing its new state (a checked box, a renamed title), so the change is its own feedback. False when the item leaves the view, moves away, or the action was done by a shortcut.";
|
|
27
|
+
};
|
|
28
|
+
readonly failsOften: {
|
|
29
|
+
readonly values: readonly [true, false];
|
|
30
|
+
readonly default: false;
|
|
31
|
+
readonly describe: "True when the action often fails or its result is computed by the server: show it pending, not done, and report failure with Retry.";
|
|
32
|
+
};
|
|
33
|
+
readonly count: {
|
|
34
|
+
readonly values: "number";
|
|
35
|
+
readonly default: 1;
|
|
36
|
+
readonly describe: "How many items the action touched. Above 1 the message names the count and the cost leans high.";
|
|
37
|
+
};
|
|
38
|
+
readonly hasUndoStack: {
|
|
39
|
+
readonly values: readonly [true, false];
|
|
40
|
+
readonly default: false;
|
|
41
|
+
readonly describe: "True when the app keeps its own undo history (acture-undo, Ctrl/Cmd+Z). Then the toast is a shortcut, the app owns Ctrl/Cmd+Z, and the action stays undoable after the toast leaves.";
|
|
42
|
+
};
|
|
43
|
+
readonly takesAway: {
|
|
44
|
+
readonly values: readonly [true, false];
|
|
45
|
+
readonly default: true;
|
|
46
|
+
readonly describe: "True when the action takes something from the user (deletes, removes, overwrites): losing its undo toast can lose their work. False when it only adds (to a playlist, a tag): if the undo is lost, nothing is gone, so no trash or undo history is called for.";
|
|
47
|
+
};
|
|
48
|
+
readonly hasTrash: {
|
|
49
|
+
readonly values: readonly [true, false];
|
|
50
|
+
readonly default: false;
|
|
51
|
+
readonly describe: "True when deleted things go to a trash or 'recently removed' that outlives the toast.";
|
|
52
|
+
};
|
|
53
|
+
readonly keepNotices: {
|
|
54
|
+
readonly values: readonly [true, false];
|
|
55
|
+
readonly default: false;
|
|
56
|
+
readonly describe: "The user's own setting 'keep notices until I close them' (WCAG 2.2.1, Timing Adjustable). True makes a compensating toast stay until closed.";
|
|
57
|
+
};
|
|
58
|
+
readonly message: {
|
|
59
|
+
readonly values: "string";
|
|
60
|
+
readonly default: undefined;
|
|
61
|
+
readonly describe: "The toast text, if known: longer text stays longer (reading time).";
|
|
62
|
+
};
|
|
63
|
+
readonly verb: {
|
|
64
|
+
readonly values: "string";
|
|
65
|
+
readonly default: undefined;
|
|
66
|
+
readonly describe: "The action's verb phrase, for the confirmation button when the advice is to confirm: 'Move {count} {item|items} to Trash', 'Add {count} {song|songs} to playlist'. `{count}` is the count and `{one|other}` picks the singular at 1; without `{count}`, a count above 1 is appended as ' (12)'.";
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
type Spec = typeof CONTEXT_FIELDS;
|
|
70
|
+
type ValueOf<F> = F extends {
|
|
71
|
+
values: readonly (infer V)[];
|
|
72
|
+
} ? V : F extends {
|
|
73
|
+
values: 'number';
|
|
74
|
+
} ? number : string;
|
|
75
|
+
/** What is known about the action to offer undo for. Every field is optional (see the defaults). */
|
|
76
|
+
type ActionContext = {
|
|
77
|
+
-readonly [K in keyof Spec]?: ValueOf<Spec[K]>;
|
|
78
|
+
};
|
|
79
|
+
type Optional = 'message' | 'verb';
|
|
80
|
+
/** {@link ActionContext} with every default applied (except `message` and `verb`, which have none). */
|
|
81
|
+
type ResolvedContext = {
|
|
82
|
+
-readonly [K in Exclude<keyof Spec, Optional>]-?: ValueOf<Spec[K]>;
|
|
83
|
+
} & {
|
|
84
|
+
message?: string;
|
|
85
|
+
verb?: string;
|
|
86
|
+
};
|
|
87
|
+
/** Common actions, named, so `advise('remove-item')` is enough for the usual case. */
|
|
88
|
+
declare const PRESETS: {
|
|
89
|
+
readonly 'remove-item': {
|
|
90
|
+
readonly describe: "Remove one item from a list or collection (a segment, a row, a card); undo puts it back.";
|
|
91
|
+
readonly context: {
|
|
92
|
+
readonly reversal: "compensating";
|
|
93
|
+
readonly cost: "low";
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
readonly 'delete-work': {
|
|
97
|
+
readonly describe: "Delete something the user built (a document, a playlist, a project).";
|
|
98
|
+
readonly context: {
|
|
99
|
+
readonly reversal: "compensating";
|
|
100
|
+
readonly cost: "high";
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
readonly 'bulk-delete': {
|
|
104
|
+
readonly describe: "Delete several items at once (pass count).";
|
|
105
|
+
readonly context: {
|
|
106
|
+
readonly reversal: "compensating";
|
|
107
|
+
readonly cost: "high";
|
|
108
|
+
readonly count: 2;
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
readonly 'move-to-trash': {
|
|
112
|
+
readonly describe: "Move one item to the Trash (soft delete); undo restores it, and so does the Trash after the toast leaves.";
|
|
113
|
+
readonly context: {
|
|
114
|
+
readonly reversal: "compensating";
|
|
115
|
+
readonly cost: "low";
|
|
116
|
+
readonly hasTrash: true;
|
|
117
|
+
readonly verb: "Move to Trash";
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
readonly 'bulk-move-to-trash': {
|
|
121
|
+
readonly describe: "Move a selection to the Trash (pass count). A timed toast when one Undo restores the whole selection; pass reversal: 'manual' when the Trash can only restore item by item, and it asks first.";
|
|
122
|
+
readonly context: {
|
|
123
|
+
readonly reversal: "compensating";
|
|
124
|
+
readonly cost: "high";
|
|
125
|
+
readonly count: 2;
|
|
126
|
+
readonly hasTrash: true;
|
|
127
|
+
readonly verb: "Move {count} {item|items} to Trash";
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
readonly 'add-to-collection': {
|
|
131
|
+
readonly describe: "Add items to a playlist, folder or collection that is not in view (pass count for a selection). One Undo takes the addition back; pass reversal: 'manual' when the only way back is editing the collection by hand, and a bulk add asks first.";
|
|
132
|
+
readonly context: {
|
|
133
|
+
readonly reversal: "compensating";
|
|
134
|
+
readonly cost: "low";
|
|
135
|
+
readonly takesAway: false;
|
|
136
|
+
readonly verb: "Add to collection";
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
readonly 'delete-permanently': {
|
|
140
|
+
readonly describe: "Delete from the Trash for good (pass count). Cannot be taken back: always confirms.";
|
|
141
|
+
readonly context: {
|
|
142
|
+
readonly reversal: "none";
|
|
143
|
+
readonly cost: "high";
|
|
144
|
+
readonly verb: "Delete {count} {item|items} permanently";
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
readonly 'empty-trash': {
|
|
148
|
+
readonly describe: "Empty the Trash (pass count). Cannot be taken back: always confirms.";
|
|
149
|
+
readonly context: {
|
|
150
|
+
readonly reversal: "none";
|
|
151
|
+
readonly cost: "high";
|
|
152
|
+
readonly verb: "Empty Trash";
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
readonly archive: {
|
|
156
|
+
readonly describe: "Archive, hide, or move out of the current view.";
|
|
157
|
+
readonly context: {
|
|
158
|
+
readonly reversal: "compensating";
|
|
159
|
+
readonly cost: "low";
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
readonly 'clear-history': {
|
|
163
|
+
readonly describe: "Clear a list of past entries (recent searches, input history).";
|
|
164
|
+
readonly context: {
|
|
165
|
+
readonly reversal: "compensating";
|
|
166
|
+
readonly cost: "low";
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
readonly 'forget-secret': {
|
|
170
|
+
readonly describe: "Forget a stored key or credential; it is held, not erased, until the window ends.";
|
|
171
|
+
readonly context: {
|
|
172
|
+
readonly reversal: "deferred";
|
|
173
|
+
readonly cost: "high";
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
readonly send: {
|
|
177
|
+
readonly describe: "Send, publish or share; held back until the window ends (undo send).";
|
|
178
|
+
readonly context: {
|
|
179
|
+
readonly reversal: "deferred";
|
|
180
|
+
readonly cost: "high";
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
readonly toggle: {
|
|
184
|
+
readonly describe: "Toggle, check, star, reorder: frequent, cheap, and its result stays in view.";
|
|
185
|
+
readonly context: {
|
|
186
|
+
readonly reversal: "compensating";
|
|
187
|
+
readonly cost: "low";
|
|
188
|
+
readonly frequency: "frequent";
|
|
189
|
+
readonly resultStaysInView: true;
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
readonly pay: {
|
|
193
|
+
readonly describe: "Pay, or any action with an external effect that cannot be called back.";
|
|
194
|
+
readonly context: {
|
|
195
|
+
readonly reversal: "none";
|
|
196
|
+
readonly cost: "high";
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
type PresetName = keyof typeof PRESETS;
|
|
201
|
+
/** Apply the defaults and check every value, with an error that names the field and the choices. */
|
|
202
|
+
declare function resolveContext(context?: ActionContext): ResolvedContext;
|
|
203
|
+
/** A preset's context, with `extra` on top; throws naming the presets when the name is unknown. */
|
|
204
|
+
declare function presetContext(name: string, extra?: ActionContext): ActionContext;
|
|
205
|
+
/**
|
|
206
|
+
* The context as a JSON Schema (draft 2020-12), from the same table: for an MCP tool's input schema,
|
|
207
|
+
* a form, or any tool that wants the vocabulary as data.
|
|
208
|
+
*/
|
|
209
|
+
declare function contextJsonSchema(): Record<string, unknown>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* `advise`: the agent-first decision function. Given what is known about an action, it says whether
|
|
213
|
+
* to offer undo at all, how (a toast, nothing, a confirmation, a pending control), for how long,
|
|
214
|
+
* what closing and expiring mean, the labels and the keys — and why, rule by rule, so an agent can
|
|
215
|
+
* explain or override any part of it.
|
|
216
|
+
*
|
|
217
|
+
* The rules are data ({@link RULES}): an ordered list a host can extend or replace (`rules`), and
|
|
218
|
+
* the numbers live in one place ({@link TIMING}). The research behind every rule is cited by its
|
|
219
|
+
* `source` (`docs/research/undo-toast-standard.md` in this package).
|
|
220
|
+
*/
|
|
221
|
+
|
|
222
|
+
/** Which surface acknowledges the action. */
|
|
223
|
+
type Surface = 'toast' | 'none' | 'confirm' | 'pending';
|
|
224
|
+
/** What pauses the toast's timer. */
|
|
225
|
+
type PauseReason = 'hover' | 'focus' | 'touch' | 'hidden';
|
|
226
|
+
interface Advice {
|
|
227
|
+
/** `toast`: an undo toast. `none`: the changed thing is its own feedback. `confirm`: ask first, naming the verb. `pending`: show the control pending, report failure with Retry. */
|
|
228
|
+
surface: Surface;
|
|
229
|
+
/** When `surface` is `confirm`: the confirm button's label, the verb with the count ("Move 12 items to Trash"), from the context's `verb`; `null` when no verb was given (then write one: never "OK" or "Yes"). */
|
|
230
|
+
confirmLabel: string | null;
|
|
231
|
+
/** When `surface` is `confirm`: keep the confirm button disabled until the user types the object's name or the count (an irreversible action over more than `timing.confirmTypeAbove` items). */
|
|
232
|
+
typeToConfirm: boolean;
|
|
233
|
+
/** How long the toast stays, in ms; `null` = until the user closes it. Meaningless unless `surface` is `toast`. */
|
|
234
|
+
durationMs: number | null;
|
|
235
|
+
/** After a pause, the timer resumes with at least this much left. */
|
|
236
|
+
resumeMinMs: number;
|
|
237
|
+
/** What pauses the timer. */
|
|
238
|
+
pauseOn: readonly PauseReason[];
|
|
239
|
+
/** When the time runs out: `commit` sends a deferred action; `forget` just hides the toast. */
|
|
240
|
+
onExpire: 'commit' | 'forget';
|
|
241
|
+
/** When the user closes the toast: as `onExpire`, but at once. */
|
|
242
|
+
onClose: 'commit' | 'forget';
|
|
243
|
+
/** Merge a burst of the same action into one toast ("3 segments removed. Undo"). */
|
|
244
|
+
coalesce: boolean;
|
|
245
|
+
/** The undo button's label. */
|
|
246
|
+
undoLabel: string;
|
|
247
|
+
/** The close (×) button's accessible name. Never "Dismiss": it reads as dismissing the action. */
|
|
248
|
+
closeLabel: string;
|
|
249
|
+
/** Shown before the message when an undo failed. */
|
|
250
|
+
failedText: string;
|
|
251
|
+
/** The undo button's label after a failure. */
|
|
252
|
+
retryLabel: string;
|
|
253
|
+
/** `focus`: the key that moves focus into the toast. `undo`: the key that undoes while it shows (`null` when the app's own undo history owns it). tinykeys syntax (`$mod` = Cmd on Apple, Ctrl elsewhere), so an acture-hotkeys keymap can take them as they are. */
|
|
254
|
+
hotkeys: {
|
|
255
|
+
focus: string | null;
|
|
256
|
+
undo: string | null;
|
|
257
|
+
};
|
|
258
|
+
/** Every rule that shaped this advice, in order. */
|
|
259
|
+
reasons: Array<{
|
|
260
|
+
rule: string;
|
|
261
|
+
why: string;
|
|
262
|
+
source: string;
|
|
263
|
+
}>;
|
|
264
|
+
/** Things the host should consider (a trash for costly deletes, say). */
|
|
265
|
+
warnings: string[];
|
|
266
|
+
}
|
|
267
|
+
/** Every number `advise` uses. Pass `timing` to change one (a user's chosen undo-send window, say). */
|
|
268
|
+
declare const TIMING: {
|
|
269
|
+
/** A timed toast with an action stays at least this long (Polaris' minimum for toasts with an action). */
|
|
270
|
+
readonly actionMs: 10000;
|
|
271
|
+
/** The window of a deferred action before it is sent (Gmail's choices: 5, 10, 20, 30 s). Let the user set it. */
|
|
272
|
+
readonly deferredMs: 10000;
|
|
273
|
+
/** Costly actions, when timed, stay at least this long. */
|
|
274
|
+
readonly highCostMs: 15000;
|
|
275
|
+
/** Words a message may have before reading time adds to the duration. */
|
|
276
|
+
readonly baseWords: 10;
|
|
277
|
+
/** Added per extra word (an estimate: no primary source gives a formula). */
|
|
278
|
+
readonly msPerExtraWord: 250;
|
|
279
|
+
/** Reading time never takes a timed toast past this. */
|
|
280
|
+
readonly maxMs: 20000;
|
|
281
|
+
/** After a pause, at least this much time is left. */
|
|
282
|
+
readonly resumeMinMs: 4000;
|
|
283
|
+
/** An irreversible action over more items than this asks the user to type to confirm. An estimate: no design system gives a number, so make it a setting. */
|
|
284
|
+
readonly confirmTypeAbove: 10;
|
|
285
|
+
};
|
|
286
|
+
type Timing = {
|
|
287
|
+
-readonly [K in keyof typeof TIMING]: number;
|
|
288
|
+
};
|
|
289
|
+
/** One rule: when it applies, what it changes, why, and where that comes from. */
|
|
290
|
+
interface Rule {
|
|
291
|
+
id: string;
|
|
292
|
+
when: (c: ResolvedContext, t: Timing) => boolean;
|
|
293
|
+
then: (advice: Advice, c: ResolvedContext, t: Timing) => Partial<Advice>;
|
|
294
|
+
why: string;
|
|
295
|
+
source: string;
|
|
296
|
+
/** Stop here: later rules do not apply (used when the surface is not a toast). */
|
|
297
|
+
final?: boolean;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* The confirm button's label from the context's `verb`: `{count}` is the count, `{one|other}` picks
|
|
301
|
+
* the singular when the count is 1 ("Move {count} {item|items} to Trash"); a verb without `{count}`
|
|
302
|
+
* gets " (12)" appended above 1. Pass `label` to `advise` to replace it (translations, say).
|
|
303
|
+
*/
|
|
304
|
+
declare function confirmLabel(c: Pick<ResolvedContext, 'count' | 'verb'>): string | null;
|
|
305
|
+
/** The rules, in the order they apply. Hosts pass `rules` to replace them, or `[...RULES, mine]` to extend. */
|
|
306
|
+
declare const RULES: readonly Rule[];
|
|
307
|
+
interface AdviseOptions {
|
|
308
|
+
/** Replace the rule list (default {@link RULES}). */
|
|
309
|
+
rules?: readonly Rule[];
|
|
310
|
+
/** Change any number (default {@link TIMING}). */
|
|
311
|
+
timing?: Partial<Timing>;
|
|
312
|
+
/** Fields forced last, whatever the rules said (recorded as the `override` reason). */
|
|
313
|
+
overrides?: Partial<Omit<Advice, 'reasons' | 'warnings'>>;
|
|
314
|
+
/** Builds `confirmLabel` whenever the surface is `confirm` (default {@link confirmLabel}): for translations or plural rules. */
|
|
315
|
+
label?: (c: ResolvedContext) => string | null;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Decide how to offer undo for an action.
|
|
319
|
+
*
|
|
320
|
+
* @param action a preset name (`'remove-item'`, see `PRESETS`) or an {@link ActionContext}
|
|
321
|
+
* @param extra context added on top of a preset (`advise('bulk-delete', { count: 12 })`)
|
|
322
|
+
* @example advise({ reversal: 'deferred', cost: 'high' }).durationMs // 15000
|
|
323
|
+
*/
|
|
324
|
+
declare function advise(action?: PresetName | ActionContext, extra?: ActionContext, { rules, timing, overrides, label }?: AdviseOptions): Advice;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* The undo offer's life, framework-free: one offer shown at a time, its timer (paused while the
|
|
328
|
+
* user is on the toast or the page is hidden), what closing, expiring and being replaced mean,
|
|
329
|
+
* coalescing a burst into one offer, and an undo that is guarded while it runs, kept on screen if
|
|
330
|
+
* it fails, and never followed by a commit of what the user asked to cancel.
|
|
331
|
+
*
|
|
332
|
+
* Renderers subscribe to it (`subscribe` + `current`, the shape React's `useSyncExternalStore`
|
|
333
|
+
* wants) and wire the page with `bindPage` (keys, hidden tab, `pagehide`). The React toast is
|
|
334
|
+
* `undo-toast/react`; any other renderer reads the same view and calls the same `bindPage`.
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
interface OfferInput {
|
|
338
|
+
/** The text: past tense, object first ("“Intro” removed"). A function receives the count of a coalesced burst. */
|
|
339
|
+
message: string | ((count: number) => string);
|
|
340
|
+
/**
|
|
341
|
+
* Take the action back: restore what the user sees (and, for a compensating action, the data).
|
|
342
|
+
* It failed when it throws, rejects, or returns `false` or `{ ok: false }` (acture's and zodal's
|
|
343
|
+
* convention; see `isFailure`).
|
|
344
|
+
*/
|
|
345
|
+
undo: () => unknown;
|
|
346
|
+
/** For a deferred action: send it. Runs once, when the window ends without an undo. */
|
|
347
|
+
commit?: () => unknown;
|
|
348
|
+
/** How to offer it. Default: `advise(context)`, re-run with the count when a burst merges. */
|
|
349
|
+
advice?: Advice;
|
|
350
|
+
/** What `advise` is told when `advice` is not given (a preset name or a context). */
|
|
351
|
+
context?: PresetName | ActionContext;
|
|
352
|
+
/** Offers with the same key merge while one shows, when the advice says `coalesce`. */
|
|
353
|
+
key?: string;
|
|
354
|
+
}
|
|
355
|
+
/** What a renderer shows. */
|
|
356
|
+
interface OfferView {
|
|
357
|
+
id: number;
|
|
358
|
+
message: string;
|
|
359
|
+
count: number;
|
|
360
|
+
advice: Advice;
|
|
361
|
+
/** `shown`; `undoing` while the undo runs (ignore presses); `failed` when it failed (offer Try again). */
|
|
362
|
+
status: 'shown' | 'undoing' | 'failed';
|
|
363
|
+
error?: unknown;
|
|
364
|
+
/** True while a pause reason holds the timer. */
|
|
365
|
+
paused: boolean;
|
|
366
|
+
}
|
|
367
|
+
interface UndoOffers {
|
|
368
|
+
/**
|
|
369
|
+
* Offer undo for an action that just happened; replaces (and retires) any offer showing, or waits
|
|
370
|
+
* until an undo in progress has settled. Returns its id, or null when the advice says no toast
|
|
371
|
+
* (`surface: 'none'`: the result is its own feedback). Throws when the advice says the action
|
|
372
|
+
* needed a confirmation or a pending control instead.
|
|
373
|
+
*/
|
|
374
|
+
offer(input: OfferInput): number | null;
|
|
375
|
+
/** Undo the offer showing (all of a coalesced burst, newest first). Resolves true when it worked. */
|
|
376
|
+
undo(): Promise<boolean>;
|
|
377
|
+
/** The user closed the toast: commit a deferred action now, or just hide it. */
|
|
378
|
+
close(): void;
|
|
379
|
+
/** Hold or release the timer for one reason (hover, focus, touch, hidden). Holds belong to the page, so they carry over to the next offer. */
|
|
380
|
+
hold(reason: PauseReason, on: boolean): void;
|
|
381
|
+
/** Commit a deferred offer now (call it on `pagehide`); the toast goes away. */
|
|
382
|
+
flush(): void;
|
|
383
|
+
/** The offer showing, or null. Same object until something changes. */
|
|
384
|
+
current(): OfferView | null;
|
|
385
|
+
/** Called after every change. Returns the unsubscribe function. */
|
|
386
|
+
subscribe(listener: () => void): () => void;
|
|
387
|
+
}
|
|
388
|
+
interface UndoOffersOptions {
|
|
389
|
+
/** Timers and clock, for tests. Default: the global ones. */
|
|
390
|
+
timers?: {
|
|
391
|
+
setTimeout(fn: () => void, ms: number): unknown;
|
|
392
|
+
clearTimeout(handle: unknown): void;
|
|
393
|
+
now(): number;
|
|
394
|
+
};
|
|
395
|
+
/** Told when a commit or an undo fails. Default: `console.error`. */
|
|
396
|
+
onError?: (error: unknown, phase: 'commit' | 'undo') => void;
|
|
397
|
+
/** Whether an undo's resolved value means it failed. Default: `false` or `{ ok: false }`. */
|
|
398
|
+
isFailure?: (result: unknown) => boolean;
|
|
399
|
+
}
|
|
400
|
+
/** `false`, or an object whose `ok` is `false` (acture's `UndoHistory.undo()`, zodal's command results). */
|
|
401
|
+
declare const defaultIsFailure: (result: unknown) => boolean;
|
|
402
|
+
/** Create an undo-offer store. Most apps need one, shared by the whole page. */
|
|
403
|
+
declare function createUndoOffers({ timers, onError, isFailure, }?: UndoOffersOptions): UndoOffers;
|
|
404
|
+
/** What `bindPage` needs from the renderer, beyond the store. */
|
|
405
|
+
interface PageBinding {
|
|
406
|
+
/** The document to listen on. Default: `document`. */
|
|
407
|
+
doc?: Document;
|
|
408
|
+
/** Move focus into the toast (to its Undo button). Called on the advice's focus key. */
|
|
409
|
+
focus?: () => void;
|
|
410
|
+
/** Whether an element is inside the toast (for Escape). */
|
|
411
|
+
contains?: (el: Element | null) => boolean;
|
|
412
|
+
}
|
|
413
|
+
/** True when `hotkey` (`F8`, `$mod+z`, `ctrl+shift+z`; `$mod`/`mod` = Cmd on Apple, Ctrl elsewhere) matches the event. */
|
|
414
|
+
declare function matchesHotkey(event: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'altKey'>, hotkey: string | null, mac?: boolean): boolean;
|
|
415
|
+
/** True for any element that edits text, including inside a shadow root, where Ctrl/Cmd+Z belongs to the field. */
|
|
416
|
+
declare function isEditable(el: Element | null): boolean;
|
|
417
|
+
/**
|
|
418
|
+
* Wire an offer store to the page, for any renderer: pause while the tab is hidden (read at once,
|
|
419
|
+
* then on every change), commit deferred offers on `pagehide` (a closed tab must not lose a send),
|
|
420
|
+
* and the advice's keys: the focus key moves focus into the toast, Ctrl/Cmd+Z undoes while it
|
|
421
|
+
* shows (outside text fields, and not when another handler already took the key), Escape inside
|
|
422
|
+
* the toast closes it. Returns the cleanup.
|
|
423
|
+
*/
|
|
424
|
+
declare function bindPage(offers: UndoOffers, { doc, focus, contains }?: PageBinding): () => void;
|
|
425
|
+
|
|
426
|
+
export { type ActionContext as A, CONTEXT_FIELDS as C, type OfferInput as O, type PresetName as P, RULES as R, type Surface as S, TIMING as T, type UndoOffers as U, type Advice as a, type AdviseOptions as b, type OfferView as c, PRESETS as d, type PageBinding as e, type PauseReason as f, type ResolvedContext as g, type Rule as h, type Timing as i, type UndoOffersOptions as j, advise as k, bindPage as l, confirmLabel as m, contextJsonSchema as n, createUndoOffers as o, defaultIsFailure as p, isEditable as q, matchesHotkey as r, presetContext as s, resolveContext as t };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { Ref, ReactNode, RefObject } from 'react';
|
|
3
|
+
import { U as UndoOffers } from '../offers-c-kCY6ff.js';
|
|
4
|
+
export { r as matchesHotkey } from '../offers-c-kCY6ff.js';
|
|
5
|
+
|
|
6
|
+
/** What a host-drawn control is given (see `renderAction`). */
|
|
7
|
+
interface ActionRenderProps {
|
|
8
|
+
/** Which control: the Undo (or Try again) button, or the close (×) button. */
|
|
9
|
+
kind: 'undo' | 'close';
|
|
10
|
+
/** Its text (Undo / Try again) or, for close, its accessible name. */
|
|
11
|
+
label: string;
|
|
12
|
+
/** Run it: undo (or retry) the action, or close the toast. */
|
|
13
|
+
onActivate: () => void;
|
|
14
|
+
/** Attach to the Undo control: the focus hotkey moves focus to it. Unset for close. */
|
|
15
|
+
ref?: Ref<HTMLButtonElement>;
|
|
16
|
+
/** The message's element id, for `aria-describedby` on the Undo control. */
|
|
17
|
+
describedBy?: string;
|
|
18
|
+
/** An undo is running: show it pending and ignore presses. */
|
|
19
|
+
pending: boolean;
|
|
20
|
+
/** The keyboard hint for Undo (⌘Z / Ctrl+Z), when the advice binds one. */
|
|
21
|
+
hint: string | null;
|
|
22
|
+
/** The class the default control has, to keep the toast's layout. */
|
|
23
|
+
className: string;
|
|
24
|
+
}
|
|
25
|
+
interface UndoToastProps {
|
|
26
|
+
/** The offer store to show. Default: the page's shared one (`getUndoOffers()`). */
|
|
27
|
+
offers?: UndoOffers;
|
|
28
|
+
/** The region's accessible name. */
|
|
29
|
+
regionLabel?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Draw the Undo and close controls with the host's own pressable component (large touch targets,
|
|
32
|
+
* a tap guard, a lockout after a press: an app for users with hand tremor needs all three). The
|
|
33
|
+
* toast keeps the policy: timing, pauses, hotkeys, coalescing, failure and Try again. Return
|
|
34
|
+
* `undefined` to keep the default control for a `kind`.
|
|
35
|
+
*/
|
|
36
|
+
renderAction?: (props: ActionRenderProps) => ReactNode | undefined;
|
|
37
|
+
}
|
|
38
|
+
/** The toast. Render it once, near the root, always (its live region must exist before a message). */
|
|
39
|
+
declare function UndoToast({ offers, regionLabel, renderAction }: UndoToastProps): react.JSX.Element;
|
|
40
|
+
/**
|
|
41
|
+
* Keep the toast above a bar docked at the bottom of the screen (a player, a tab bar): the
|
|
42
|
+
* distance from the bar's top to the bottom of the viewport, measured live, becomes
|
|
43
|
+
* `--undo-toast-dock` on the root element. No hard-coded offset: the toast sits at the very
|
|
44
|
+
* bottom when the bar is gone (unmounted, or slid off-screen while it stays mounted), right above
|
|
45
|
+
* it when it is there, floating bars (a safe-area gap below them) included. `active: false`
|
|
46
|
+
* removes the offset for a bar hidden some other way.
|
|
47
|
+
*
|
|
48
|
+
* Pass a ref, or the element itself (from a callback ref kept in state) when the bar mounts
|
|
49
|
+
* after the component that calls this hook: a ref that is null when the effect runs is not
|
|
50
|
+
* watched afterwards.
|
|
51
|
+
*/
|
|
52
|
+
declare function useBottomDock(target: RefObject<HTMLElement | null> | HTMLElement | null, { property, active }?: {
|
|
53
|
+
property?: string;
|
|
54
|
+
active?: boolean;
|
|
55
|
+
}): void;
|
|
56
|
+
|
|
57
|
+
export { type ActionRenderProps, UndoToast, type UndoToastProps, useBottomDock };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { bindPage, getUndoOffers, isApple } from '../chunk-6IIGXUXK.js';
|
|
2
|
+
export { matchesHotkey } from '../chunk-6IIGXUXK.js';
|
|
3
|
+
import '../chunk-JP44LPGK.js';
|
|
4
|
+
import { useSyncExternalStore, useRef, useEffect, useId, useLayoutEffect } from 'react';
|
|
5
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
function UndoToast({ offers = getUndoOffers(), regionLabel = "Notifications", renderAction }) {
|
|
8
|
+
const view = useSyncExternalStore(offers.subscribe, offers.current, () => null);
|
|
9
|
+
const toastRef = useRef(null);
|
|
10
|
+
const undoRef = useRef(null);
|
|
11
|
+
const returnFocus = useRef(null);
|
|
12
|
+
useEffect(
|
|
13
|
+
() => bindPage(offers, {
|
|
14
|
+
focus: () => {
|
|
15
|
+
const active = document.activeElement;
|
|
16
|
+
if (active instanceof HTMLElement && !toastRef.current?.contains(active)) returnFocus.current = active;
|
|
17
|
+
undoRef.current?.focus();
|
|
18
|
+
},
|
|
19
|
+
contains: (el) => !!el && !!toastRef.current?.contains(el)
|
|
20
|
+
}),
|
|
21
|
+
[offers]
|
|
22
|
+
);
|
|
23
|
+
return /* @__PURE__ */ jsx("section", { className: "undo-toast-region", "aria-label": regionLabel, children: /* @__PURE__ */ jsx("div", { role: "status", "aria-live": "polite", "aria-atomic": "true", children: view && /* @__PURE__ */ jsx(Toast, { view, offers, toastRef, undoRef, returnFocus, renderAction }, view.id) }) });
|
|
24
|
+
}
|
|
25
|
+
function Toast({ view, offers, toastRef, undoRef, returnFocus, renderAction }) {
|
|
26
|
+
const messageId = useId();
|
|
27
|
+
const own = useRef(null);
|
|
28
|
+
useLayoutEffect(
|
|
29
|
+
() => () => {
|
|
30
|
+
const el = own.current;
|
|
31
|
+
if (el && el.contains(document.activeElement)) {
|
|
32
|
+
const target = returnFocus.current;
|
|
33
|
+
returnFocus.current = null;
|
|
34
|
+
if (target?.isConnected) target.focus();
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
[returnFocus]
|
|
38
|
+
);
|
|
39
|
+
const { advice, status } = view;
|
|
40
|
+
const failed = status === "failed";
|
|
41
|
+
const undoing = status === "undoing";
|
|
42
|
+
const hint = advice.hotkeys.undo ? isApple() ? "\u2318Z" : "Ctrl+Z" : null;
|
|
43
|
+
return /* @__PURE__ */ jsxs(
|
|
44
|
+
"div",
|
|
45
|
+
{
|
|
46
|
+
ref: (el) => {
|
|
47
|
+
own.current = el;
|
|
48
|
+
toastRef.current = el;
|
|
49
|
+
},
|
|
50
|
+
className: "undo-toast",
|
|
51
|
+
"data-status": status,
|
|
52
|
+
onPointerEnter: (e) => e.pointerType === "mouse" && offers.hold("hover", true),
|
|
53
|
+
onPointerLeave: (e) => e.pointerType === "mouse" && offers.hold("hover", false),
|
|
54
|
+
onPointerDown: (e) => e.pointerType !== "mouse" && offers.hold("touch", true),
|
|
55
|
+
onPointerUp: (e) => e.pointerType !== "mouse" && offers.hold("touch", false),
|
|
56
|
+
onPointerCancel: () => offers.hold("touch", false),
|
|
57
|
+
onFocus: (e) => {
|
|
58
|
+
const from = e.relatedTarget;
|
|
59
|
+
if (from instanceof HTMLElement && !e.currentTarget.contains(from)) returnFocus.current = from;
|
|
60
|
+
offers.hold("focus", true);
|
|
61
|
+
},
|
|
62
|
+
onBlur: (e) => {
|
|
63
|
+
if (!e.currentTarget.contains(e.relatedTarget)) offers.hold("focus", false);
|
|
64
|
+
},
|
|
65
|
+
children: [
|
|
66
|
+
/* @__PURE__ */ jsx("span", { id: messageId, className: "undo-toast-message", children: failed ? `${advice.failedText} ${view.message}` : view.message }),
|
|
67
|
+
renderAction?.({
|
|
68
|
+
kind: "undo",
|
|
69
|
+
label: failed ? advice.retryLabel : advice.undoLabel,
|
|
70
|
+
onActivate: () => {
|
|
71
|
+
if (!undoing) void offers.undo();
|
|
72
|
+
},
|
|
73
|
+
ref: undoRef,
|
|
74
|
+
describedBy: messageId,
|
|
75
|
+
pending: undoing,
|
|
76
|
+
hint: failed ? null : hint,
|
|
77
|
+
className: "undo-toast-undo"
|
|
78
|
+
}) ?? /* @__PURE__ */ jsxs(
|
|
79
|
+
"button",
|
|
80
|
+
{
|
|
81
|
+
ref: undoRef,
|
|
82
|
+
type: "button",
|
|
83
|
+
className: "undo-toast-undo",
|
|
84
|
+
"aria-describedby": messageId,
|
|
85
|
+
"aria-disabled": undoing || void 0,
|
|
86
|
+
"aria-busy": undoing || void 0,
|
|
87
|
+
"data-pending": undoing || void 0,
|
|
88
|
+
onClick: () => void offers.undo(),
|
|
89
|
+
children: [
|
|
90
|
+
failed ? advice.retryLabel : advice.undoLabel,
|
|
91
|
+
hint && !failed && /* @__PURE__ */ jsx("kbd", { className: "undo-toast-hint", "aria-hidden": "true", children: hint })
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
),
|
|
95
|
+
renderAction?.({
|
|
96
|
+
kind: "close",
|
|
97
|
+
label: advice.closeLabel,
|
|
98
|
+
onActivate: () => offers.close(),
|
|
99
|
+
pending: false,
|
|
100
|
+
hint: null,
|
|
101
|
+
className: "undo-toast-close"
|
|
102
|
+
}) ?? /* @__PURE__ */ jsx("button", { type: "button", className: "undo-toast-close", "aria-label": advice.closeLabel, onClick: () => offers.close(), children: /* @__PURE__ */ jsx("svg", { viewBox: "0 0 16 16", width: "16", height: "16", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M4 4l8 8M12 4l-8 8", stroke: "currentColor", strokeWidth: "1.75", strokeLinecap: "round" }) }) })
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
function useBottomDock(target, { property = "--undo-toast-dock", active = true } = {}) {
|
|
108
|
+
const isRef = !!target && !(typeof HTMLElement !== "undefined" && target instanceof HTMLElement);
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const el = isRef ? target.current : target;
|
|
111
|
+
const root = document.documentElement;
|
|
112
|
+
if (!el || !active) return;
|
|
113
|
+
const viewport = () => Math.max(window.innerHeight, root.clientHeight);
|
|
114
|
+
const measure = () => {
|
|
115
|
+
const top = el.getBoundingClientRect().top;
|
|
116
|
+
const vh = viewport();
|
|
117
|
+
const lift = Number.isFinite(top) ? Math.min(vh, Math.max(0, vh - top)) : el.getBoundingClientRect().height;
|
|
118
|
+
root.style.setProperty(property, `${lift}px`);
|
|
119
|
+
};
|
|
120
|
+
measure();
|
|
121
|
+
const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(measure, { threshold: [0, 0.25, 0.5, 0.75, 1] });
|
|
122
|
+
io?.observe(el);
|
|
123
|
+
const own = (e) => {
|
|
124
|
+
if (e.target === el) measure();
|
|
125
|
+
};
|
|
126
|
+
for (const ev of ["transitionend", "transitioncancel", "animationend"]) el.addEventListener(ev, own);
|
|
127
|
+
window.addEventListener("resize", measure);
|
|
128
|
+
const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(measure);
|
|
129
|
+
ro?.observe(el);
|
|
130
|
+
return () => {
|
|
131
|
+
io?.disconnect();
|
|
132
|
+
ro?.disconnect();
|
|
133
|
+
for (const ev of ["transitionend", "transitioncancel", "animationend"]) el.removeEventListener(ev, own);
|
|
134
|
+
window.removeEventListener("resize", measure);
|
|
135
|
+
root.style.removeProperty(property);
|
|
136
|
+
};
|
|
137
|
+
}, [isRef ? target : target, isRef, property, active]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { UndoToast, useBottomDock };
|