mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 02:03:16 +00:00
Improve Custom Components docs: explain editor replacement, simplify example
This commit is contained in:
parent
a5741df69a
commit
6bd5e419a6
1 changed files with 15 additions and 12 deletions
|
|
@ -956,32 +956,34 @@ const current = ctx.ui.getEditorText();
|
||||||
|
|
||||||
### Custom Components
|
### Custom Components
|
||||||
|
|
||||||
For complex UI, use `ctx.ui.custom()` with full TUI components:
|
For complex UI, use `ctx.ui.custom()`. This temporarily replaces the editor with your component until `done()` is called:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Text, Box, Component } from "@mariozechner/pi-tui";
|
import { Text, Component } from "@mariozechner/pi-tui";
|
||||||
|
|
||||||
const result = await ctx.ui.custom((tui, theme, done) => {
|
const result = await ctx.ui.custom<boolean>((tui, theme, done) => {
|
||||||
// Create a component that handles keyboard input
|
|
||||||
const box = new Box(0, 0);
|
|
||||||
const text = new Text("Press Enter to confirm, Escape to cancel", 1, 1);
|
const text = new Text("Press Enter to confirm, Escape to cancel", 1, 1);
|
||||||
box.add(text);
|
|
||||||
|
|
||||||
box.onKey = (key) => {
|
text.onKey = (key) => {
|
||||||
if (key === "return") done({ confirmed: true });
|
if (key === "return") done(true);
|
||||||
if (key === "escape") done({ confirmed: false });
|
if (key === "escape") done(false);
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
return box;
|
return text;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result.confirmed) {
|
if (result) {
|
||||||
// User pressed Enter
|
// User pressed Enter
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
See [tui.md](tui.md) for the full component API.
|
The callback receives:
|
||||||
|
- `tui` - TUI instance (for screen dimensions, focus management)
|
||||||
|
- `theme` - Current theme for styling
|
||||||
|
- `done(value)` - Call to close component and return value
|
||||||
|
|
||||||
|
See [tui.md](tui.md) for the full component API and [examples/extensions/](../examples/extensions/) for working examples (snake.ts, todo.ts, qna.ts).
|
||||||
|
|
||||||
### Message Rendering
|
### Message Rendering
|
||||||
|
|
||||||
|
|
@ -1031,6 +1033,7 @@ theme.fg("dim", text) // Tertiary text
|
||||||
// Text styles
|
// Text styles
|
||||||
theme.bold(text)
|
theme.bold(text)
|
||||||
theme.italic(text)
|
theme.italic(text)
|
||||||
|
theme.strikethrough(text)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Error Handling
|
## Error Handling
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue