Skip to content

Commit 231cfe7

Browse files
committed
Add native keyboard dispatch and WPT keyboard adapters
1 parent d4f4dc9 commit 231cfe7

11 files changed

Lines changed: 890 additions & 4 deletions

File tree

‎packages/ui/src/dispatch.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { createNativeDispatch } from './dispatch.ts';
3+
import { createHTMLDocument, KeyboardEvent } from './dom.ts';
4+
5+
describe('KeyboardEvent.keyCode', () => {
6+
it('defaults to zero for constructed events and accepts an explicit legacy code', () => {
7+
expect(new KeyboardEvent('keydown', { key: 'Tab' }).keyCode).toBe(0);
8+
expect(new KeyboardEvent('keydown', { key: 'Tab', keyCode: 9 }).keyCode).toBe(9);
9+
});
10+
11+
it('supplies compatibility codes for native key events', () => {
12+
const document = createHTMLDocument(() => {});
13+
const dispatch = createNativeDispatch(document);
14+
const received: [string, number, boolean][] = [];
15+
document.body!.addEventListener('keydown', (event) => {
16+
const keyboard = event as KeyboardEvent;
17+
received.push([keyboard.key, keyboard.keyCode, keyboard.isTrusted]);
18+
});
19+
20+
dispatch({ type: 'keydown', key: 'Tab', code: 'Tab' });
21+
dispatch({ type: 'keydown', key: 'Tab', code: 'Tab', shiftKey: true });
22+
dispatch({ type: 'keydown', key: 'A', code: 'KeyA', shiftKey: true });
23+
dispatch({ type: 'keydown', key: '!', code: 'Digit1', shiftKey: true });
24+
dispatch({ type: 'keydown', key: 'Unidentified' });
25+
26+
expect(received).toEqual([
27+
['Tab', 9, true],
28+
['Tab', 9, true],
29+
['A', 65, true],
30+
['!', 49, true],
31+
['Unidentified', 0, true],
32+
]);
33+
});
34+
});

‎packages/ui/src/dispatch.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* oxlint-disable bombshell-dev/exported-function-async -- DOM event dispatch is synchronous. */
2+
3+
import {
4+
dispatchEvent,
5+
KeyboardEvent,
6+
nativeKeyCode,
7+
type Document,
8+
type KeyboardEventInit,
9+
} from './dom.ts';
10+
11+
export interface NativeEventDispatch {
12+
(event: NativeEvent): void;
13+
}
14+
15+
export function createNativeDispatch(document: Document): NativeEventDispatch {
16+
return (event: NativeEvent): void => {
17+
if (document.activeElement) {
18+
dispatchEvent(document.activeElement, {
19+
event: new KeyboardEvent(event.type, {
20+
...event,
21+
keyCode: nativeKeyCode(event),
22+
bubbles: true,
23+
cancelable: true,
24+
composed: true,
25+
}),
26+
isTrusted: true,
27+
});
28+
}
29+
};
30+
}
31+
32+
export type NativeEvent = NativeKeyboardEvent;
33+
34+
export interface NativeKeyboardEvent extends NativeEventModifiers {
35+
type: 'keydown' | 'keyup';
36+
key: string;
37+
code?: string;
38+
location?: number;
39+
repeat?: boolean;
40+
isComposing?: boolean;
41+
}
42+
43+
export type NativeEventModifiers = Pick<
44+
KeyboardEventInit,
45+
'altKey' | 'ctrlKey' | 'metaKey' | 'shiftKey'
46+
>;

‎packages/ui/src/dom.ts‎

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,87 @@ export class FocusEvent extends Event {
928928
}
929929
}
930930

931+
export interface KeyboardEventInit extends EventOptions {
932+
key?: string;
933+
code?: string;
934+
keyCode?: number;
935+
location?: number;
936+
repeat?: boolean;
937+
isComposing?: boolean;
938+
altKey?: boolean;
939+
ctrlKey?: boolean;
940+
metaKey?: boolean;
941+
shiftKey?: boolean;
942+
}
943+
944+
export class KeyboardEvent extends Event {
945+
static readonly DOM_KEY_LOCATION_STANDARD = 0;
946+
static readonly DOM_KEY_LOCATION_LEFT = 1;
947+
static readonly DOM_KEY_LOCATION_RIGHT = 2;
948+
static readonly DOM_KEY_LOCATION_NUMPAD = 3;
949+
950+
readonly DOM_KEY_LOCATION_STANDARD = KeyboardEvent.DOM_KEY_LOCATION_STANDARD;
951+
readonly DOM_KEY_LOCATION_LEFT = KeyboardEvent.DOM_KEY_LOCATION_LEFT;
952+
readonly DOM_KEY_LOCATION_RIGHT = KeyboardEvent.DOM_KEY_LOCATION_RIGHT;
953+
readonly DOM_KEY_LOCATION_NUMPAD = KeyboardEvent.DOM_KEY_LOCATION_NUMPAD;
954+
955+
readonly #keyCode: number;
956+
readonly key: string;
957+
readonly code: string;
958+
readonly location: number;
959+
readonly repeat: boolean;
960+
readonly isComposing: boolean;
961+
readonly altKey: boolean;
962+
readonly ctrlKey: boolean;
963+
readonly metaKey: boolean;
964+
readonly shiftKey: boolean;
965+
966+
constructor(type: string, options: KeyboardEventInit = {}) {
967+
super(type, options);
968+
this.key = options.key ?? '';
969+
this.code = options.code ?? '';
970+
this.#keyCode = (options.keyCode ?? 0) >>> 0;
971+
this.location = options.location ?? 0;
972+
this.repeat = options.repeat ?? false;
973+
this.isComposing = options.isComposing ?? false;
974+
this.altKey = options.altKey ?? false;
975+
this.ctrlKey = options.ctrlKey ?? false;
976+
this.metaKey = options.metaKey ?? false;
977+
this.shiftKey = options.shiftKey ?? false;
978+
}
979+
980+
get keyCode(): number {
981+
return this.#keyCode;
982+
}
983+
984+
getModifierState(key: string): boolean {
985+
switch (key) {
986+
case 'Alt':
987+
return this.altKey;
988+
case 'Control':
989+
return this.ctrlKey;
990+
case 'Meta':
991+
return this.metaKey;
992+
case 'Shift':
993+
return this.shiftKey;
994+
default:
995+
return false;
996+
}
997+
}
998+
}
999+
1000+
export function nativeKeyCode(event: { key: string; code?: string }): number {
1001+
if (/^[a-z]$/i.test(event.key)) {
1002+
return event.key.toUpperCase().charCodeAt(0);
1003+
}
1004+
if (/^[0-9]$/.test(event.key)) {
1005+
return event.key.charCodeAt(0);
1006+
}
1007+
if (event.code && /^Digit[0-9]$/.test(event.code)) {
1008+
return event.code.charCodeAt(5);
1009+
}
1010+
return fixedKeyCodes.get(event.key) ?? 0;
1011+
}
9311012
/* HTML */
9321013

9331014
export function createHTMLDocument(notify: Node['notify'], title?: string): HTMLDocument {
@@ -1138,7 +1219,9 @@ export class HTMLAnchorElement extends mixin(HTMLElement, [Focusable]) {}
11381219

11391220
export class HTMLButtonElement extends mixin(HTMLElement, [Disablable, Focusable]) {}
11401221

1141-
export class HTMLInputElement extends mixin(HTMLElement, [Disablable, Focusable]) {}
1222+
export class HTMLInputElement extends mixin(HTMLElement, [Disablable, Focusable]) {
1223+
value = '';
1224+
}
11421225

11431226
export class HTMLSelectElement extends mixin(HTMLElement, [Disablable, Focusable]) {}
11441227

@@ -1260,7 +1343,7 @@ function transitionActiveElement(document: Document, to: Element | null): void {
12601343
}
12611344
}
12621345

1263-
function isFocusable(element: HTMLElement): boolean {
1346+
export function isFocusable(element: HTMLElement): boolean {
12641347
if (!element.isConnected) {
12651348
return false;
12661349
}
@@ -1614,3 +1697,24 @@ interface DispatchState {
16141697
const eventStates = new WeakMap<Event, DispatchState>();
16151698
const eventRegistries = new WeakMap<EventTarget, ListenerRegistry>();
16161699
const eventHosts = new WeakMap<Document, EventTarget>();
1700+
1701+
const fixedKeyCodes = new Map<string, number>([
1702+
['Backspace', 8],
1703+
['Tab', 9],
1704+
['Enter', 13],
1705+
['Shift', 16],
1706+
['Control', 17],
1707+
['Alt', 18],
1708+
['CapsLock', 20],
1709+
['Escape', 27],
1710+
[' ', 32],
1711+
['PageUp', 33],
1712+
['PageDown', 34],
1713+
['End', 35],
1714+
['Home', 36],
1715+
['ArrowLeft', 37],
1716+
['ArrowUp', 38],
1717+
['ArrowRight', 39],
1718+
['ArrowDown', 40],
1719+
['Delete', 46],
1720+
]);

0 commit comments

Comments
 (0)