Skip to main content

Undo and Redo

PRO

The functionality is available in PRO Edition only

The Kanban widget can track card changes so users can step backward and forward through their edits. This guide covers how history works, how to surface it through the toolbar, and how to wire custom keyboard shortcuts.

Enabling history​

Set the history prop on <Kanban> to true:

<script setup>
import { Kanban } from "@svar-ui/vue-kanban";

const columns = [
{ id: "backlog", label: "Backlog" },
{ id: "doing", label: "In Progress" },
{ id: "done", label: "Done" },
];

const cards = [
{ id: 1, label: "Task A", column: "backlog" },
{ id: 2, label: "Task B", column: "doing" },
];
</script>

<template>
<Kanban :cards="cards" :columns="columns" :history="true" />
</template>

With history off (the default), undo and redo are no-ops and no snapshots are taken. The widget reads history once, when it creates the store. Toggling the prop later has no effect, so decide at mount time.

Once enabled, every mutating card action (add-card, update-card, move-card, duplicate-card, delete-card) saves a shallow copy of the card array before the change applies. undo restores the most recent snapshot and pushes the current state onto the redo stack. redo reverses the process.

A few things to know:

  • History tracks cards only. Column changes, filter state, and sort order aren't captured. Collapsing or expanding a column or swimlane isn't a history step either.
  • A move-card that changes both the column and the swimlane is one action, so it's one history step.
  • Pushing a new entry clears the redo stack. After an undo, any new edit discards the steps you could have redone.
  • The widget groups consecutive update-card calls for the same card within a short time window into one history entry, so rapid edits in the editor don't flood the stack.
  • Tight bursts of the same action type (for example, several quick deletes) also become one entry.
  • Restoring a snapshot closes the editor (editorData resets to null).
  • Adding, deleting, or moving a column or swimlane resets both stacks once the action succeeds. Renaming one doesn't.
  • Changing the cards or columns props doesn't reset history. The widget applies prop changes incrementally, so the stacks survive the update.
  • History is local client state. It's designed for edit-then-save workflows, not as a backend-synchronized undo protocol.

Triggering undo/redo​

With history enabled, dispatch the undo or redo action through the API:

<script setup>
import { ref } from "vue";
import { Kanban } from "@svar-ui/vue-kanban";

const api = ref(null);
const columns = [
{ id: "backlog", label: "Backlog" },
{ id: "doing", label: "In Progress" },
{ id: "done", label: "Done" },
];
const cards = [];
</script>

<template>
<button :onclick="() => api.exec('undo', {})">Undo</button>
<button :onclick="() => api.exec('redo', {})">Redo</button>

<Kanban ref="api" :cards="cards" :columns="columns" :history="true" />
</template>

Both actions take an empty object as payload. When there's nothing to undo or redo, the call is a no-op.

You can check the current stack depths with api.getState().history, which returns { undo: number, redo: number }. For reactive updates, use api.getReactiveState().history.

Toolbar integration​

The built-in Toolbar component includes ready-made undo and redo buttons. Enable them with the undo prop:

<script setup>
import { ref } from "vue";
import { Kanban, Toolbar, Editor } from "@svar-ui/vue-kanban";

const api = ref(null);
const columns = [
{ id: "backlog", label: "Backlog" },
{ id: "doing", label: "In Progress" },
{ id: "done", label: "Done" },
];
const cards = [
{ id: 1, label: "Task A", column: "backlog" },
{ id: 2, label: "Task B", column: "doing" },
];
</script>

<template>
<Toolbar :api="api" :undo="true" />
<Kanban ref="api" :cards="cards" :columns="columns" :history="true" />
<Editor v-if="api" :api="api" />
</template>

Toolbar with undo and redo buttons next to the kanban board

The toolbar subscribes to the reactive history state. It disables each button when its stack is empty. Without :history="true" on <Kanban>, both buttons render but stay permanently disabled.

Combine undo with the other toolbar features:

<template>
<Toolbar :api="api" :add="true" :undo="true" :sort="true" />
</template>

This renders the full default toolbar: add-card button, undo/redo buttons, and a sort menu.

Custom shortcuts​

The widget doesn't bind keyboard shortcuts for undo and redo. Wire them yourself by listening for key events and dispatching the actions:

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import { Kanban, Editor } from "@svar-ui/vue-kanban";

const api = ref(null);
const columns = [
{ id: "backlog", label: "Backlog" },
{ id: "doing", label: "In Progress" },
{ id: "done", label: "Done" },
];
const cards = [];

function handleKeydown(e) {
if (!api.value) return;
const mod = e.ctrlKey || e.metaKey;
if (mod && e.key === "z" && !e.shiftKey) {
e.preventDefault();
api.value.exec("undo", {});
} else if (mod && e.key === "z" && e.shiftKey) {
e.preventDefault();
api.value.exec("redo", {});
}
}

onMounted(() => window.addEventListener("keydown", handleKeydown));
onUnmounted(() => window.removeEventListener("keydown", handleKeydown));
</script>

<template>
<Kanban ref="api" :cards="cards" :columns="columns" :history="true" />
<Editor v-if="api" :api="api" />
</template>

This binds Ctrl+Z for undo and Ctrl+Shift+Z for redo at the window level. The metaKey check covers Cmd+Z on macOS. Adjust the bindings or scope the listener to a wrapper element to fit your app.