Explore the index
Browse components
SystemsexperimentalReactTypeScript

Undo Window

A short recovery window for reversible actions.

Undo window

Archive a project, then choose Undo or restore it from the archive.

A real browser viewport. Scroll within the preview when the sample needs more space. Configuration stays in sync between sizes.

01What it does

Shows the outcome of a recent action and offers a time-limited request to reverse it. The host owns persistence, the deadline and the result of restoration. The quick undo is one active opportunity; provide a separate recovery route for important actions.

In this previewArchive a fictional project, then undo it. Change the simulated result to inspect failure and use the archive to restore later.

Design decisions

A reversible action can proceed without a blocking confirmation when recovery is straightforward. The quick window is not the only recovery path: the sample archive remains available after it ends.

Only one quick-undo target is active. A newer archive supersedes the earlier target; the host remains responsible for actual persistence and recovery.

02Install

Registry install

registry:component

Run this in a React project with components.json. The component and its stylesheet land in components/lab/undo-window/.

npx shadcn@latest add https://lab.tanishk.me/r/undo-window.json

Files included

3
  • components/lab/undo-window/undo-window.tsxcomponent
  • components/lab/undo-window/undo-window.cssstyles
  • components/lab/undo-window/undo-policy.tslogic

Manual install: copy each file into the same folder and preserve its relative imports. Open the source

03Usage

undo-window.usage.tsx
import { UndoWindow } from "@/components/lab/undo-window/undo-window";

<UndoWindow entry={recentAction} onUndo={requestRestore} onExpire={finalizeAction} />

04Props

Props accepted by Undo Window
PropTypeDefaultNotes
entryUndoEntry | null—Host-owned action identity, label, absolute deadline and confirmed status. Use a new id for each action.
onUndo(id: string) => void—Requests restoration. The host reports pending, restored or failed through entry.
onExpire(id: string) => void—Notifies the host that the deadline passed; the host validates and finalizes the action.

05Accessibility

  • The action and result are announced in a polite status region; the remaining time is not repeatedly announced.
  • Undo is a keyboard-operable button with a visible focus state. Provide an independent recovery route for consequential actions.
  • The surface uses no motion to convey its state.

06Source

3 files, 98 lines. The combined copy is a reference; save each file separately for use.

components/lab/undo-window/undo-window.tsx— press enter to show or hide the source
tsx
"use client";

import { useCallback, useEffect, useRef, useState } from "react";
import { canRequestUndo } from "./undo-policy";
import "./undo-window.css";

export type UndoStatus = "available" | "pending" | "restored" | "failed" | "expired";
export type UndoEntry = { id: string; label: string; expiresAt: number; status: UndoStatus };
export type UndoWindowProps = {
/** A single host-owned action. Replace its id when a new action supersedes it. */
entry: UndoEntry | null;
/** Request reversal; the host must report success or failure in entry.status. */
onUndo: (id: string) => void;
/** The deadline passed. The host decides how and when to finalize. */
onExpire: (id: string) => void;
};

export function UndoWindow({ entry, onUndo, onExpire }: UndoWindowProps) {
const [now, setNow] = useState(() => Date.now());
const requested = useRef<string | null>(null);
const expired = useRef<string | null>(null);
const expireRef = useRef(onExpire);
const actionRef = useRef<HTMLButtonElement>(null);
const statusRef = useRef<HTMLDivElement>(null);
useEffect(() => { expireRef.current = onExpire; }, [onExpire]);
const id = entry?.id;
const deadline = entry?.expiresAt;
const status = entry?.status;

const preserveFocus = useCallback(() => {
if (document.activeElement === actionRef.current) statusRef.current?.focus();
}, []);

useEffect(() => {
requested.current = null;
expired.current = null;
}, [id]);

useEffect(() => {
if (!id || status !== "available" || deadline === undefined) return;
const tick = () => {
const current = Date.now();
setNow(current);
if (current >= deadline && expired.current !== id) {
expired.current = id;
preserveFocus();
expireRef.current(id);
}
};
tick();
const timer = window.setInterval(tick, 1000);
document.addEventListener("visibilitychange", tick);
return () => { window.clearInterval(timer); document.removeEventListener("visibilitychange", tick); };
}, [id, status, deadline, preserveFocus]);

if (!entry) return null;
const remaining = Math.max(0, Math.ceil((entry.expiresAt - now) / 1000));
const available = canRequestUndo(entry.status, entry.expiresAt, now);
const request = () => {
const current = Date.now();
if (!canRequestUndo(entry.status, entry.expiresAt, current)) {
if (entry.status === "available" && expired.current !== entry.id) {
expired.current = entry.id;
preserveFocus();
onExpire(entry.id);
}
return;
}
if (requested.current === entry.id) return;
requested.current = entry.id;
preserveFocus();
onUndo(entry.id);
};
const message = entry.status === "pending" ? "Restoring…" : entry.status === "restored" ? "Restored" : entry.status === "failed" ? "Could not restore" : entry.status === "expired" || !available ? "Undo window ended" : "Archived";
return (
<section className="uw-window" aria-label="Recent action">
<div className="uw-copy" ref={statusRef} tabIndex={-1} role="status" aria-live="polite">
<strong>{message}</strong><span>{entry.label}</span>
{entry.status === "failed" ? <span>Try restoring it from the archive.</span> : null}
</div>
{available ? <div className="uw-action"><span>{remaining}s to undo</span><button ref={actionRef} type="button" onClick={request}>Undo</button></div> : null}
</section>
);
}
components/lab/undo-window/undo-window.css— press enter to show or hide the source
css
.uw-window { display:flex; justify-content:space-between; align-items:center; gap:16px; padding:14px 16px; border:1px solid #dedbe9; border-radius:12px; background:#f6f4ff; color:#24202f; font:inherit; }
.uw-copy { display:grid; gap:2px; min-width:0; font-size:14px; line-height:1.45; }
.uw-copy span { color:#555160; overflow-wrap:anywhere; }
.uw-action { display:flex; align-items:center; gap:12px; white-space:nowrap; color:#555160; font-size:12px; }
.uw-action button { border:0; border-radius:8px; padding:8px 12px; background:#4f27e0; color:white; font:inherit; font-weight:600; cursor:pointer; }
.uw-action button:focus-visible { outline:2px solid #4f27e0; outline-offset:3px; }
@media (max-width:400px) { .uw-window { align-items:flex-start; flex-direction:column; } }
components/lab/undo-window/undo-policy.ts— press enter to show or hide the source
ts
/** The host owns the deadline and the operation. */
export function canRequestUndo(status: string, expiresAt: number, now: number): boolean {
return status === "available" && Number.isFinite(expiresAt) && now < expiresAt;
}