Skip to content

Commit 4f8fb36

Browse files
committed
✨ keyboard: dispatch key events to focused elements
1 parent b7c337f commit 4f8fb36

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import type { KeyDown, KeyEvent } from '@bomb.sh/tty';
2+
import type { Node } from './core.ts';
3+
import { describe, expect, test } from 'vitest';
4+
import { DispatchApi } from './dispatch.ts';
5+
import { FocusApi } from './focus.ts';
6+
import { createHost, type Host } from './host.ts';
7+
import { KeyboardApi } from './keyboard.ts';
8+
9+
describe('keyboard dispatch', () => {
10+
test('routes keydown, keyup, and keyrepeat to the currently focused node', () => {
11+
const host = setupKeyboardHost();
12+
const first = createFocusable(host);
13+
const second = createFocusable(host);
14+
const firstEvents: RecordedKeyEvent[] = [];
15+
const secondEvents: RecordedKeyEvent[] = [];
16+
recordKeyboard(first, firstEvents);
17+
recordKeyboard(second, secondEvents);
18+
setFocus(second);
19+
20+
const events: KeyEvent[] = [
21+
{ type: 'keydown', key: 'a', code: 'a', text: 'a' },
22+
{ type: 'keyup', key: 'a', code: 'a' },
23+
{ type: 'keyrepeat', key: 'a', code: 'a', text: 'a' },
24+
];
25+
for (const event of events) {
26+
expect(DispatchApi.at(host.root).dispatch(event).ok).toBe(true);
27+
}
28+
29+
expect(firstEvents).toEqual([]);
30+
expect(secondEvents.map(({ node }) => node)).toEqual([second, second, second]);
31+
for (const [index, event] of events.entries()) {
32+
expect(secondEvents[index]?.event).toBe(event);
33+
}
34+
});
35+
36+
test('returns ordinary dispatch success without a handled payload', () => {
37+
const host = setupKeyboardHost();
38+
const target = createFocusable(host);
39+
recordKeyboard(target, []);
40+
41+
expect(DispatchApi.at(host.root).dispatch(keydown('a', 'a'))).toEqual({ ok: true });
42+
});
43+
44+
test('delegates non-key and malformed key-like events unchanged', () => {
45+
const host = setupKeyboardHost();
46+
const delegated: unknown[] = [];
47+
DispatchApi.decorate(host.root, () => ({
48+
dispatch(event) {
49+
delegated.push(event);
50+
return super.dispatch(event);
51+
},
52+
}));
53+
54+
DispatchApi.at(host.root).dispatch(keydown('a', 'a'));
55+
const others: unknown[] = [
56+
{ type: 'resize', width: 80, height: 24 },
57+
{ type: 'keydown', code: 'a' },
58+
{ type: 'keydown', key: 'a' },
59+
{ type: 'keypress', key: 'a', code: 'a' },
60+
];
61+
for (const event of others) {
62+
DispatchApi.at(host.root).dispatch(event);
63+
}
64+
65+
expect(delegated).toEqual(others);
66+
});
67+
});
68+
69+
interface RecordedKeyEvent {
70+
node: Node;
71+
event: KeyEvent;
72+
}
73+
74+
function setupKeyboardHost(): Host {
75+
const host = createHost();
76+
DispatchApi.init(host.root);
77+
FocusApi.init(host.root);
78+
KeyboardApi.init(host.root);
79+
return host;
80+
}
81+
82+
function createFocusable(host: Host): Node {
83+
const element = host.createElement('box');
84+
host.insertBefore(host.element, element);
85+
setFocusable(element.node!);
86+
return element.node!;
87+
}
88+
89+
function setFocus(node: Node): void {
90+
FocusApi.at(node).setFocus(node);
91+
}
92+
93+
function setFocusable(node: Node): void {
94+
FocusApi.at(node).setFocusable();
95+
}
96+
97+
function keydown(code: KeyDown['code'], text?: string): KeyDown {
98+
return {
99+
type: 'keydown',
100+
key: text ?? code,
101+
code,
102+
...(text === undefined ? {} : { text }),
103+
};
104+
}
105+
106+
function recordKeyboard(node: Node, events: RecordedKeyEvent[]): void {
107+
KeyboardApi.decorate(node, () => ({
108+
keydown(event) {
109+
events.push({ node: this.node, event });
110+
},
111+
keyup(event) {
112+
events.push({ node: this.node, event });
113+
},
114+
keyrepeat(event) {
115+
events.push({ node: this.node, event });
116+
},
117+
}));
118+
}

‎packages/ui/src/keyboard.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// oxlint-disable no-unused-vars
2+
import type { KeyDown, KeyEvent, KeyRepeat, KeyUp } from '@bomb.sh/tty';
3+
import { DispatchApi } from './dispatch.ts';
4+
import { FocusApi } from './focus.ts';
5+
import { Api } from './core/api.ts';
6+
import type { Node } from './core/node.ts';
7+
8+
export class KeyboardApi extends Api {
9+
constructor(node: Node) {
10+
super(node);
11+
12+
// re-dispatch key events to the current focus
13+
DispatchApi.decorate(node, () => ({
14+
dispatch(event) {
15+
if (isKeyEvent(event)) {
16+
const focus = FocusApi.at(this.node).getFocus();
17+
const api = KeyboardApi.at(focus);
18+
19+
api[event.type](event as KeyDown & KeyUp & KeyRepeat);
20+
21+
return { ok: true };
22+
} else {
23+
return super.dispatch(event);
24+
}
25+
},
26+
}));
27+
}
28+
29+
keydown(_event: KeyDown): void {}
30+
keyup(_event: KeyUp): void {}
31+
keyrepeat(_event: KeyRepeat): void {}
32+
}
33+
34+
function isKeyEvent(value: unknown): value is KeyEvent {
35+
const x = value as KeyEvent;
36+
37+
return (
38+
!!x &&
39+
typeof x.key === 'string' &&
40+
typeof x.code === 'string' &&
41+
['keyup', 'keydown', 'keyrepeat'].includes(x.type)
42+
);
43+
}

0 commit comments

Comments
 (0)