Ink-UI

Collection of customizable UI components for CLIs made with Ink

README


Ink UI


Collection of customizable UI components for CLIs made with Ink .


Install


  1. ``` shell
  2. npm install @inkjs/ui
  3. ```

This assumes you've already set up Ink . The easiest way to get started is create-ink-app .

Components


Text input


Documentation

TextInputis used for entering any single-line input with an optional autocomplete.

  1. ``` js
  2. import {TextInput} from '@inkjs/ui';

  3. <TextInput
  4. placeholder="Enter your name..."
  5. onSubmit={name => {
  6.   // `name` contains user input
  7. }}
  8. />;
  9. ```

undefined text-input.gif

Email input


Documentation

EmailInputis used for entering an email. After "@" character is entered, domain can be autocompleted from the list of most popular email providers.

  1. ``` js
  2. import {EmailInput} from '@inkjs/ui';

  3. <EmailInput
  4. placeholder="Enter email..."
  5. onSubmit={email => {
  6.   // `email` contains user input
  7. }}
  8. />;
  9. ```

undefined email-input.gif

Password input


Documentation

PasswordInputis used for entering sensitive data, like passwords, API keys and so on. It works the same way as TextInput, except input value is masked and replaced with asterisks ("*").

  1. ``` js
  2. import {PasswordInput} from '@inkjs/ui';

  3. <PasswordInput
  4. placeholder="Enter password..."
  5. onSubmit={password => {
  6.   // `password` contains user input
  7. }}
  8. />;
  9. ```

undefined password-input.gif

Confirm input


Documentation

ConfirmInputshows a common "Y/n" input to confirm or cancel an operation your CLI wants to perform.

  1. ``` js
  2. import {ConfirmInput} from '@inkjs/ui';

  3. <ConfirmInput
  4. onConfirm={() => {
  5.   // confirmed
  6. }}
  7. onCancel={() => {
  8.   // cancelled
  9. }}
  10. />;
  11. ```

undefined

Select


Documentation

Selectshows a scrollable list of options for a user to choose from.

  1. ``` js
  2. import {Select} from '@inkjs/ui';

  3. <Select
  4. options={[
  5.   {
  6.    label: 'Red',
  7.    value: 'red',
  8.   },
  9.   {
  10.    label: 'Green',
  11.    value: 'green',
  12.   },
  13.   {
  14.    label: 'Yellow',
  15.    value: 'yellow',
  16.   },
  17.   /* ... */
  18. ]}
  19. onChange={newValue => {
  20.   // `newValue` equals the `value` field of the selected option
  21.   // For example, "yellow"
  22. }}
  23. />;
  24. ```

undefined select.gif

Multi select


Documentation

MultiSelectis similar to Select, except user can choose multiple options.

  1. ``` js
  2. import {MultiSelect} from '@inkjs/ui';

  3. <MultiSelect
  4. options={[
  5.   {
  6.    label: 'Red',
  7.    value: 'red',
  8.   },
  9.   {
  10.    label: 'Green',
  11.    value: 'green',
  12.   },
  13.   {
  14.    label: 'Yellow',
  15.    value: 'yellow',
  16.   },
  17.   /* ... */
  18. ]}
  19. onChange={newValue => {
  20.   // `newValue` is an array of `value` fields of the selected options
  21.   // For example, ["green", "yellow"]
  22. }}
  23. />;
  24. ```

undefined multi-select.gif

Spinner


Documentation

Spinnerindicates that something is being processed and CLI is waiting for it to complete.

  1. ``` js
  2. import {Spinner} from '@inkjs/ui';

  3. <Spinner label="Loading" />;
  4. ```

undefined spinner.gif

Progress bar


Documentation

ProgressBaris an extended version of Spinner, where it's possible to calculate a progress percentage.

  1. ``` js
  2. import {ProgressBar} from '@inkjs/ui';

  3. // `progress` must be a number between 0 and 100
  4. <ProgressBar value={progress} />;
  5. ```

undefined progress-bar.gif

Badge


Documentation

Badgecan be used to indicate a status of a certain item, usually positioned nearby the element it's related to.

  1. ``` js
  2. import {Badge} from '@inkjs/ui';

  3. <Badge color="green">Pass</Badge>
  4. <Badge color="red">Fail</Badge>
  5. <Badge color="yellow">Warn</Badge>
  6. <Badge color="blue">Todo</Badge>
  7. ```

undefined

Status message


Documentation

StatusMessagecan also be used to indicate a status, but when longer explanation of such status is required.

  1. ``` js
  2. import {StatusMessage} from '@inkjs/ui';

  3. <StatusMessage variant="success">
  4. New version is deployed to production
  5. </StatusMessage>

  6. <StatusMessage variant="error">
  7.   Failed to deploy a new version of this app
  8. </StatusMessage>

  9. <StatusMessage variant="warning">
  10.     Health checks aren't configured
  11. </StatusMessage>

  12. <StatusMessage variant="info">
  13.     This version is already deployed
  14. </StatusMessage>
  15. ```

undefined

Alert


Documentation

Alertis used to focus user's attention to important messages.

  1. ``` js
  2. import {Alert} from '@inkjs/ui';

  3. <Alert variant="success">
  4.     A new version of this CLI is available
  5. </Alert>

  6. <Alert variant="error">
  7.     Your license is expired
  8. </Alert>

  9. <Alert variant="warning">
  10.     Current version of this CLI has been deprecated
  11. </Alert>

  12. <Alert variant="info">
  13.     API won't be available tomorrow night
  14. </Alert>
  15. ```

undefined

Unordered list


Documentation

UnorderedListis used to show lists of items.

  1. ``` js
  2. import {UnorderedList} from '@inkjs/ui';

  3. <UnorderedList>
  4. <UnorderedList.Item>
  5.   <Text>Red</Text>
  6. </UnorderedList.Item>

  7. <UnorderedList.Item>
  8.   <Text>Green</Text>

  9.   <UnorderedList>
  10.    <UnorderedList.Item>
  11.     <Text>Light</Text>
  12.    </UnorderedList.Item>

  13.    <UnorderedList.Item>
  14.     <Text>Dark</Text>
  15.    </UnorderedList.Item>
  16.   </UnorderedList>
  17. </UnorderedList.Item>

  18. <UnorderedList.Item>
  19.   <Text>Blue</Text>
  20. </UnorderedList.Item>
  21. </UnorderedList>;
  22. ```

undefined

Ordered list


Documentation

OrderedListis used to show lists of numbered items.

  1. ``` js
  2. import {OrderedList} from '@inkjs/ui';

  3. <OrderedList>
  4. <OrderedList.Item>
  5.   <Text>Red</Text>
  6. </OrderedList.Item>

  7. <OrderedList.Item>
  8.   <Text>Green</Text>

  9.   <OrderedList>
  10.    <OrderedList.Item>
  11.     <Text>Light</Text>
  12.    </OrderedList.Item>

  13.    <OrderedList.Item>
  14.     <Text>Dark</Text>
  15.    </OrderedList.Item>
  16.   </OrderedList>
  17. </OrderedList.Item>

  18. <OrderedList.Item>
  19.   <Text>Blue</Text>
  20. </OrderedList.Item>
  21. </OrderedList>;
  22. ```

undefined

Theming


All component have their styles defined in a theme, which is accessible to components via React context. Ink UI ships with a default theme and it can be customized or replaced with a different theme altogether.

Let's get a quick look on how to customize a Spinner's component theme. Here's how it looks by default:

undefined spinner.gif

First, look up component's default theme, which will give an overview which parts does this component consist of. Documentation of each component includes a link to component's theme.tsfile on top. In the case of Spinner, it's source/components/spinner/theme.ts .

Here's the part we care about:

  1. ``` tsx
  2. const theme = {
  3. styles: {
  4.   container: (): BoxProps => ({
  5.    gap: 1,
  6.   }),
  7.   frame: (): TextProps => ({
  8.    color: 'blue',
  9.   }),
  10.   label: (): TextProps => ({}),
  11. },
  12. } satisfies ComponentTheme;

  13. export default theme;
  14. ```

This component theme hints that Spinnerhas 3 parts: container, frame and a label. So to customize the color of the spinner itself, we'd want to change the colorprop returned from the framefunction.

To customize the default theme, use extendThemefunction and make that custom theme available to children components via ThemeProvider.

  1. ``` tsx
  2. import {render, type TextProps} from 'ink';
  3. import {Spinner, ThemeProvider, extendTheme, defaultTheme} from '@inkjs/ui';

  4. const customTheme = extendTheme(defaultTheme, {
  5. components: {
  6.   Spinner: {
  7.    styles: {
  8.     frame: (): TextProps => ({
  9.      color: 'magenta',
  10.     }),
  11.    },
  12.   },
  13. },
  14. });

  15. function Example() {
  16. return (
  17.   <ThemeProvider theme={customTheme}>
  18.    <Spinner label="Loading" />
  19.   </ThemeProvider>
  20. );
  21. }

  22. render(<Example />);
  23. ```

With custom theme applied, Spinnernow renders a magenta spinner, instead of the default blue one.

undefined spinner-theme.gif

There are also cases where styles change based on some condition. For example, StatusMessage changes the color of an icon based on the variantprop.

Here's a sample code from its theme .

  1. ``` ts
  2. const colorByVariant = {
  3. success: 'green',
  4. error: 'red',
  5. warning: 'yellow',
  6. info: 'blue',
  7. };

  8. const theme = {
  9. styles: {
  10.   icon: ({variant}) => ({
  11.    color: colorByVariant[variant],
  12.   }),
  13. },
  14. };
  15. ```

Since each field in stylesobject is a function, it can return different styles based on the props that were passed in or a state of a component.

Component themes can also include configuration for rendering a component in a configobject, that's not related to styling. For example, UnorderedList specifies a marker, which is a character that's rendered before each list item.

Here's a sample code from its theme .

  1. ``` ts
  2. const theme = {
  3. config: () => ({
  4.   marker: '─',
  5. }),
  6. };
  7. ```

undefined

Changing markerto '+'would render this:

undefined

Components shipped in Ink UI automatically read the necessary styles and configuration from a theme. However, if you're adding a new custom component and a theme for it, use useComponentThemehook to access it.

  1. ``` tsx
  2. import React, {render, Text, type TextProps} from 'ink';
  3. import {
  4. ThemeProvider,
  5. defaultTheme,
  6. extendTheme,
  7. useComponentTheme,
  8. type ComponentTheme,
  9. } from '@inkjs/ui';

  10. const customLabelTheme = {
  11. styles: {
  12.   label: (): TextProps => ({
  13.    color: 'green',
  14.   }),
  15. },
  16. } satisfies ComponentTheme;

  17. type CustomLabelTheme = typeof customLabelTheme;

  18. const customTheme = extendTheme(defaultTheme, {
  19. components: {
  20.   CustomLabel: customLabelTheme,
  21. },
  22. });

  23. function CustomLabel() {
  24. const {styles} = useComponentTheme<CustomLabelTheme>('CustomLabel');

  25. return <Text {...styles.label()}>Hello world</Text>;
  26. }

  27. function Example() {
  28. return (
  29.   <ThemeProvider theme={customTheme}>
  30.    <CustomLabel />
  31.   </ThemeProvider>
  32. );
  33. }

  34. render(<Example />);
  35. ```