Skip to content

TextBoxWidget

An editable single-line text input widget for capturing user text.

TextBox is a focusable widget that accepts keyboard input. When focused, it displays a cursor and allows users to type, navigate, select, and edit text. It's commonly used in forms, search inputs, and any scenario requiring user-provided text.

Basic Usage

Create a text input using the fluent API and handle changes with OnTextChanged:

csharp

State Binding

The TextBox doesn't store state internally. You provide the current text value and update your state in the OnTextChanged handler. This pattern gives you full control over the text content.

Event Handlers

TextBox provides two event handlers for different use cases.

OnTextChanged

Called whenever the text content changes (typing, deleting, pasting):

csharp
v.TextBox(state.Text)
    .OnTextChanged(args => {
        state.Text = args.NewText;
        Console.WriteLine($"Changed from '{args.OldText}' to '{args.NewText}'");
    })

The TextChangedEventArgs provides:

  • OldText - The text content before the change
  • NewText - The text content after the change
  • Widget - The source TextBoxWidget
  • Node - The underlying TextBoxNode
  • Context - Access to the application context

OnSubmit

Called when the user presses Enter in the text box:

csharp

The TextSubmittedEventArgs provides:

  • Text - The submitted text content
  • Widget - The source TextBoxWidget
  • Node - The underlying TextBoxNode
  • Context - Access to the application context

Use OnSubmit for:

  • Chat/message input
  • Command entry
  • Single-field forms
  • Search boxes

Keyboard Navigation

TextBox supports comprehensive keyboard navigation:

KeyAction
CharactersInsert at cursor position
BackspaceDelete character before cursor
DeleteDelete character at cursor
/ Move cursor by one character
HomeMove cursor to start of text
EndMove cursor to end of text
Shift+← / Shift+→Extend selection left/right
Shift+HomeSelect from cursor to start
Shift+EndSelect from cursor to end
Ctrl+ASelect all text
EnterSubmit (if OnSubmit handler is set)

Mouse Support

In terminals that support mouse input:

  • Click - Position cursor at click location
  • Double-click - Select all text

Focus Behavior

TextBox visually indicates its focus state:

StateAppearance
Unfocused[text content]
Focused[text▌content] with visible cursor
HoveredFaint cursor preview at mouse position
csharp

The focused TextBox shows:

  • A block cursor at the current position
  • The cursor shape changes to a blinking bar

Focus Navigation

  • Tab - Move focus to the next focusable widget
  • Shift+Tab - Move focus to the previous focusable widget

Text Selection

Users can select text using keyboard shortcuts:

csharp

Selected text is highlighted with configurable colors. Operations like typing or pressing Backspace replace the selected text.

Form Example

Here's a complete form with multiple TextBox widgets:

csharp

Unicode Support

TextBox correctly handles Unicode text including:

  • Wide characters (CJK): 日本語, 中文, 한국어
  • Emoji: 🎉 🚀 ✨
  • Combining characters: é, ñ

Navigation and deletion work on grapheme clusters, so pressing Backspace on an emoji deletes the entire emoji, not individual code points.

Try it yourself—navigate through the text with arrow keys, delete some emoji, and use the reset button to restore the original content:

csharp

Theming

Customize TextBox appearance using theme elements:

csharp
var theme = Hex1bTheme.Create()
    .Set(TextBoxTheme.CursorForegroundColor, Hex1bColor.Black)
    .Set(TextBoxTheme.CursorBackgroundColor, Hex1bColor.Yellow)
    .Set(TextBoxTheme.SelectionForegroundColor, Hex1bColor.White)
    .Set(TextBoxTheme.SelectionBackgroundColor, Hex1bColor.Blue)
    .Set(TextBoxTheme.LeftBracket, "< ")
    .Set(TextBoxTheme.RightBracket, " >");

await using var terminal = Hex1bTerminal.CreateBuilder()
    .WithHex1bApp((app, options) =>
    {
        options.Theme = theme;
        return ctx => /* ... */;
    })
    .Build();

await terminal.RunAsync();

Available Theme Elements

ElementTypeDefaultDescription
ForegroundColorHex1bColorDefaultText color
BackgroundColorHex1bColorDefaultBackground color
FocusedForegroundColorHex1bColorDefaultText color when focused
CursorForegroundColorHex1bColorBlackCursor text color
CursorBackgroundColorHex1bColorWhiteCursor background color
SelectionForegroundColorHex1bColorBlackSelected text color
SelectionBackgroundColorHex1bColorCyanSelection background
HoverCursorForegroundColorHex1bColorDefaultHover cursor text color
HoverCursorBackgroundColorHex1bColorDarkGrayHover cursor background
LeftBracketstring"["Left bracket decoration
RightBracketstring"]"Right bracket decoration

Released under the MIT License.