@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
82 lines (80 loc) • 2.74 kB
JavaScript
import { createStore } from "./_store.js";
// src/keyboard-layout.ts
let store;
let users = 0;
/** Counts reads, so the newest one that succeeds wins. */
let started = 0;
let stored = 0;
/**
* The store that holds the browser's keyboard layout once `loadKeyboardLayout`
* has loaded it. Framework adapters read it to label physical bindings, so
* `[KeyQ]` shows as `A` on a French AZERTY keyboard, and devtools show it.
* Matching and recording never read it.
*
* The state is `null` until the layout loads, and stays `null` where the
* browser has no layout API: Firefox, Safari, servers, and cross-origin frames
* without the `keyboard-map` permission. Pass the state to `formatForDisplay`
* as `layoutMap` when you format labels yourself.
*/
function getKeyboardLayoutStore() {
return store ??= createStore(null);
}
function refresh() {
const read = ++started;
let pending;
try {
pending = Promise.resolve(navigator.keyboard.getLayoutMap());
} catch {
return;
}
pending.then((entries) => {
if (read < stored) return;
stored = read;
const next = new Map(entries);
const layoutStore = getKeyboardLayoutStore();
const last = layoutStore.state;
if (last?.size === next.size && [...next].every(([code, key]) => last.get(code) === key)) return;
layoutStore.setState(() => next);
}).catch(() => {});
}
/**
* Loads the browser's keyboard layout into `getKeyboardLayoutStore()`, with
* `navigator.keyboard.getLayoutMap()`. Only Chromium browsers have it, so the
* layout stays `null` elsewhere and labels keep their conventional form.
*
* Framework adapters call this for you when a label or layout primitive
* mounts. Call it yourself only without an adapter, in the browser, after
* your page has rendered.
*
* The first caller reads the layout. Browsers send no event when the user
* switches layouts, so while any caller is active, the layout is read again
* each time the window regains focus.
*
* @returns A function that ends this caller's interest. When the last caller
* stops, refreshing stops. The loaded layout stays in the store, so the next
* label has it at once.
*
* @example
* ```ts
* const stop = loadKeyboardLayout()
* getKeyboardLayoutStore().subscribe((layoutMap) => {
* label.textContent = formatForDisplay('[KeyW]', {
* layoutMap: layoutMap ?? undefined,
* })
* })
* ```
*/
function loadKeyboardLayout() {
if (typeof window === "undefined" || typeof window.addEventListener !== "function") return () => {};
if (!users++) {
window.addEventListener("focus", refresh);
refresh();
}
let active = true;
return () => {
if (!active) return;
active = false;
if (!--users) window.removeEventListener("focus", refresh);
};
}
export { getKeyboardLayoutStore, loadKeyboardLayout };