@@ -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", () => {