xonecas.io
70160
Ce

cel-tui / commit 65483ae

fix(core): Tab/Shift+Tab continue from last focused position after Escape

After Escape unfocuses an element, Tab and Shift+Tab now continue
from the position of the element that lost focus, instead of
restarting from the first/last focusable. This fixes the perceived
'swapped' behavior where Tab went backward (to the element just
left) and Shift+Tab went forward.

Track lastFocusedIndex in changeFocus() when blurring, clear it
when focusing a new target. Tab/Shift+Tab use this index as the
starting position when nothing is currently focused.

xonecas2026-04-04 65483ae8c38510d2f72861b83624e82bb77ad85d parent dfe0a70

2 files changed

packages/core/src/cel.test.ts+113−0
@@ -751,6 +751,119 @@ describe("cel end-to-end", () => {
751751 await waitForRender();
752752 expect(btnFocused).toBe(true);
753753 });
754+
755+ test("Escape then Tab advances past the element that was unfocused", async () => {
756+ const term = setup(20, 5);
757+ let focused: string | null = "ti";
758+
759+ cel.viewport(() =>
760+ VStack({ width: 20, height: 5 }, [
761+ TextInput({
762+ value: "text",
763+ focused: focused === "ti",
764+ onChange: () => {},
765+ onFocus: () => {
766+ focused = "ti";
767+ },
768+ onBlur: () => {
769+ if (focused === "ti") focused = null;
770+ },
771+ }),
772+ HStack(
773+ {
774+ onClick: () => {},
775+ focused: focused === "btn",
776+ onFocus: () => {
777+ focused = "btn";
778+ },
779+ onBlur: () => {
780+ if (focused === "btn") focused = null;
781+ },
782+ },
783+ [Text("Btn")],
784+ ),
785+ ]),
786+ );
787+ await waitForRender();
788+ expect(focused as string | null).toBe("ti");
789+
790+ // Escape to unfocus TextInput
791+ term.sendInput("\x1b");
792+ await waitForRender();
793+ expect(focused as string | null).toBe(null);
794+
795+ // Tab should go FORWARD to the button (next after TextInput),
796+ // not back to the TextInput
797+ term.sendInput("\t");
798+ await waitForRender();
799+ expect(focused as string | null).toBe("btn");
800+ });
801+
802+ test("Escape then Shift+Tab goes backward from the unfocused element", async () => {
803+ const term = setup(20, 5);
804+ let focused: string | null = null;
805+
806+ cel.viewport(() =>
807+ VStack({ width: 20, height: 5 }, [
808+ HStack(
809+ {
810+ onClick: () => {},
811+ focused: focused === "a",
812+ onFocus: () => {
813+ focused = "a";
814+ },
815+ onBlur: () => {
816+ if (focused === "a") focused = null;
817+ },
818+ },
819+ [Text("A")],
820+ ),
821+ HStack(
822+ {
823+ onClick: () => {},
824+ focused: focused === "b",
825+ onFocus: () => {
826+ focused = "b";
827+ },
828+ onBlur: () => {
829+ if (focused === "b") focused = null;
830+ },
831+ },
832+ [Text("B")],
833+ ),
834+ HStack(
835+ {
836+ onClick: () => {},
837+ focused: focused === "c",
838+ onFocus: () => {
839+ focused = "c";
840+ },
841+ onBlur: () => {
842+ if (focused === "c") focused = null;
843+ },
844+ },
845+ [Text("C")],
846+ ),
847+ ]),
848+ );
849+ await waitForRender();
850+
851+ // Tab to B
852+ term.sendInput("\t"); // → A
853+ await waitForRender();
854+ term.sendInput("\t"); // → B
855+ await waitForRender();
856+ expect(focused as string | null).toBe("b");
857+
858+ // Escape, then Shift+Tab should go backward to A
859+ term.sendInput("\x1b");
860+ await waitForRender();
861+ expect(focused as string | null).toBe(null);
862+
863+ term.sendInput("\x1b[Z"); // Shift+Tab
864+ await waitForRender();
865+ expect(focused as string | null).toBe("a");
866+ });
754867 });
755868
756869 describe("TextInput mouse wheel scroll", () => {
packages/core/src/cel.ts+23−2
@@ -37,6 +37,7 @@ let renderScheduled = false;
3737 let prevBuffer: CellBuffer | null = null;
3838 let currentBuffer: CellBuffer | null = null;
3939 let currentLayouts: LayoutNode[] = [];
40+let lastFocusedIndex = -1;
4041
4142 function doRender(): void {
4243 renderScheduled = false;
@@ -157,8 +158,16 @@ function findFocusedElement(): LayoutNode | null {
157158 function changeFocus(target: LayoutNode | null): void {
158159 const current = findFocusedElement();
159160
160- // Blur current
161+ // Blur current — save position for Tab/Shift+Tab continuity
161162 if (current && current !== target) {
163+ const topLayer = currentLayouts[currentLayouts.length - 1];
164+ if (topLayer) {
165+ const focusables = collectFocusable(topLayer);
166+ let idx = focusables.indexOf(current);
167+ if (idx === -1)
168+ idx = focusables.findIndex((f) => f.node === current.node);
169+ lastFocusedIndex = idx;
170+ }
162171 const props =
163172 current.node.type === "textinput"
164173 ? current.node.props
@@ -170,8 +179,9 @@ function changeFocus(target: LayoutNode | null): void {
170179 }
171180 }
172181
173- // Focus new target
182+ // Focus new target — clear saved position
174183 if (target && target !== current) {
184+ lastFocusedIndex = -1;
175185 const props =
176186 target.node.type === "textinput"
177187 ? target.node.props
@@ -331,6 +341,16 @@ function handleKeyEvent(key: string, rawData?: string): void {
331341 currentIdx = focusables.findIndex((f) => f.node === current.node);
332342 }
333343
344+ // If still not found, use the last focused element's position
345+ // so Tab/Shift+Tab continues from where focus was lost (e.g. after Escape)
346+ if (
347+ currentIdx === -1 &&
348+ lastFocusedIndex >= 0 &&
349+ lastFocusedIndex < focusables.length
350+ ) {
351+ currentIdx = lastFocusedIndex;
352+ }
353+
334354 let nextIdx: number;
335355 if (key === "tab") {
336356 nextIdx = currentIdx === -1 ? 0 : (currentIdx + 1) % focusables.length;
@@ -584,6 +604,7 @@ export const cel = {
584604 currentBuffer = null;
585605 currentLayouts = [];
586606 renderScheduled = false;
607+ lastFocusedIndex = -1;
587608 },
588609
589610 /** @internal */