textarea.mdx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ---
  2. title: Textarea
  3. description: Multi-line text input with selection and keybindings
  4. order: 4
  5. ---
  6. # Textarea
  7. Multi-line text input with cursor, selection, and rich keybindings.
  8. ## Basic usage
  9. ### Renderable API
  10. ```typescript
  11. import { TextareaRenderable, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const textarea = new TextareaRenderable(renderer, {
  14. id: "notes",
  15. width: 50,
  16. height: 6,
  17. placeholder: "Type notes here...",
  18. backgroundColor: "#1a1a1a",
  19. focusedBackgroundColor: "#222222",
  20. textColor: "#FFFFFF",
  21. cursorColor: "#00FF88",
  22. })
  23. renderer.root.add(textarea)
  24. textarea.focus()
  25. ```
  26. ## Submit handling
  27. Bind a submit action and listen for `onSubmit`:
  28. ```typescript
  29. import { TextareaRenderable } from "@opentui/core"
  30. const textarea = new TextareaRenderable(renderer, {
  31. width: 50,
  32. height: 6,
  33. onSubmit: () => {
  34. console.log("Submitted:", textarea.plainText)
  35. },
  36. keyBindings: [{ name: "return", ctrl: true, action: "submit" }],
  37. })
  38. ```
  39. ## Placeholder styling
  40. ```typescript
  41. const textarea = new TextareaRenderable(renderer, {
  42. width: 40,
  43. height: 4,
  44. placeholder: "Type here",
  45. placeholderColor: "#666666",
  46. })
  47. ```
  48. ## Construct API
  49. > Not available yet. Use `TextareaRenderable` for now.
  50. ## Properties
  51. | Property | Type | Default | Description |
  52. | ------------------------ | ------------------------------------- | ----------- | --------------------------------- |
  53. | `width` | `number` or `string` | - | Width in characters or percentage |
  54. | `height` | `number` or `string` | - | Height in rows or percentage |
  55. | `initialValue` | `string` | `""` | Initial text content |
  56. | `placeholder` | `string`, `StyledText`, or `null` | `null` | Placeholder content |
  57. | `placeholderColor` | `string` or `RGBA` | `#666666` | Placeholder color |
  58. | `backgroundColor` | `string` or `RGBA` | transparent | Background when unfocused |
  59. | `textColor` | `string` or `RGBA` | `#FFFFFF` | Text color when unfocused |
  60. | `focusedBackgroundColor` | `string` or `RGBA` | transparent | Background when focused |
  61. | `focusedTextColor` | `string` or `RGBA` | `#FFFFFF` | Text color when focused |
  62. | `wrapMode` | `"none"`, `"char"`, or `"word"` | `"word"` | Line wrapping mode |
  63. | `selectionBg` | `string` or `RGBA` | - | Selection background |
  64. | `selectionFg` | `string` or `RGBA` | - | Selection foreground |
  65. | `cursorColor` | `string` or `RGBA` | `#FFFFFF` | Cursor color |
  66. | `cursorStyle` | `CursorStyleOptions` | - | Cursor style and blinking |
  67. | `keyBindings` | `KeyBinding[]` | - | Custom keybindings |
  68. | `keyAliasMap` | `Record<string, string>` | - | Key alias mapping |
  69. | `onSubmit` | `(event: SubmitEvent) => void` | - | Submit handler |
  70. | `onContentChange` | `(event: ContentChangeEvent) => void` | - | Fired on content changes |
  71. | `onCursorChange` | `(event: CursorChangeEvent) => void` | - | Fired on cursor movement |
  72. ## Useful properties
  73. | Property | Type | Description |
  74. | ----------------------- | -------------------------- | --------------------------------------------------------------- |
  75. | `plainText` | `string` | Current text content |
  76. | `cursorOffset` | `number` | Cursor offset in the buffer |
  77. | `cursorCharacterOffset` | `number \| undefined` | Offset of the character under the cursor (skips trailing blank) |
  78. | `logicalCursor` | `{ row, col }` | Logical line/column of the cursor |
  79. | `visualCursor` | `{ row, col, visualLine }` | Visual (post-wrap) cursor position |
  80. | `traits` | `EditorTraits` | Editor traits published to hosting UI (see [Traits](#traits)) |
  81. ## Cursor and selection control
  82. `TextareaRenderable` (and its base `EditBufferRenderable`) exposes a programmatic API. You can move the cursor, edit text, and drive selections from your own keybindings or commands. All selection-aware movement methods accept `{ select: true }` to extend the current selection instead of moving the cursor.
  83. ### Cursor movement
  84. ```typescript
  85. textarea.setCursor(row, col)
  86. textarea.moveCursorLeft()
  87. textarea.moveCursorRight({ select: true })
  88. textarea.moveCursorUp()
  89. textarea.moveCursorDown()
  90. textarea.moveWordForward({ select: true })
  91. textarea.moveWordBackward()
  92. textarea.gotoLine(0)
  93. textarea.gotoLineStart()
  94. textarea.gotoLineTextEnd()
  95. textarea.gotoLineHome({ select: true }) // Emacs-style smart home
  96. textarea.gotoLineEnd()
  97. textarea.gotoVisualLineHome()
  98. textarea.gotoVisualLineEnd()
  99. textarea.gotoBufferHome()
  100. textarea.gotoBufferEnd({ select: true })
  101. ```
  102. ### Selection
  103. ```typescript
  104. textarea.setSelection(start, end) // half-open offsets
  105. textarea.setSelectionInclusive(start, end) // inclusive end
  106. textarea.selectAll()
  107. textarea.clearSelection()
  108. textarea.deleteSelection()
  109. ```
  110. ### Editing
  111. ```typescript
  112. textarea.insertChar("a")
  113. textarea.insertText("\ninserted")
  114. textarea.deleteChar() // forward delete
  115. textarea.deleteCharBackward() // backspace
  116. textarea.deleteWordForward()
  117. textarea.deleteWordBackward()
  118. textarea.deleteToLineEnd()
  119. textarea.deleteToLineStart()
  120. textarea.deleteLine()
  121. textarea.newLine()
  122. textarea.undo()
  123. textarea.redo()
  124. ```
  125. These methods update the cursor, clear any active global selection, and call `requestRender()` for you.
  126. ## Traits
  127. The `traits` property lets an editor advertise intent to a host UI — which built-in keys it wants to capture, whether it wants the chrome to visually suspend, and a short status label for a footer or status bar. Assigning a new `EditorTraits` object emits the `traits-changed` event when the value differs from the previous one.
  128. ```typescript
  129. import { EditBufferRenderableEvents, type EditorTraits } from "@opentui/core"
  130. textarea.traits = {
  131. capture: ["escape", "submit"], // consume these before host binds
  132. suspend: false,
  133. status: "Composing reply",
  134. } satisfies EditorTraits
  135. textarea.on(EditBufferRenderableEvents.TRAITS_CHANGED, (traits) => {
  136. updateFooter(traits.status ?? "")
  137. })
  138. ```
  139. | Field | Type | Description |
  140. | --------- | ----------------- | ------------------------------------------------------------------------------- |
  141. | `capture` | `EditorCapture[]` | Keys the editor wants to capture: `"escape"`, `"navigate"`, `"submit"`, `"tab"` |
  142. | `suspend` | `boolean` | Hint to the host to suspend ambient UI (dim borders, hide hints, etc.) |
  143. | `status` | `string` | Optional short label surfacing editor mode in a status bar |
  144. Traits reset to an empty object when you destroy the renderable. Use `isEditBufferRenderable(renderable)` if you need to distinguish editor renderables from plain text renderables in a generic tree.