Skip to content

Commit f056fea

Browse files
author
Nikolai Lopin
authored
feat: add secondary variant for text component (#170)
1 parent 830940b commit f056fea

File tree

5 files changed

+121
-33
lines changed

5 files changed

+121
-33
lines changed

src/components/Text/Text.spec.tsx

+38-15
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,74 @@
11
import { render } from '@testing-library/react';
22
import * as React from 'react';
3-
import { Colors } from '../../essentials';
3+
import { SemanticColors } from '../../essentials/Colors/Colors';
4+
45
import { Text } from './Text';
56

67
describe('Text', () => {
7-
it('renders a <span> by default', () => {
8-
expect(render(<Text />)).toMatchHtmlTag('span');
8+
describe('default render', () => {
9+
it('renders a <span> by default', () => {
10+
expect(render(<Text />)).toMatchHtmlTag('span');
11+
});
12+
13+
it('has a primary color by default', () => {
14+
expect(render(<Text />).container.firstChild).toHaveStyle(`
15+
color: ${SemanticColors.text.primary};
16+
`);
17+
});
18+
19+
it('renders the children', () => {
20+
expect(render(<Text>Content</Text>).getByText('Content')).toBeInTheDocument();
21+
});
922
});
1023

1124
it('respects the "as" prop', () => {
1225
expect(render(<Text as="b" />)).toMatchHtmlTag('b');
1326
});
1427

15-
it('renders the children', () => {
16-
expect(render(<Text>Content</Text>).getByText('Content')).toBeInTheDocument();
28+
it('has an inverted primary color if inverted property is set', () => {
29+
expect(render(<Text inverted />).container.firstChild).toHaveStyle(`
30+
color: ${SemanticColors.text.primaryInverted};
31+
`);
1732
});
1833

1934
describe('if the weak property is set', () => {
2035
it('has a dim color', () => {
2136
expect(render(<Text weak />).container.firstChild).toHaveStyle(`
22-
color: ${Colors.AUTHENTIC_BLUE_550};
37+
color: ${SemanticColors.text.secondary};
2338
`);
2439
});
2540

2641
it('has a dimmer color if inverted', () => {
2742
expect(render(<Text weak inverted />).container.firstChild).toHaveStyle(`
28-
color: ${Colors.AUTHENTIC_BLUE_350};
43+
color: ${SemanticColors.text.secondaryInverted};
44+
`);
45+
});
46+
});
47+
48+
describe('if the `secondary` property is set', () => {
49+
it('has a dim color', () => {
50+
expect(render(<Text secondary />).container.firstChild).toHaveStyle(`
51+
color: ${SemanticColors.text.secondary};
52+
`);
53+
});
54+
55+
it('has a dimmer color if inverted', () => {
56+
expect(render(<Text secondary inverted />).container.firstChild).toHaveStyle(`
57+
color: ${SemanticColors.text.secondaryInverted};
2958
`);
3059
});
3160
});
3261

3362
describe('if the disabled property is set', () => {
3463
it('has the disabled color', () => {
3564
expect(render(<Text disabled />).container.firstChild).toHaveStyle(`
36-
color: ${Colors.AUTHENTIC_BLUE_350};
65+
color: ${SemanticColors.text.disabled};
3766
`);
3867
});
3968
it('has a stronger disabled color if inverted', () => {
4069
expect(render(<Text disabled inverted />).container.firstChild).toHaveStyle(`
41-
color: ${Colors.AUTHENTIC_BLUE_550};
70+
color: ${SemanticColors.text.disabledInverted};
4271
`);
4372
});
4473
});
45-
46-
it('has a bright color if inverted property is set', () => {
47-
expect(render(<Text inverted />).container.firstChild).toHaveStyle(`
48-
color: ${Colors.WHITE};
49-
`);
50-
});
5174
});

src/components/Text/Text.tsx

+17-7
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
textAlign,
1414
TextAlignProps
1515
} from 'styled-system';
16-
import { Colors } from '../../essentials';
1716
import { theme } from '../../essentials/theme';
1817
import { get } from '../../utils/themeGet';
18+
import { deprecatedProperty } from '../../utils/deprecatedProperty';
1919

2020
interface TextProps
2121
extends ComponentPropsWithoutRef<'span'>,
@@ -33,28 +33,38 @@ interface TextProps
3333
inverted?: boolean;
3434
/**
3535
* Adjust color to indicate secondary information
36+
* @deprecated use `secondary` instead
3637
*/
3738
weak?: boolean;
39+
/**
40+
* Adjust color to indicate secondary information
41+
*/
42+
secondary?: boolean;
3843
/**
3944
* Adjust color to display a disabled text element
4045
*/
4146
disabled?: boolean;
4247
}
4348

44-
function determineTextColor({ weak, inverted, disabled }: TextProps) {
49+
function determineTextColor(props: TextProps) {
50+
const { weak, secondary, inverted, disabled } = props;
51+
if (weak !== undefined) {
52+
deprecatedProperty('Text', weak, 'weak', 'secondary', 'Rename `weak` to `secondary` to remove the warning.');
53+
}
54+
4555
if (disabled) {
46-
return inverted ? Colors.AUTHENTIC_BLUE_550 : Colors.AUTHENTIC_BLUE_350;
56+
return get(inverted ? 'semanticColors.text.disabledInverted' : 'semanticColors.text.disabled')(props);
4757
}
4858

49-
if (weak) {
50-
return inverted ? Colors.AUTHENTIC_BLUE_350 : Colors.AUTHENTIC_BLUE_550;
59+
if (secondary || weak) {
60+
return get(inverted ? 'semanticColors.text.secondaryInverted' : 'semanticColors.text.secondary')(props);
5161
}
5262

5363
if (inverted) {
54-
return Colors.WHITE;
64+
return get('semanticColors.text.primaryInverted')(props);
5565
}
5666

57-
return Colors.AUTHENTIC_BLUE_900;
67+
return get('semanticColors.text.primary')(props);
5868
}
5969

6070
const Text = styled.span.attrs({ theme })<TextProps>`

src/components/Text/docs/Text.mdx

+24-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ route: /components/text
66

77
import { Playground } from 'docz';
88
import { ItemWrapper } from '../../../../docs/components/ItemWrapper.ts';
9-
import { Text } from '../Text.tsx';
10-
import { Colors } from '../../../essentials/Colors/Colors.ts';
119
import { StyledSystemLinks } from '../../../docs/StyledSystemLinks'
10+
import { Text } from '../Text.tsx';
11+
import { TextPropsTable } from './TextPropsTable.tsx';
1212

1313
# Text
1414

@@ -18,7 +18,22 @@ The Text component is a wrapper component that will apply typography styles to t
1818
- Avoid using sizes outside the given range
1919
- Text default size is medium
2020

21-
<StyledSystemLinks component="Text" supportedProps={ ['margin', 'fontSize', 'fontWeight', 'fontFamily', 'textAlign'] }/>
21+
## Usage
22+
23+
By default, `Text` uses the primary color, which is the color for most interface text, whether body or headlines.
24+
25+
Use the `secondary` color for supplemental information with diminished contrast (often, the “gray text”).
26+
Like form microcopy, captions, helper text, and other medium emphasized text elements.
27+
28+
Use the `disabled` color for text in blocks, form elements, or other components that are not available
29+
to a user at the moment (due to either permissions, dependencies, or pre-requisites) and
30+
need to feature “incidental” states.
31+
32+
## Properties
33+
34+
<TextPropsTable />
35+
36+
<StyledSystemLinks component="Text" supportedProps={ ['margin', 'fontSize', 'fontWeight', 'fontFamily', 'textAlign'] } mt={2}/>
2237

2338
## Playground
2439

@@ -50,14 +65,14 @@ The Text component is a wrapper component that will apply typography styles to t
5065
<Text as="p" inverted fontSize={0}>paragraph inverted small</Text>
5166
</ItemWrapper>
5267
<ItemWrapper>
53-
<Text as="p" weak>paragraph weak large</Text>
54-
<Text as="p" weak fontSize={1}>paragraph weak medium</Text>
55-
<Text as="p" weak fontSize={0}>paragraph weak small</Text>
68+
<Text as="p" secondary>paragraph secondary large</Text>
69+
<Text as="p" secondary fontSize={1}>paragraph secondary medium</Text>
70+
<Text as="p" secondary fontSize={0}>paragraph secondary small</Text>
5671
</ItemWrapper>
5772
<ItemWrapper inverted>
58-
<Text as="p" weak inverted>paragraph weak inverted large</Text>
59-
<Text as="p" weak fontSize={1} inverted>paragraph weak inverted medium</Text>
60-
<Text as="p" weak fontSize={0} inverted>paragraph weak inverted small</Text>
73+
<Text as="p" secondary inverted>paragraph secondary inverted large</Text>
74+
<Text as="p" secondary fontSize={1} inverted>paragraph secondary inverted medium</Text>
75+
<Text as="p" secondary fontSize={0} inverted>paragraph secondary inverted small</Text>
6176
</ItemWrapper>
6277
<ItemWrapper>
6378
<Text as="p" disabled>paragraph disabled large</Text>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as React from 'react';
2+
import { PropsTable } from '../../../docs/PropsTable';
3+
4+
export const TextPropsTable = () => {
5+
const props = [
6+
{
7+
name: 'fontWeight',
8+
type: '"normal" | "semibold" | "bold"',
9+
description: 'The font-weight property specifies the weight (or boldness) of the font',
10+
defaultValue: '"normal"'
11+
},
12+
{
13+
name: 'disabled',
14+
type: 'boolean',
15+
description:
16+
'Adjust color to display a disabled text element. Takes precedence over `inverted` and `secondary` props',
17+
defaultValue: 'false'
18+
},
19+
{
20+
name: 'inverted',
21+
type: 'boolean',
22+
description: 'Adjust color for display on a dark background',
23+
defaultValue: 'false'
24+
},
25+
{
26+
name: 'secondary',
27+
type: 'boolean',
28+
description: 'Adjust color to indicate secondary information',
29+
defaultValue: 'false'
30+
},
31+
{
32+
name: 'weak (deprecated)',
33+
type: 'boolean',
34+
description: 'Same as `secondary`',
35+
defaultValue: 'false'
36+
}
37+
];
38+
return <PropsTable props={props} />;
39+
};

src/docs/StyledSystemLinks.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const mapping = {
1616

1717
export const StyledSystemLinks: FC<StyledSystemLinksProps> = ({
1818
component,
19-
supportedProps
19+
supportedProps,
20+
...rest
2021
}: StyledSystemLinksProps) => {
2122
const propToLink = (prop: string) => {
2223
const lowercaseProp = prop.toLowerCase();
@@ -45,7 +46,7 @@ export const StyledSystemLinks: FC<StyledSystemLinksProps> = ({
4546
}, []);
4647

4748
return (
48-
<Text as="p">
49+
<Text {...rest} as="p">
4950
The {component} supports{' '}
5051
{supportedPropsTextParts.map((c, index) => (
5152
// eslint-disable-next-line react/no-array-index-key

0 commit comments

Comments
 (0)