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
| Prop | Type | Description |
|---|---|---|
enabled | boolean | Listen while the screen is focused. Defaults to true. |
bindings | ShortcutOverrides | Controlled chord overrides by action ID. |
actions | readonly ShortcutDefinition[] | Append actions with namespaced IDs, such as acme:hold, after the defaults. |
onConflict | (conflict: ShortcutConflict) => void | Reports each conflict on mount, then only conflicts absent from the previous resolution. |
children | ReactNode | The 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 andTaxBasedOnwhile open, andDiscountForm,PriceFormandChargeFormwhile mounted. The phone-widthNavDrawerblocks 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+ArrowLeftandAlt+ArrowRightdo 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), soAlt+Pchecks out from the search box.
Default chords
| Action | Label | Chord |
|---|---|---|
sale.search | Search products | Alt+F |
sale.checkout | Checkout | Alt+P |
cart.new | New cart | Alt+N |
cart.prev | Previous cart | Alt+ArrowLeft |
cart.next | Next cart | Alt+ArrowRight |
cart.discount | Add discount | Alt+D |
cart.void | Void order | Alt+V |
shortcuts.help | Keyboard shortcuts | Alt+Slash |
Built-in registrations
| Action | Registered by | Available while |
|---|---|---|
sale.search | Catalogue | Catalogue is mounted. |
sale.checkout | CartFoot | The Checkout button is enabled. |
cart.discount | Cart | The Order discount control is shown. |
cart.new, cart.prev, cart.next | OpenSaleTabs | The + tab or the corresponding arrow key can move to another cart, with no wrap. |
cart.void | CartFoot | The app supplies a foot action with shortcut: 'cart.void'. |
shortcuts.help | ShortcutHelpSheet | The 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.
| Conflict | Meaning |
|---|---|
invalid | The chord cannot be parsed. A refused override falls back to the action's default. |
reserved | The chord is reserved for the browser or system. A refused override falls back to the default. |
duplicate | Multiple overrides, or multiple defaults with no override, claim the same chord. None of those contenders gets it. |
displaced | An override claims another action's default chord. The displaced action is unbound. |
unknown | The 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.