@opentui/react 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +464 -0
- package/index.js +418 -0
- package/jsx-dev-runtime.d.ts +2 -0
- package/jsx-dev-runtime.js +1 -0
- package/jsx-namespace.d.ts +25 -0
- package/jsx-runtime.d.ts +2 -0
- package/jsx-runtime.js +1 -0
- package/package.json +50 -0
- package/src/components/app.d.ts +8 -0
- package/src/components/index.d.ts +27 -0
- package/src/hooks/use-keyboard.d.ts +2 -0
- package/src/hooks/use-renderer.d.ts +1 -0
- package/src/hooks/use-resize.d.ts +1 -0
- package/src/index.d.ts +8 -0
- package/src/reconciler/host-config.d.ts +9 -0
- package/src/reconciler/reconciler.d.ts +5 -0
- package/src/reconciler/renderer.d.ts +3 -0
- package/src/reconciler/renderer.js +374 -0
- package/src/types/components.d.ts +32 -0
- package/src/types/extend.d.ts +15 -0
- package/src/types/host.d.ts +9 -0
- package/src/utils/id.d.ts +2 -0
- package/src/utils/index.d.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 opentui
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
# @opentui/react
|
|
2
|
+
|
|
3
|
+
A React renderer for building terminal user interfaces using OpenTUI core. Create rich, interactive console applications with familiar React patterns and components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun install @opentui/react @opentui/core react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { render } from "@opentui/react"
|
|
15
|
+
|
|
16
|
+
function App() {
|
|
17
|
+
return (
|
|
18
|
+
<group>
|
|
19
|
+
<text fg="#00FF00">Hello, Terminal!</text>
|
|
20
|
+
<box title="Welcome" padding={2}>
|
|
21
|
+
<text>Welcome to OpenTUI with React!"</text>
|
|
22
|
+
</box>
|
|
23
|
+
</group>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
render(<App />)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Core Concepts
|
|
31
|
+
|
|
32
|
+
### Components
|
|
33
|
+
|
|
34
|
+
OpenTUI React provides several built-in components that map to OpenTUI core renderables:
|
|
35
|
+
|
|
36
|
+
- **`<text>`** - Display text with styling
|
|
37
|
+
- **`<box>`** - Container with borders and layout
|
|
38
|
+
- **`<group>`** - Layout container for organizing components
|
|
39
|
+
- **`<input>`** - Text input field
|
|
40
|
+
- **`<select>`** - Selection dropdown
|
|
41
|
+
- **`<tab-select>`** - Tab-based selection
|
|
42
|
+
|
|
43
|
+
### Styling
|
|
44
|
+
|
|
45
|
+
Components can be styled using props or the `style` prop:
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
// Direct props
|
|
49
|
+
<text fg="#FF0000">Hello</text>
|
|
50
|
+
|
|
51
|
+
// Style prop
|
|
52
|
+
<box style={{ backgroundColor: "blue", padding: 2 }}>
|
|
53
|
+
<text>Styled content</text>
|
|
54
|
+
</box>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## API Reference
|
|
58
|
+
|
|
59
|
+
### `render(element, config?)`
|
|
60
|
+
|
|
61
|
+
Renders a React element to the terminal.
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { render } from "@opentui/react"
|
|
65
|
+
|
|
66
|
+
await render(<App />, {
|
|
67
|
+
// Optional renderer configuration
|
|
68
|
+
exitOnCtrlC: false,
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Parameters:**
|
|
73
|
+
|
|
74
|
+
- `element`: React element to render
|
|
75
|
+
- `config?`: Optional `CliRendererConfig` object
|
|
76
|
+
|
|
77
|
+
### Hooks
|
|
78
|
+
|
|
79
|
+
#### `useRenderer()`
|
|
80
|
+
|
|
81
|
+
Access the OpenTUI renderer instance.
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
import { useRenderer } from "@opentui/react"
|
|
85
|
+
|
|
86
|
+
function MyComponent() {
|
|
87
|
+
const renderer = useRenderer()
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
renderer.toggleDebugOverlay()
|
|
91
|
+
}, [])
|
|
92
|
+
|
|
93
|
+
return <text>Debug available</text>
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### `useKeyboard(handler)`
|
|
98
|
+
|
|
99
|
+
Handle keyboard events.
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
import { useKeyboard } from "@opentui/react"
|
|
103
|
+
|
|
104
|
+
function MyComponent() {
|
|
105
|
+
useKeyboard((key) => {
|
|
106
|
+
if (key.name === "escape") {
|
|
107
|
+
process.exit(0)
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
return <text>Press ESC to exit</text>
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### `useOnResize(callback)`
|
|
116
|
+
|
|
117
|
+
Handle terminal resize events.
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
import { useOnResize, useRenderer } from "@opentui/react"
|
|
121
|
+
import { useEffect } from "react"
|
|
122
|
+
|
|
123
|
+
function MyComponent() {
|
|
124
|
+
const renderer = useRenderer()
|
|
125
|
+
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
renderer.console.show()
|
|
128
|
+
}, [renderer])
|
|
129
|
+
|
|
130
|
+
useOnResize((width, height) => {
|
|
131
|
+
console.log(`Terminal resized to ${width}x${height}`)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
return <text>Resize-aware component</text>
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Components
|
|
139
|
+
|
|
140
|
+
### Text Component
|
|
141
|
+
|
|
142
|
+
Display text with rich formatting.
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
import { bold, fg, t } from "@opentui/core"
|
|
146
|
+
|
|
147
|
+
function TextExample() {
|
|
148
|
+
return (
|
|
149
|
+
<group>
|
|
150
|
+
{/* Simple text */}
|
|
151
|
+
<text>Hello World</text>
|
|
152
|
+
|
|
153
|
+
{/* Rich text with children */}
|
|
154
|
+
<text>{bold(fg("red")("Bold Red Text"))}</text>
|
|
155
|
+
|
|
156
|
+
{/* Template literals */}
|
|
157
|
+
<text>{t`${bold("Bold")} and ${fg("blue")("Blue")}`}</text>
|
|
158
|
+
</group>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Box Component
|
|
164
|
+
|
|
165
|
+
Container with borders and layout capabilities.
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
function BoxExample() {
|
|
169
|
+
return (
|
|
170
|
+
<group flexDirection="column">
|
|
171
|
+
{/* Basic box */}
|
|
172
|
+
<box>
|
|
173
|
+
<text>Simple box</text>
|
|
174
|
+
</box>
|
|
175
|
+
|
|
176
|
+
{/* Box with title and styling */}
|
|
177
|
+
<box title="Settings" borderStyle="double" padding={2} backgroundColor="blue">
|
|
178
|
+
<text>Box content</text>
|
|
179
|
+
</box>
|
|
180
|
+
|
|
181
|
+
{/* Styled box */}
|
|
182
|
+
<box
|
|
183
|
+
style={{
|
|
184
|
+
width: 40,
|
|
185
|
+
height: 10,
|
|
186
|
+
margin: 1,
|
|
187
|
+
alignItems: "center",
|
|
188
|
+
justifyContent: "center",
|
|
189
|
+
}}
|
|
190
|
+
>
|
|
191
|
+
<text>Centered content</text>
|
|
192
|
+
</box>
|
|
193
|
+
</group>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Group Component
|
|
199
|
+
|
|
200
|
+
Layout container for organizing multiple components.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
function GroupExample() {
|
|
204
|
+
return (
|
|
205
|
+
<group flexDirection="row">
|
|
206
|
+
<box>
|
|
207
|
+
<text>Left</text>
|
|
208
|
+
</box>
|
|
209
|
+
<box>
|
|
210
|
+
<text>Right</text>
|
|
211
|
+
</box>
|
|
212
|
+
</group>
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Input Component
|
|
218
|
+
|
|
219
|
+
Text input field with event handling.
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
import { useState } from "react"
|
|
223
|
+
|
|
224
|
+
function InputExample() {
|
|
225
|
+
const [value, setValue] = useState("")
|
|
226
|
+
const [focused, setFocused] = useState(true)
|
|
227
|
+
|
|
228
|
+
return (
|
|
229
|
+
<box title="Enter your name" style={{ height: 3 }}>
|
|
230
|
+
<input
|
|
231
|
+
placeholder="Type here..."
|
|
232
|
+
focused={focused}
|
|
233
|
+
onInput={setValue}
|
|
234
|
+
onSubmit={(value) => console.log("Submitted:", value)}
|
|
235
|
+
style={{
|
|
236
|
+
focusedBackgroundColor: "#333333",
|
|
237
|
+
}}
|
|
238
|
+
/>
|
|
239
|
+
</box>
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Select Component
|
|
245
|
+
|
|
246
|
+
Dropdown selection component.
|
|
247
|
+
|
|
248
|
+
```tsx
|
|
249
|
+
import type { SelectOption } from "@opentui/core"
|
|
250
|
+
import { useState } from "react"
|
|
251
|
+
|
|
252
|
+
function SelectExample() {
|
|
253
|
+
const [selectedIndex, setSelectedIndex] = useState(0)
|
|
254
|
+
|
|
255
|
+
const options: SelectOption[] = [
|
|
256
|
+
{ name: "Option 1", description: "Option 1 description", value: "opt1" },
|
|
257
|
+
{ name: "Option 2", description: "Option 2 description", value: "opt2" },
|
|
258
|
+
{ name: "Option 3", description: "Option 3 description", value: "opt3" },
|
|
259
|
+
]
|
|
260
|
+
|
|
261
|
+
return (
|
|
262
|
+
<box style={{ height: 24 }}>
|
|
263
|
+
<select
|
|
264
|
+
style={{ height: 22 }}
|
|
265
|
+
options={options}
|
|
266
|
+
focused={true}
|
|
267
|
+
onChange={(index, option) => {
|
|
268
|
+
setSelectedIndex(index)
|
|
269
|
+
console.log("Selected:", option)
|
|
270
|
+
}}
|
|
271
|
+
/>
|
|
272
|
+
</box>
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Examples
|
|
278
|
+
|
|
279
|
+
### Login Form
|
|
280
|
+
|
|
281
|
+
```tsx
|
|
282
|
+
import { useState, useCallback } from "react"
|
|
283
|
+
import { render, useKeyboard } from "@opentui/react"
|
|
284
|
+
|
|
285
|
+
function LoginForm() {
|
|
286
|
+
const [username, setUsername] = useState("")
|
|
287
|
+
const [password, setPassword] = useState("")
|
|
288
|
+
const [focused, setFocused] = useState<"username" | "password">("username")
|
|
289
|
+
const [status, setStatus] = useState("idle")
|
|
290
|
+
|
|
291
|
+
useKeyboard((key) => {
|
|
292
|
+
if (key.name === "tab") {
|
|
293
|
+
setFocused((prev) => (prev === "username" ? "password" : "username"))
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
const handleSubmit = useCallback(() => {
|
|
298
|
+
if (username === "admin" && password === "secret") {
|
|
299
|
+
setStatus("success")
|
|
300
|
+
} else {
|
|
301
|
+
setStatus("error")
|
|
302
|
+
}
|
|
303
|
+
}, [username, password])
|
|
304
|
+
|
|
305
|
+
return (
|
|
306
|
+
<group style={{ padding: 2, flexDirection: "column" }}>
|
|
307
|
+
<text fg="#FFFF00">Login Form</text>
|
|
308
|
+
|
|
309
|
+
<box title="Username" style={{ width: 40, height: 3, marginTop: 1 }}>
|
|
310
|
+
<input
|
|
311
|
+
placeholder="Enter username..."
|
|
312
|
+
onInput={setUsername}
|
|
313
|
+
onSubmit={handleSubmit}
|
|
314
|
+
focused={focused === "username"}
|
|
315
|
+
/>
|
|
316
|
+
</box>
|
|
317
|
+
|
|
318
|
+
<box title="Password" style={{ width: 40, height: 3, marginTop: 1 }}>
|
|
319
|
+
<input
|
|
320
|
+
placeholder="Enter password..."
|
|
321
|
+
onInput={setPassword}
|
|
322
|
+
onSubmit={handleSubmit}
|
|
323
|
+
focused={focused === "password"}
|
|
324
|
+
/>
|
|
325
|
+
</box>
|
|
326
|
+
|
|
327
|
+
<text
|
|
328
|
+
style={{
|
|
329
|
+
fg: status === "success" ? "green" : status === "error" ? "red" : "#999",
|
|
330
|
+
}}
|
|
331
|
+
>
|
|
332
|
+
{status.toUpperCase()}
|
|
333
|
+
</text>
|
|
334
|
+
</group>
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
render(<LoginForm />)
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Counter with Timer
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
import { useState, useEffect } from "react"
|
|
345
|
+
import { render } from "@opentui/react"
|
|
346
|
+
|
|
347
|
+
function Counter() {
|
|
348
|
+
const [count, setCount] = useState(0)
|
|
349
|
+
|
|
350
|
+
useEffect(() => {
|
|
351
|
+
const interval = setInterval(() => {
|
|
352
|
+
setCount((prev) => prev + 1)
|
|
353
|
+
}, 1000)
|
|
354
|
+
|
|
355
|
+
return () => clearInterval(interval)
|
|
356
|
+
}, [])
|
|
357
|
+
|
|
358
|
+
return (
|
|
359
|
+
<box title="Counter" style={{ padding: 2 }}>
|
|
360
|
+
<text fg="#00FF00">{`Count: ${count}`}</text>
|
|
361
|
+
</box>
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
render(<Counter />)
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### Styled Text Showcase
|
|
369
|
+
|
|
370
|
+
```tsx
|
|
371
|
+
import { blue, bold, red, t, underline } from "@opentui/core"
|
|
372
|
+
import { render } from "@opentui/react"
|
|
373
|
+
|
|
374
|
+
function StyledTextShowcase() {
|
|
375
|
+
return (
|
|
376
|
+
<group style={{ flexDirection: "column" }}>
|
|
377
|
+
<text>Simple text</text>
|
|
378
|
+
<text>{bold("Bold text")}</text>
|
|
379
|
+
<text>{underline("Underlined text")}</text>
|
|
380
|
+
<text>{red("Red text")}</text>
|
|
381
|
+
<text>{blue("Blue text")}</text>
|
|
382
|
+
<text>{bold(red("Bold red text"))}</text>
|
|
383
|
+
<text>{t`${bold("Bold")} and ${blue("blue")} combined`}</text>
|
|
384
|
+
</group>
|
|
385
|
+
)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
render(<StyledTextShowcase />)
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
## Component Extension
|
|
392
|
+
|
|
393
|
+
You can create custom components by extending OpenTUI's base renderables:
|
|
394
|
+
|
|
395
|
+
```tsx
|
|
396
|
+
import { BoxRenderable, OptimizedBuffer, RGBA } from "@opentui/core"
|
|
397
|
+
import { extend, render } from "@opentui/react"
|
|
398
|
+
|
|
399
|
+
// Create custom component class
|
|
400
|
+
class ButtonRenderable extends BoxRenderable {
|
|
401
|
+
private _label: string = "Button"
|
|
402
|
+
|
|
403
|
+
constructor(id: string, options: any) {
|
|
404
|
+
super(id, options)
|
|
405
|
+
this.borderStyle = "single"
|
|
406
|
+
this.padding = 1
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
protected renderSelf(buffer: OptimizedBuffer): void {
|
|
410
|
+
super.renderSelf(buffer)
|
|
411
|
+
|
|
412
|
+
const centerX = this.x + Math.floor(this.width / 2 - this._label.length / 2)
|
|
413
|
+
const centerY = this.y + Math.floor(this.height / 2)
|
|
414
|
+
|
|
415
|
+
buffer.drawText(this._label, centerX, centerY, RGBA.fromInts(255, 255, 255, 255))
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
set label(value: string) {
|
|
419
|
+
this._label = value
|
|
420
|
+
this.needsUpdate()
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Add TypeScript support
|
|
425
|
+
declare module "@opentui/react" {
|
|
426
|
+
interface OpenTUIComponents {
|
|
427
|
+
button: typeof ButtonRenderable
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Register the component
|
|
432
|
+
extend({ button: ButtonRenderable })
|
|
433
|
+
|
|
434
|
+
// Use in JSX
|
|
435
|
+
function App() {
|
|
436
|
+
return (
|
|
437
|
+
<group>
|
|
438
|
+
<button label="Click me!" style={{ backgroundColor: "blue" }} />
|
|
439
|
+
<button label="Another button" style={{ backgroundColor: "green" }} />
|
|
440
|
+
</group>
|
|
441
|
+
)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
render(<App />)
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## TypeScript Configuration
|
|
448
|
+
|
|
449
|
+
For optimal TypeScript support, configure your `tsconfig.json`:
|
|
450
|
+
|
|
451
|
+
```json
|
|
452
|
+
{
|
|
453
|
+
"compilerOptions": {
|
|
454
|
+
"lib": ["ESNext", "DOM"],
|
|
455
|
+
"target": "ESNext",
|
|
456
|
+
"module": "ESNext",
|
|
457
|
+
"moduleResolution": "bundler",
|
|
458
|
+
"jsx": "react-jsx",
|
|
459
|
+
"jsxImportSource": "@opentui/react",
|
|
460
|
+
"strict": true,
|
|
461
|
+
"skipLibCheck": true
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
```
|