Skip to content

useUndoRedo

Undo/redo history for drawing overlays and indicator toggles. Automatically connected to useDrawingTools and useIndicators via a shared context ref — actions are recorded without manual wiring.

Keyboard shortcuts: Ctrl+Z (undo), Ctrl+Y / Ctrl+Shift+Z (redo). Shortcuts are ignored while typing in input/textarea/contentEditable elements. Multiple instances are safe: only the first mounted instance owns the shortcuts and the shared listener (ownership moves to the next instance on unmount).

import { useUndoRedo } from "react-klinecharts-ui";
const { canUndo, canRedo, undo, redo, pushAction, clear } = useUndoRedo();
PropertyTypeDescription
canUndobooleanWhether there are actions to undo
canRedobooleanWhether there are actions to redo
undo() => voidUndo the last action
redo() => voidRedo the last undone action
pushAction(action: UndoRedoAction) => voidPush a new action (clears redo)
clear() => voidClear all history
TypeTriggerUndoRedo
overlay_addedUser completes a drawingRemoves the overlayRe-creates it
overlays_removedremoveAllDrawings()Restores all removed overlaysRe-removes them
indicator_toggledAdd/remove indicatorReverses the toggleRe-applies it

useUndoRedo registers a pushAction callback on undoRedoListenerRef (shared via provider context). When useDrawingTools finishes a drawing or useIndicators toggles an indicator, they call the ref to record the action — no prop drilling required. Just mount useUndoRedo once and the history fills in automatically. The provider keeps an ownership registry of all instances, and only the current owner receives recorded actions and keyboard events.