
Security News
Node.js Considers Public Workflow for Security Reports Amid AI-Driven Surge
Node.js is debating whether AI-driven security report volume warrants moving more vulnerability reports into public workflows.
@opentui/react
Advanced tools
A React renderer for building terminal user interfaces using OpenTUI core. Create rich, interactive console applications with familiar React patterns and components.
Quick start with bun and create-tui:
bun create tui --template react
Manual installation:
bun install @opentui/react @opentui/core react
import { render } from "@opentui/react"
function App() {
return <text>Hello, world!</text>
}
render(<App />)
For optimal TypeScript support, configure your tsconfig.json:
{
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"jsxImportSource": "@opentui/react",
"strict": true,
"skipLibCheck": true
}
}
OpenTUI React provides several built-in components that map to OpenTUI core renderables:
<text> - Display text with styling<box> - Container with borders and layout<input> - Text input field<select> - Selection dropdown<scrollbox> - A scrollable box<tab-select> - Tab-based selection<ascii-font> - Display ASCII art text with different font stylesHelpers:
<span>, <strong>, <em>, <u>, <b>, <i>, <br> - Text modifiers (must be used inside of the text component)Components can be styled using props or the style prop:
// Direct props
<box backgroundColor="blue" padding={2}>
<text>Hello, world!</text>
</box>
// Style prop
<box style={{ backgroundColor: "blue", padding: 2 }}>
<text>Hello, world!</text>
</box>
render(element, config?)Renders a React element to the terminal.
import { render } from "@opentui/react"
await render(<App />, {
// Optional renderer configuration
exitOnCtrlC: false,
})
Parameters:
element: React element to renderconfig?: Optional CliRendererConfig objectuseRenderer()Access the OpenTUI renderer instance.
import { useRenderer } from "@opentui/react"
function App() {
const renderer = useRenderer()
useEffect(() => {
renderer.console.show()
console.log("Hello, from the console!")
}, [])
return <box />
}
useKeyboard(handler)Handle keyboard events.
import { useKeyboard } from "@opentui/react"
function App() {
useKeyboard((key) => {
if (key.name === "escape") {
process.exit(0)
}
})
return <text>Press ESC to exit</text>
}
useOnResize(callback)Handle terminal resize events.
import { useOnResize, useRenderer } from "@opentui/react"
import { useEffect } from "react"
function App() {
const renderer = useRenderer()
useEffect(() => {
renderer.console.show()
}, [renderer])
useOnResize((width, height) => {
console.log(`Terminal resized to ${width}x${height}`)
})
return <text>Resize-aware component</text>
}
useTerminalDimensions()Get current terminal dimensions and automatically update when the terminal is resized.
import { useTerminalDimensions } from "@opentui/react"
function App() {
const { width, height } = useTerminalDimensions()
return (
<box>
<text>
Terminal dimensions: {width}x{height}
</text>
<box style={{ width: Math.floor(width / 2), height: Math.floor(height / 3) }}>
<text>Half-width, third-height box</text>
</box>
</box>
)
}
Returns: An object with width and height properties representing the current terminal dimensions.
useTimeline(options?)Create and manage animations using OpenTUI's timeline system. This hook automatically registers and unregisters the timeline with the animation engine.
import { render, useTimeline } from "@opentui/react"
import { useEffect, useState } from "react"
function App() {
const [width, setWidth] = useState(0)
const timeline = useTimeline({
duration: 2000,
loop: false,
})
useEffect(() => {
timeline.add(
{
width,
},
{
width: 50,
duration: 2000,
ease: "linear",
onUpdate: (animation) => {
setWidth(animation.targets[0].width)
},
},
)
}, [])
return <box style={{ width, backgroundColor: "#6a5acd" }} />
}
Parameters:
options?: Optional TimelineOptions object with properties:
duration?: Animation duration in milliseconds (default: 1000)loop?: Whether the timeline should loop (default: false)autoplay?: Whether to automatically start the timeline (default: true)onComplete?: Callback when timeline completesonPause?: Callback when timeline is pausedReturns: A Timeline instance with methods:
add(target, properties, startTime): Add animation to timelineplay(): Start the timelinepause(): Pause the timelinerestart(): Restart the timeline from beginningDisplay text with rich formatting.
function App() {
return (
<box>
{/* Simple text */}
<text>Hello World</text>
{/* Rich text with children */}
<text>
<span fg="red">Red Text</span>
</text>
{/* Text modifiers */}
<text>
<strong>Bold</strong>, <em>Italic</em>, and <u>Underlined</u>
</text>
</box>
)
}
Container with borders and layout capabilities.
function App() {
return (
<box flexDirection="column">
{/* Basic box */}
<box border>
<text>Simple box</text>
</box>
{/* Box with title and styling */}
<box title="Settings" border borderStyle="double" padding={2} backgroundColor="blue">
<text>Box content</text>
</box>
{/* Styled box */}
<box
style={{
border: true,
width: 40,
height: 10,
margin: 1,
alignItems: "center",
justifyContent: "center",
}}
>
<text>Centered content</text>
</box>
</box>
)
}
Text input field with event handling.
import { useState } from "react"
function App() {
const [value, setValue] = useState("")
return (
<box title="Enter your name" style={{ border: true, height: 3 }}>
<input
placeholder="Type here..."
focused
onInput={setValue}
onSubmit={(value) => console.log("Submitted:", value)}
/>
</box>
)
}
Dropdown selection component.
import type { SelectOption } from "@opentui/core"
import { useState } from "react"
function App() {
const [selectedIndex, setSelectedIndex] = useState(0)
const options: SelectOption[] = [
{ name: "Option 1", description: "Option 1 description", value: "opt1" },
{ name: "Option 2", description: "Option 2 description", value: "opt2" },
{ name: "Option 3", description: "Option 3 description", value: "opt3" },
]
return (
<box style={{ border: true, height: 24 }}>
<select
style={{ height: 22 }}
options={options}
focused={true}
onChange={(index, option) => {
setSelectedIndex(index)
console.log("Selected:", option)
}}
/>
</box>
)
}
A scrollable box.
function App() {
return (
<scrollbox
style={{
rootOptions: {
backgroundColor: "#24283b",
},
wrapperOptions: {
backgroundColor: "#1f2335",
},
viewportOptions: {
backgroundColor: "#1a1b26",
},
contentOptions: {
backgroundColor: "#16161e",
},
scrollbarOptions: {
showArrows: true,
trackOptions: {
foregroundColor: "#7aa2f7",
backgroundColor: "#414868",
},
},
}}
focused
>
{Array.from({ length: 1000 }).map((_, i) => (
<box
key={i}
style={{ width: "100%", padding: 1, marginBottom: 1, backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449" }}
>
<text content={`Box ${i}`} />
</box>
))}
</scrollbox>
)
}
Display ASCII art text with different font styles.
import { measureText } from "@opentui/core"
import { useState } from "react"
function App() {
const text = "ASCII"
const [font, setFont] = useState<"block" | "shade" | "slick" | "tiny">("tiny")
const { width, height } = measureText({
text,
font,
})
return (
<box style={{ border: true, paddingLeft: 1, paddingRight: 1 }}>
<box
style={{
height: 8,
border: true,
marginBottom: 1,
}}
>
<select
focused
onChange={(_, option) => setFont(option?.value)}
showScrollIndicator
options={[
{
name: "Tiny",
description: "Tiny font",
value: "tiny",
},
{
name: "Block",
description: "Block font",
value: "block",
},
{
name: "Slick",
description: "Slick font",
value: "slick",
},
{
name: "Shade",
description: "Shade font",
value: "shade",
},
]}
style={{ flexGrow: 1 }}
/>
</box>
<ascii-font style={{ width, height }} text={text} font={font} />
</box>
)
}
import { render, useKeyboard } from "@opentui/react"
import { useCallback, useState } from "react"
function App() {
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
const [focused, setFocused] = useState<"username" | "password">("username")
const [status, setStatus] = useState("idle")
useKeyboard((key) => {
if (key.name === "tab") {
setFocused((prev) => (prev === "username" ? "password" : "username"))
}
})
const handleSubmit = useCallback(() => {
if (username === "admin" && password === "secret") {
setStatus("success")
} else {
setStatus("error")
}
}, [username, password])
return (
<box style={{ border: true, padding: 2, flexDirection: "column", gap: 1 }}>
<text fg="#FFFF00">Login Form</text>
<box title="Username" style={{ border: true, width: 40, height: 3 }}>
<input
placeholder="Enter username..."
onInput={setUsername}
onSubmit={handleSubmit}
focused={focused === "username"}
/>
</box>
<box title="Password" style={{ border: true, width: 40, height: 3 }}>
<input
placeholder="Enter password..."
onInput={setPassword}
onSubmit={handleSubmit}
focused={focused === "password"}
/>
</box>
<text
style={{
fg: status === "success" ? "green" : status === "error" ? "red" : "#999",
}}
>
{status.toUpperCase()}
</text>
</box>
)
}
render(<App />)
import { render } from "@opentui/react"
import { useEffect, useState } from "react"
function App() {
const [count, setCount] = useState(0)
useEffect(() => {
const interval = setInterval(() => {
setCount((prev) => prev + 1)
}, 1000)
return () => clearInterval(interval)
}, [])
return (
<box title="Counter" style={{ padding: 2 }}>
<text fg="#00FF00">{`Count: ${count}`}</text>
</box>
)
}
render(<App />)
import { TextAttributes } from "@opentui/core"
import { render, useTimeline } from "@opentui/react"
import { useEffect, useState } from "react"
type Stats = {
cpu: number
memory: number
network: number
disk: number
}
export const App = () => {
const [stats, setAnimatedStats] = useState<Stats>({
cpu: 0,
memory: 0,
network: 0,
disk: 0,
})
const timeline = useTimeline({
duration: 3000,
loop: false,
})
useEffect(() => {
timeline.add(
stats,
{
cpu: 85,
memory: 70,
network: 95,
disk: 60,
duration: 3000,
ease: "linear",
onUpdate: (values) => {
setAnimatedStats({ ...values.targets[0] })
},
},
0,
)
}, [])
const statsMap = [
{ name: "CPU", key: "cpu", color: "#6a5acd" },
{ name: "Memory", key: "memory", color: "#4682b4" },
{ name: "Network", key: "network", color: "#20b2aa" },
{ name: "Disk", key: "disk", color: "#daa520" },
]
return (
<box
title="System Monitor"
style={{
margin: 1,
padding: 1,
border: true,
marginLeft: 2,
marginRight: 2,
borderStyle: "single",
borderColor: "#4a4a4a",
}}
>
{statsMap.map((stat) => (
<box key={stat.key}>
<box flexDirection="row" justifyContent="space-between">
<text>{stat.name}</text>
<text attributes={TextAttributes.DIM}>{Math.round(stats[stat.key as keyof Stats])}%</text>
</box>
<box style={{ backgroundColor: "#333333" }}>
<box style={{ width: `${stats[stat.key as keyof Stats]}%`, height: 1, backgroundColor: stat.color }} />
</box>
</box>
))}
</box>
)
}
render(<App />)
import { render } from "@opentui/react"
function App() {
return (
<>
<text>Simple text</text>
<text>
<strong>Bold text</strong>
</text>
<text>
<u>Underlined text</u>
</text>
<text>
<span fg="red">Red text</span>
</text>
<text>
<span fg="blue">Blue text</span>
</text>
<text>
<strong fg="red">Bold red text</strong>
</text>
<text>
<strong>Bold</strong> and <span fg="blue">blue</span> combined
</text>
</>
)
}
render(<App />)
You can create custom components by extending OpenTUIs base renderables:
import { BoxRenderable, OptimizedBuffer, RGBA, type BoxOptions, type RenderContext } from "@opentui/core"
import { extend, render } from "@opentui/react"
// Create custom component class
class ButtonRenderable extends BoxRenderable {
private _label: string = "Button"
constructor(ctx: RenderContext, options: BoxOptions & { label?: string }) {
super(ctx, {
border: true,
borderStyle: "single",
minHeight: 3,
...options,
})
if (options.label) {
this._label = options.label
}
}
protected renderSelf(buffer: OptimizedBuffer): void {
super.renderSelf(buffer)
const centerX = this.x + Math.floor(this.width / 2 - this._label.length / 2)
const centerY = this.y + Math.floor(this.height / 2)
buffer.drawText(this._label, centerX, centerY, RGBA.fromInts(255, 255, 255, 255))
}
set label(value: string) {
this._label = value
this.requestRender()
}
}
// Add TypeScript support
declare module "@opentui/react" {
interface OpenTUIComponents {
consoleButton: typeof ButtonRenderable
}
}
// Register the component
extend({ consoleButton: ButtonRenderable })
// Use in JSX
function App() {
return (
<box>
<consoleButton label="Click me!" style={{ backgroundColor: "blue" }} />
<consoleButton label="Another button" style={{ backgroundColor: "green" }} />
</box>
)
}
render(<App />)
FAQs
React renderer for building terminal user interfaces using OpenTUI core
The npm package @opentui/react receives a total of 94,977 weekly downloads. As such, @opentui/react popularity was classified as popular.
We found that @opentui/react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Node.js is debating whether AI-driven security report volume warrants moving more vulnerability reports into public workflows.

Security News
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.

Security News
Open source attacks are accelerating as AI coding agents pull in dependencies faster, with less human review.