@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
61 lines (59 loc) • 2.28 kB
JavaScript
import { resolveHotkeys as resolveHotkeys$1 } from "./_alternates.js";
import { detectPlatform } from "./platform.js";
// src/alternates.ts
/**
* Resolves every hotkey of a hotkey for one platform, in order: its entry
* for the platform, then its alternates. The first one is the hotkey that
* labels show. An alternate without a hotkey on the platform is left out,
* and so is one equal to an earlier hotkey. A chord or a sequence returns
* itself, and no hotkey returns an empty array.
*
* Registration and formatting resolve hotkey objects themselves. Call this
* to read the hotkeys yourself, such as to check each one for conflicts.
*
* @param hotkey - A hotkey object, or any other hotkey
* @param platform - The target platform. Defaults to auto-detection.
*
* @example
* ```ts
* const redo = {
* default: 'Mod+Y',
* mac: 'Mod+Shift+Z',
* alternates: [{ default: 'Mod+Shift+Z', mac: null }],
* } as const
* resolveHotkeys(redo, 'windows') // ['Mod+Y', 'Mod+Shift+Z']
* resolveHotkeys(redo, 'mac') // ['Mod+Shift+Z']
* resolveHotkeys({ default: null, alternates: ['F1', 'F2'] }, 'mac') // ['F1', 'F2']
* ```
*/
function resolveHotkeys(hotkey, platform = detectPlatform()) {
return resolveHotkeys$1(hotkey, platform);
}
/**
* Resolves the hotkeys of a hotkey object for one platform that come after
* the one labels show: `resolveHotkeys` without its first hotkey. A hotkey
* without alternates returns an empty array.
*
* Call this to list the alternates next to a label, such as in a cheat
* sheet. Where the platform has only alternates, the first of them is the
* label, so it is not in this list.
*
* @param hotkey - A hotkey object, or any other hotkey
* @param platform - The target platform. Defaults to auto-detection.
*
* @example
* ```ts
* const redo = {
* default: 'Mod+Y',
* mac: 'Mod+Shift+Z',
* alternates: [{ default: 'Mod+Shift+Z', mac: null }],
* } as const
* resolveHotkeyAlternates(redo, 'windows') // ['Mod+Shift+Z']
* resolveHotkeyAlternates(redo, 'mac') // []
* resolveHotkeyAlternates({ default: null, alternates: ['F1', 'F2'] }, 'mac') // ['F2']
* ```
*/
function resolveHotkeyAlternates(hotkey, platform = detectPlatform()) {
return resolveHotkeys$1(hotkey, platform).slice(1);
}
export { resolveHotkeyAlternates, resolveHotkeys };