A focused button that turns natively disabled loses the focus to <body> in Chrome. The app disables the very button that was pressed on ~60 sites — a mutation pending, a pager at its last page, undo with nothing left, a player at the end of its queue, a form gone clean — so a keyboard user was thrown out of the page on every such press. One primitive instead of a fix per site: ui/FocusSafeButton renders a focused-and-disabled (or busy) button as aria-disabled with a click guard, and applies the native attribute again once the focus is elsewhere. It asks the document at render time as well as listening for focus events, because a window without focus fires none (measured). ui/Button and ui/form's Switch are built on it; 57 hand-rolled buttons swapped their tag for it keeping their classes, and three hand-copies of the rule (the channel page's Block and Hide, ChannelDiscovery's Subscribe) now pass `busy`. The soft state's dim look is one rule in index.css. `local/focus-safe-disabled` refuses a new raw <button disabled>, pinned by focusSafe.contract.test.ts. lib/plexDetailUi moved to components/: it is components, and lib may not import the primitive. Measured on localdev in Chrome: the audit log's Next button keeps the focus on reaching the last page, reads aria-disabled, and the guarded click does nothing.
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import { useEffect } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Redo2, Undo2 } from "lucide-react";
|
|
import { dispatch, useKeyScope } from "../../lib/keymap";
|
|
import { FocusSafeButton } from "./FocusSafeButton";
|
|
|
|
// 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]);
|
|
|
|
const btn =
|
|
"p-1.5 rounded-lg border border-border text-muted enabled:hover:text-fg enabled:hover:border-accent disabled:opacity-30 transition";
|
|
return (
|
|
<div className="inline-flex items-center gap-1">
|
|
<FocusSafeButton
|
|
onClick={onUndo}
|
|
disabled={!canUndo}
|
|
title={t("common.undo")}
|
|
aria-label={t("common.undo")}
|
|
className={btn}
|
|
>
|
|
<Undo2 className="w-4 h-4" />
|
|
</FocusSafeButton>
|
|
<FocusSafeButton
|
|
onClick={onRedo}
|
|
disabled={!canRedo}
|
|
title={t("common.redo")}
|
|
aria-label={t("common.redo")}
|
|
className={btn}
|
|
>
|
|
<Redo2 className="w-4 h-4" />
|
|
</FocusSafeButton>
|
|
</div>
|
|
);
|
|
}
|