xonecas.io
70160
Ce

cel-tui / commit 2f123dc

feat(core): implement full onKeyPress bubbling from focused element

Keys now bubble up from the focused element through all ancestors,
not just the root. The nearest ancestor with onKeyPress handles it.

- findPathTo() builds root-to-target path via depth-first search
- handleKeyEvent() uses the focused element's path for bubbling
- Falls back to root-only path when nothing is focused
- Fixed findFocusedTextInput() to not match focused containers
  (was crashing when a focused HStack was treated as TextInput)

xonecas2026-04-03 2f123dc25e301422dfac50813bb7b584e6e10c06 parent 4736c2d

3 files changed

TODO.md+1−1
@@ -53,7 +53,7 @@ What's implemented, what's missing, and what needs hardening. Reference `spec.md
5353 - ✅ Scroll routing (innermost scrollable, fires `onScroll`)
5454 - ✅ Key routing to `onKeyPress` handlers
5555 - ✅ TextInput key consumption (editing keys consumed, modifiers bubble)
56-- 🔧 `onKeyPress` bubbling — currently only checks the root container, should walk up from focused element through all ancestors
56+- ✅ `onKeyPress` bubbling — walks up from focused element through all ancestors
5757 - ✅ Focus traversal (Tab/Shift+Tab cycling through focusable elements)
5858 - ✅ Escape to unfocus
5959 - ✅ Enter to activate focused clickable container
packages/core/src/cel.test.ts+132−0
@@ -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 });
packages/core/src/cel.ts+42−2
@@ -350,7 +350,23 @@ function handleKeyEvent(key: string): void {
350350 }
351351 }
352352
353- // Key not consumed by TextInput — bubble through layers
353+ // Key not consumed by TextInput — bubble up from focused element
354+ const focused = findFocusedElement();
355+ if (focused) {
356+ for (let i = currentLayouts.length - 1; i >= 0; i--) {
357+ const path = findPathTo(currentLayouts[i]!, focused);
358+ if (path) {
359+ const handler = findKeyPressHandler(path);
360+ if (handler) {
361+ handler.handler(key);
362+ cel.render();
363+ return;
364+ }
365+ }
366+ }
367+ }
368+
369+ // No focused element — try root onKeyPress on topmost layer
354370 for (let i = currentLayouts.length - 1; i >= 0; i--) {
355371 const layoutRoot = currentLayouts[i]!;
356372 const path = [layoutRoot];
@@ -363,9 +379,33 @@ function handleKeyEvent(key: string): void {
363379 }
364380 }
365381
382+/**
383+ * Build the path from root to a target node (depth-first search).
384+ * Returns the path array [root, ..., target] or null if not found.
385+ */
386+function findPathTo(root: LayoutNode, target: LayoutNode): LayoutNode[] | null {
387+ if (root === target || root.node === target.node) return [root];
388+ for (const child of root.children) {
389+ const childPath = findPathTo(child, target);
390+ if (childPath) return [root, ...childPath];
391+ }
392+ return null;
393+}
394+
366395 function findFocusedTextInput(): LayoutNode | null {
367396 for (let i = currentLayouts.length - 1; i >= 0; i--) {
368- const found = findFocusedInTree(currentLayouts[i]!);
397+ const found = findFocusedTextInputInTree(currentLayouts[i]!);
398+ if (found) return found;
399+ }
400+ return null;
401+}
402+
403+function findFocusedTextInputInTree(ln: LayoutNode): LayoutNode | null {
404+ if (ln.node.type === "textinput" && ln.node.props.focused) {
405+ return ln;
406+ }
407+ for (const child of ln.children) {
408+ const found = findFocusedTextInputInTree(child);
369409 if (found) return found;
370410 }
371411 return null;