undoAction()
Let someone undo a recent change. Local reversible action.
Try it
Archive the sample task. It disappears immediately and you have five seconds to undo. The final commit here only updates this local demo.
One task in your list.
<div id="your-component">
<div class="task-preview" data-item><span class="task-checkbox" aria-hidden="true"></span><div><strong>Review the landing page</strong><small>Design · Today</small></div><span class="tag">To do</span></div><div class="empty-task" hidden>Task archived.</div><div class="demo-row"><button data-remove>Archive task</button><button data-undo disabled>Undo archive</button><button data-reset hidden>Reset example</button></div><p class="demo-status" role="status">One task in your list.</p>
</div>import * as kit from 'subtly';
const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function setup() {
let action;
const item = el.querySelector('[data-item]');
const empty = el.querySelector('.empty-task');
const remove = el.querySelector('[data-remove]');
const undo = el.querySelector('[data-undo]');
const reset = el.querySelector('[data-reset]');
function restore() {
item.hidden = false; empty.hidden = true;
remove.disabled = false; undo.disabled = true; reset.hidden = true;
status('Task restored.');
}
remove.addEventListener('click', () => {
action = kit.undoAction({
duration: 5000,
apply() {
item.hidden = true; empty.hidden = false;
remove.disabled = true; undo.disabled = false;
status('Task archived. You have 5 seconds to undo.');
},
revert: restore,
commit() {
undo.disabled = true; reset.hidden = false;
status('Archive confirmed in this demo.');
}
});
action.start(() => status('Could not archive. Task restored.'));
}, { signal });
undo.addEventListener('click', () => action?.undo(), { signal });
reset.addEventListener('click', restore, { signal });
return () => action?.destroy();
}
const stop = setup();
// Call when this component is removed.
function cleanup() {
listeners.abort();
stop?.();
}Add it to your project
- Copy the HTML. Use the HTML tab above and place the markup inside a dedicated component root. Layout classes belong to this documentation website; use your own styles.
- Connect the behavior. Copy the JavaScript tab, update
#your-componentand initialize it after the elements exist. Replace simulated operations with your own data. - Clean up on unmount. Call the example’s
cleanup()when the component is removed. It aborts application listeners and releases the utility’s timers or observers.
The preview and JavaScript tab use the same utility wiring. status() writes plain text into the example’s status element. wait() is a simulated local delay. For examples with data-ix attributes, start the declarative runtime once at application level, as shown in the code. Use your framework guide to place setup and cleanup correctly.
API and lifecycle
Provide apply(), revert(), commit() and an optional duration, defaulting to 5 seconds. start() applies a local preview and starts the undo window. The permanent commit runs after the deadline.
- Interface
apply/revert/commit/duration; start/undo/finish/destroy.- Lifecycle
- undo() reverts while pending; finish() commits early. destroy() cancels a pending action and reverts its preview. Handle commit errors so the user can recover.
Accessibility and safety
Implementation notes
Use for reversible low-risk actions; do not rely on page-exit timers for critical writes.
Use for reversible, low-risk changes and keep Undo keyboard accessible. Do not rely on a page-exit timer to complete a critical permanent write.
What to check
Remove an item, undo before the deadline, then repeat and let it commit. Test commit rejection and unmounting during the undo window.
View documented module source
/** Undo window for reversible actions. Apply a local preview immediately;
* commit only after the deadline. Undo before commit restores the preview.
* For permanent/critical operations use explicit confirmation instead.
* Do not use page-exit timers for writes that must reliably complete. */
export function undoAction({ apply, revert, commit, duration = 5000 }) {
let state = 'idle', timer;
async function finish() {
if (state !== 'pending') return false;
clearTimeout(timer); state = 'committing';
try { await commit(); state = 'committed'; return true; }
catch (error) { state = 'failed'; revert(); throw error; }
}
return {
get state() { return state; },
start(onError = () => {}) { if (state !== 'idle') return false; apply(); state = 'pending'; timer = setTimeout(() => finish().catch(onError),duration); return true; },
undo() { if (state !== 'pending') return false; clearTimeout(timer); state = 'undone'; revert(); return true; },
finish,
destroy() { if(state === 'pending') { clearTimeout(timer); state='cancelled'; revert(); } }
};
}
/** Accessible announcements. Mount a persistent empty live region once;
* write updates as text, never HTML. Avoid repeating visible focused errors.
* Use polite for routine status; assertive only for urgent interruptions. */
export function liveAnnouncer({ priority = 'polite', parent = document.body } = {}) {
const node = document.createElement('div');
node.className = 'ix-sr-only'; node.setAttribute('aria-live',priority); node.setAttribute('aria-atomic','true'); parent.append(node);
let timer;
return { announce(message) { clearTimeout(timer); node.textContent=''; timer=setTimeout(()=>{node.textContent=String(message);},50); }, destroy() { clearTimeout(timer); node.remove(); } };
}