@mk-co/neox-cli 2.0.7
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 +254 -0
- package/README.md +172 -0
- package/dist/cli/main.js +1891 -0
- package/dist/vendor/ink/license +10 -0
- package/dist/vendor/ink/readme.md +2358 -0
- package/dist/vendor/ink/src/global.d.ts +36 -0
- package/package.json +103 -0
|
@@ -0,0 +1,2358 @@
|
|
|
1
|
+
[](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<br>
|
|
7
|
+
<br>
|
|
8
|
+
<img width="240" alt="Ink" src="media/logo.png">
|
|
9
|
+
<br>
|
|
10
|
+
<br>
|
|
11
|
+
<br>
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
> React for CLIs. Build and test your CLI output using components.
|
|
15
|
+
|
|
16
|
+
[](https://github.com/vadimdemedes/ink/actions)
|
|
17
|
+
[](https://npmjs.com/package/ink)
|
|
18
|
+
|
|
19
|
+
Ink provides the same component-based UI building experience that React offers in the browser, but for command-line apps.
|
|
20
|
+
It uses [Yoga](https://github.com/facebook/yoga) to build Flexbox layouts in the terminal, so most CSS-like properties are available in Ink as well.
|
|
21
|
+
If you are already familiar with React, you already know Ink.
|
|
22
|
+
|
|
23
|
+
Since Ink is a React renderer, all features of React are supported.
|
|
24
|
+
Head over to the [React](https://reactjs.org) website for documentation on how to use it.
|
|
25
|
+
Only Ink's methods are documented in this readme.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
<div align="center">
|
|
30
|
+
<p>
|
|
31
|
+
<p>
|
|
32
|
+
<sup>
|
|
33
|
+
<a href="https://opencollective.com/vadimdemedes">My open source work is supported by the community ❤️</a>
|
|
34
|
+
</sup>
|
|
35
|
+
</p>
|
|
36
|
+
</p>
|
|
37
|
+
</div>
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
npm install ink react
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```jsx
|
|
48
|
+
import React, {useState, useEffect} from 'react';
|
|
49
|
+
import {render, Text} from 'ink';
|
|
50
|
+
|
|
51
|
+
const Counter = () => {
|
|
52
|
+
const [counter, setCounter] = useState(0);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const timer = setInterval(() => {
|
|
56
|
+
setCounter(previousCounter => previousCounter + 1);
|
|
57
|
+
}, 100);
|
|
58
|
+
|
|
59
|
+
return () => {
|
|
60
|
+
clearInterval(timer);
|
|
61
|
+
};
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
64
|
+
return <Text color="green">{counter} tests passed</Text>;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
render(<Counter />);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
<img src="media/demo.svg" width="600">
|
|
71
|
+
|
|
72
|
+
Feel free to play around with the code and fork this Repl at [https://repl.it/@vadimdemedes/ink-counter-demo](https://repl.it/@vadimdemedes/ink-counter-demo).
|
|
73
|
+
|
|
74
|
+
## Who's Using Ink?
|
|
75
|
+
|
|
76
|
+
- [Claude Code](https://github.com/anthropics/claude-code) - An agentic coding tool made by Anthropic.
|
|
77
|
+
- [Gemini CLI](https://github.com/google-gemini/gemini-cli) - An agentic coding tool made by Google.
|
|
78
|
+
- [GitHub Copilot for CLI](https://githubnext.com/projects/copilot-cli) - Just say what you want the shell to do.
|
|
79
|
+
- [Canva CLI](https://www.canva.dev/docs/apps/canva-cli/) - CLI for creating and managing Canva Apps.
|
|
80
|
+
- [Cloudflare's Wrangler](https://github.com/cloudflare/wrangler2) - The CLI for Cloudflare Workers.
|
|
81
|
+
- [Linear](https://linear.app) - Linear built an internal CLI for managing deployments, configs, and other housekeeping tasks.
|
|
82
|
+
- [Gatsby](https://www.gatsbyjs.org) - Gatsby is a modern web framework for blazing-fast websites.
|
|
83
|
+
- [tap](https://node-tap.org) - A Test-Anything-Protocol library for JavaScript.
|
|
84
|
+
- [Terraform CDK](https://github.com/hashicorp/terraform-cdk) - Cloud Development Kit (CDK) for HashiCorp Terraform.
|
|
85
|
+
- [Specify CLI](https://specifyapp.com) - Automate the distribution of your design tokens.
|
|
86
|
+
- [Twilio's SIGNAL](https://github.com/twilio-labs/plugin-signal2020) - CLI for Twilio's SIGNAL conference. [Blog post](https://www.twilio.com/blog/building-conference-cli-in-react).
|
|
87
|
+
- [Typewriter](https://github.com/segmentio/typewriter) - Generates strongly-typed [Segment](https://segment.com) analytics clients from arbitrary JSON Schema.
|
|
88
|
+
- [Prisma](https://www.prisma.io) - The unified data layer for modern applications.
|
|
89
|
+
- [Blitz](https://blitzjs.com) - The Fullstack React Framework.
|
|
90
|
+
- [New York Times](https://github.com/nytimes/kyt) - NYT uses Ink's `kyt` - a toolkit that encapsulates and manages the configuration for web apps.
|
|
91
|
+
- [tink](https://github.com/npm/tink) - A next-generation runtime and package manager.
|
|
92
|
+
- [Inkle](https://github.com/jrr/inkle) - A Wordle game.
|
|
93
|
+
- [loki](https://github.com/oblador/loki) - Visual regression testing tool for Storybook.
|
|
94
|
+
- [Bit](https://github.com/teambit/bit) - Build, distribute, and collaborate on components.
|
|
95
|
+
- [Remirror](https://github.com/remirror/remirror) - Your friendly, world-class editor toolkit.
|
|
96
|
+
- [Prime](https://github.com/birkir/prime) - Open-source GraphQL CMS.
|
|
97
|
+
- [emoj](https://github.com/sindresorhus/emoj) - Find relevant emojis.
|
|
98
|
+
- [emma](https://github.com/maticzav/emma-cli) - Find and install npm packages easily.
|
|
99
|
+
- [npm-check-extras](https://github.com/akgondber/npm-check-extras) - Check for outdated and unused dependencies, and run update/delete actions on selected ones.
|
|
100
|
+
- [swiff](https://github.com/simple-integrated-marketing/swiff) - Multi-environment command-line tools for time-saving web developers.
|
|
101
|
+
- [share](https://github.com/marionebl/share-cli) - Share files quickly.
|
|
102
|
+
- [Kubelive](https://github.com/ameerthehacker/kubelive) - A CLI for Kubernetes that provides live data about the cluster and its resources.
|
|
103
|
+
- [changelog-view](https://github.com/jdeniau/changelog-view) - View changelogs.
|
|
104
|
+
- [cfpush](https://github.com/mamachanko/cfpush) - Interactive Cloud Foundry tutorial.
|
|
105
|
+
- [startd](https://github.com/mgrip/startd) - Turn your React component into a web app.
|
|
106
|
+
- [wiki-cli](https://github.com/hexrcs/wiki-cli) - Search Wikipedia and read article summaries.
|
|
107
|
+
- [garson](https://github.com/goliney/garson) - Build interactive, config-based command-line interfaces.
|
|
108
|
+
- [git-contrib-calendar](https://github.com/giannisp/git-contrib-calendar) - Display a contributions calendar for any Git repository.
|
|
109
|
+
- [gitgud](https://github.com/GitGud-org/GitGud) - Interactive command-line GUI for Git.
|
|
110
|
+
- [Autarky](https://github.com/pranshuchittora/autarky) - Find and delete old `node_modules` directories to free up disk space.
|
|
111
|
+
- [fast-cli](https://github.com/sindresorhus/fast-cli) - Test your download and upload speeds.
|
|
112
|
+
- [tasuku](https://github.com/privatenumber/tasuku) - Minimal task runner.
|
|
113
|
+
- [mnswpr](https://github.com/mordv/mnswpr) - A Minesweeper game.
|
|
114
|
+
- [lrn](https://github.com/krychu/lrn) - Learning by repetition.
|
|
115
|
+
- [turdle](https://github.com/mynameisankit/turdle) - A Wordle game.
|
|
116
|
+
- [Shopify CLI](https://github.com/Shopify/cli) - Build apps, themes, and storefronts for the Shopify platform.
|
|
117
|
+
- [ToDesktop CLI](https://www.todesktop.com/electron) - All-in-one platform for building Electron apps.
|
|
118
|
+
- [Walle](https://github.com/Pobepto/walle) - A full-featured crypto wallet for EVM networks.
|
|
119
|
+
- [Sudoku](https://github.com/mrozio13pl/sudoku-in-terminal) - A Sudoku game.
|
|
120
|
+
- [Sea Trader](https://github.com/zyishai/sea-trader) - A Taipan!-inspired trading simulator game.
|
|
121
|
+
- [srtd](https://github.com/t1mmen/srtd) - Live-reloading SQL templates for Supabase projects.
|
|
122
|
+
- [tweakcc](https://github.com/Piebald-AI/tweakcc) - Customize your Claude Code styling.
|
|
123
|
+
- [argonaut](https://github.com/darksworm/argonaut) - Manage Argo CD resources.
|
|
124
|
+
- [Qodo Command](https://github.com/qodo-ai/command) - Build, run, and manage AI agents.
|
|
125
|
+
- [Nanocoder](https://github.com/nano-collective/nanocoder) - A community-built, local-first AI coding agent with multi-provider support.
|
|
126
|
+
- [Neovate Code](https://github.com/neovateai/neovate-code) - An agentic coding tool made by AntGroup.
|
|
127
|
+
- [instagram-cli](https://github.com/supreme-gg-gg/instagram-cli) - Instagram client.
|
|
128
|
+
- [ElevenLabs CLI](https://github.com/elevenlabs/cli) - ElevenLabs agents client.
|
|
129
|
+
|
|
130
|
+
*(PRs welcome. Append new entries at the end. Repos must have 100+ stars and showcase Ink beyond a basic list picker.)*
|
|
131
|
+
|
|
132
|
+
## Contents
|
|
133
|
+
|
|
134
|
+
- [Getting Started](#getting-started)
|
|
135
|
+
- [Components](#components)
|
|
136
|
+
- [`<Text>`](#text)
|
|
137
|
+
- [`<Box>`](#box)
|
|
138
|
+
- [`<Newline>`](#newline)
|
|
139
|
+
- [`<Spacer>`](#spacer)
|
|
140
|
+
- [`<Static>`](#static)
|
|
141
|
+
- [`<Transform>`](#transform)
|
|
142
|
+
- [Hooks](#hooks)
|
|
143
|
+
- [`useInput`](#useinputinputhandler-options)
|
|
144
|
+
- [`useApp`](#useapp)
|
|
145
|
+
- [`useStdin`](#usestdin)
|
|
146
|
+
- [`useStdout`](#usestdout)
|
|
147
|
+
- [`useStderr`](#usestderr)
|
|
148
|
+
- [`useFocus`](#usefocusoptions)
|
|
149
|
+
- [`useFocusManager`](#usefocusmanager)
|
|
150
|
+
- [API](#api)
|
|
151
|
+
- [Testing](#testing)
|
|
152
|
+
- [Using React Devtools](#using-react-devtools)
|
|
153
|
+
- [Screen Reader Support](#screen-reader-support)
|
|
154
|
+
- [Useful Components](#useful-components)
|
|
155
|
+
- [Useful Hooks](#useful-hooks)
|
|
156
|
+
- [Examples](#examples)
|
|
157
|
+
|
|
158
|
+
## Getting Started
|
|
159
|
+
|
|
160
|
+
Use [create-ink-app](https://github.com/vadimdemedes/create-ink-app) to quickly scaffold a new Ink-based CLI.
|
|
161
|
+
|
|
162
|
+
```sh
|
|
163
|
+
npx create-ink-app my-ink-cli
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Alternatively, create a TypeScript project:
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
npx create-ink-app --typescript my-ink-cli
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
<details><summary>Manual JavaScript setup</summary>
|
|
173
|
+
<p>
|
|
174
|
+
Ink requires the same Babel setup as you would do for regular React-based apps in the browser.
|
|
175
|
+
|
|
176
|
+
Set up Babel with a React preset to ensure all examples in this readme work as expected.
|
|
177
|
+
After [installing Babel](https://babeljs.io/docs/en/usage), install `@babel/preset-react` and insert the following configuration in `babel.config.json`:
|
|
178
|
+
|
|
179
|
+
```sh
|
|
180
|
+
npm install --save-dev @babel/preset-react
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
```json
|
|
184
|
+
{
|
|
185
|
+
"presets": ["@babel/preset-react"]
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Next, create a file `source.js`, where you'll type code that uses Ink:
|
|
190
|
+
|
|
191
|
+
```jsx
|
|
192
|
+
import React from 'react';
|
|
193
|
+
import {render, Text} from 'ink';
|
|
194
|
+
|
|
195
|
+
const Demo = () => <Text>Hello World</Text>;
|
|
196
|
+
|
|
197
|
+
render(<Demo />);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Then, transpile this file with Babel:
|
|
201
|
+
|
|
202
|
+
```sh
|
|
203
|
+
npx babel source.js -o cli.js
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Now you can run `cli.js` with Node.js:
|
|
207
|
+
|
|
208
|
+
```sh
|
|
209
|
+
node cli
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
If you don't like transpiling files during development, you can use [import-jsx](https://github.com/vadimdemedes/import-jsx) or [@esbuild-kit/esm-loader](https://github.com/esbuild-kit/esm-loader) to `import` a JSX file and transpile it on the fly.
|
|
213
|
+
|
|
214
|
+
</p>
|
|
215
|
+
</details>
|
|
216
|
+
|
|
217
|
+
Ink uses [Yoga](https://github.com/facebook/yoga), a Flexbox layout engine, to build great user interfaces for your CLIs using familiar CSS-like properties you've used when building apps for the browser.
|
|
218
|
+
It's important to remember that each element is a Flexbox container.
|
|
219
|
+
Think of it as if every `<div>` in the browser had `display: flex`.
|
|
220
|
+
See [`<Box>`](#box) built-in component below for documentation on how to use Flexbox layouts in Ink.
|
|
221
|
+
Note that all text must be wrapped in a [`<Text>`](#text) component.
|
|
222
|
+
|
|
223
|
+
## Components
|
|
224
|
+
|
|
225
|
+
### `<Text>`
|
|
226
|
+
|
|
227
|
+
This component can display text and change its style to make it bold, underlined, italic, or strikethrough.
|
|
228
|
+
|
|
229
|
+
```jsx
|
|
230
|
+
import {render, Text} from 'ink';
|
|
231
|
+
|
|
232
|
+
const Example = () => (
|
|
233
|
+
<>
|
|
234
|
+
<Text color="green">I am green</Text>
|
|
235
|
+
<Text color="black" backgroundColor="white">
|
|
236
|
+
I am black on white
|
|
237
|
+
</Text>
|
|
238
|
+
<Text color="#ffffff">I am white</Text>
|
|
239
|
+
<Text bold>I am bold</Text>
|
|
240
|
+
<Text italic>I am italic</Text>
|
|
241
|
+
<Text underline>I am underline</Text>
|
|
242
|
+
<Text strikethrough>I am strikethrough</Text>
|
|
243
|
+
<Text inverse>I am inversed</Text>
|
|
244
|
+
</>
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
render(<Example />);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
**Note:** `<Text>` allows only text nodes and nested `<Text>` components inside of it. For example, `<Box>` component can't be used inside `<Text>`.
|
|
251
|
+
|
|
252
|
+
#### color
|
|
253
|
+
|
|
254
|
+
Type: `string`
|
|
255
|
+
|
|
256
|
+
Change text color.
|
|
257
|
+
Ink uses [chalk](https://github.com/chalk/chalk) under the hood, so all its functionality is supported.
|
|
258
|
+
|
|
259
|
+
```jsx
|
|
260
|
+
<Text color="green">Green</Text>
|
|
261
|
+
<Text color="#005cc5">Blue</Text>
|
|
262
|
+
<Text color="rgb(232, 131, 136)">Red</Text>
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
<img src="media/text-color.jpg" width="247">
|
|
266
|
+
|
|
267
|
+
#### backgroundColor
|
|
268
|
+
|
|
269
|
+
Type: `string`
|
|
270
|
+
|
|
271
|
+
Same as `color` above, but for background.
|
|
272
|
+
|
|
273
|
+
```jsx
|
|
274
|
+
<Text backgroundColor="green" color="white">Green</Text>
|
|
275
|
+
<Text backgroundColor="#005cc5" color="white">Blue</Text>
|
|
276
|
+
<Text backgroundColor="rgb(232, 131, 136)" color="white">Red</Text>
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
<img src="media/text-backgroundColor.jpg" width="226">
|
|
280
|
+
|
|
281
|
+
#### dimColor
|
|
282
|
+
|
|
283
|
+
Type: `boolean`\
|
|
284
|
+
Default: `false`
|
|
285
|
+
|
|
286
|
+
Dim the color (make it less bright).
|
|
287
|
+
|
|
288
|
+
```jsx
|
|
289
|
+
<Text color="red" dimColor>
|
|
290
|
+
Dimmed Red
|
|
291
|
+
</Text>
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
<img src="media/text-dimColor.jpg" width="138">
|
|
295
|
+
|
|
296
|
+
#### bold
|
|
297
|
+
|
|
298
|
+
Type: `boolean`\
|
|
299
|
+
Default: `false`
|
|
300
|
+
|
|
301
|
+
Make the text bold.
|
|
302
|
+
|
|
303
|
+
#### italic
|
|
304
|
+
|
|
305
|
+
Type: `boolean`\
|
|
306
|
+
Default: `false`
|
|
307
|
+
|
|
308
|
+
Make the text italic.
|
|
309
|
+
|
|
310
|
+
#### underline
|
|
311
|
+
|
|
312
|
+
Type: `boolean`\
|
|
313
|
+
Default: `false`
|
|
314
|
+
|
|
315
|
+
Make the text underlined.
|
|
316
|
+
|
|
317
|
+
#### strikethrough
|
|
318
|
+
|
|
319
|
+
Type: `boolean`\
|
|
320
|
+
Default: `false`
|
|
321
|
+
|
|
322
|
+
Make the text crossed with a line.
|
|
323
|
+
|
|
324
|
+
#### inverse
|
|
325
|
+
|
|
326
|
+
Type: `boolean`\
|
|
327
|
+
Default: `false`
|
|
328
|
+
|
|
329
|
+
Invert background and foreground colors.
|
|
330
|
+
|
|
331
|
+
```jsx
|
|
332
|
+
<Text inverse color="yellow">
|
|
333
|
+
Inversed Yellow
|
|
334
|
+
</Text>
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
<img src="media/text-inverse.jpg" width="138">
|
|
338
|
+
|
|
339
|
+
#### wrap
|
|
340
|
+
|
|
341
|
+
Type: `string`\
|
|
342
|
+
Allowed values: `wrap` `truncate` `truncate-start` `truncate-middle` `truncate-end`\
|
|
343
|
+
Default: `wrap`
|
|
344
|
+
|
|
345
|
+
This property tells Ink to wrap or truncate text if its width is larger than the container.
|
|
346
|
+
If `wrap` is passed (the default), Ink will wrap text and split it into multiple lines.
|
|
347
|
+
If `truncate-*` is passed, Ink will truncate text instead, resulting in one line of text with the rest cut off.
|
|
348
|
+
|
|
349
|
+
```jsx
|
|
350
|
+
<Box width={7}>
|
|
351
|
+
<Text>Hello World</Text>
|
|
352
|
+
</Box>
|
|
353
|
+
//=> 'Hello\nWorld'
|
|
354
|
+
|
|
355
|
+
// `truncate` is an alias to `truncate-end`
|
|
356
|
+
<Box width={7}>
|
|
357
|
+
<Text wrap="truncate">Hello World</Text>
|
|
358
|
+
</Box>
|
|
359
|
+
//=> 'Hello…'
|
|
360
|
+
|
|
361
|
+
<Box width={7}>
|
|
362
|
+
<Text wrap="truncate-middle">Hello World</Text>
|
|
363
|
+
</Box>
|
|
364
|
+
//=> 'He…ld'
|
|
365
|
+
|
|
366
|
+
<Box width={7}>
|
|
367
|
+
<Text wrap="truncate-start">Hello World</Text>
|
|
368
|
+
</Box>
|
|
369
|
+
//=> '…World'
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### `<Box>`
|
|
373
|
+
|
|
374
|
+
`<Box>` is an essential Ink component to build your layout.
|
|
375
|
+
It's like `<div style="display: flex">` in the browser.
|
|
376
|
+
|
|
377
|
+
```jsx
|
|
378
|
+
import {render, Box, Text} from 'ink';
|
|
379
|
+
|
|
380
|
+
const Example = () => (
|
|
381
|
+
<Box margin={2}>
|
|
382
|
+
<Text>This is a box with margin</Text>
|
|
383
|
+
</Box>
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
render(<Example />);
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
#### Dimensions
|
|
390
|
+
|
|
391
|
+
##### width
|
|
392
|
+
|
|
393
|
+
Type: `number` `string`
|
|
394
|
+
|
|
395
|
+
Width of the element in spaces.
|
|
396
|
+
You can also set it as a percentage, which will calculate the width based on the width of the parent element.
|
|
397
|
+
|
|
398
|
+
```jsx
|
|
399
|
+
<Box width={4}>
|
|
400
|
+
<Text>X</Text>
|
|
401
|
+
</Box>
|
|
402
|
+
//=> 'X '
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
```jsx
|
|
406
|
+
<Box width={10}>
|
|
407
|
+
<Box width="50%">
|
|
408
|
+
<Text>X</Text>
|
|
409
|
+
</Box>
|
|
410
|
+
<Text>Y</Text>
|
|
411
|
+
</Box>
|
|
412
|
+
//=> 'X Y'
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
##### height
|
|
416
|
+
|
|
417
|
+
Type: `number` `string`
|
|
418
|
+
|
|
419
|
+
Height of the element in lines (rows).
|
|
420
|
+
You can also set it as a percentage, which will calculate the height based on the height of the parent element.
|
|
421
|
+
|
|
422
|
+
```jsx
|
|
423
|
+
<Box height={4}>
|
|
424
|
+
<Text>X</Text>
|
|
425
|
+
</Box>
|
|
426
|
+
//=> 'X\n\n\n'
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
```jsx
|
|
430
|
+
<Box height={6} flexDirection="column">
|
|
431
|
+
<Box height="50%">
|
|
432
|
+
<Text>X</Text>
|
|
433
|
+
</Box>
|
|
434
|
+
<Text>Y</Text>
|
|
435
|
+
</Box>
|
|
436
|
+
//=> 'X\n\n\nY\n\n'
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
##### minWidth
|
|
440
|
+
|
|
441
|
+
Type: `number`
|
|
442
|
+
|
|
443
|
+
Sets a minimum width of the element.
|
|
444
|
+
Percentages aren't supported yet; see https://github.com/facebook/yoga/issues/872.
|
|
445
|
+
|
|
446
|
+
##### minHeight
|
|
447
|
+
|
|
448
|
+
Type: `number`
|
|
449
|
+
|
|
450
|
+
Sets a minimum height of the element.
|
|
451
|
+
Percentages aren't supported yet; see https://github.com/facebook/yoga/issues/872.
|
|
452
|
+
|
|
453
|
+
#### Padding
|
|
454
|
+
|
|
455
|
+
##### paddingTop
|
|
456
|
+
|
|
457
|
+
Type: `number`\
|
|
458
|
+
Default: `0`
|
|
459
|
+
|
|
460
|
+
Top padding.
|
|
461
|
+
|
|
462
|
+
##### paddingBottom
|
|
463
|
+
|
|
464
|
+
Type: `number`\
|
|
465
|
+
Default: `0`
|
|
466
|
+
|
|
467
|
+
Bottom padding.
|
|
468
|
+
|
|
469
|
+
##### paddingLeft
|
|
470
|
+
|
|
471
|
+
Type: `number`\
|
|
472
|
+
Default: `0`
|
|
473
|
+
|
|
474
|
+
Left padding.
|
|
475
|
+
|
|
476
|
+
##### paddingRight
|
|
477
|
+
|
|
478
|
+
Type: `number`\
|
|
479
|
+
Default: `0`
|
|
480
|
+
|
|
481
|
+
Right padding.
|
|
482
|
+
|
|
483
|
+
##### paddingX
|
|
484
|
+
|
|
485
|
+
Type: `number`\
|
|
486
|
+
Default: `0`
|
|
487
|
+
|
|
488
|
+
Horizontal padding. Equivalent to setting `paddingLeft` and `paddingRight`.
|
|
489
|
+
|
|
490
|
+
##### paddingY
|
|
491
|
+
|
|
492
|
+
Type: `number`\
|
|
493
|
+
Default: `0`
|
|
494
|
+
|
|
495
|
+
Vertical padding. Equivalent to setting `paddingTop` and `paddingBottom`.
|
|
496
|
+
|
|
497
|
+
##### padding
|
|
498
|
+
|
|
499
|
+
Type: `number`\
|
|
500
|
+
Default: `0`
|
|
501
|
+
|
|
502
|
+
Padding on all sides. Equivalent to setting `paddingTop`, `paddingBottom`, `paddingLeft` and `paddingRight`.
|
|
503
|
+
|
|
504
|
+
```jsx
|
|
505
|
+
<Box paddingTop={2}><Text>Top</Text></Box>
|
|
506
|
+
<Box paddingBottom={2}><Text>Bottom</Text></Box>
|
|
507
|
+
<Box paddingLeft={2}><Text>Left</Text></Box>
|
|
508
|
+
<Box paddingRight={2}><Text>Right</Text></Box>
|
|
509
|
+
<Box paddingX={2}><Text>Left and right</Text></Box>
|
|
510
|
+
<Box paddingY={2}><Text>Top and bottom</Text></Box>
|
|
511
|
+
<Box padding={2}><Text>Top, bottom, left and right</Text></Box>
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
#### Margin
|
|
515
|
+
|
|
516
|
+
##### marginTop
|
|
517
|
+
|
|
518
|
+
Type: `number`\
|
|
519
|
+
Default: `0`
|
|
520
|
+
|
|
521
|
+
Top margin.
|
|
522
|
+
|
|
523
|
+
##### marginBottom
|
|
524
|
+
|
|
525
|
+
Type: `number`\
|
|
526
|
+
Default: `0`
|
|
527
|
+
|
|
528
|
+
Bottom margin.
|
|
529
|
+
|
|
530
|
+
##### marginLeft
|
|
531
|
+
|
|
532
|
+
Type: `number`\
|
|
533
|
+
Default: `0`
|
|
534
|
+
|
|
535
|
+
Left margin.
|
|
536
|
+
|
|
537
|
+
##### marginRight
|
|
538
|
+
|
|
539
|
+
Type: `number`\
|
|
540
|
+
Default: `0`
|
|
541
|
+
|
|
542
|
+
Right margin.
|
|
543
|
+
|
|
544
|
+
##### marginX
|
|
545
|
+
|
|
546
|
+
Type: `number`\
|
|
547
|
+
Default: `0`
|
|
548
|
+
|
|
549
|
+
Horizontal margin. Equivalent to setting `marginLeft` and `marginRight`.
|
|
550
|
+
|
|
551
|
+
##### marginY
|
|
552
|
+
|
|
553
|
+
Type: `number`\
|
|
554
|
+
Default: `0`
|
|
555
|
+
|
|
556
|
+
Vertical margin. Equivalent to setting `marginTop` and `marginBottom`.
|
|
557
|
+
|
|
558
|
+
##### margin
|
|
559
|
+
|
|
560
|
+
Type: `number`\
|
|
561
|
+
Default: `0`
|
|
562
|
+
|
|
563
|
+
Margin on all sides. Equivalent to setting `marginTop`, `marginBottom`, `marginLeft` and `marginRight`.
|
|
564
|
+
|
|
565
|
+
```jsx
|
|
566
|
+
<Box marginTop={2}><Text>Top</Text></Box>
|
|
567
|
+
<Box marginBottom={2}><Text>Bottom</Text></Box>
|
|
568
|
+
<Box marginLeft={2}><Text>Left</Text></Box>
|
|
569
|
+
<Box marginRight={2}><Text>Right</Text></Box>
|
|
570
|
+
<Box marginX={2}><Text>Left and right</Text></Box>
|
|
571
|
+
<Box marginY={2}><Text>Top and bottom</Text></Box>
|
|
572
|
+
<Box margin={2}><Text>Top, bottom, left and right</Text></Box>
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
#### Gap
|
|
576
|
+
|
|
577
|
+
#### gap
|
|
578
|
+
|
|
579
|
+
Type: `number`\
|
|
580
|
+
Default: `0`
|
|
581
|
+
|
|
582
|
+
Size of the gap between an element's columns and rows. A shorthand for `columnGap` and `rowGap`.
|
|
583
|
+
|
|
584
|
+
```jsx
|
|
585
|
+
<Box gap={1} width={3} flexWrap="wrap">
|
|
586
|
+
<Text>A</Text>
|
|
587
|
+
<Text>B</Text>
|
|
588
|
+
<Text>C</Text>
|
|
589
|
+
</Box>
|
|
590
|
+
// A B
|
|
591
|
+
//
|
|
592
|
+
// C
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
#### columnGap
|
|
596
|
+
|
|
597
|
+
Type: `number`\
|
|
598
|
+
Default: `0`
|
|
599
|
+
|
|
600
|
+
Size of the gap between an element's columns.
|
|
601
|
+
|
|
602
|
+
```jsx
|
|
603
|
+
<Box columnGap={1}>
|
|
604
|
+
<Text>A</Text>
|
|
605
|
+
<Text>B</Text>
|
|
606
|
+
</Box>
|
|
607
|
+
// A B
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
#### rowGap
|
|
611
|
+
|
|
612
|
+
Type: `number`\
|
|
613
|
+
Default: `0`
|
|
614
|
+
|
|
615
|
+
Size of the gap between an element's rows.
|
|
616
|
+
|
|
617
|
+
```jsx
|
|
618
|
+
<Box flexDirection="column" rowGap={1}>
|
|
619
|
+
<Text>A</Text>
|
|
620
|
+
<Text>B</Text>
|
|
621
|
+
</Box>
|
|
622
|
+
// A
|
|
623
|
+
//
|
|
624
|
+
// B
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
#### Flex
|
|
628
|
+
|
|
629
|
+
##### flexGrow
|
|
630
|
+
|
|
631
|
+
Type: `number`\
|
|
632
|
+
Default: `0`
|
|
633
|
+
|
|
634
|
+
See [flex-grow](https://css-tricks.com/almanac/properties/f/flex-grow/).
|
|
635
|
+
|
|
636
|
+
```jsx
|
|
637
|
+
<Box>
|
|
638
|
+
<Text>Label:</Text>
|
|
639
|
+
<Box flexGrow={1}>
|
|
640
|
+
<Text>Fills all remaining space</Text>
|
|
641
|
+
</Box>
|
|
642
|
+
</Box>
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
##### flexShrink
|
|
646
|
+
|
|
647
|
+
Type: `number`\
|
|
648
|
+
Default: `1`
|
|
649
|
+
|
|
650
|
+
See [flex-shrink](https://css-tricks.com/almanac/properties/f/flex-shrink/).
|
|
651
|
+
|
|
652
|
+
```jsx
|
|
653
|
+
<Box width={20}>
|
|
654
|
+
<Box flexShrink={2} width={10}>
|
|
655
|
+
<Text>Will be 1/4</Text>
|
|
656
|
+
</Box>
|
|
657
|
+
<Box width={10}>
|
|
658
|
+
<Text>Will be 3/4</Text>
|
|
659
|
+
</Box>
|
|
660
|
+
</Box>
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
##### flexBasis
|
|
664
|
+
|
|
665
|
+
Type: `number` `string`
|
|
666
|
+
|
|
667
|
+
See [flex-basis](https://css-tricks.com/almanac/properties/f/flex-basis/).
|
|
668
|
+
|
|
669
|
+
```jsx
|
|
670
|
+
<Box width={6}>
|
|
671
|
+
<Box flexBasis={3}>
|
|
672
|
+
<Text>X</Text>
|
|
673
|
+
</Box>
|
|
674
|
+
<Text>Y</Text>
|
|
675
|
+
</Box>
|
|
676
|
+
//=> 'X Y'
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
```jsx
|
|
680
|
+
<Box width={6}>
|
|
681
|
+
<Box flexBasis="50%">
|
|
682
|
+
<Text>X</Text>
|
|
683
|
+
</Box>
|
|
684
|
+
<Text>Y</Text>
|
|
685
|
+
</Box>
|
|
686
|
+
//=> 'X Y'
|
|
687
|
+
```
|
|
688
|
+
|
|
689
|
+
##### flexDirection
|
|
690
|
+
|
|
691
|
+
Type: `string`\
|
|
692
|
+
Allowed values: `row` `row-reverse` `column` `column-reverse`
|
|
693
|
+
|
|
694
|
+
See [flex-direction](https://css-tricks.com/almanac/properties/f/flex-direction/).
|
|
695
|
+
|
|
696
|
+
```jsx
|
|
697
|
+
<Box>
|
|
698
|
+
<Box marginRight={1}>
|
|
699
|
+
<Text>X</Text>
|
|
700
|
+
</Box>
|
|
701
|
+
<Text>Y</Text>
|
|
702
|
+
</Box>
|
|
703
|
+
// X Y
|
|
704
|
+
|
|
705
|
+
<Box flexDirection="row-reverse">
|
|
706
|
+
<Text>X</Text>
|
|
707
|
+
<Box marginRight={1}>
|
|
708
|
+
<Text>Y</Text>
|
|
709
|
+
</Box>
|
|
710
|
+
</Box>
|
|
711
|
+
// Y X
|
|
712
|
+
|
|
713
|
+
<Box flexDirection="column">
|
|
714
|
+
<Text>X</Text>
|
|
715
|
+
<Text>Y</Text>
|
|
716
|
+
</Box>
|
|
717
|
+
// X
|
|
718
|
+
// Y
|
|
719
|
+
|
|
720
|
+
<Box flexDirection="column-reverse">
|
|
721
|
+
<Text>X</Text>
|
|
722
|
+
<Text>Y</Text>
|
|
723
|
+
</Box>
|
|
724
|
+
// Y
|
|
725
|
+
// X
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
##### flexWrap
|
|
729
|
+
|
|
730
|
+
Type: `string`\
|
|
731
|
+
Allowed values: `nowrap` `wrap` `wrap-reverse`
|
|
732
|
+
|
|
733
|
+
See [flex-wrap](https://css-tricks.com/almanac/properties/f/flex-wrap/).
|
|
734
|
+
|
|
735
|
+
```jsx
|
|
736
|
+
<Box width={2} flexWrap="wrap">
|
|
737
|
+
<Text>A</Text>
|
|
738
|
+
<Text>BC</Text>
|
|
739
|
+
</Box>
|
|
740
|
+
// A
|
|
741
|
+
// B C
|
|
742
|
+
```
|
|
743
|
+
|
|
744
|
+
```jsx
|
|
745
|
+
<Box flexDirection="column" height={2} flexWrap="wrap">
|
|
746
|
+
<Text>A</Text>
|
|
747
|
+
<Text>B</Text>
|
|
748
|
+
<Text>C</Text>
|
|
749
|
+
</Box>
|
|
750
|
+
// A C
|
|
751
|
+
// B
|
|
752
|
+
```
|
|
753
|
+
|
|
754
|
+
##### alignItems
|
|
755
|
+
|
|
756
|
+
Type: `string`\
|
|
757
|
+
Allowed values: `flex-start` `center` `flex-end`
|
|
758
|
+
|
|
759
|
+
See [align-items](https://css-tricks.com/almanac/properties/a/align-items/).
|
|
760
|
+
|
|
761
|
+
```jsx
|
|
762
|
+
<Box alignItems="flex-start">
|
|
763
|
+
<Box marginRight={1}>
|
|
764
|
+
<Text>X</Text>
|
|
765
|
+
</Box>
|
|
766
|
+
<Text>
|
|
767
|
+
A
|
|
768
|
+
<Newline/>
|
|
769
|
+
B
|
|
770
|
+
<Newline/>
|
|
771
|
+
C
|
|
772
|
+
</Text>
|
|
773
|
+
</Box>
|
|
774
|
+
// X A
|
|
775
|
+
// B
|
|
776
|
+
// C
|
|
777
|
+
|
|
778
|
+
<Box alignItems="center">
|
|
779
|
+
<Box marginRight={1}>
|
|
780
|
+
<Text>X</Text>
|
|
781
|
+
</Box>
|
|
782
|
+
<Text>
|
|
783
|
+
A
|
|
784
|
+
<Newline/>
|
|
785
|
+
B
|
|
786
|
+
<Newline/>
|
|
787
|
+
C
|
|
788
|
+
</Text>
|
|
789
|
+
</Box>
|
|
790
|
+
// A
|
|
791
|
+
// X B
|
|
792
|
+
// C
|
|
793
|
+
|
|
794
|
+
<Box alignItems="flex-end">
|
|
795
|
+
<Box marginRight={1}>
|
|
796
|
+
<Text>X</Text>
|
|
797
|
+
</Box>
|
|
798
|
+
<Text>
|
|
799
|
+
A
|
|
800
|
+
<Newline/>
|
|
801
|
+
B
|
|
802
|
+
<Newline/>
|
|
803
|
+
C
|
|
804
|
+
</Text>
|
|
805
|
+
</Box>
|
|
806
|
+
// A
|
|
807
|
+
// B
|
|
808
|
+
// X C
|
|
809
|
+
```
|
|
810
|
+
|
|
811
|
+
##### alignSelf
|
|
812
|
+
|
|
813
|
+
Type: `string`\
|
|
814
|
+
Default: `auto`\
|
|
815
|
+
Allowed values: `auto` `flex-start` `center` `flex-end`
|
|
816
|
+
|
|
817
|
+
See [align-self](https://css-tricks.com/almanac/properties/a/align-self/).
|
|
818
|
+
|
|
819
|
+
```jsx
|
|
820
|
+
<Box height={3}>
|
|
821
|
+
<Box alignSelf="flex-start">
|
|
822
|
+
<Text>X</Text>
|
|
823
|
+
</Box>
|
|
824
|
+
</Box>
|
|
825
|
+
// X
|
|
826
|
+
//
|
|
827
|
+
//
|
|
828
|
+
|
|
829
|
+
<Box height={3}>
|
|
830
|
+
<Box alignSelf="center">
|
|
831
|
+
<Text>X</Text>
|
|
832
|
+
</Box>
|
|
833
|
+
</Box>
|
|
834
|
+
//
|
|
835
|
+
// X
|
|
836
|
+
//
|
|
837
|
+
|
|
838
|
+
<Box height={3}>
|
|
839
|
+
<Box alignSelf="flex-end">
|
|
840
|
+
<Text>X</Text>
|
|
841
|
+
</Box>
|
|
842
|
+
</Box>
|
|
843
|
+
//
|
|
844
|
+
//
|
|
845
|
+
// X
|
|
846
|
+
```
|
|
847
|
+
|
|
848
|
+
##### justifyContent
|
|
849
|
+
|
|
850
|
+
Type: `string`\
|
|
851
|
+
Allowed values: `flex-start` `center` `flex-end` `space-between` `space-around` `space-evenly`
|
|
852
|
+
|
|
853
|
+
See [justify-content](https://css-tricks.com/almanac/properties/j/justify-content/).
|
|
854
|
+
|
|
855
|
+
```jsx
|
|
856
|
+
<Box justifyContent="flex-start">
|
|
857
|
+
<Text>X</Text>
|
|
858
|
+
</Box>
|
|
859
|
+
// [X ]
|
|
860
|
+
|
|
861
|
+
<Box justifyContent="center">
|
|
862
|
+
<Text>X</Text>
|
|
863
|
+
</Box>
|
|
864
|
+
// [ X ]
|
|
865
|
+
|
|
866
|
+
<Box justifyContent="flex-end">
|
|
867
|
+
<Text>X</Text>
|
|
868
|
+
</Box>
|
|
869
|
+
// [ X]
|
|
870
|
+
|
|
871
|
+
<Box justifyContent="space-between">
|
|
872
|
+
<Text>X</Text>
|
|
873
|
+
<Text>Y</Text>
|
|
874
|
+
</Box>
|
|
875
|
+
// [X Y]
|
|
876
|
+
|
|
877
|
+
<Box justifyContent="space-around">
|
|
878
|
+
<Text>X</Text>
|
|
879
|
+
<Text>Y</Text>
|
|
880
|
+
</Box>
|
|
881
|
+
// [ X Y ]
|
|
882
|
+
|
|
883
|
+
<Box justifyContent="space-evenly">
|
|
884
|
+
<Text>X</Text>
|
|
885
|
+
<Text>Y</Text>
|
|
886
|
+
</Box>
|
|
887
|
+
// [ X Y ]
|
|
888
|
+
```
|
|
889
|
+
|
|
890
|
+
#### Visibility
|
|
891
|
+
|
|
892
|
+
##### display
|
|
893
|
+
|
|
894
|
+
Type: `string`\
|
|
895
|
+
Allowed values: `flex` `none`\
|
|
896
|
+
Default: `flex`
|
|
897
|
+
|
|
898
|
+
Set this property to `none` to hide the element.
|
|
899
|
+
|
|
900
|
+
##### overflowX
|
|
901
|
+
|
|
902
|
+
Type: `string`\
|
|
903
|
+
Allowed values: `visible` `hidden`\
|
|
904
|
+
Default: `visible`
|
|
905
|
+
|
|
906
|
+
Behavior for an element's overflow in the horizontal direction.
|
|
907
|
+
|
|
908
|
+
##### overflowY
|
|
909
|
+
|
|
910
|
+
Type: `string`\
|
|
911
|
+
Allowed values: `visible` `hidden`\
|
|
912
|
+
Default: `visible`
|
|
913
|
+
|
|
914
|
+
Behavior for an element's overflow in the vertical direction.
|
|
915
|
+
|
|
916
|
+
##### overflow
|
|
917
|
+
|
|
918
|
+
Type: `string`\
|
|
919
|
+
Allowed values: `visible` `hidden`\
|
|
920
|
+
Default: `visible`
|
|
921
|
+
|
|
922
|
+
A shortcut for setting `overflowX` and `overflowY` at the same time.
|
|
923
|
+
|
|
924
|
+
#### Borders
|
|
925
|
+
|
|
926
|
+
##### borderStyle
|
|
927
|
+
|
|
928
|
+
Type: `string`\
|
|
929
|
+
Allowed values: `single` `double` `round` `bold` `singleDouble` `doubleSingle` `classic` | `BoxStyle`
|
|
930
|
+
|
|
931
|
+
Add a border with a specified style.
|
|
932
|
+
If `borderStyle` is `undefined` (the default), no border will be added.
|
|
933
|
+
Ink uses border styles from the [`cli-boxes`](https://github.com/sindresorhus/cli-boxes) module.
|
|
934
|
+
|
|
935
|
+
```jsx
|
|
936
|
+
<Box flexDirection="column">
|
|
937
|
+
<Box>
|
|
938
|
+
<Box borderStyle="single" marginRight={2}>
|
|
939
|
+
<Text>single</Text>
|
|
940
|
+
</Box>
|
|
941
|
+
|
|
942
|
+
<Box borderStyle="double" marginRight={2}>
|
|
943
|
+
<Text>double</Text>
|
|
944
|
+
</Box>
|
|
945
|
+
|
|
946
|
+
<Box borderStyle="round" marginRight={2}>
|
|
947
|
+
<Text>round</Text>
|
|
948
|
+
</Box>
|
|
949
|
+
|
|
950
|
+
<Box borderStyle="bold">
|
|
951
|
+
<Text>bold</Text>
|
|
952
|
+
</Box>
|
|
953
|
+
</Box>
|
|
954
|
+
|
|
955
|
+
<Box marginTop={1}>
|
|
956
|
+
<Box borderStyle="singleDouble" marginRight={2}>
|
|
957
|
+
<Text>singleDouble</Text>
|
|
958
|
+
</Box>
|
|
959
|
+
|
|
960
|
+
<Box borderStyle="doubleSingle" marginRight={2}>
|
|
961
|
+
<Text>doubleSingle</Text>
|
|
962
|
+
</Box>
|
|
963
|
+
|
|
964
|
+
<Box borderStyle="classic">
|
|
965
|
+
<Text>classic</Text>
|
|
966
|
+
</Box>
|
|
967
|
+
</Box>
|
|
968
|
+
</Box>
|
|
969
|
+
```
|
|
970
|
+
|
|
971
|
+
<img src="media/box-borderStyle.jpg" width="521">
|
|
972
|
+
|
|
973
|
+
Alternatively, pass a custom border style like so:
|
|
974
|
+
|
|
975
|
+
```jsx
|
|
976
|
+
<Box
|
|
977
|
+
borderStyle={{
|
|
978
|
+
topLeft: '↘',
|
|
979
|
+
top: '↓',
|
|
980
|
+
topRight: '↙',
|
|
981
|
+
left: '→',
|
|
982
|
+
bottomLeft: '↗',
|
|
983
|
+
bottom: '↑',
|
|
984
|
+
bottomRight: '↖',
|
|
985
|
+
right: '←'
|
|
986
|
+
}}
|
|
987
|
+
>
|
|
988
|
+
<Text>Custom</Text>
|
|
989
|
+
</Box>
|
|
990
|
+
```
|
|
991
|
+
|
|
992
|
+
See example in [examples/borders](examples/borders/borders.tsx).
|
|
993
|
+
|
|
994
|
+
##### borderColor
|
|
995
|
+
|
|
996
|
+
Type: `string`
|
|
997
|
+
|
|
998
|
+
Change border color.
|
|
999
|
+
A shorthand for setting `borderTopColor`, `borderRightColor`, `borderBottomColor`, and `borderLeftColor`.
|
|
1000
|
+
|
|
1001
|
+
```jsx
|
|
1002
|
+
<Box borderStyle="round" borderColor="green">
|
|
1003
|
+
<Text>Green Rounded Box</Text>
|
|
1004
|
+
</Box>
|
|
1005
|
+
```
|
|
1006
|
+
|
|
1007
|
+
<img src="media/box-borderColor.jpg" width="228">
|
|
1008
|
+
|
|
1009
|
+
##### borderTopColor
|
|
1010
|
+
|
|
1011
|
+
Type: `string`
|
|
1012
|
+
|
|
1013
|
+
Change top border color.
|
|
1014
|
+
Accepts the same values as [`color`](#color) in `<Text>` component.
|
|
1015
|
+
|
|
1016
|
+
```jsx
|
|
1017
|
+
<Box borderStyle="round" borderTopColor="green">
|
|
1018
|
+
<Text>Hello world</Text>
|
|
1019
|
+
</Box>
|
|
1020
|
+
```
|
|
1021
|
+
|
|
1022
|
+
##### borderRightColor
|
|
1023
|
+
|
|
1024
|
+
Type: `string`
|
|
1025
|
+
|
|
1026
|
+
Change the right border color.
|
|
1027
|
+
Accepts the same values as [`color`](#color) in `<Text>` component.
|
|
1028
|
+
|
|
1029
|
+
```jsx
|
|
1030
|
+
<Box borderStyle="round" borderRightColor="green">
|
|
1031
|
+
<Text>Hello world</Text>
|
|
1032
|
+
</Box>
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
##### borderBottomColor
|
|
1036
|
+
|
|
1037
|
+
Type: `string`
|
|
1038
|
+
|
|
1039
|
+
Change the bottom border color.
|
|
1040
|
+
Accepts the same values as [`color`](#color) in `<Text>` component.
|
|
1041
|
+
|
|
1042
|
+
```jsx
|
|
1043
|
+
<Box borderStyle="round" borderBottomColor="green">
|
|
1044
|
+
<Text>Hello world</Text>
|
|
1045
|
+
</Box>
|
|
1046
|
+
```
|
|
1047
|
+
|
|
1048
|
+
##### borderLeftColor
|
|
1049
|
+
|
|
1050
|
+
Type: `string`
|
|
1051
|
+
|
|
1052
|
+
Change the left border color.
|
|
1053
|
+
Accepts the same values as [`color`](#color) in `<Text>` component.
|
|
1054
|
+
|
|
1055
|
+
```jsx
|
|
1056
|
+
<Box borderStyle="round" borderLeftColor="green">
|
|
1057
|
+
<Text>Hello world</Text>
|
|
1058
|
+
</Box>
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
##### borderDimColor
|
|
1062
|
+
|
|
1063
|
+
Type: `boolean`\
|
|
1064
|
+
Default: `false`
|
|
1065
|
+
|
|
1066
|
+
Dim the border color.
|
|
1067
|
+
A shorthand for setting `borderTopDimColor`, `borderBottomDimColor`, `borderLeftDimColor`, and `borderRightDimColor`.
|
|
1068
|
+
|
|
1069
|
+
```jsx
|
|
1070
|
+
<Box borderStyle="round" borderDimColor>
|
|
1071
|
+
<Text>Hello world</Text>
|
|
1072
|
+
</Box>
|
|
1073
|
+
```
|
|
1074
|
+
|
|
1075
|
+
##### borderTopDimColor
|
|
1076
|
+
|
|
1077
|
+
Type: `boolean`\
|
|
1078
|
+
Default: `false`
|
|
1079
|
+
|
|
1080
|
+
Dim the top border color.
|
|
1081
|
+
|
|
1082
|
+
```jsx
|
|
1083
|
+
<Box borderStyle="round" borderTopDimColor>
|
|
1084
|
+
<Text>Hello world</Text>
|
|
1085
|
+
</Box>
|
|
1086
|
+
```
|
|
1087
|
+
|
|
1088
|
+
##### borderBottomDimColor
|
|
1089
|
+
|
|
1090
|
+
Type: `boolean`\
|
|
1091
|
+
Default: `false`
|
|
1092
|
+
|
|
1093
|
+
Dim the bottom border color.
|
|
1094
|
+
|
|
1095
|
+
```jsx
|
|
1096
|
+
<Box borderStyle="round" borderBottomDimColor>
|
|
1097
|
+
<Text>Hello world</Text>
|
|
1098
|
+
</Box>
|
|
1099
|
+
```
|
|
1100
|
+
|
|
1101
|
+
##### borderLeftDimColor
|
|
1102
|
+
|
|
1103
|
+
Type: `boolean`\
|
|
1104
|
+
Default: `false`
|
|
1105
|
+
|
|
1106
|
+
Dim the left border color.
|
|
1107
|
+
|
|
1108
|
+
```jsx
|
|
1109
|
+
<Box borderStyle="round" borderLeftDimColor>
|
|
1110
|
+
<Text>Hello world</Text>
|
|
1111
|
+
</Box>
|
|
1112
|
+
```
|
|
1113
|
+
|
|
1114
|
+
##### borderRightDimColor
|
|
1115
|
+
|
|
1116
|
+
Type: `boolean`\
|
|
1117
|
+
Default: `false`
|
|
1118
|
+
|
|
1119
|
+
Dim the right border color.
|
|
1120
|
+
|
|
1121
|
+
```jsx
|
|
1122
|
+
<Box borderStyle="round" borderRightDimColor>
|
|
1123
|
+
<Text>Hello world</Text>
|
|
1124
|
+
</Box>
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
##### borderTop
|
|
1128
|
+
|
|
1129
|
+
Type: `boolean`\
|
|
1130
|
+
Default: `true`
|
|
1131
|
+
|
|
1132
|
+
Determines whether the top border is visible.
|
|
1133
|
+
|
|
1134
|
+
##### borderRight
|
|
1135
|
+
|
|
1136
|
+
Type: `boolean`\
|
|
1137
|
+
Default: `true`
|
|
1138
|
+
|
|
1139
|
+
Determines whether the right border is visible.
|
|
1140
|
+
|
|
1141
|
+
##### borderBottom
|
|
1142
|
+
|
|
1143
|
+
Type: `boolean`\
|
|
1144
|
+
Default: `true`
|
|
1145
|
+
|
|
1146
|
+
Determines whether the bottom border is visible.
|
|
1147
|
+
|
|
1148
|
+
##### borderLeft
|
|
1149
|
+
|
|
1150
|
+
Type: `boolean`\
|
|
1151
|
+
Default: `true`
|
|
1152
|
+
|
|
1153
|
+
Determines whether the left border is visible.
|
|
1154
|
+
|
|
1155
|
+
#### Background
|
|
1156
|
+
|
|
1157
|
+
##### backgroundColor
|
|
1158
|
+
|
|
1159
|
+
Type: `string`
|
|
1160
|
+
|
|
1161
|
+
Background color for the element.
|
|
1162
|
+
|
|
1163
|
+
Accepts the same values as [`color`](#color) in the `<Text>` component.
|
|
1164
|
+
|
|
1165
|
+
```jsx
|
|
1166
|
+
<Box flexDirection="column">
|
|
1167
|
+
<Box backgroundColor="red" width={20} height={5} alignSelf="flex-start">
|
|
1168
|
+
<Text>Red background</Text>
|
|
1169
|
+
</Box>
|
|
1170
|
+
|
|
1171
|
+
<Box backgroundColor="#FF8800" width={20} height={3} marginTop={1} alignSelf="flex-start">
|
|
1172
|
+
<Text>Orange background</Text>
|
|
1173
|
+
</Box>
|
|
1174
|
+
|
|
1175
|
+
<Box backgroundColor="rgb(0, 255, 0)" width={20} height={3} marginTop={1} alignSelf="flex-start">
|
|
1176
|
+
<Text>Green background</Text>
|
|
1177
|
+
</Box>
|
|
1178
|
+
</Box>
|
|
1179
|
+
```
|
|
1180
|
+
|
|
1181
|
+
The background color fills the entire `<Box>` area and is inherited by child `<Text>` components unless they specify their own `backgroundColor`.
|
|
1182
|
+
|
|
1183
|
+
```jsx
|
|
1184
|
+
<Box backgroundColor="blue" alignSelf="flex-start">
|
|
1185
|
+
<Text>Blue inherited </Text>
|
|
1186
|
+
<Text backgroundColor="yellow">Yellow override </Text>
|
|
1187
|
+
<Text>Blue inherited again</Text>
|
|
1188
|
+
</Box>
|
|
1189
|
+
```
|
|
1190
|
+
|
|
1191
|
+
Background colors work with borders and padding:
|
|
1192
|
+
|
|
1193
|
+
```jsx
|
|
1194
|
+
<Box backgroundColor="cyan" borderStyle="round" padding={1} alignSelf="flex-start">
|
|
1195
|
+
<Text>Background with border and padding</Text>
|
|
1196
|
+
</Box>
|
|
1197
|
+
```
|
|
1198
|
+
|
|
1199
|
+
See example in [examples/box-backgrounds](examples/box-backgrounds/box-backgrounds.tsx).
|
|
1200
|
+
|
|
1201
|
+
### `<Newline>`
|
|
1202
|
+
|
|
1203
|
+
Adds one or more newline (`\n`) characters.
|
|
1204
|
+
Must be used within `<Text>` components.
|
|
1205
|
+
|
|
1206
|
+
#### count
|
|
1207
|
+
|
|
1208
|
+
Type: `number`\
|
|
1209
|
+
Default: `1`
|
|
1210
|
+
|
|
1211
|
+
Number of newlines to insert.
|
|
1212
|
+
|
|
1213
|
+
```jsx
|
|
1214
|
+
import {render, Text, Newline} from 'ink';
|
|
1215
|
+
|
|
1216
|
+
const Example = () => (
|
|
1217
|
+
<Text>
|
|
1218
|
+
<Text color="green">Hello</Text>
|
|
1219
|
+
<Newline />
|
|
1220
|
+
<Text color="red">World</Text>
|
|
1221
|
+
</Text>
|
|
1222
|
+
);
|
|
1223
|
+
|
|
1224
|
+
render(<Example />);
|
|
1225
|
+
```
|
|
1226
|
+
|
|
1227
|
+
Output:
|
|
1228
|
+
|
|
1229
|
+
```
|
|
1230
|
+
Hello
|
|
1231
|
+
World
|
|
1232
|
+
```
|
|
1233
|
+
|
|
1234
|
+
### `<Spacer>`
|
|
1235
|
+
|
|
1236
|
+
A flexible space that expands along the major axis of its containing layout.
|
|
1237
|
+
It's useful as a shortcut for filling all the available space between elements.
|
|
1238
|
+
|
|
1239
|
+
For example, using `<Spacer>` in a `<Box>` with default flex direction (`row`) will position "Left" on the left side and will push "Right" to the right side.
|
|
1240
|
+
|
|
1241
|
+
```jsx
|
|
1242
|
+
import {render, Box, Text, Spacer} from 'ink';
|
|
1243
|
+
|
|
1244
|
+
const Example = () => (
|
|
1245
|
+
<Box>
|
|
1246
|
+
<Text>Left</Text>
|
|
1247
|
+
<Spacer />
|
|
1248
|
+
<Text>Right</Text>
|
|
1249
|
+
</Box>
|
|
1250
|
+
);
|
|
1251
|
+
|
|
1252
|
+
render(<Example />);
|
|
1253
|
+
```
|
|
1254
|
+
|
|
1255
|
+
In a vertical flex direction (`column`), it will position "Top" at the top of the container and push "Bottom" to the bottom.
|
|
1256
|
+
Note that the container needs to be tall enough to see this in effect.
|
|
1257
|
+
|
|
1258
|
+
```jsx
|
|
1259
|
+
import {render, Box, Text, Spacer} from 'ink';
|
|
1260
|
+
|
|
1261
|
+
const Example = () => (
|
|
1262
|
+
<Box flexDirection="column" height={10}>
|
|
1263
|
+
<Text>Top</Text>
|
|
1264
|
+
<Spacer />
|
|
1265
|
+
<Text>Bottom</Text>
|
|
1266
|
+
</Box>
|
|
1267
|
+
);
|
|
1268
|
+
|
|
1269
|
+
render(<Example />);
|
|
1270
|
+
```
|
|
1271
|
+
|
|
1272
|
+
### `<Static>`
|
|
1273
|
+
|
|
1274
|
+
`<Static>` component permanently renders its output above everything else.
|
|
1275
|
+
It's useful for displaying activity like completed tasks or logs - things that
|
|
1276
|
+
don't change after they're rendered (hence the name "Static").
|
|
1277
|
+
|
|
1278
|
+
It's preferred to use `<Static>` for use cases like these when you can't know
|
|
1279
|
+
or control the number of items that need to be rendered.
|
|
1280
|
+
|
|
1281
|
+
For example, [Tap](https://github.com/tapjs/node-tap) uses `<Static>` to display
|
|
1282
|
+
a list of completed tests. [Gatsby](https://github.com/gatsbyjs/gatsby) uses it
|
|
1283
|
+
to display a list of generated pages while still displaying a live progress bar.
|
|
1284
|
+
|
|
1285
|
+
```jsx
|
|
1286
|
+
import React, {useState, useEffect} from 'react';
|
|
1287
|
+
import {render, Static, Box, Text} from 'ink';
|
|
1288
|
+
|
|
1289
|
+
const Example = () => {
|
|
1290
|
+
const [tests, setTests] = useState([]);
|
|
1291
|
+
|
|
1292
|
+
useEffect(() => {
|
|
1293
|
+
let completedTests = 0;
|
|
1294
|
+
let timer;
|
|
1295
|
+
|
|
1296
|
+
const run = () => {
|
|
1297
|
+
// Fake 10 completed tests
|
|
1298
|
+
if (completedTests++ < 10) {
|
|
1299
|
+
setTests(previousTests => [
|
|
1300
|
+
...previousTests,
|
|
1301
|
+
{
|
|
1302
|
+
id: previousTests.length,
|
|
1303
|
+
title: `Test #${previousTests.length + 1}`
|
|
1304
|
+
}
|
|
1305
|
+
]);
|
|
1306
|
+
|
|
1307
|
+
timer = setTimeout(run, 100);
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
|
|
1311
|
+
run();
|
|
1312
|
+
|
|
1313
|
+
return () => {
|
|
1314
|
+
clearTimeout(timer);
|
|
1315
|
+
};
|
|
1316
|
+
}, []);
|
|
1317
|
+
|
|
1318
|
+
return (
|
|
1319
|
+
<>
|
|
1320
|
+
{/* This part will be rendered once to the terminal */}
|
|
1321
|
+
<Static items={tests}>
|
|
1322
|
+
{test => (
|
|
1323
|
+
<Box key={test.id}>
|
|
1324
|
+
<Text color="green">✔ {test.title}</Text>
|
|
1325
|
+
</Box>
|
|
1326
|
+
)}
|
|
1327
|
+
</Static>
|
|
1328
|
+
|
|
1329
|
+
{/* This part keeps updating as state changes */}
|
|
1330
|
+
<Box marginTop={1}>
|
|
1331
|
+
<Text dimColor>Completed tests: {tests.length}</Text>
|
|
1332
|
+
</Box>
|
|
1333
|
+
</>
|
|
1334
|
+
);
|
|
1335
|
+
};
|
|
1336
|
+
|
|
1337
|
+
render(<Example />);
|
|
1338
|
+
```
|
|
1339
|
+
|
|
1340
|
+
**Note:** `<Static>` only renders new items in the `items` prop and ignores items
|
|
1341
|
+
that were previously rendered. This means that when you add new items to the `items`
|
|
1342
|
+
array, changes you make to previous items will not trigger a rerender.
|
|
1343
|
+
|
|
1344
|
+
See [examples/static](examples/static/static.tsx) for an example usage of `<Static>` component.
|
|
1345
|
+
|
|
1346
|
+
#### items
|
|
1347
|
+
|
|
1348
|
+
Type: `Array`
|
|
1349
|
+
|
|
1350
|
+
Array of items of any type to render using the function you pass as a component child.
|
|
1351
|
+
|
|
1352
|
+
#### style
|
|
1353
|
+
|
|
1354
|
+
Type: `object`
|
|
1355
|
+
|
|
1356
|
+
Styles to apply to a container of child elements.
|
|
1357
|
+
See [`<Box>`](#box) for supported properties.
|
|
1358
|
+
|
|
1359
|
+
```jsx
|
|
1360
|
+
<Static items={...} style={{padding: 1}}>
|
|
1361
|
+
{...}
|
|
1362
|
+
</Static>
|
|
1363
|
+
```
|
|
1364
|
+
|
|
1365
|
+
#### children(item)
|
|
1366
|
+
|
|
1367
|
+
Type: `Function`
|
|
1368
|
+
|
|
1369
|
+
Function that is called to render every item in the `items` array.
|
|
1370
|
+
The first argument is the item itself, and the second argument is the index of that item in the
|
|
1371
|
+
`items` array.
|
|
1372
|
+
|
|
1373
|
+
Note that a `key` must be assigned to the root component.
|
|
1374
|
+
|
|
1375
|
+
```jsx
|
|
1376
|
+
<Static items={['a', 'b', 'c']}>
|
|
1377
|
+
{(item, index) => {
|
|
1378
|
+
// This function is called for every item in ['a', 'b', 'c']
|
|
1379
|
+
// `item` is 'a', 'b', 'c'
|
|
1380
|
+
// `index` is 0, 1, 2
|
|
1381
|
+
return (
|
|
1382
|
+
<Box key={index}>
|
|
1383
|
+
<Text>Item: {item}</Text>
|
|
1384
|
+
</Box>
|
|
1385
|
+
);
|
|
1386
|
+
}}
|
|
1387
|
+
</Static>
|
|
1388
|
+
```
|
|
1389
|
+
|
|
1390
|
+
### `<Transform>`
|
|
1391
|
+
|
|
1392
|
+
Transform a string representation of React components before they're written to output.
|
|
1393
|
+
For example, you might want to apply a [gradient to text](https://github.com/sindresorhus/ink-gradient), [add a clickable link](https://github.com/sindresorhus/ink-link), or [create some text effects](https://github.com/sindresorhus/ink-big-text).
|
|
1394
|
+
These use cases can't accept React nodes as input; they expect a string.
|
|
1395
|
+
That's what the `<Transform>` component does: it gives you an output string of its child components and lets you transform it in any way.
|
|
1396
|
+
|
|
1397
|
+
**Note:** `<Transform>` must be applied only to `<Text>` children components and shouldn't change the dimensions of the output; otherwise, the layout will be incorrect.
|
|
1398
|
+
|
|
1399
|
+
```jsx
|
|
1400
|
+
import {render, Transform} from 'ink';
|
|
1401
|
+
|
|
1402
|
+
const Example = () => (
|
|
1403
|
+
<Transform transform={output => output.toUpperCase()}>
|
|
1404
|
+
<Text>Hello World</Text>
|
|
1405
|
+
</Transform>
|
|
1406
|
+
);
|
|
1407
|
+
|
|
1408
|
+
render(<Example />);
|
|
1409
|
+
```
|
|
1410
|
+
|
|
1411
|
+
Since the `transform` function converts all characters to uppercase, the final output rendered to the terminal will be "HELLO WORLD", not "Hello World".
|
|
1412
|
+
|
|
1413
|
+
When the output wraps to multiple lines, it can be helpful to know which line is being processed.
|
|
1414
|
+
|
|
1415
|
+
For example, to implement a hanging indent component, you can indent all the lines except for the first.
|
|
1416
|
+
|
|
1417
|
+
```jsx
|
|
1418
|
+
import {render, Transform} from 'ink';
|
|
1419
|
+
|
|
1420
|
+
const HangingIndent = ({content, indent = 4, children, ...props}) => (
|
|
1421
|
+
<Transform
|
|
1422
|
+
transform={(line, index) =>
|
|
1423
|
+
index === 0 ? line : ' '.repeat(indent) + line
|
|
1424
|
+
}
|
|
1425
|
+
{...props}
|
|
1426
|
+
>
|
|
1427
|
+
{children}
|
|
1428
|
+
</Transform>
|
|
1429
|
+
);
|
|
1430
|
+
|
|
1431
|
+
const text =
|
|
1432
|
+
'WHEN I WROTE the following pages, or rather the bulk of them, ' +
|
|
1433
|
+
'I lived alone, in the woods, a mile from any neighbor, in a ' +
|
|
1434
|
+
'house which I had built myself, on the shore of Walden Pond, ' +
|
|
1435
|
+
'in Concord, Massachusetts, and earned my living by the labor ' +
|
|
1436
|
+
'of my hands only. I lived there two years and two months. At ' +
|
|
1437
|
+
'present I am a sojourner in civilized life again.';
|
|
1438
|
+
|
|
1439
|
+
// Other text properties are allowed as well
|
|
1440
|
+
render(
|
|
1441
|
+
<HangingIndent bold dimColor indent={4}>
|
|
1442
|
+
{text}
|
|
1443
|
+
</HangingIndent>
|
|
1444
|
+
);
|
|
1445
|
+
```
|
|
1446
|
+
|
|
1447
|
+
#### transform(outputLine, index)
|
|
1448
|
+
|
|
1449
|
+
Type: `Function`
|
|
1450
|
+
|
|
1451
|
+
Function that transforms children output.
|
|
1452
|
+
It accepts children and must return transformed children as well.
|
|
1453
|
+
|
|
1454
|
+
##### children
|
|
1455
|
+
|
|
1456
|
+
Type: `string`
|
|
1457
|
+
|
|
1458
|
+
Output of child components.
|
|
1459
|
+
|
|
1460
|
+
##### index
|
|
1461
|
+
|
|
1462
|
+
Type: `number`
|
|
1463
|
+
|
|
1464
|
+
The zero-indexed line number of the line that's currently being transformed.
|
|
1465
|
+
|
|
1466
|
+
## Hooks
|
|
1467
|
+
|
|
1468
|
+
### useInput(inputHandler, options?)
|
|
1469
|
+
|
|
1470
|
+
This hook is used for handling user input.
|
|
1471
|
+
It's a more convenient alternative to using `useStdin` and listening for `data` events.
|
|
1472
|
+
The callback you pass to `useInput` is called for each character when the user enters any input.
|
|
1473
|
+
However, if the user pastes text and it's more than one character, the callback will be called only once, and the whole string will be passed as `input`.
|
|
1474
|
+
You can find a full example of using `useInput` at [examples/use-input](examples/use-input/use-input.tsx).
|
|
1475
|
+
|
|
1476
|
+
```jsx
|
|
1477
|
+
import {useInput} from 'ink';
|
|
1478
|
+
|
|
1479
|
+
const UserInput = () => {
|
|
1480
|
+
useInput((input, key) => {
|
|
1481
|
+
if (input === 'q') {
|
|
1482
|
+
// Exit program
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
if (key.leftArrow) {
|
|
1486
|
+
// Left arrow key pressed
|
|
1487
|
+
}
|
|
1488
|
+
});
|
|
1489
|
+
|
|
1490
|
+
return …
|
|
1491
|
+
};
|
|
1492
|
+
```
|
|
1493
|
+
|
|
1494
|
+
#### inputHandler(input, key)
|
|
1495
|
+
|
|
1496
|
+
Type: `Function`
|
|
1497
|
+
|
|
1498
|
+
The handler function that you pass to `useInput` receives two arguments:
|
|
1499
|
+
|
|
1500
|
+
##### input
|
|
1501
|
+
|
|
1502
|
+
Type: `string`
|
|
1503
|
+
|
|
1504
|
+
The input that the program received.
|
|
1505
|
+
|
|
1506
|
+
##### key
|
|
1507
|
+
|
|
1508
|
+
Type: `object`
|
|
1509
|
+
|
|
1510
|
+
Handy information about a key that was pressed.
|
|
1511
|
+
|
|
1512
|
+
###### key.leftArrow
|
|
1513
|
+
|
|
1514
|
+
###### key.rightArrow
|
|
1515
|
+
|
|
1516
|
+
###### key.upArrow
|
|
1517
|
+
|
|
1518
|
+
###### key.downArrow
|
|
1519
|
+
|
|
1520
|
+
Type: `boolean`\
|
|
1521
|
+
Default: `false`
|
|
1522
|
+
|
|
1523
|
+
If an arrow key was pressed, the corresponding property will be `true`.
|
|
1524
|
+
For example, if the user presses the left arrow key, `key.leftArrow` equals `true`.
|
|
1525
|
+
|
|
1526
|
+
###### key.return
|
|
1527
|
+
|
|
1528
|
+
Type: `boolean`\
|
|
1529
|
+
Default: `false`
|
|
1530
|
+
|
|
1531
|
+
Return (Enter) key was pressed.
|
|
1532
|
+
|
|
1533
|
+
###### key.escape
|
|
1534
|
+
|
|
1535
|
+
Type: `boolean`\
|
|
1536
|
+
Default: `false`
|
|
1537
|
+
|
|
1538
|
+
Escape key was pressed.
|
|
1539
|
+
|
|
1540
|
+
###### key.ctrl
|
|
1541
|
+
|
|
1542
|
+
Type: `boolean`\
|
|
1543
|
+
Default: `false`
|
|
1544
|
+
|
|
1545
|
+
Ctrl key was pressed.
|
|
1546
|
+
|
|
1547
|
+
###### key.shift
|
|
1548
|
+
|
|
1549
|
+
Type: `boolean`\
|
|
1550
|
+
Default: `false`
|
|
1551
|
+
|
|
1552
|
+
Shift key was pressed.
|
|
1553
|
+
|
|
1554
|
+
###### key.tab
|
|
1555
|
+
|
|
1556
|
+
Type: `boolean`\
|
|
1557
|
+
Default: `false`
|
|
1558
|
+
|
|
1559
|
+
Tab key was pressed.
|
|
1560
|
+
|
|
1561
|
+
###### key.backspace
|
|
1562
|
+
|
|
1563
|
+
Type: `boolean`\
|
|
1564
|
+
Default: `false`
|
|
1565
|
+
|
|
1566
|
+
Backspace key was pressed.
|
|
1567
|
+
|
|
1568
|
+
###### key.delete
|
|
1569
|
+
|
|
1570
|
+
Type: `boolean`\
|
|
1571
|
+
Default: `false`
|
|
1572
|
+
|
|
1573
|
+
Delete key was pressed.
|
|
1574
|
+
|
|
1575
|
+
###### key.pageDown
|
|
1576
|
+
|
|
1577
|
+
###### key.pageUp
|
|
1578
|
+
|
|
1579
|
+
Type: `boolean`\
|
|
1580
|
+
Default: `false`
|
|
1581
|
+
|
|
1582
|
+
If the Page Up or Page Down key was pressed, the corresponding property will be `true`.
|
|
1583
|
+
For example, if the user presses Page Down, `key.pageDown` equals `true`.
|
|
1584
|
+
|
|
1585
|
+
###### key.meta
|
|
1586
|
+
|
|
1587
|
+
Type: `boolean`\
|
|
1588
|
+
Default: `false`
|
|
1589
|
+
|
|
1590
|
+
[Meta key](https://en.wikipedia.org/wiki/Meta_key) was pressed.
|
|
1591
|
+
|
|
1592
|
+
#### options
|
|
1593
|
+
|
|
1594
|
+
Type: `object`
|
|
1595
|
+
|
|
1596
|
+
##### isActive
|
|
1597
|
+
|
|
1598
|
+
Type: `boolean`\
|
|
1599
|
+
Default: `true`
|
|
1600
|
+
|
|
1601
|
+
Enable or disable capturing of user input.
|
|
1602
|
+
Useful when there are multiple `useInput` hooks used at once to avoid handling the same input several times.
|
|
1603
|
+
|
|
1604
|
+
### useApp()
|
|
1605
|
+
|
|
1606
|
+
`useApp` is a React hook that exposes a method to manually exit the app (unmount).
|
|
1607
|
+
|
|
1608
|
+
#### exit(error?)
|
|
1609
|
+
|
|
1610
|
+
Type: `Function`
|
|
1611
|
+
|
|
1612
|
+
Exit (unmount) the whole Ink app.
|
|
1613
|
+
|
|
1614
|
+
##### error
|
|
1615
|
+
|
|
1616
|
+
Type: `Error`
|
|
1617
|
+
|
|
1618
|
+
Optional error. If passed, [`waitUntilExit`](waituntilexit) will reject with that error.
|
|
1619
|
+
|
|
1620
|
+
```js
|
|
1621
|
+
import {useApp} from 'ink';
|
|
1622
|
+
|
|
1623
|
+
const Example = () => {
|
|
1624
|
+
const {exit} = useApp();
|
|
1625
|
+
|
|
1626
|
+
// Exit the app after 5 seconds
|
|
1627
|
+
useEffect(() => {
|
|
1628
|
+
setTimeout(() => {
|
|
1629
|
+
exit();
|
|
1630
|
+
}, 5000);
|
|
1631
|
+
}, []);
|
|
1632
|
+
|
|
1633
|
+
return …
|
|
1634
|
+
};
|
|
1635
|
+
```
|
|
1636
|
+
|
|
1637
|
+
### useStdin()
|
|
1638
|
+
|
|
1639
|
+
`useStdin` is a React hook that exposes the stdin stream.
|
|
1640
|
+
|
|
1641
|
+
#### stdin
|
|
1642
|
+
|
|
1643
|
+
Type: `stream.Readable`\
|
|
1644
|
+
Default: `process.stdin`
|
|
1645
|
+
|
|
1646
|
+
The stdin stream passed to `render()` in `options.stdin`, or `process.stdin` by default.
|
|
1647
|
+
Useful if your app needs to handle user input.
|
|
1648
|
+
|
|
1649
|
+
```js
|
|
1650
|
+
import {useStdin} from 'ink';
|
|
1651
|
+
|
|
1652
|
+
const Example = () => {
|
|
1653
|
+
const {stdin} = useStdin();
|
|
1654
|
+
|
|
1655
|
+
return …
|
|
1656
|
+
};
|
|
1657
|
+
```
|
|
1658
|
+
|
|
1659
|
+
#### isRawModeSupported
|
|
1660
|
+
|
|
1661
|
+
Type: `boolean`
|
|
1662
|
+
|
|
1663
|
+
A boolean flag determining if the current `stdin` supports `setRawMode`.
|
|
1664
|
+
A component using `setRawMode` might want to use `isRawModeSupported` to nicely fall back in environments where raw mode is not supported.
|
|
1665
|
+
|
|
1666
|
+
```jsx
|
|
1667
|
+
import {useStdin} from 'ink';
|
|
1668
|
+
|
|
1669
|
+
const Example = () => {
|
|
1670
|
+
const {isRawModeSupported} = useStdin();
|
|
1671
|
+
|
|
1672
|
+
return isRawModeSupported ? (
|
|
1673
|
+
<MyInputComponent />
|
|
1674
|
+
) : (
|
|
1675
|
+
<MyComponentThatDoesntUseInput />
|
|
1676
|
+
);
|
|
1677
|
+
};
|
|
1678
|
+
```
|
|
1679
|
+
|
|
1680
|
+
#### setRawMode(isRawModeEnabled)
|
|
1681
|
+
|
|
1682
|
+
Type: `function`
|
|
1683
|
+
|
|
1684
|
+
##### isRawModeEnabled
|
|
1685
|
+
|
|
1686
|
+
Type: `boolean`
|
|
1687
|
+
|
|
1688
|
+
See [`setRawMode`](https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode).
|
|
1689
|
+
Ink exposes this function to be able to handle <kbd>Ctrl</kbd>+<kbd>C</kbd>, that's why you should use Ink's `setRawMode` instead of `process.stdin.setRawMode`.
|
|
1690
|
+
|
|
1691
|
+
**Warning:** This function will throw unless the current `stdin` supports `setRawMode`. Use [`isRawModeSupported`](#israwmodesupported) to detect `setRawMode` support.
|
|
1692
|
+
|
|
1693
|
+
```js
|
|
1694
|
+
import {useStdin} from 'ink';
|
|
1695
|
+
|
|
1696
|
+
const Example = () => {
|
|
1697
|
+
const {setRawMode} = useStdin();
|
|
1698
|
+
|
|
1699
|
+
useEffect(() => {
|
|
1700
|
+
setRawMode(true);
|
|
1701
|
+
|
|
1702
|
+
return () => {
|
|
1703
|
+
setRawMode(false);
|
|
1704
|
+
};
|
|
1705
|
+
});
|
|
1706
|
+
|
|
1707
|
+
return …
|
|
1708
|
+
};
|
|
1709
|
+
```
|
|
1710
|
+
|
|
1711
|
+
### useStdout()
|
|
1712
|
+
|
|
1713
|
+
`useStdout` is a React hook that exposes the stdout stream where Ink renders your app.
|
|
1714
|
+
|
|
1715
|
+
#### stdout
|
|
1716
|
+
|
|
1717
|
+
Type: `stream.Writable`\
|
|
1718
|
+
Default: `process.stdout`
|
|
1719
|
+
|
|
1720
|
+
```js
|
|
1721
|
+
import {useStdout} from 'ink';
|
|
1722
|
+
|
|
1723
|
+
const Example = () => {
|
|
1724
|
+
const {stdout} = useStdout();
|
|
1725
|
+
|
|
1726
|
+
return …
|
|
1727
|
+
};
|
|
1728
|
+
```
|
|
1729
|
+
|
|
1730
|
+
#### write(data)
|
|
1731
|
+
|
|
1732
|
+
Write any string to stdout while preserving Ink's output.
|
|
1733
|
+
It's useful when you want to display external information outside of Ink's rendering and ensure there's no conflict between the two.
|
|
1734
|
+
It's similar to `<Static>`, except it can't accept components; it only works with strings.
|
|
1735
|
+
|
|
1736
|
+
##### data
|
|
1737
|
+
|
|
1738
|
+
Type: `string`
|
|
1739
|
+
|
|
1740
|
+
Data to write to stdout.
|
|
1741
|
+
|
|
1742
|
+
```js
|
|
1743
|
+
import {useStdout} from 'ink';
|
|
1744
|
+
|
|
1745
|
+
const Example = () => {
|
|
1746
|
+
const {write} = useStdout();
|
|
1747
|
+
|
|
1748
|
+
useEffect(() => {
|
|
1749
|
+
// Write a single message to stdout, above Ink's output
|
|
1750
|
+
write('Hello from Ink to stdout\n');
|
|
1751
|
+
}, []);
|
|
1752
|
+
|
|
1753
|
+
return …
|
|
1754
|
+
};
|
|
1755
|
+
```
|
|
1756
|
+
|
|
1757
|
+
See additional usage example in [examples/use-stdout](examples/use-stdout/use-stdout.tsx).
|
|
1758
|
+
|
|
1759
|
+
### useStderr()
|
|
1760
|
+
|
|
1761
|
+
`useStderr` is a React hook that exposes the stderr stream.
|
|
1762
|
+
|
|
1763
|
+
#### stderr
|
|
1764
|
+
|
|
1765
|
+
Type: `stream.Writable`\
|
|
1766
|
+
Default: `process.stderr`
|
|
1767
|
+
|
|
1768
|
+
Stderr stream.
|
|
1769
|
+
|
|
1770
|
+
```js
|
|
1771
|
+
import {useStderr} from 'ink';
|
|
1772
|
+
|
|
1773
|
+
const Example = () => {
|
|
1774
|
+
const {stderr} = useStderr();
|
|
1775
|
+
|
|
1776
|
+
return …
|
|
1777
|
+
};
|
|
1778
|
+
```
|
|
1779
|
+
|
|
1780
|
+
#### write(data)
|
|
1781
|
+
|
|
1782
|
+
Write any string to stderr while preserving Ink's output.
|
|
1783
|
+
|
|
1784
|
+
It's useful when you want to display external information outside of Ink's rendering and ensure there's no conflict between the two.
|
|
1785
|
+
It's similar to `<Static>`, except it can't accept components; it only works with strings.
|
|
1786
|
+
|
|
1787
|
+
##### data
|
|
1788
|
+
|
|
1789
|
+
Type: `string`
|
|
1790
|
+
|
|
1791
|
+
Data to write to stderr.
|
|
1792
|
+
|
|
1793
|
+
```js
|
|
1794
|
+
import {useStderr} from 'ink';
|
|
1795
|
+
|
|
1796
|
+
const Example = () => {
|
|
1797
|
+
const {write} = useStderr();
|
|
1798
|
+
|
|
1799
|
+
useEffect(() => {
|
|
1800
|
+
// Write a single message to stderr, above Ink's output
|
|
1801
|
+
write('Hello from Ink to stderr\n');
|
|
1802
|
+
}, []);
|
|
1803
|
+
|
|
1804
|
+
return …
|
|
1805
|
+
};
|
|
1806
|
+
```
|
|
1807
|
+
|
|
1808
|
+
### useFocus(options?)
|
|
1809
|
+
|
|
1810
|
+
A component that uses the `useFocus` hook becomes "focusable" to Ink, so when the user presses <kbd>Tab</kbd>, Ink will switch focus to this component.
|
|
1811
|
+
If there are multiple components that execute the `useFocus` hook, focus will be given to them in the order in which these components are rendered.
|
|
1812
|
+
This hook returns an object with an `isFocused` boolean property, which determines whether this component is focused.
|
|
1813
|
+
|
|
1814
|
+
#### options
|
|
1815
|
+
|
|
1816
|
+
##### autoFocus
|
|
1817
|
+
|
|
1818
|
+
Type: `boolean`\
|
|
1819
|
+
Default: `false`
|
|
1820
|
+
|
|
1821
|
+
Auto-focus this component if there's no active (focused) component right now.
|
|
1822
|
+
|
|
1823
|
+
##### isActive
|
|
1824
|
+
|
|
1825
|
+
Type: `boolean`\
|
|
1826
|
+
Default: `true`
|
|
1827
|
+
|
|
1828
|
+
Enable or disable this component's focus, while still maintaining its position in the list of focusable components.
|
|
1829
|
+
This is useful for inputs that are temporarily disabled.
|
|
1830
|
+
|
|
1831
|
+
##### id
|
|
1832
|
+
|
|
1833
|
+
Type: `string`\
|
|
1834
|
+
Required: `false`
|
|
1835
|
+
|
|
1836
|
+
Set a component's focus ID, which can be used to programmatically focus the component. This is useful for large interfaces with many focusable elements to avoid having to cycle through all of them.
|
|
1837
|
+
|
|
1838
|
+
```jsx
|
|
1839
|
+
import {render, useFocus, Text} from 'ink';
|
|
1840
|
+
|
|
1841
|
+
const Example = () => {
|
|
1842
|
+
const {isFocused} = useFocus();
|
|
1843
|
+
|
|
1844
|
+
return <Text>{isFocused ? 'I am focused' : 'I am not focused'}</Text>;
|
|
1845
|
+
};
|
|
1846
|
+
|
|
1847
|
+
render(<Example />);
|
|
1848
|
+
```
|
|
1849
|
+
|
|
1850
|
+
See example in [examples/use-focus](examples/use-focus/use-focus.tsx) and [examples/use-focus-with-id](examples/use-focus-with-id/use-focus-with-id.tsx).
|
|
1851
|
+
|
|
1852
|
+
### useFocusManager()
|
|
1853
|
+
|
|
1854
|
+
This hook exposes methods to enable or disable focus management for all components or manually switch focus to the next or previous components.
|
|
1855
|
+
|
|
1856
|
+
#### enableFocus()
|
|
1857
|
+
|
|
1858
|
+
Enable focus management for all components.
|
|
1859
|
+
|
|
1860
|
+
**Note:** You don't need to call this method manually unless you've disabled focus management. Focus management is enabled by default.
|
|
1861
|
+
|
|
1862
|
+
```js
|
|
1863
|
+
import {useFocusManager} from 'ink';
|
|
1864
|
+
|
|
1865
|
+
const Example = () => {
|
|
1866
|
+
const {enableFocus} = useFocusManager();
|
|
1867
|
+
|
|
1868
|
+
useEffect(() => {
|
|
1869
|
+
enableFocus();
|
|
1870
|
+
}, []);
|
|
1871
|
+
|
|
1872
|
+
return …
|
|
1873
|
+
};
|
|
1874
|
+
```
|
|
1875
|
+
|
|
1876
|
+
#### disableFocus()
|
|
1877
|
+
|
|
1878
|
+
Disable focus management for all components.
|
|
1879
|
+
The currently active component (if there's one) will lose its focus.
|
|
1880
|
+
|
|
1881
|
+
```js
|
|
1882
|
+
import {useFocusManager} from 'ink';
|
|
1883
|
+
|
|
1884
|
+
const Example = () => {
|
|
1885
|
+
const {disableFocus} = useFocusManager();
|
|
1886
|
+
|
|
1887
|
+
useEffect(() => {
|
|
1888
|
+
disableFocus();
|
|
1889
|
+
}, []);
|
|
1890
|
+
|
|
1891
|
+
return …
|
|
1892
|
+
};
|
|
1893
|
+
```
|
|
1894
|
+
|
|
1895
|
+
#### focusNext()
|
|
1896
|
+
|
|
1897
|
+
Switch focus to the next focusable component.
|
|
1898
|
+
If there's no active component right now, focus will be given to the first focusable component.
|
|
1899
|
+
If the active component is the last in the list of focusable components, focus will be switched to the first focusable component.
|
|
1900
|
+
|
|
1901
|
+
**Note:** Ink calls this method when user presses <kbd>Tab</kbd>.
|
|
1902
|
+
|
|
1903
|
+
```js
|
|
1904
|
+
import {useFocusManager} from 'ink';
|
|
1905
|
+
|
|
1906
|
+
const Example = () => {
|
|
1907
|
+
const {focusNext} = useFocusManager();
|
|
1908
|
+
|
|
1909
|
+
useEffect(() => {
|
|
1910
|
+
focusNext();
|
|
1911
|
+
}, []);
|
|
1912
|
+
|
|
1913
|
+
return …
|
|
1914
|
+
};
|
|
1915
|
+
```
|
|
1916
|
+
|
|
1917
|
+
#### focusPrevious()
|
|
1918
|
+
|
|
1919
|
+
Switch focus to the previous focusable component.
|
|
1920
|
+
If there's no active component right now, focus will be given to the first focusable component.
|
|
1921
|
+
If the active component is the first in the list of focusable components, focus will be switched to the last focusable component.
|
|
1922
|
+
|
|
1923
|
+
**Note:** Ink calls this method when user presses <kbd>Shift</kbd>+<kbd>Tab</kbd>.
|
|
1924
|
+
|
|
1925
|
+
```js
|
|
1926
|
+
import {useFocusManager} from 'ink';
|
|
1927
|
+
|
|
1928
|
+
const Example = () => {
|
|
1929
|
+
const {focusPrevious} = useFocusManager();
|
|
1930
|
+
|
|
1931
|
+
useEffect(() => {
|
|
1932
|
+
focusPrevious();
|
|
1933
|
+
}, []);
|
|
1934
|
+
|
|
1935
|
+
return …
|
|
1936
|
+
};
|
|
1937
|
+
```
|
|
1938
|
+
|
|
1939
|
+
#### focus(id)
|
|
1940
|
+
|
|
1941
|
+
##### id
|
|
1942
|
+
|
|
1943
|
+
Type: `string`
|
|
1944
|
+
|
|
1945
|
+
Switch focus to the component with the given [`id`](#id).
|
|
1946
|
+
If there's no component with that ID, focus will be given to the next focusable component.
|
|
1947
|
+
|
|
1948
|
+
```js
|
|
1949
|
+
import {useFocusManager, useInput} from 'ink';
|
|
1950
|
+
|
|
1951
|
+
const Example = () => {
|
|
1952
|
+
const {focus} = useFocusManager();
|
|
1953
|
+
|
|
1954
|
+
useInput(input => {
|
|
1955
|
+
if (input === 's') {
|
|
1956
|
+
// Focus the component with focus ID 'someId'
|
|
1957
|
+
focus('someId');
|
|
1958
|
+
}
|
|
1959
|
+
});
|
|
1960
|
+
|
|
1961
|
+
return …
|
|
1962
|
+
};
|
|
1963
|
+
```
|
|
1964
|
+
|
|
1965
|
+
### useIsScreenReaderEnabled()
|
|
1966
|
+
|
|
1967
|
+
Returns whether a screen reader is enabled. This is useful when you want to render different output for screen readers.
|
|
1968
|
+
|
|
1969
|
+
```jsx
|
|
1970
|
+
import {useIsScreenReaderEnabled, Text} from 'ink';
|
|
1971
|
+
|
|
1972
|
+
const Example = () => {
|
|
1973
|
+
const isScreenReaderEnabled = useIsScreenReaderEnabled();
|
|
1974
|
+
|
|
1975
|
+
return (
|
|
1976
|
+
<Text>
|
|
1977
|
+
{isScreenReaderEnabled
|
|
1978
|
+
? 'Screen reader is enabled'
|
|
1979
|
+
: 'Screen reader is disabled'}
|
|
1980
|
+
</Text>
|
|
1981
|
+
);
|
|
1982
|
+
};
|
|
1983
|
+
```
|
|
1984
|
+
|
|
1985
|
+
## API
|
|
1986
|
+
|
|
1987
|
+
#### render(tree, options?)
|
|
1988
|
+
|
|
1989
|
+
Returns: [`Instance`](#instance)
|
|
1990
|
+
|
|
1991
|
+
Mount a component and render the output.
|
|
1992
|
+
|
|
1993
|
+
##### tree
|
|
1994
|
+
|
|
1995
|
+
Type: `ReactElement`
|
|
1996
|
+
|
|
1997
|
+
##### options
|
|
1998
|
+
|
|
1999
|
+
Type: `object`
|
|
2000
|
+
|
|
2001
|
+
###### stdout
|
|
2002
|
+
|
|
2003
|
+
Type: `stream.Writable`\
|
|
2004
|
+
Default: `process.stdout`
|
|
2005
|
+
|
|
2006
|
+
Output stream where the app will be rendered.
|
|
2007
|
+
|
|
2008
|
+
###### stdin
|
|
2009
|
+
|
|
2010
|
+
Type: `stream.Readable`\
|
|
2011
|
+
Default: `process.stdin`
|
|
2012
|
+
|
|
2013
|
+
Input stream where app will listen for input.
|
|
2014
|
+
|
|
2015
|
+
###### stderr
|
|
2016
|
+
|
|
2017
|
+
Type: `stream.Writable`\
|
|
2018
|
+
Default: `process.stderr`
|
|
2019
|
+
|
|
2020
|
+
Error stream.
|
|
2021
|
+
|
|
2022
|
+
###### exitOnCtrlC
|
|
2023
|
+
|
|
2024
|
+
Type: `boolean`\
|
|
2025
|
+
Default: `true`
|
|
2026
|
+
|
|
2027
|
+
Configure whether Ink should listen for Ctrl+C keyboard input and exit the app.
|
|
2028
|
+
This is needed in case `process.stdin` is in [raw mode](https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode), because then Ctrl+C is ignored by default and the process is expected to handle it manually.
|
|
2029
|
+
|
|
2030
|
+
###### patchConsole
|
|
2031
|
+
|
|
2032
|
+
Type: `boolean`\
|
|
2033
|
+
Default: `true`
|
|
2034
|
+
|
|
2035
|
+
Patch console methods to ensure console output doesn't mix with Ink's output.
|
|
2036
|
+
When any of the `console.*` methods are called (like `console.log()`), Ink intercepts their output, clears the main output, renders output from the console method, and then rerenders the main output again.
|
|
2037
|
+
That way, both are visible and don't overlap each other.
|
|
2038
|
+
|
|
2039
|
+
This functionality is powered by [patch-console](https://github.com/vadimdemedes/patch-console), so if you need to disable Ink's interception of output but want to build something custom, you can use that.
|
|
2040
|
+
|
|
2041
|
+
###### onRender
|
|
2042
|
+
|
|
2043
|
+
Type: `({renderTime: number}) => void`\
|
|
2044
|
+
Default: `undefined`
|
|
2045
|
+
|
|
2046
|
+
Runs the given callback after each render and re-render with a metrics object.
|
|
2047
|
+
|
|
2048
|
+
###### debug
|
|
2049
|
+
|
|
2050
|
+
Type: `boolean`\
|
|
2051
|
+
Default: `false`
|
|
2052
|
+
|
|
2053
|
+
If `true`, each update will be rendered as separate output, without replacing the previous one.
|
|
2054
|
+
|
|
2055
|
+
###### maxFps
|
|
2056
|
+
|
|
2057
|
+
Type: `number`\
|
|
2058
|
+
Default: `30`
|
|
2059
|
+
|
|
2060
|
+
Maximum frames per second for render updates.
|
|
2061
|
+
This controls how frequently the UI can update to prevent excessive re-rendering.
|
|
2062
|
+
Higher values allow more frequent updates but may impact performance.
|
|
2063
|
+
Setting it to a lower value may be useful for components that update very frequently, to reduce CPU usage.
|
|
2064
|
+
|
|
2065
|
+
###### incrementalRendering
|
|
2066
|
+
|
|
2067
|
+
Type: `boolean`\
|
|
2068
|
+
Default: `false`
|
|
2069
|
+
|
|
2070
|
+
Enable incremental rendering mode which only updates changed lines instead of redrawing the entire output.
|
|
2071
|
+
This can reduce flickering and improve performance for frequently updating UIs.
|
|
2072
|
+
|
|
2073
|
+
#### Instance
|
|
2074
|
+
|
|
2075
|
+
This is the object that `render()` returns.
|
|
2076
|
+
|
|
2077
|
+
##### rerender(tree)
|
|
2078
|
+
|
|
2079
|
+
Replace the previous root node with a new one or update the props of the current root node.
|
|
2080
|
+
|
|
2081
|
+
###### tree
|
|
2082
|
+
|
|
2083
|
+
Type: `ReactElement`
|
|
2084
|
+
|
|
2085
|
+
```jsx
|
|
2086
|
+
// Update props of the root node
|
|
2087
|
+
const {rerender} = render(<Counter count={1} />);
|
|
2088
|
+
rerender(<Counter count={2} />);
|
|
2089
|
+
|
|
2090
|
+
// Replace root node
|
|
2091
|
+
const {rerender} = render(<OldCounter />);
|
|
2092
|
+
rerender(<NewCounter />);
|
|
2093
|
+
```
|
|
2094
|
+
|
|
2095
|
+
##### unmount()
|
|
2096
|
+
|
|
2097
|
+
Manually unmount the whole Ink app.
|
|
2098
|
+
|
|
2099
|
+
```jsx
|
|
2100
|
+
const {unmount} = render(<MyApp />);
|
|
2101
|
+
unmount();
|
|
2102
|
+
```
|
|
2103
|
+
|
|
2104
|
+
##### waitUntilExit()
|
|
2105
|
+
|
|
2106
|
+
Returns a promise that resolves when the app is unmounted.
|
|
2107
|
+
|
|
2108
|
+
```jsx
|
|
2109
|
+
const {unmount, waitUntilExit} = render(<MyApp />);
|
|
2110
|
+
|
|
2111
|
+
setTimeout(unmount, 1000);
|
|
2112
|
+
|
|
2113
|
+
await waitUntilExit(); // resolves after `unmount()` is called
|
|
2114
|
+
```
|
|
2115
|
+
|
|
2116
|
+
##### clear()
|
|
2117
|
+
|
|
2118
|
+
Clear output.
|
|
2119
|
+
|
|
2120
|
+
```jsx
|
|
2121
|
+
const {clear} = render(<MyApp />);
|
|
2122
|
+
clear();
|
|
2123
|
+
```
|
|
2124
|
+
|
|
2125
|
+
#### measureElement(ref)
|
|
2126
|
+
|
|
2127
|
+
Measure the dimensions of a particular `<Box>` element.
|
|
2128
|
+
Returns an object with `width` and `height` properties.
|
|
2129
|
+
This function is useful when your component needs to know the amount of available space it has. You can use it when you need to change the layout based on the length of its content.
|
|
2130
|
+
|
|
2131
|
+
**Note:** `measureElement()` returns correct results only after the initial render, when the layout has been calculated. Until then, `width` and `height` equal zero. It's recommended to call `measureElement()` in a `useEffect` hook, which fires after the component has rendered.
|
|
2132
|
+
|
|
2133
|
+
##### ref
|
|
2134
|
+
|
|
2135
|
+
Type: `MutableRef`
|
|
2136
|
+
|
|
2137
|
+
A reference to a `<Box>` element captured with the `ref` property.
|
|
2138
|
+
See [Refs](https://reactjs.org/docs/refs-and-the-dom.html) for more information on how to capture references.
|
|
2139
|
+
|
|
2140
|
+
```jsx
|
|
2141
|
+
import {render, measureElement, Box, Text} from 'ink';
|
|
2142
|
+
|
|
2143
|
+
const Example = () => {
|
|
2144
|
+
const ref = useRef();
|
|
2145
|
+
|
|
2146
|
+
useEffect(() => {
|
|
2147
|
+
const {width, height} = measureElement(ref.current);
|
|
2148
|
+
// width = 100, height = 1
|
|
2149
|
+
}, []);
|
|
2150
|
+
|
|
2151
|
+
return (
|
|
2152
|
+
<Box width={100}>
|
|
2153
|
+
<Box ref={ref}>
|
|
2154
|
+
<Text>This box will stretch to 100 width</Text>
|
|
2155
|
+
</Box>
|
|
2156
|
+
</Box>
|
|
2157
|
+
);
|
|
2158
|
+
};
|
|
2159
|
+
|
|
2160
|
+
render(<Example />);
|
|
2161
|
+
```
|
|
2162
|
+
|
|
2163
|
+
## Testing
|
|
2164
|
+
|
|
2165
|
+
Ink components are simple to test with [ink-testing-library](https://github.com/vadimdemedes/ink-testing-library).
|
|
2166
|
+
Here's a simple example that checks how the component is rendered:
|
|
2167
|
+
|
|
2168
|
+
```jsx
|
|
2169
|
+
import React from 'react';
|
|
2170
|
+
import {Text} from 'ink';
|
|
2171
|
+
import {render} from 'ink-testing-library';
|
|
2172
|
+
|
|
2173
|
+
const Test = () => <Text>Hello World</Text>;
|
|
2174
|
+
const {lastFrame} = render(<Test />);
|
|
2175
|
+
|
|
2176
|
+
lastFrame() === 'Hello World'; //=> true
|
|
2177
|
+
```
|
|
2178
|
+
|
|
2179
|
+
Check out [ink-testing-library](https://github.com/vadimdemedes/ink-testing-library) for more examples and full documentation.
|
|
2180
|
+
|
|
2181
|
+
## Using React Devtools
|
|
2182
|
+
|
|
2183
|
+

|
|
2184
|
+
|
|
2185
|
+
Ink supports [React Devtools](https://github.com/facebook/react/tree/master/packages/react-devtools) out of the box. To enable integration with React Devtools in your Ink-based CLI, first ensure you have installed the optional `react-devtools-core` dependency, and then run your app with the `DEV=true` environment variable:
|
|
2186
|
+
|
|
2187
|
+
```sh
|
|
2188
|
+
DEV=true my-cli
|
|
2189
|
+
```
|
|
2190
|
+
|
|
2191
|
+
Then, start React Devtools itself:
|
|
2192
|
+
|
|
2193
|
+
```sh
|
|
2194
|
+
npx react-devtools
|
|
2195
|
+
```
|
|
2196
|
+
|
|
2197
|
+
After it starts, you should see the component tree of your CLI.
|
|
2198
|
+
You can even inspect and change the props of components, and see the results immediately in the CLI, without restarting it.
|
|
2199
|
+
|
|
2200
|
+
**Note**: You must manually quit your CLI via <kbd>Ctrl</kbd>+<kbd>C</kbd> after you're done testing.
|
|
2201
|
+
|
|
2202
|
+
## Screen Reader Support
|
|
2203
|
+
|
|
2204
|
+
Ink has basic support for screen readers.
|
|
2205
|
+
|
|
2206
|
+
To enable it, you can either pass the `isScreenReaderEnabled` option to the `render` function or set the `INK_SCREEN_READER` environment variable to `true`.
|
|
2207
|
+
|
|
2208
|
+
Ink implements a small subset of functionality from the [ARIA specification](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA).
|
|
2209
|
+
|
|
2210
|
+
```jsx
|
|
2211
|
+
render(<MyApp />, {isScreenReaderEnabled: true});
|
|
2212
|
+
```
|
|
2213
|
+
|
|
2214
|
+
When screen reader support is enabled, Ink will try its best to generate a screen-reader-friendly output.
|
|
2215
|
+
|
|
2216
|
+
For example, for this code:
|
|
2217
|
+
|
|
2218
|
+
```jsx
|
|
2219
|
+
<Box aria-role="checkbox" aria-state={{checked: true}}>
|
|
2220
|
+
<Text>Accept terms and conditions</Text>
|
|
2221
|
+
</Box>
|
|
2222
|
+
```
|
|
2223
|
+
|
|
2224
|
+
Ink will generate the following output for screen readers:
|
|
2225
|
+
|
|
2226
|
+
```
|
|
2227
|
+
(checked) checkbox: Accept terms and conditions
|
|
2228
|
+
```
|
|
2229
|
+
|
|
2230
|
+
You can also provide a custom label for screen readers if you want to render something different for them.
|
|
2231
|
+
|
|
2232
|
+
For example, if you are building a progress bar, you can use `aria-label` to provide a more descriptive label for screen readers.
|
|
2233
|
+
|
|
2234
|
+
```jsx
|
|
2235
|
+
<Box>
|
|
2236
|
+
<Box width="50%" height={1} backgroundColor="green" />
|
|
2237
|
+
<Text aria-label="Progress: 50%">50%</Text>
|
|
2238
|
+
</Box>
|
|
2239
|
+
```
|
|
2240
|
+
|
|
2241
|
+
In the example above, the screen reader will read "Progress: 50%" instead of "50%".
|
|
2242
|
+
|
|
2243
|
+
### `aria-label`
|
|
2244
|
+
|
|
2245
|
+
Type: `string`
|
|
2246
|
+
|
|
2247
|
+
A label for the element for screen readers.
|
|
2248
|
+
|
|
2249
|
+
### `aria-hidden`
|
|
2250
|
+
|
|
2251
|
+
Type: `boolean`\
|
|
2252
|
+
Default: `false`
|
|
2253
|
+
|
|
2254
|
+
Hide the element from screen readers.
|
|
2255
|
+
|
|
2256
|
+
##### aria-role
|
|
2257
|
+
|
|
2258
|
+
Type: `string`
|
|
2259
|
+
|
|
2260
|
+
The role of the element.
|
|
2261
|
+
|
|
2262
|
+
Supported values:
|
|
2263
|
+
- `button`
|
|
2264
|
+
- `checkbox`
|
|
2265
|
+
- `radio`
|
|
2266
|
+
- `radiogroup`
|
|
2267
|
+
- `list`
|
|
2268
|
+
- `listitem`
|
|
2269
|
+
- `menu`
|
|
2270
|
+
- `menuitem`
|
|
2271
|
+
- `progressbar`
|
|
2272
|
+
- `tab`
|
|
2273
|
+
- `tablist`
|
|
2274
|
+
- `timer`
|
|
2275
|
+
- `toolbar`
|
|
2276
|
+
- `table`
|
|
2277
|
+
|
|
2278
|
+
##### aria-state
|
|
2279
|
+
|
|
2280
|
+
Type: `object`
|
|
2281
|
+
|
|
2282
|
+
The state of the element.
|
|
2283
|
+
|
|
2284
|
+
Supported values:
|
|
2285
|
+
- `checked` (boolean)
|
|
2286
|
+
- `disabled` (boolean)
|
|
2287
|
+
- `expanded` (boolean)
|
|
2288
|
+
- `selected` (boolean)
|
|
2289
|
+
|
|
2290
|
+
## Creating Components
|
|
2291
|
+
|
|
2292
|
+
When building custom components, it's important to keep accessibility in mind. While Ink provides the building blocks, ensuring your components are accessible will make your CLIs usable by a wider audience.
|
|
2293
|
+
|
|
2294
|
+
### General Principles
|
|
2295
|
+
|
|
2296
|
+
- **Provide screen reader-friendly output:** Use the `useIsScreenReaderEnabled` hook to detect if a screen reader is active. You can then render more descriptive output for screen reader users.
|
|
2297
|
+
- **Leverage ARIA props:** For components that have a specific role (e.g., a checkbox or button), use the `aria-role`, `aria-state`, and `aria-label` props on `<Box>` and `<Text>` to provide semantic meaning to screen readers.
|
|
2298
|
+
|
|
2299
|
+
For a practical example of building an accessible component, see the [ARIA example](/examples/aria/aria.tsx).
|
|
2300
|
+
|
|
2301
|
+
## Useful Components
|
|
2302
|
+
|
|
2303
|
+
- [ink-text-input](https://github.com/vadimdemedes/ink-text-input) - Text input.
|
|
2304
|
+
- [ink-spinner](https://github.com/vadimdemedes/ink-spinner) - Spinner.
|
|
2305
|
+
- [ink-select-input](https://github.com/vadimdemedes/ink-select-input) - Select (dropdown) input.
|
|
2306
|
+
- [ink-link](https://github.com/sindresorhus/ink-link) - Link.
|
|
2307
|
+
- [ink-gradient](https://github.com/sindresorhus/ink-gradient) - Gradient color.
|
|
2308
|
+
- [ink-big-text](https://github.com/sindresorhus/ink-big-text) - Awesome text.
|
|
2309
|
+
- [ink-picture](https://github.com/endernoke/ink-picture) - Display images.
|
|
2310
|
+
- [ink-tab](https://github.com/jdeniau/ink-tab) - Tab.
|
|
2311
|
+
- [ink-color-pipe](https://github.com/LitoMore/ink-color-pipe) - Create color text with simpler style strings.
|
|
2312
|
+
- [ink-multi-select](https://github.com/karaggeorge/ink-multi-select) - Select one or more values from a list
|
|
2313
|
+
- [ink-divider](https://github.com/JureSotosek/ink-divider) - A divider.
|
|
2314
|
+
- [ink-progress-bar](https://github.com/brigand/ink-progress-bar) - Progress bar.
|
|
2315
|
+
- [ink-table](https://github.com/maticzav/ink-table) - Table.
|
|
2316
|
+
- [ink-ascii](https://github.com/hexrcs/ink-ascii) - Awesome text component with more font choices, based on Figlet.
|
|
2317
|
+
- [ink-markdown](https://github.com/cameronhunter/ink-markdown) - Render syntax highlighted Markdown.
|
|
2318
|
+
- [ink-quicksearch-input](https://github.com/Eximchain/ink-quicksearch-input) - Select component with fast, quicksearch-like navigation.
|
|
2319
|
+
- [ink-confirm-input](https://github.com/kevva/ink-confirm-input) - Yes/No confirmation input.
|
|
2320
|
+
- [ink-syntax-highlight](https://github.com/vsashyn/ink-syntax-highlight) - Code syntax highlighting.
|
|
2321
|
+
- [ink-form](https://github.com/lukasbach/ink-form) - Form.
|
|
2322
|
+
- [ink-task-list](https://github.com/privatenumber/ink-task-list) - Task list.
|
|
2323
|
+
- [ink-spawn](https://github.com/kraenhansen/ink-spawn) - Spawn child processes.
|
|
2324
|
+
- [ink-titled-box](https://github.com/mishieck/ink-titled-box) - Box with a title.
|
|
2325
|
+
- [ink-chart](https://github.com/pppp606/ink-chart) - Sparkline and bar chart.
|
|
2326
|
+
- [ink-scroll-view](https://github.com/ByteLandTechnology/ink-scroll-view) - Scroll container.
|
|
2327
|
+
- [ink-scroll-list](https://github.com/ByteLandTechnology/ink-scroll-list) - Scrollable list.
|
|
2328
|
+
|
|
2329
|
+
## Useful Hooks
|
|
2330
|
+
|
|
2331
|
+
- [ink-use-stdout-dimensions](https://github.com/cameronhunter/ink-monorepo/tree/master/packages/ink-use-stdout-dimensions) - Subscribe to stdout dimensions.
|
|
2332
|
+
|
|
2333
|
+
## Examples
|
|
2334
|
+
|
|
2335
|
+
The [`examples`](/examples) directory contains a set of real examples. You can run them with:
|
|
2336
|
+
|
|
2337
|
+
```bash
|
|
2338
|
+
npm run example examples/[example name]
|
|
2339
|
+
# e.g. npm run example examples/borders
|
|
2340
|
+
```
|
|
2341
|
+
|
|
2342
|
+
- [Jest](examples/jest/jest.tsx) - Implementation of basic Jest UI.
|
|
2343
|
+
- [Counter](examples/counter/counter.tsx) - A simple counter that increments every 100ms.
|
|
2344
|
+
- [Form with validation](https://github.com/final-form/rff-cli-example) - Manage form state using [Final Form](https://github.com/final-form/final-form#-final-form).
|
|
2345
|
+
- [Borders](examples/borders/borders.tsx) - Add borders to the `<Box>` component.
|
|
2346
|
+
- [Suspense](examples/suspense/suspense.tsx) - Use React Suspense.
|
|
2347
|
+
- [Table](examples/table/table.tsx) - Renders a table with multiple columns and rows.
|
|
2348
|
+
- [Focus management](examples/use-focus/use-focus.tsx) - Use the `useFocus` hook to manage focus between components.
|
|
2349
|
+
- [User input](examples/use-input/use-input.tsx) - Listen for user input.
|
|
2350
|
+
- [Write to stdout](examples/use-stdout/use-stdout.tsx) - Write to stdout, bypassing main Ink output.
|
|
2351
|
+
- [Write to stderr](examples/use-stderr/use-stderr.tsx) - Write to stderr, bypassing main Ink output.
|
|
2352
|
+
- [Static](examples/static/static.tsx) - Use the `<Static>` component to render permanent output.
|
|
2353
|
+
- [Child process](examples/subprocess-output) - Renders output from a child process.
|
|
2354
|
+
|
|
2355
|
+
## Maintainers
|
|
2356
|
+
|
|
2357
|
+
- [Vadim Demedes](https://github.com/vadimdemedes)
|
|
2358
|
+
- [Sindre Sorhus](https://github.com/sindresorhus)
|