Util

KEYS

Canonical KeyboardEvent.key constants — avoid magic strings in keydown handlers. The WrKey type is the union of every value, useful for narrowly-typed key callbacks.

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

NameDescriptionTypeDefault
KEYSCanonical KeyboardEvent.key constants — ENTER, ESCAPE, ARROW_UP, ARROW_DOWN, TAB, SPACE, …const record
WrKeyUnion of every value in KEYS — drop into function signatures for type-safe key matching.type alias

See also