@@ -432,4 +432,136 @@ describe("cel end-to-end", () => {
432432 expect(focused).toEqual(["btn1"]);
433433 });
434434 });
435+
436+ describe("onKeyPress bubbling", () => {
437+ test("key bubbles from focused element up through ancestors", async () => {
438+ const term = setup(20, 5);
439+ const received: string[] = [];
440+
441+ cel.viewport(() =>
442+ VStack(
443+ {
444+ width: 20,
445+ height: 5,
446+ onKeyPress: (key) => {
447+ received.push("root:" + key);
448+ },
449+ },
450+ [
451+ VStack(
452+ {
453+ onKeyPress: (key) => {
454+ received.push("mid:" + key);
455+ },
456+ },
457+ [
458+ HStack(
459+ {
460+ onClick: () => {},
461+ focused: true,
462+ },
463+ [Text("Focused Btn")],
464+ ),
465+ ],
466+ ),
467+ ],
468+ ),
469+ );
470+ await waitForRender();
471+
472+ // Send a key — should bubble from focused btn through mid, then root
473+ term.sendInput("x");
474+ await waitForRender();
475+
476+ // Nearest ancestor with onKeyPress is "mid", so it handles first
477+ expect(received).toEqual(["mid:x"]);
478+ });
479+
480+ test("key reaches root onKeyPress when no intermediate handlers", async () => {
481+ const term = setup(20, 5);
482+ let rootKey = "";
483+
484+ cel.viewport(() =>
485+ VStack(
486+ {
487+ width: 20,
488+ height: 5,
489+ onKeyPress: (key) => {
490+ rootKey = key;
491+ },
492+ },
493+ [
494+ HStack(
495+ {
496+ onClick: () => {},
497+ focused: true,
498+ },
499+ [Text("Btn")],
500+ ),
501+ ],
502+ ),
503+ );
504+ await waitForRender();
505+
506+ term.sendInput("y");
507+ await waitForRender();
508+ expect(rootKey).toBe("y");
509+ });
510+
511+ test("modifier key from focused TextInput bubbles to ancestors", async () => {
512+ const term = setup(20, 5);
513+ let parentKey = "";
514+ let inputValue = "hello";
515+
516+ cel.viewport(() =>
517+ VStack(
518+ {
519+ width: 20,
520+ height: 5,
521+ onKeyPress: (key) => {
522+ parentKey = key;
523+ },
524+ },
525+ [
526+ TextInput({
527+ value: inputValue,
528+ onChange: (v) => {
529+ inputValue = v;
530+ },
531+ focused: true,
532+ }),
533+ ],
534+ ),
535+ );
536+ await waitForRender();
537+
538+ // Ctrl+S is not an editing key — should bubble up
539+ term.sendInput("\x13");
540+ await waitForRender();
541+ expect(parentKey).toBe("ctrl+s");
542+ });
543+
544+ test("unfocused state bubbles from root only", async () => {
545+ const term = setup(20, 5);
546+ let rootKey = "";
547+
548+ cel.viewport(() =>
549+ VStack(
550+ {
551+ width: 20,
552+ height: 5,
553+ onKeyPress: (key) => {
554+ rootKey = key;
555+ },
556+ },
557+ [Text("no focus")],
558+ ),
559+ );
560+ await waitForRender();
561+
562+ term.sendInput("z");
563+ await waitForRender();
564+ expect(rootKey).toBe("z");
565+ });
566+ });
435567 });