Skip to content

Commit

Permalink
Add inverse prop to Text component (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
goliney committed Aug 2, 2020
1 parent 373db19 commit 8e8c1ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Binary file added media/text-inverse.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions readme.md
Expand Up @@ -205,6 +205,7 @@ const Example = () => (
<Text italic>I am italic</Text>
<Text underline>I am underline</Text>
<Text strikethrough>I am strikethrough</Text>
<Text inverse>I am inversed</Text>
</>
);

Expand Down Expand Up @@ -285,6 +286,21 @@ Default: `false`

Make the text crossed with a line.

#### inverse

Type: `boolean`\
Default: `false`

Inverse background and foreground colors.

```jsx
<Text inverse color="yellow">
Inversed Yellow
</Text>
```

<img src="media/text-inverse.jpg" width="138">

#### wrap

Type: `string`\
Expand Down
10 changes: 10 additions & 0 deletions src/components/Text.tsx
Expand Up @@ -42,6 +42,11 @@ export interface Props {
*/
readonly strikethrough?: boolean;

/**
* Inverse background and foreground colors.
*/
readonly inverse?: boolean;

/**
* This property tells Ink to wrap or truncate text if its width is larger than container.
* If `wrap` is passed (by default), Ink will wrap text and split it into multiple lines.
Expand All @@ -62,6 +67,7 @@ const Text: FC<Props> = ({
italic,
underline,
strikethrough,
inverse,
wrap,
children
}) => {
Expand Down Expand Up @@ -98,6 +104,10 @@ const Text: FC<Props> = ({
children = chalk.strikethrough(children);
}

if (inverse) {
children = chalk.inverse(children);
}

return children;
};

Expand Down
5 changes: 5 additions & 0 deletions test/text.tsx
Expand Up @@ -120,6 +120,11 @@ test('text with ansi256 background color', t => {
t.is(output, chalk.bgAnsi256(194)('Test'));
});

test('text with inversion', t => {
const output = renderToString(<Text inverse>Test</Text>);
t.is(output, chalk.inverse('Test'));
});

test('remeasure text when text is changed', t => {
const Test = ({add}) => (
<Box>
Expand Down

0 comments on commit 8e8c1ba

Please sign in to comment.