Tally UI

ShortcutProvider

Bind keyboard shortcuts to actions on the focused screen and list the chords that are available.

Import

import { ShortcutProvider, useShortcut, useShortcuts } from '@tallyui/components';

Usage

Mount the provider at the screen root. Pass the screen's focus signal so a screen that stays mounted in the background cannot run an action.

function SaleScreen({ checkout }: { checkout: () => Promise<void> }) {
  const isFocused = useIsFocused();
  return (
    <ShortcutProvider enabled={isFocused}>
      <CheckoutButton checkout={checkout} />
    </ShortcutProvider>
  );
}

function CheckoutButton({ checkout }: { checkout: () => Promise<void> }) {
  useShortcut('sale.checkout', checkout);
  return <Button title="Checkout" onPress={checkout} />;
}

Props

PropTypeDescription
enabledbooleanListen while the screen is focused. Defaults to true.
bindingsShortcutOverridesControlled chord overrides by action ID.
actionsreadonly ShortcutDefinition[]Append actions with namespaced IDs, such as acme:hold, after the defaults.
onConflict(conflict: ShortcutConflict) => voidReports each conflict on mount, then only conflicts absent from the previous resolution.
childrenReactNodeThe screen content; the provider adds no layout.

useShortcut

Call useShortcut(id, run) in the component that owns the action. Pass { enabled: false } to unregister it temporarily. If several components register the same ID, the most recent registration runs; removing it restores the previous one. If run returns a promise, further presses for that ID are prevented but do not run until it settles. The action presents its own errors. Without a provider, the hook does nothing. A held key's auto-repeat is ignored.

useShortcuts

Use useShortcuts() in a help sheet or settings screen inside the provider. Each row contains the ID, label, normalized chord (or null), and whether the action can run now. Without a provider the list is empty.

const shortcuts = useShortcuts();
return shortcuts.filter(row => row.available).map(row => (
  <Text key={row.id}>{row.chord}: {row.label}</Text>
));

ShortcutHelpSheet is one such listing.

useShortcutBlock

While a block is active, the provider runs no chord and prevents none. useShortcuts() lists every row as unavailable until the last block is released. The block is taken during commit, so a chord pressed as an overlay opens is already blocked.

TallyUI's Dialog and AlertDialog content block automatically, including every sheet, the refund modal and ConfirmDialog. The cart line keypad popover blocks while open, and TenderPane blocks during the tender stage. If your app builds an overlay on something else, call useShortcutBlock(open) in that overlay:

const [open, setOpen] = useState(false);
useShortcutBlock(open);
return <CustomOverlay open={open} onOpenChange={setOpen} />;
  • Pickers and inline forms block too: PopoverContent, SelectContent, ComboboxContent, DropdownMenuContent, ContextMenuContent, CustomerDropdown, the catalogue's variation popover and TaxBasedOn while open, and DiscountForm, PriceForm and ChargeForm while mounted. The phone-width NavDrawer blocks while open; Escape still closes it.
  • In a text field, the arrow, Home/End, PageUp/PageDown, Backspace and Delete keys go to the field whatever the modifiers, so Alt+ArrowLeft and Alt+ArrowRight do not switch carts from inside a field. A field also keeps Ctrl or Meta with A, C, V, X, Z or Y (by letter or by physical key, so non-Latin layouts are covered), and every key while an IME is composing. A field also keeps any key pressed with AltGr. Alt+letter chords still run there, unless the layout makes that key a dead key (Option+E and Option+N on a US Mac), so Alt+P checks out from the search box.

Default chords

ActionLabelChord
sale.searchSearch productsAlt+F
sale.checkoutCheckoutAlt+P
cart.newNew cartAlt+N
cart.prevPrevious cartAlt+ArrowLeft
cart.nextNext cartAlt+ArrowRight
cart.discountAdd discountAlt+D
cart.voidVoid orderAlt+V
shortcuts.helpKeyboard shortcutsAlt+Slash

Built-in registrations

ActionRegistered byAvailable while
sale.searchCatalogueCatalogue is mounted.
sale.checkoutCartFootThe Checkout button is enabled.
cart.discountCartThe Order discount control is shown.
cart.new, cart.prev, cart.nextOpenSaleTabsThe + tab or the corresponding arrow key can move to another cart, with no wrap.
cart.voidCartFootThe app supplies a foot action with shortcut: 'cart.void'.
shortcuts.helpShortcutHelpSheetThe sheet is mounted.

A cart navigation chord pressed in a field leaves focus in that field.

Rebinding and conflicts

bindings is controlled: the app persists it, not TallyUI. For example, bindings={{ 'sale.search': 'Alt+S', 'cart.void': null }} moves search and unbinds void. Omit an ID to use its default. resolveBindings returns the resolved rows and conflicts without mounting a provider.

ConflictMeaning
invalidThe chord cannot be parsed. A refused override falls back to the action's default.
reservedThe chord is reserved for the browser or system. A refused override falls back to the default.
duplicateMultiple overrides, or multiple defaults with no override, claim the same chord. None of those contenders gets it.
displacedAn override claims another action's default chord. The displaced action is unbound.
unknownThe override names an ID not present in the definitions. It is ignored.

Web and native

An enabled web provider adds one capture-phase keydown listener to the window, so it hears a chord pressed in a react-native-web TextInput, which stops the key's propagation. A registered action calls preventDefault, never stopPropagation, so the barcode scanner still sees every key. Dead keys and composed characters are left alone in editable fields so accented typing works. Unregistered actions do not prevent the default.

Native adds no listener and lists every action as unavailable. Escape stays with each component's own handler.