@@ -2,8 +2,8 @@ import { afterEach, describe, expect, test } from "bun:test";
22 import { cel } from "./cel.js";
33 import { MockTerminal } from "./terminal.js";
44 import { Text } from "./primitives/text.js";
5-import { VStack } from "./primitives/stacks.js";
6-import { HStack } from "./primitives/stacks.js";
5+import { TextInput } from "./primitives/text-input.js";
6+import { VStack, HStack } from "./primitives/stacks.js";
77
88 describe("cel end-to-end", () => {
99 let term: MockTerminal;
@@ -204,4 +204,232 @@ describe("cel end-to-end", () => {
204204 expect(scrollOffset).toBe(1);
205205 });
206206 });
207+
208+ describe("focus system", () => {
209+ test("Tab moves focus to next focusable element", async () => {
210+ const term = setup(20, 5);
211+ const focused: string[] = [];
212+ let focus1 = false;
213+ let focus2 = false;
214+
215+ cel.viewport(() =>
216+ VStack({ width: 20, height: 5 }, [
217+ HStack(
218+ {
219+ onClick: () => {},
220+ focused: focus1,
221+ onFocus: () => {
222+ focus1 = true;
223+ focus2 = false;
224+ focused.push("btn1");
225+ },
226+ onBlur: () => {
227+ focus1 = false;
228+ },
229+ },
230+ [Text("Btn1")],
231+ ),
232+ HStack(
233+ {
234+ onClick: () => {},
235+ focused: focus2,
236+ onFocus: () => {
237+ focus2 = true;
238+ focus1 = false;
239+ focused.push("btn2");
240+ },
241+ onBlur: () => {
242+ focus2 = false;
243+ },
244+ },
245+ [Text("Btn2")],
246+ ),
247+ ]),
248+ );
249+ await waitForRender();
250+
251+ // Tab to focus first element
252+ term.sendInput("\t");
253+ await waitForRender();
254+ expect(focused).toEqual(["btn1"]);
255+
256+ // Tab to focus second element
257+ term.sendInput("\t");
258+ await waitForRender();
259+ expect(focused).toEqual(["btn1", "btn2"]);
260+ });
261+
262+ test("Shift+Tab moves focus backwards", async () => {
263+ const term = setup(20, 5);
264+ let focus1 = false;
265+ let focus2 = true;
266+ const focused: string[] = [];
267+
268+ cel.viewport(() =>
269+ VStack({ width: 20, height: 5 }, [
270+ HStack(
271+ {
272+ onClick: () => {},
273+ focused: focus1,
274+ onFocus: () => {
275+ focus1 = true;
276+ focus2 = false;
277+ focused.push("btn1");
278+ },
279+ onBlur: () => {
280+ focus1 = false;
281+ },
282+ },
283+ [Text("Btn1")],
284+ ),
285+ HStack(
286+ {
287+ onClick: () => {},
288+ focused: focus2,
289+ onFocus: () => {
290+ focus2 = true;
291+ focus1 = false;
292+ focused.push("btn2");
293+ },
294+ onBlur: () => {
295+ focus2 = false;
296+ },
297+ },
298+ [Text("Btn2")],
299+ ),
300+ ]),
301+ );
302+ await waitForRender();
303+
304+ // Shift+Tab from btn2 should focus btn1
305+ term.sendInput("\x1b[Z");
306+ await waitForRender();
307+ expect(focused).toEqual(["btn1"]);
308+ });
309+
310+ test("Escape unfocuses the current element", async () => {
311+ const term = setup(20, 5);
312+ let blurred = false;
313+ let btnFocused = true;
314+
315+ cel.viewport(() =>
316+ VStack({ width: 20, height: 5 }, [
317+ HStack(
318+ {
319+ onClick: () => {},
320+ focused: btnFocused,
321+ onBlur: () => {
322+ blurred = true;
323+ btnFocused = false;
324+ },
325+ },
326+ [Text("Btn")],
327+ ),
328+ ]),
329+ );
330+ await waitForRender();
331+
332+ term.sendInput("\x1b");
333+ await waitForRender();
334+ expect(blurred).toBe(true);
335+ });
336+
337+ test("Enter activates focused clickable container", async () => {
338+ const term = setup(20, 5);
339+ let clicked = false;
340+
341+ cel.viewport(() =>
342+ VStack({ width: 20, height: 5 }, [
343+ HStack(
344+ {
345+ onClick: () => {
346+ clicked = true;
347+ },
348+ focused: true,
349+ },
350+ [Text("Btn")],
351+ ),
352+ ]),
353+ );
354+ await waitForRender();
355+
356+ term.sendInput("\r");
357+ await waitForRender();
358+ expect(clicked).toBe(true);
359+ });
360+
361+ test("mouse click on focusable element fires onFocus", async () => {
362+ const term = setup(20, 5);
363+ let btnFocused = false;
364+
365+ cel.viewport(() =>
366+ VStack({ width: 20, height: 5 }, [
367+ HStack(
368+ {
369+ onClick: () => {},
370+ focused: btnFocused,
371+ onFocus: () => {
372+ btnFocused = true;
373+ },
374+ },
375+ [Text("Btn")],
376+ ),
377+ ]),
378+ );
379+ await waitForRender();
380+
381+ // Click on the button at (1, 0)
382+ term.sendInput("\x1b[<0;2;1m");
383+ await waitForRender();
384+ expect(btnFocused).toBe(true);
385+ });
386+
387+ test("Tab wraps around to first element", async () => {
388+ const term = setup(20, 5);
389+ let focus1 = false;
390+ let focus2 = true;
391+ const focused: string[] = [];
392+
393+ cel.viewport(() =>
394+ VStack({ width: 20, height: 5 }, [
395+ HStack(
396+ {
397+ onClick: () => {},
398+ focused: focus1,
399+ onFocus: () => {
400+ focus1 = true;
401+ focus2 = false;
402+ focused.push("btn1");
403+ },
404+ onBlur: () => {
405+ focus1 = false;
406+ },
407+ },
408+ [Text("Btn1")],
409+ ),
410+ HStack(
411+ {
412+ onClick: () => {},
413+ focused: focus2,
414+ onFocus: () => {
415+ focus2 = true;
416+ focus1 = false;
417+ focused.push("btn2");
418+ },
419+ onBlur: () => {
420+ focus2 = false;
421+ },
422+ },
423+ [Text("Btn2")],
424+ ),
425+ ]),
426+ );
427+ await waitForRender();
428+
429+ // Tab from btn2 (last) should wrap to btn1
430+ term.sendInput("\t");
431+ await waitForRender();
432+ expect(focused).toEqual(["btn1"]);
433+ });
434+ });
207435 });