Files
siftlode/frontend/src/components/ui/UndoToolbar.tsx
T
peter 81936e9f56 feat(design): every button on its family, the chip primitive, the lint rule (E28 S3b2)
The second half of the button sweep: the remaining hand-rolled buttons in
feature components, the design lab and the components/ui composites now
stand on Button, IconButton, Pressable, Chip or DragHandle.

- Chip + DismissibleChip (filter / token / tag / suggest / ghost, 24px
  floor, 44 on touch) replace five chip geometries; FilterChipGroup
  renders Chips; overflow lanes measure a mirror painted by chipClass.
- The Plex control row is IconButton's player rung (36, 20px glyph,
  round, overlay; 44 on touch); the YT modal bar is on the sm rung with
  pressed cycle toggles and names that survive a folded label.
- New variants: IconButton ghost-accent, danger-soft; Button warn on a
  new state-warn utility. The .btn-{tone} recipes and btnCls are gone.
- Fixed on the way: membership rows (collection editor, Plex playlist
  add) announce checked/mixed; a one-letter tag chip was 21px wide;
  descriptionLinks moved out of lib/ (it renders components).
- local/button-primitive: a raw <button>/<FocusSafeButton> outside the
  control-primitive files is a lint error (contract-tested, seen red).
2026-09-24 23:03:23 +02:00

58 lines
1.9 KiB
TypeScript

import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Redo2, Undo2 } from "lucide-react";
import { dispatch, useKeyScope } from "../../lib/keymap";
import { IconButton } from "./IconButton";
// Reusable undo/redo buttons with Ctrl/Cmd+Z (undo) and Ctrl/Cmd+Y or Ctrl/Cmd+Shift+Z
// (redo) shortcuts. Pairs with useUndoable but takes plain props, so it works with any
// undo source. The chords and the typing guard come from the shared keymap (`undo` scope), so
// they are listed in the help sheet with everything else instead of only living here.
export default function UndoToolbar({
canUndo,
canRedo,
onUndo,
onRedo,
}: {
canUndo: boolean;
canRedo: boolean;
onUndo: () => void;
onRedo: () => void;
}) {
const { t } = useTranslation();
// Only while a key actually DOES something: the sheet must not advertise Ctrl+Z on a page where
// nothing has been done yet and the handler returns immediately (E10 review round 1) — the same
// honest-states gate PlexBrowse uses for its Backspace.
useKeyScope("undo", canUndo || canRedo);
useEffect(() => {
function onKey(e: KeyboardEvent) {
dispatch(e, "undo", {
undo: (ev) => {
if (!canUndo) return;
ev.preventDefault();
onUndo();
},
redo: (ev) => {
if (!canRedo) return;
ev.preventDefault();
onRedo();
},
});
}
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [canUndo, canRedo, onUndo, onRedo]);
return (
<div className="inline-flex items-center gap-1">
<IconButton variant="outline" onClick={onUndo} disabled={!canUndo} label={t("common.undo")}>
<Undo2 />
</IconButton>
<IconButton variant="outline" onClick={onRedo} disabled={!canRedo} label={t("common.redo")}>
<Redo2 />
</IconButton>
</div>
);
}