@tanstack/react-hotkeys
Version:
React adapter for TanStack Hotkeys
56 lines (53 loc) • 2.11 kB
JavaScript
'use client';
import { usePlatform } from "./_usePlatform.js";
import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
import { useKeyboardLayout } from "./useKeyboardLayout.js";
import { formatForAria } from "@tanstack/hotkeys";
import { announceLiveHotkeys, createFormatter } from "@tanstack/hotkeys/adapter";
import { useMemo } from "react";
// src/useFormatForAria.ts
/**
* Returns `formatForAria` with the platform from `HotkeysProvider`, or the
* detected platform without one. A `platform` passed to the returned function
* wins. Use its value for `aria-keyshortcuts`, so screen readers announce the
* shortcut. It returns `undefined` for a multi-step sequence, no hotkey, or a
* hotkey that can't fire right now (one that is disabled, in a suspended
* scope, a suspended character key, or not registered), so React omits the
* attribute.
*
* Physical bindings such as `[KeyQ]` name the key the user presses in
* Chromium browsers, once the keyboard layout has loaded. See
* `useKeyboardLayout`. A `layoutMap` passed to the returned function wins.
*
* Call the hook once at the top of the component; the returned function can
* be called anywhere. Server-rendered apps should set the provider's
* `defaultOptions.platform` from the request with `detectPlatform(userAgent)`,
* so the server HTML matches the user's platform.
*
* @example
* ```tsx
* function SearchButton() {
* const appHotkeys = useAppHotkeys()
* const formatForAria = useFormatForAria()
* const formatForDisplay = useFormatForDisplay()
* const { hotkey } = appHotkeys['search.open']
*
* return (
* <button aria-keyshortcuts={formatForAria(hotkey)}>
* Search {hotkey && <kbd>{formatForDisplay(hotkey)}</kbd>}
* </button>
* )
* }
* ```
*/
function useFormatForAria() {
const platform = usePlatform();
const layoutMap = useKeyboardLayout();
const registrations = useHotkeyRegistrations();
return useMemo(() => announceLiveHotkeys(createFormatter(formatForAria, () => platform, () => layoutMap), () => registrations), [
platform,
layoutMap,
registrations
]);
}
export { useFormatForAria };