← Back to all posts

Ink 3

July 27, 2020

Ink 3 is available today. Ink lets you build command-line apps using React and have fun while doing so too.

Ink is a special project to me, because it's been surreal to watch how a tiny experiment grew into a full-featured command-line UI toolkit that's used by Gatsby, Parcel, Yarn, Terraform, tap, Prisma, Shopify, New York Times and many others. I had no idea it had such a great future when I was just playing with React trying to make it render things to a string and output it in the terminal.

I'm excited to share all the updates with you, so let's dive right in to see what Ink 3 has to offer.

Stability and Performance

While it's not so much of a feature, I think it deserves a top spot. People loved using React to declaratively build UIs for their command-line apps, but sometimes it ended up hurting their end performance. For example, Gatsby noticed their build process slowed down, which is obviously not the tradeoff I want Ink's users to accept.

Ink 3 got rid of quite a few rendering bugs and in some cases I observed a 2x performance improvement when there's a high frequency of re-renders. Text rendering and the whole support for Flexbox was rethinked from the ground up and rewritten. It's also much simpler than it was before, which makes issues easier to debug, reproduce and solve.

Previous version of Ink had multiple hacky workarounds for various layout issues, so it didn't always render what you would expect. Ink 3 should be a significant improvement in that area.

Colors in <Text> component

Previously you had to use a separate <Color> component for applying color to text. This could quickly get noisy:

1<Color red>
2 <Text bold>Hello World</Text>
3</Color>

In Ink 3, all functionality from <Color> component was merged into <Text> and simplified.

1<Text bold color="red">
2 Hello World
3</Text>

If your terminal supports extended range of colors, you can even use HEX or RGB colors.

1<Text color="green">Green</Text>
2<Text color="#005cc5">Blue</Text>
3<Text color="rgb(232, 131, 136)">Red</Text>

Text can also have a colorful background by setting the backgroundColor property, which accepts the same values as color.

1<Text color="black" backgroundColor="green">
2 Black on Green
3</Text>

Learn more about <Text> component.

Borders

Boxes can have stylish borders now! Fun fact, this feature unexpectedly uncovered a range of layout issues in Ink. I noticed how borders were rendered incorrectly around boxes and uncovered how box dimensions differ in Ink and Yoga, which sparked a rewrite of the entire rendering flow.

1 <Box
2 borderStyle="round"
3 borderColor="green"
4 paddingX={1}
5 flexDirection="column"
6 >
7 <Text color="green">New version is available!</Text>
8 <Text>
9 Run <Text color="blue">npm i -g my-cli</Text> to update
10 </Text>
11</Box>

Oh, and there are 7 border styles in total, so you can pick the one you like the most.

Learn more about borders.

Hooks all the way

Ink 3 offers a set of powerful hooks to manage your app:

I want to mention useInput in particular, because it has seen significant improvements since Ink 2. Now it parses user input more reliably and can properly detect when user presses combinations of keys, like Shift + Tab for example. If it wasn't working properly for you before, I encourage you to give it a try again.

Learn more about built-in hooks.

New <Static> component

If you need to render unpredictable and possibly large number of items, use <Static> component. Think of <Static> as a terminal-specific implementation for a virtual list technique, where only necessary items are rendered. <Static> renders all new items above the rest of your UI permanently, which means you can't update them once they are displayed. This makes <Static> a perfect candidate for tools like Jest, where hundreds of completed tests could need to be logged.

1<Static items={['Test #1', 'Test #2', 'Test #3']}>
2 {(test, index) => (
3 <Text key={index} color="green">
4 Completed {test}
5 </Text>
6 )}
7</Static>

In Ink 3, <Static> is almost twice as performant as it was before and API has been simplified to match libraries like react-tiny-virtual-list.

Here's an example of a UI similar to Jest, where completed tests are rendered using <Static>.

Learn more about <Static> component.

Support for React Devtools

Yes, you read that right. You can use React Devtools to inspect the output of your Ink app. The magical bit is that you can actually change the props of any component and see your CLI update on the fly without having to restart it.

Integration with React DevtoolsIntegration with React Devtools

You don't need to make any changes to support it, just set DEV=true environment variable and open Devtools:

$ DEV=true node examples/counter
$ npx react-devtools

Built-in error handler and log interception

When one of your components throws an error, React displays a quite verbose error message to the console and a long stack trace. Most often that stack trace isn't super useful, because it points to React internals.

Ink 3 offers a simpler and cleaner solution to this problem by defining a global React error boundary and rendering a beautiful overview of the error.

Error handler UIError handler UI

To improve developer UX further, Ink 3 intercepts calls to console.log, console.error and other methods to ensure logs are displayed correctly above your app's UI and not interfere with each other. This is necessary, because Ink re-renders your UI all the time, which can overwrite the output from console.log.

Focus management

If your app has more than one user input displayed at once, it can become tricky to detect which input has the focus and should receive data. To make this easier, Ink offers two new React hooks:

When component calls useFocus hook, it notifies Ink that there's a new focusable component in the UI. Each time user presses Tab, Ink is going to pass focus to the next focusable component in the queue.

1 const First = () => {
2 const {isFocused} = useFocus();
3
4 return (
5 <Text>
6 {isFocused ? 'First item is focused' : 'First item is not focused'}
7 </Text>
8 );
9 };
10
11const Second = () => {
12 const {isFocused} = useFocus();
13
14 return (
15 <Text>
16 {isFocused ? 'Second item is focused' : 'Second item is not focused'}
17 </Text>
18 );
19};
20
21const Example = () => (
22 <>
23 <First />
24 <Second />
25 </>
26);

In the example above, as user presses Tab, focus will be passed back and forth between First and Second components.

When your app needs to manage focus manually, without waiting on user input, you can use useFocusManager hook. It lets you switch focus to the next or previous component or turn it on/off completely.

Learn more about focus management.

New <Spacer> component

<Spacer> component is a handy box, which automatically expands to fill all the available space. It's quite similar to the one built into SwiftUI. In the following example, "Left" and "Right" labels will be pushed to the sides, because <Spacer> fills the space between them.

1<Box>
2 <Text>Left</Text>
3 <Spacer />
4 <Text>Right</Text>
5</Box>

Similarly, when container has a column direction, <Spacer> will expand vertically instead.

1<Box flexDirection="column" height={10}>
2 <Text>Top</Text>
3 <Spacer />
4 <Text>Bottom</Text>
5</Box>

New <Newline> component

<Newline> component is basically a replacement for manually inserting one or many \n characters. Note that it can be used only inside <Text> components.

1<Text>
2 Hello
3 <Newline />
4 World
5</Text>

If you need more than one newline character to be inserted, add a count property:

1<Newline count={3} />

Thank you!

Lots of great companies and developers already use Ink to build their CLIs and I can't wait to see what you come up with this next generation of Ink! For those who are interested in migrating from Ink 2, there's a migration guide available.

I want to say thank you to everyone who has contributed to Ink 3 and helped by testing the pre-release version to make sure final release is as bug free as possible ❤️

Special thanks to:

Check out the new Ink on GitHub.

If you want to receive occasional emails about new stuff I build, you can subscribe below. Alternatively, feel free to subscribe via RSS feed.

← Back to all posts