Skip to content

useUndoBatch

Batches undoable actions into one Sonner toast whose Undo restores them all and retries what failed.

For actions that are quick to do and safe to reverse, such as archiving: do the action at once, then push how to undo it. Every push while the toast is open joins one batch and the message counts again (3 tasks archived); the toast's Undo reverses the whole batch, newest first. count is how many things one entry stands for, durationMs how long the toast stays (6000 by default), and id keeps two batches on one screen apart. An undo that fails stays in the batch and the toast returns as an error, so Undo tries again. The hook binds no key: undo() reverses the last batch from your own handler, even after the toast has gone, and clear() forgets it. The page needs one Toaster from @khaeli/sonner.

Archive with undo

  • Write the migration guide
  • Add shortcuts to the editor
  • Fix the export time zone
  • Review the pricing page copy

Installation

npx shadcn@latest add @khaeli/use-undo-batch

Installs with

npm packages
  • sonner ^2.0.8

Keyboard

EnterSpace
On a row's button, archives the task. Move focus to the next row first, since the pressed button leaves the page.
Alt T
Moves focus to the toasts, where Tab reaches Undo. The key belongs to @khaeli/sonner.

Accessible name

  • The toast is announced when it appears, without moving focus. Write message as a full phrase with the number in it: 2 tasks archived.
  • A button that repeats on every row is named after its row: Archive Write the migration guide.

Registry JSON

The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/use-undo-batch.json