Skip to main content

Overview

The TextNodeRenderable component enables rich text composition by building tree structures of styled text nodes. Each node can have its own colors, attributes (bold, italic, underline), and can contain child nodes that inherit and override parent styles.

Basic Usage

Props

string | RGBA
Foreground color for the text node. Inherits from parent if not specified.
string | RGBA
Background color for the text node. Inherits from parent if not specified.
number
default:"0"
Text attributes as a bitmask:
  • 1: Bold
  • 2: Italic
  • 4: Underline
  • 8: Dim
Combine with bitwise OR: 1 | 4 for bold + underline.
Link URL for clickable text.
string
Optional unique identifier for the node.

Static Methods

fromString()

Create a TextNode from a string.

fromNodes()

Create a TextNode containing multiple child nodes.

Methods

add()

Add a child node, string, or StyledText.
Parameters:
  • obj: TextNodeRenderable | StyledText | string
  • index?: Optional insertion index
Returns: number - Index where the item was inserted

remove()

Remove a child node by ID.

insertBefore()

Insert a node before an anchor node.

clear()

Remove all children.

toChunks()

Convert the node tree to TextChunks with inherited styles.

Examples

Basic Styled Text

Nested Composition

Dynamic Updates

Complex Document Structure

Style Inheritance

Properties

children

Access or modify the node’s children.

parent

Access the parent node.

Styling Properties

Get or set styling properties dynamically:

Helper Functions

OpenTUI provides convenient helper functions for common text styling:

Use Cases

  • Syntax highlighting: Build custom syntax highlighting by composing colored nodes
  • Rich text editors: Dynamic text editing with inline styling
  • Status displays: Color-coded status indicators with icons
  • Log viewers: Styled log messages with severity colors
  • Documentation: Complex formatted documents with mixed styling
  • Text - Plain and styled text display
  • Code - Syntax-highlighted code
  • Markdown - Markdown rendering