@@ -975,4 +975,434 @@ describe("cel end-to-end", () => {
975975 expect(row0).toBe("line2");
976976 });
977977 });
978+
979+ describe("uncontrolled focus", () => {
980+ test("Tab focuses first clickable element and Enter activates it", async () => {
981+ const term = setup(20, 5);
982+ let clicked = false;
983+
984+ cel.viewport(() =>
985+ VStack({ width: 20, height: 5 }, [
986+ HStack(
987+ {
988+ onClick: () => {
989+ clicked = true;
990+ },
991+ },
992+ [Text("Btn")],
993+ ),
994+ ]),
995+ );
996+ await waitForRender();
997+
998+ // Tab to focus the button
999+ term.sendInput("\t");
1000+ await waitForRender();
1001+
1002+ // Enter to activate
1003+ term.sendInput("\r");
1004+ await waitForRender();
1005+
1006+ expect(clicked).toBe(true);
1007+ });
1008+
1009+ test("Tab cycles through focusable elements in order", async () => {
1010+ const term = setup(20, 5);
1011+ const clicks: string[] = [];
1012+
1013+ cel.viewport(() =>
1014+ VStack({ width: 20, height: 5 }, [
1015+ HStack({ onClick: () => clicks.push("btn1") }, [Text("Btn1")]),
1016+ HStack({ onClick: () => clicks.push("btn2") }, [Text("Btn2")]),
1017+ ]),
1018+ );
1019+ await waitForRender();
1020+
1021+ // Tab to first, activate
1022+ term.sendInput("\t");
1023+ await waitForRender();
1024+ term.sendInput("\r");
1025+ await waitForRender();
1026+ expect(clicks).toEqual(["btn1"]);
1027+
1028+ // Tab to second, activate
1029+ term.sendInput("\t");
1030+ await waitForRender();
1031+ term.sendInput("\r");
1032+ await waitForRender();
1033+ expect(clicks).toEqual(["btn1", "btn2"]);
1034+ });
1035+
1036+ test("Shift+Tab moves focus backwards", async () => {
1037+ const term = setup(20, 5);
1038+ const clicks: string[] = [];
1039+
1040+ cel.viewport(() =>
1041+ VStack({ width: 20, height: 5 }, [
1042+ HStack({ onClick: () => clicks.push("btn1") }, [Text("Btn1")]),
1043+ HStack({ onClick: () => clicks.push("btn2") }, [Text("Btn2")]),
1044+ ]),
1045+ );
1046+ await waitForRender();
1047+
1048+ // Shift+Tab to focus last element
1049+ term.sendInput("\x1b[Z");
1050+ await waitForRender();
1051+ term.sendInput("\r");
1052+ await waitForRender();
1053+ expect(clicks).toEqual(["btn2"]);
1054+
1055+ // Shift+Tab to go backward to first
1056+ term.sendInput("\x1b[Z");
1057+ await waitForRender();
1058+ term.sendInput("\r");
1059+ await waitForRender();
1060+ expect(clicks).toEqual(["btn2", "btn1"]);
1061+ });
1062+
1063+ test("Escape unfocuses and Enter does nothing", async () => {
1064+ const term = setup(20, 5);
1065+ let clicked = false;
1066+
1067+ cel.viewport(() =>
1068+ VStack({ width: 20, height: 5 }, [
1069+ HStack(
1070+ {
1071+ onClick: () => {
1072+ clicked = true;
1073+ },
1074+ },
1075+ [Text("Btn")],
1076+ ),
1077+ ]),
1078+ );
1079+ await waitForRender();
1080+
1081+ // Tab to focus, then Escape to unfocus
1082+ term.sendInput("\t");
1083+ await waitForRender();
1084+ term.sendInput("\x1b");
1085+ await waitForRender();
1086+
1087+ // Enter should do nothing — no element is focused
1088+ term.sendInput("\r");
1089+ await waitForRender();
1090+ expect(clicked).toBe(false);
1091+ });
1092+
1093+ test("mouse click focuses element and Enter activates it", async () => {
1094+ const term = setup(20, 5);
1095+ let clicked = false;
1096+
1097+ cel.viewport(() =>
1098+ VStack({ width: 20, height: 5 }, [
1099+ HStack({}, [Text("not clickable")]),
1100+ HStack(
1101+ {
1102+ onClick: () => {
1103+ clicked = true;
1104+ },
1105+ },
1106+ [Text("Clickable")],
1107+ ),
1108+ ]),
1109+ );
1110+ await waitForRender();
1111+
1112+ // Click on the second element (row 1)
1113+ term.sendInput("\x1b[<0;2;2m");
1114+ await waitForRender();
1115+
1116+ // Enter should activate the clicked element
1117+ term.sendInput("\r");
1118+ await waitForRender();
1119+ expect(clicked).toBe(true);
1120+ });
1121+
1122+ test("onFocus fires as notification in uncontrolled mode", async () => {
1123+ const term = setup(20, 5);
1124+ const events: string[] = [];
1125+
1126+ cel.viewport(() =>
1127+ VStack({ width: 20, height: 5 }, [
1128+ HStack(
1129+ {
1130+ onClick: () => {},
1131+ onFocus: () => events.push("focus"),
1132+ },
1133+ [Text("Btn")],
1134+ ),
1135+ ]),
1136+ );
1137+ await waitForRender();
1138+
1139+ term.sendInput("\t");
1140+ await waitForRender();
1141+ expect(events).toEqual(["focus"]);
1142+ });
1143+
1144+ test("onBlur fires as notification in uncontrolled mode", async () => {
1145+ const term = setup(20, 5);
1146+ const events: string[] = [];
1147+
1148+ cel.viewport(() =>
1149+ VStack({ width: 20, height: 5 }, [
1150+ HStack(
1151+ {
1152+ onClick: () => {},
1153+ onFocus: () => events.push("focus1"),
1154+ onBlur: () => events.push("blur1"),
1155+ },
1156+ [Text("Btn1")],
1157+ ),
1158+ HStack(
1159+ {
1160+ onClick: () => {},
1161+ onFocus: () => events.push("focus2"),
1162+ },
1163+ [Text("Btn2")],
1164+ ),
1165+ ]),
1166+ );
1167+ await waitForRender();
1168+
1169+ // Tab to first
1170+ term.sendInput("\t");
1171+ await waitForRender();
1172+ expect(events).toEqual(["focus1"]);
1173+
1174+ // Tab to second — first should get onBlur
1175+ term.sendInput("\t");
1176+ await waitForRender();
1177+ expect(events).toEqual(["focus1", "blur1", "focus2"]);
1178+ });
1179+
1180+ test("Tab focuses uncontrolled TextInput and typing works", async () => {
1181+ const term = setup(20, 5);
1182+ let value = "";
1183+
1184+ cel.viewport(() =>
1185+ VStack({ width: 20, height: 5 }, [
1186+ TextInput({
1187+ value,
1188+ onChange: (v) => {
1189+ value = v;
1190+ },
1191+ }),
1192+ ]),
1193+ );
1194+ await waitForRender();
1195+
1196+ // Tab to focus the TextInput
1197+ term.sendInput("\t");
1198+ await waitForRender();
1199+
1200+ // Type a character
1201+ term.sendInput("x");
1202+ await waitForRender();
1203+ expect(value).toBe("x");
1204+ });
1205+
1206+ test("Tab wraps from last to first element", async () => {
1207+ const term = setup(20, 5);
1208+ const clicks: string[] = [];
1209+
1210+ cel.viewport(() =>
1211+ VStack({ width: 20, height: 5 }, [
1212+ HStack({ onClick: () => clicks.push("btn1") }, [Text("Btn1")]),
1213+ HStack({ onClick: () => clicks.push("btn2") }, [Text("Btn2")]),
1214+ ]),
1215+ );
1216+ await waitForRender();
1217+
1218+ // Tab to first, Tab to second, Tab wraps to first
1219+ term.sendInput("\t");
1220+ await waitForRender();
1221+ term.sendInput("\t");
1222+ await waitForRender();
1223+ term.sendInput("\t");
1224+ await waitForRender();
1225+ term.sendInput("\r");
1226+ await waitForRender();
1227+ expect(clicks).toEqual(["btn1"]);
1228+ });
1229+
1230+ test("mixed controlled and uncontrolled elements", async () => {
1231+ const term = setup(20, 5);
1232+ const clicks: string[] = [];
1233+ let controlledFocused = false;
1234+
1235+ cel.viewport(() =>
1236+ VStack({ width: 20, height: 5 }, [
1237+ HStack(
1238+ {
1239+ onClick: () => clicks.push("controlled"),
1240+ focused: controlledFocused,
1241+ onFocus: () => {
1242+ controlledFocused = true;
1243+ },
1244+ onBlur: () => {
1245+ controlledFocused = false;
1246+ },
1247+ },
1248+ [Text("Controlled")],
1249+ ),
1250+ HStack({ onClick: () => clicks.push("uncontrolled") }, [
1251+ Text("Uncontrolled"),
1252+ ]),
1253+ ]),
1254+ );
1255+ await waitForRender();
1256+
1257+ // Tab to first (controlled)
1258+ term.sendInput("\t");
1259+ await waitForRender();
1260+ expect(controlledFocused).toBe(true);
1261+
1262+ // Tab to second (uncontrolled), Enter to activate
1263+ term.sendInput("\t");
1264+ await waitForRender();
1265+ expect(controlledFocused).toBe(false);
1266+ term.sendInput("\r");
1267+ await waitForRender();
1268+ expect(clicks).toEqual(["uncontrolled"]);
1269+ });
1270+ });
1271+
1272+ describe("focusStyle rendering", () => {
1273+ test("focusStyle bgColor applied when element is focused via Tab", async () => {
1274+ const term = setup(20, 3);
1275+
1276+ cel.viewport(() =>
1277+ VStack({ width: 20, height: 3 }, [
1278+ HStack(
1279+ {
1280+ onClick: () => {},
1281+ bgColor: "black",
1282+ focusStyle: { bgColor: "cyan" },
1283+ },
1284+ [Text("Btn")],
1285+ ),
1286+ ]),
1287+ );
1288+ await waitForRender();
1289+
1290+ // Before focus — normal bgColor
1291+ let buf = cel._getBuffer()!;
1292+ expect(buf.get(0, 0).bgColor).toBe("black");
1293+
1294+ // Tab to focus
1295+ term.sendInput("\t");
1296+ await waitForRender();
1297+
1298+ // After focus — focusStyle bgColor
1299+ buf = cel._getBuffer()!;
1300+ expect(buf.get(0, 0).bgColor).toBe("cyan");
1301+ });
1302+
1303+ test("focusStyle removed when focus moves to another element", async () => {
1304+ const term = setup(20, 3);
1305+
1306+ cel.viewport(() =>
1307+ VStack({ width: 20, height: 3 }, [
1308+ HStack(
1309+ {
1310+ onClick: () => {},
1311+ bgColor: "black",
1312+ focusStyle: { bgColor: "cyan" },
1313+ },
1314+ [Text("Btn1")],
1315+ ),
1316+ HStack(
1317+ {
1318+ onClick: () => {},
1319+ bgColor: "black",
1320+ focusStyle: { bgColor: "yellow" },
1321+ },
1322+ [Text("Btn2")],
1323+ ),
1324+ ]),
1325+ );
1326+ await waitForRender();
1327+
1328+ // Tab to first
1329+ term.sendInput("\t");
1330+ await waitForRender();
1331+ let buf = cel._getBuffer()!;
1332+ expect(buf.get(0, 0).bgColor).toBe("cyan");
1333+ expect(buf.get(0, 1).bgColor).toBe("black");
1334+
1335+ // Tab to second — first loses focusStyle, second gains it
1336+ term.sendInput("\t");
1337+ await waitForRender();
1338+ buf = cel._getBuffer()!;
1339+ expect(buf.get(0, 0).bgColor).toBe("black");
1340+ expect(buf.get(0, 1).bgColor).toBe("yellow");
1341+ });
1342+
1343+ test("focusStyle fgColor inherited by child Text when focused", async () => {
1344+ const term = setup(20, 3);
1345+
1346+ cel.viewport(() =>
1347+ VStack({ width: 20, height: 3 }, [
1348+ HStack(
1349+ {
1350+ onClick: () => {},
1351+ fgColor: "white",
1352+ focusStyle: { fgColor: "black", bgColor: "cyan" },
1353+ },
1354+ [Text("Btn")],
1355+ ),
1356+ ]),
1357+ );
1358+ await waitForRender();
1359+
1360+ // Before focus — Text inherits white
1361+ let buf = cel._getBuffer()!;
1362+ expect(buf.get(0, 0).fgColor).toBe("white");
1363+
1364+ // Tab to focus
1365+ term.sendInput("\t");
1366+ await waitForRender();
1367+
1368+ // After focus — Text inherits black from focusStyle
1369+ buf = cel._getBuffer()!;
1370+ expect(buf.get(0, 0).fgColor).toBe("black");
1371+ });
1372+
1373+ test("focusStyle with controlled focus", async () => {
1374+ const term = setup(20, 3);
1375+ let isFocused = true;
1376+
1377+ cel.viewport(() =>
1378+ VStack({ width: 20, height: 3 }, [
1379+ HStack(
1380+ {
1381+ onClick: () => {},
1382+ focused: isFocused,
1383+ bgColor: "black",
1384+ focusStyle: { bgColor: "cyan" },
1385+ onBlur: () => {
1386+ isFocused = false;
1387+ },
1388+ },
1389+ [Text("Btn")],
1390+ ),
1391+ ]),
1392+ );
1393+ await waitForRender();
1394+
1395+ // Controlled focused=true — focusStyle applies
1396+ let buf = cel._getBuffer()!;
1397+ expect(buf.get(0, 0).bgColor).toBe("cyan");
1398+
1399+ // Escape to blur
1400+ term.sendInput("\x1b");
1401+ await waitForRender();
1402+
1403+ // focused=false — normal style
1404+ buf = cel._getBuffer()!;
1405+ expect(buf.get(0, 0).bgColor).toBe("black");
1406+ });
1407+ });
9781408 });