@@ -895,6 +895,84 @@ function renderOverlay(state: AppState): Node {
895895 );
896896 }
897897
898+// ---------------------------------------------------------------------------
899+// Input area
900+// ---------------------------------------------------------------------------
901+
902+/** Stable callbacks for the controlled TextInput. */
903+export interface InputController {
904+ /** Update the controlled input value and re-render. */
905+ onChange: (value: string) => void;
906+ /** Mark the input as focused and re-render. */
907+ onFocus: () => void;
908+ /** Mark the input as blurred and re-render. */
909+ onBlur: () => void;
910+ /** Intercept submit/autocomplete keys before default editing runs. */
911+ onKeyPress: (key: string) => boolean | undefined;
912+}
913+
914+/**
915+ * Create stable handlers for the main TextInput.
916+ *
917+ * cel-tui keys TextInput cursor/scroll state by the `onChange` function
918+ * reference, so these callbacks must be created once and reused across
919+ * renders.
920+ *
921+ * @param state - Application state used by the handlers.
922+ * @returns Stable callbacks for the controlled TextInput.
923+ */
924+export function createInputController(state: AppState): InputController {
925+ return {
926+ onChange: (value) => {
927+ inputValue = value;
928+ cel.render();
929+ },
930+ onFocus: () => {
931+ inputFocused = true;
932+ cel.render();
933+ },
934+ onBlur: () => {
935+ inputFocused = false;
936+ cel.render();
937+ },
938+ onKeyPress: (key) => {
939+ if (key === "enter") {
940+ const raw = inputValue;
941+ inputValue = "";
942+ cel.render();
943+ handleInput(raw, state);
944+ return false;
945+ }
946+ if (key === "tab" && inputValue.startsWith("/")) {
947+ showCommandAutocomplete(state);
948+ return false;
949+ }
950+ },
951+ };
952+}
953+
954+/**
955+ * Render the padded input area.
956+ *
957+ * @param controller - Stable TextInput callbacks.
958+ * @returns The input area node.
959+ */
960+export function renderInputArea(controller: InputController): Node {
961+ return VStack({ padding: { x: 1 } }, [
962+ TextInput({
963+ flex: 1,
964+ maxHeight: 10,
965+ value: inputValue,
966+ onChange: controller.onChange,
967+ placeholder: Text("message…", { fgColor: "color08" }),
968+ focused: inputFocused,
969+ onFocus: controller.onFocus,
970+ onBlur: controller.onBlur,
971+ onKeyPress: controller.onKeyPress,
972+ }),
973+ ]);
974+}
975+
898976 // ---------------------------------------------------------------------------
899977 // Command handlers
900978 // ---------------------------------------------------------------------------
@@ -1628,6 +1706,7 @@ async function gracefulExit(state: AppState): Promise<void> {
16281706 */
16291707 export function startUI(state: AppState): void {
16301708 const terminal = new ProcessTerminal();
1709+ const inputController = createInputController(state);
16311710 cel.init(terminal);
16321711
16331712 cel.viewport(() => {
@@ -1673,40 +1752,7 @@ export function startUI(state: AppState): void {
16731752 renderDivider(state, cols),
16741753
16751754 // ── Input area ──
1676- VStack({ padding: { x: 1 } }, [
1677- TextInput({
1678- flex: 1,
1679- maxHeight: 10,
1680- value: inputValue,
1681- onChange: (value) => {
1682- inputValue = value;
1683- cel.render();
1684- },
1685- placeholder: Text("message…", { fgColor: "color08" }),
1686- focused: inputFocused,
1687- onFocus: () => {
1688- inputFocused = true;
1689- cel.render();
1690- },
1691- onBlur: () => {
1692- inputFocused = false;
1693- cel.render();
1694- },
1695- onKeyPress: (key) => {
1696- if (key === "enter") {
1697- const raw = inputValue;
1698- inputValue = "";
1699- cel.render();
1700- handleInput(raw, state);
1701- return false;
1702- }
1703- if (key === "tab" && inputValue.startsWith("/")) {
1704- showCommandAutocomplete(state);
1705- return false;
1706- }
1707- },
1708- }),
1709- ]),
1755+ renderInputArea(inputController),
17101756
17111757 // ── Static divider ──
17121758 Divider({ fgColor: state.theme.divider }),