Skip to content

Commit 2ced7ff

Browse files
committed
✨ keyboard: dispatch key events to focused elements
1 parent 2ed552d commit 2ced7ff

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

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

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

‎packages/ui/src/keyboard.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// oxlint-disable no-unused-vars
2+
import type { KeyDown, KeyEvent, KeyRepeat, KeyUp } from '@bomb.sh/tty';
3+
import { createApi } from './core.ts';
4+
import type { Host } from './host.ts';
5+
import { DispatchApi } from './dispatch.ts';
6+
import { getFocus } from './focus.ts';
7+
8+
export function useKeyboard(host: Host): void {
9+
DispatchApi.around(host.root, {
10+
dispatch([node, event], next) {
11+
if (isKeyEvent(event)) {
12+
const focus = getFocus(node);
13+
KeyboardApi.invoke(event.type, [focus, event as KeyDown & KeyUp & KeyRepeat]);
14+
return { ok: true };
15+
} else {
16+
return next(node, event);
17+
}
18+
},
19+
});
20+
}
21+
22+
export const KeyboardApi = createApi('keyboard', {
23+
keydown(node, event: KeyDown): void {},
24+
keyup(node, event: KeyUp): void {},
25+
keyrepeat(node, event: KeyRepeat): void {},
26+
});
27+
28+
function isKeyEvent(value: unknown): value is KeyEvent {
29+
const x = value as KeyEvent;
30+
31+
return (
32+
!!x &&
33+
typeof x.key === 'string' &&
34+
typeof x.code === 'string' &&
35+
['keyup', 'keydown', 'keyrepeat'].includes(x.type)
36+
);
37+
}

0 commit comments

Comments
 (0)