Files
paseo/packages/app/src/components/ui/normalize-display-chord.ts
T
Christoph Leiter 90aa8bbefc feat(shortcuts): allow shortcuts to be unassigned (#2510)
Allow users to clear and restore shortcut bindings, persist the explicit unassigned state, and keep shortcut displays and actions aligned with the binding that fires. Includes focused unit coverage and desktop renderer Playwright coverage.
2026-08-09 01:31:40 +02:00

29 lines
1006 B
TypeScript

import type { ShortcutKey } from "@/utils/format-shortcut";
/**
* The combos a `Shortcut` should render, or `null` when it should render
* nothing at all.
*
* `keys={[]}` is the trap this exists to close: it wraps to `[[]]`, whose first
* element is an empty array — truthy — so a first-element check waves it
* through and an empty badge pill renders. A shortcut with no keys, whether the
* user unassigned it or it ships without a default combo, has to produce no
* element at all.
*/
export function normalizeDisplayChord(
chord?: ShortcutKey[][],
keys?: ShortcutKey[],
): ShortcutKey[][] | null {
const combos = chord ?? (keys ? [keys] : []);
if (combos.length === 0) {
return null;
}
// A chord with an empty step cannot be drawn honestly: rendering it would
// show a blank pill, and dropping the step would advertise a shorter
// sequence than the one that actually fires.
if (combos.some((combo) => combo.length === 0)) {
return null;
}
return combos;
}