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
package/package.json
CHANGED
|
@@ -1,6 +1,90 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "undo-toast",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Agent-first undo toasts: a decision function that says whether and how to offer undo for an action (duration, labels, keys, what closing means, and why), a framework-free offer store, and a React toast that follows the standard.",
|
|
5
|
+
"author": "Thor Whalen",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": [
|
|
9
|
+
"*.css"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=20"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/thorwhalen/undo-toast.git"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/thorwhalen/undo-toast#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/thorwhalen/undo-toast/issues"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"undo",
|
|
24
|
+
"toast",
|
|
25
|
+
"snackbar",
|
|
26
|
+
"notification",
|
|
27
|
+
"accessibility",
|
|
28
|
+
"wcag",
|
|
29
|
+
"react",
|
|
30
|
+
"agent",
|
|
31
|
+
"ux"
|
|
32
|
+
],
|
|
33
|
+
"bin": {
|
|
34
|
+
"undo-toast": "./dist/cli/main.js"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"skills",
|
|
39
|
+
"docs",
|
|
40
|
+
"AGENTS.md",
|
|
41
|
+
"README.md",
|
|
42
|
+
"LICENSE"
|
|
43
|
+
],
|
|
44
|
+
"exports": {
|
|
45
|
+
".": {
|
|
46
|
+
"types": "./dist/index.d.ts",
|
|
47
|
+
"import": "./dist/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./react": {
|
|
50
|
+
"types": "./dist/react/index.d.ts",
|
|
51
|
+
"import": "./dist/react/index.js"
|
|
52
|
+
},
|
|
53
|
+
"./dom": {
|
|
54
|
+
"types": "./dist/dom/index.d.ts",
|
|
55
|
+
"import": "./dist/dom/index.js"
|
|
56
|
+
},
|
|
57
|
+
"./global": "./dist/undo-toast.global.js",
|
|
58
|
+
"./styles.css": "./dist/styles.css",
|
|
59
|
+
"./package.json": "./package.json"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"typecheck": "tsc --noEmit",
|
|
65
|
+
"prepack": "npm run build"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"react": "^18 || ^19"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"react": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@testing-library/react": "^16.3.0",
|
|
77
|
+
"@types/node": "^22.20.5",
|
|
78
|
+
"@types/react": "^19.3.0",
|
|
79
|
+
"@types/react-dom": "^19.3.0",
|
|
80
|
+
"jsdom": "^27.0.0",
|
|
81
|
+
"react": "^19.3.1",
|
|
82
|
+
"react-dom": "^19.3.1",
|
|
83
|
+
"tsup": "^8.5.1",
|
|
84
|
+
"typescript": "^5.9.3",
|
|
85
|
+
"vitest": "^4.1.12"
|
|
86
|
+
},
|
|
87
|
+
"publishConfig": {
|
|
88
|
+
"access": "public"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: undo-toast
|
|
3
|
+
description: Decide whether and how to offer undo after a user action, and build it with the undo-toast package — an undo toast (snackbar), nothing, a confirmation, or a pending control; how long it stays, what closing and expiring mean (commit or forget), the labels, the keys, placement above a docked bottom bar. Use whenever a web UI removes, deletes, archives, clears, sends, forgets or bulk-changes something, on "X deleted. Undo", "undo toast", "snackbar", "toast with undo", "confirm or undo?", "should this have a confirmation dialog", "how long should the toast stay", "where should the toast go", and whenever an app already has its own hand-made undo bar.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# undo-toast — decide how to offer undo, then offer it
|
|
7
|
+
|
|
8
|
+
Undo beats a confirmation dialog for anything reversible: people learn to click through dialogs without reading. But not every action wants a toast, and the toast's details (how long, what closing means, which keys) are where hand-made versions go wrong. **Ask the package, do not guess.**
|
|
9
|
+
|
|
10
|
+
## 1. Ask for the advice
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npx undo-toast advise remove-item --message "“Intro” removed"
|
|
14
|
+
npx undo-toast advise --reversal deferred --cost high # an undo send
|
|
15
|
+
npx undo-toast presets # named actions; fields / schema: what you can say; rules: what advise applies
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
or in code: `advise('remove-item')`, `advise({ reversal: 'compensating', cost: 'high', count: 12 })`. The answer is JSON: `surface` (`toast | none | confirm | pending`), `durationMs` (`null` = until closed), `onExpire` / `onClose` (`commit | forget`), `pauseOn`, `coalesce`, labels, `hotkeys`, and `reasons` (each rule that fired, why, and its source). **Read the reasons**; quote them when you explain the choice. `warnings` are things to fix in the host (a trash for costly deletes).
|
|
19
|
+
|
|
20
|
+
What to tell it (run `npx undo-toast fields` for the full table):
|
|
21
|
+
|
|
22
|
+
| Field | Ask yourself |
|
|
23
|
+
|---|---|
|
|
24
|
+
| `reversal` | Is it already done, with one Undo applying the inverse of the whole thing, in place (`compensating`)? Held back until the window ends (`deferred`: undo send, a held key)? Recoverable only by hand, item by item, somewhere else (`manual`: editing the playlist, restoring one by one from a trash)? Impossible to take back (`none`)? |
|
|
25
|
+
| `cost` | Would a mistake lose the user's work, many items, or something hard to recreate (`high`)? |
|
|
26
|
+
| `resultStaysInView` | Does the changed thing stay on screen showing its new state (a checkbox, a rename)? Then it is its own feedback. |
|
|
27
|
+
| `frequency`, `count` | A burst of the same action merges into one toast; several items lengthen the window. |
|
|
28
|
+
| `hasUndoStack`, `hasTrash` | Does the app keep an undo history (Ctrl/Cmd+Z) or a trash? Then the toast is a shortcut, not the only way back. |
|
|
29
|
+
| `takesAway` | Does it take something away (delete, remove, overwrite)? `false` for an addition (to a playlist, a tag): a lost toast loses nothing, so no trash is called for. |
|
|
30
|
+
| `keepNotices` | The user's setting "keep notices until I close them" (WCAG 2.2.1). |
|
|
31
|
+
| `verb` | What the confirm button should say if it comes to that: `'Move {count} {item|items} to Trash'` (`{count}` is filled in). |
|
|
32
|
+
|
|
33
|
+
If the surface is not `toast`, do what it says: `none` (the change is the feedback; no toast), `confirm` (a dialog whose button is `confirmLabel`, the verb with the count, "Move 12 items to Trash", never "OK"; the button disabled until typed when `typeToConfirm`; then a pending button), `pending` (the control pending until the server answers; a failure toast with Retry that stays).
|
|
34
|
+
|
|
35
|
+
### Bulk actions on a selection
|
|
36
|
+
|
|
37
|
+
The default: **ask for confirmation, with the count, unless one Undo takes the whole operation back, in place.** "Recoverable" is not enough: if taking it back means going to the playlist or the Trash and finding the items one by one, it is `reversal: 'manual'` and `advise` returns `confirm` (rule `bulk-confirm`). Earn the toast instead by running the whole selection as **one undo step** (one acture-undo transaction / history entry, one Restore for the batch), then say `compensating`; the reason `bulk-one-step-undo` explains the choice in words you can quote. Never show both a confirmation and an undo toast for one action. Presets: `move-to-trash`, `bulk-move-to-trash`, `add-to-collection`, and `delete-permanently` / `empty-trash` (always confirm; typed above `timing.confirmTypeAbove`, 10 by default, an estimate to expose as a setting). Take the count and the payload from the same selection object.
|
|
38
|
+
|
|
39
|
+
## 2. Offer it
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { offerUndo, offerFromHistory, offerFromMutationFeedback } from 'undo-toast';
|
|
43
|
+
import { UndoToast, useBottomDock } from 'undo-toast/react';
|
|
44
|
+
import 'undo-toast/styles.css';
|
|
45
|
+
|
|
46
|
+
// once, near the root (its live region must exist before the first message):
|
|
47
|
+
<UndoToast />
|
|
48
|
+
|
|
49
|
+
// after the action:
|
|
50
|
+
removeSegment(item);
|
|
51
|
+
offerUndo({ message: `“${item.title}” removed`, undo: () => insertSegment(item, index), context: 'remove-item' });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- **Deferred** (nothing sent yet): pass `commit` too. It runs once when the window ends, when the user closes the toast, when another offer replaces it, or on `pagehide`. `undo` then only restores the screen.
|
|
55
|
+
- **A docked bottom bar** (a player, a tab bar): `useBottomDock(barRef)` on it. The toast then sits just above its visible part, and at the very bottom when the bar is gone or slid off-screen (a transform); `{ active: false }` for a bar hidden some other way. Never hard-code an offset (`bottom: 11rem`): that is how a toast ends up a third of the way up the screen.
|
|
56
|
+
- **A burst** (triage deletes): `key: 'remove'` and a message function `(n) => \`${n} segments removed\``; the advice for `frequency: 'frequent'` coalesces.
|
|
57
|
+
- **An app with its own undo history** (acture-undo): `offerUndo(offerFromHistory(history, { message, context: 'remove-item' }))` after the dispatch. Undo rolls the history back one entry, `{ ok: false }` shows as a failure, and Ctrl/Cmd+Z stays the app's.
|
|
58
|
+
- **zodal undoable mutations**: `onFeedback: (f) => { const o = offerFromMutationFeedback(f, { message: 'Row deleted' }); if (o) offerUndo(o); }`. zodal owns that window's timer, so the toast lasts as long and does not pause (until i2mint/zodal#146); a too-late undo (`false`) shows as a failure. Set zodal's `undoSeconds` from `advise({ reversal: 'deferred' }).durationMs / 1000`.
|
|
59
|
+
- **Undo results**: `undo` fails when it throws, rejects, or returns `false` or `{ ok: false }` (change with `isFailure` on `createUndoOffers`). A failed undo stays on screen with *Try again*, and a deferred action is never committed once Undo was pressed.
|
|
60
|
+
- **Another renderer** (sonner, a design system's toast): read `subscribe` + `current()`, call `undo`, `close`, `hold`, and call `bindPage(offers, { focus, contains })` once for the keys, the hidden-tab pause and `pagehide`. The labels (`undoLabel`, `closeLabel`, `failedText`, `retryLabel`) and `hotkeys` (tinykeys syntax, `$mod+z`) come in the advice. Keep the standing rules below.
|
|
61
|
+
|
|
62
|
+
## 3. The standing rules (the toast already follows them; keep them in any other renderer)
|
|
63
|
+
|
|
64
|
+
- **Placement**: bottom-left on a wide screen (bottom-right is where chat widgets live), full width at the bottom on a phone, above any docked bar and the home indicator.
|
|
65
|
+
- **One at a time**: a new offer replaces the old (a deferred one commits). No stacks.
|
|
66
|
+
- **The close control is an × named "Close notification"**, never "Dismiss" (read as "dismiss the action", which is Undo) and never "Keep" (read as "keep the item").
|
|
67
|
+
- **No default button, never steal focus**: the next Enter belongs to whatever the user was doing. F8 moves focus into the toast; Escape inside it closes it; Ctrl/Cmd+Z undoes while it shows (outside text fields, and unless the app's history owns the key).
|
|
68
|
+
- **Duration follows the way back**: the only way back → the toast stays until Undo, Close or the next action (Material 3, WCAG 2.2.1); undo also reachable elsewhere (Ctrl/Cmd+Z, a trash) → 10 s; deferred → a finite window (10 s, 15 s when costly) the user should be able to set (`timing.deferredMs`). **Pause** on hover, focus, touch and a hidden tab; resume with at least 4 s left.
|
|
69
|
+
- **Polite**: `role="status"` in a region mounted before any message; never `role="alert"` for an undo.
|
|
70
|
+
- **Words**: past tense, the object first: "“Intro” removed". After an undo, no second toast: the item comes back where it was.
|
|
71
|
+
- **Never** a countdown bar, a stack of toasts, top-right placement, or a library's 4-5 s default for a toast that is the only way back.
|
|
72
|
+
|
|
73
|
+
## Sources
|
|
74
|
+
|
|
75
|
+
The research behind every rule, with references: `docs/research/undo-toast-standard.md` in this package (`node_modules/undo-toast/docs/…`); for bulk actions, `docs/research/bulk-confirm.md`. The decision record: `docs/adr/0001-undo-toast-package.md`.
|