Usage
import { KEYS, type WrKey } from 'ngwr/utils';
if (event.key === KEYS.ESCAPE) {
close();
}
function onArrow(key: WrKey) { /* strictly-typed */ }Why ngwr provides this
KeyboardEvent.key is the standard, but the values are surprising: arrow keys are 'ArrowUp' (not 'Up'), space is ' ' (a literal space, not 'Space'), and a typo like 'Esacpe' silently never matches. KEYS gives you autocomplete, spec-correct values, and WrKey as a union for type-safe function signatures.
// Native — string literals are typo-prone and the spec is surprising.
if (event.key === 'Esacpe') close(); // typo → silently never fires
if (event.key === 'Up') {} // ← wrong, it's 'ArrowUp'
if (event.key === 'Space') {} // ← wrong, it's ' ' (a literal space)
// ngwr — autocomplete + spec-correct + WrKey union for type-safe matching.
if (event.key === KEYS.ESCAPE) close();
if (event.key === KEYS.ARROW_UP) prev();
if (event.key === KEYS.SPACE) toggle();API
| Name | Description | Type | Default |
|---|---|---|---|
KEYS | Canonical KeyboardEvent.key constants — ENTER, ESCAPE, ARROW_UP, ARROW_DOWN, TAB, SPACE, … | const record | — |
WrKey | Union of every value in KEYS — drop into function signatures for type-safe key matching. | type alias | — |