Overview
TheTextNodeRenderable 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: Bold2: Italic4: Underline8: Dim
1 | 4 for bold + underline.{ url: string }
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.obj:TextNodeRenderable | StyledText | stringindex?: Optional insertion index
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
Links
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