@jrichman/ink 6.3.1

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