Skip to content

Commit 90d5709

Browse files
laurkimaveline
andauthored
[Typography foundations] Add Text component (#6521)
* [Typography foundations] Add Text component Co-authored-by: Aveline Thelen <[email protected]> * [Typography foundations] Add changeset * [Typography foundations] Remove variant mapping We expect users to determine element type even if `variant` is passed in, so as to decouple styling from element type. * [Typography foundations] Clean up exports * [Typography foundations] Add color options * [Typography foundations] Add defaults for `as`/`variant` and make `as` required * [Typography foundations] Add support for `visuallyHidden` prop * [Typography foundations] Add prop descriptions * [Typography foundations] Remove `a` as element type * [Typography foundations] Make `variant` required prop * [Typography foundations] Rename colors to align with tokens Co-authored-by: Aveline Thelen <[email protected]>
1 parent f18901b commit 90d5709

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

.changeset/curly-llamas-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris': minor
3+
---
4+
5+
Added new `Text` component
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
@import '../../styles/common';
2+
3+
.root {
4+
margin: 0;
5+
text-align: inherit;
6+
}
7+
8+
.block {
9+
display: block;
10+
}
11+
12+
.truncate {
13+
overflow: hidden;
14+
white-space: nowrap;
15+
text-overflow: ellipsis;
16+
}
17+
18+
.start {
19+
text-align: start;
20+
}
21+
22+
.center {
23+
text-align: center;
24+
}
25+
26+
.end {
27+
text-align: end;
28+
}
29+
30+
.justify {
31+
text-align: justify;
32+
}
33+
34+
.success {
35+
color: var(--p-text-success);
36+
}
37+
38+
.critical {
39+
color: var(--p-text-critical);
40+
}
41+
42+
.warning {
43+
color: var(--p-text-warning);
44+
}
45+
46+
.subdued {
47+
color: var(--p-text-subdued);
48+
}
49+
50+
.regular {
51+
font-weight: var(--p-font-weight-regular);
52+
}
53+
54+
.medium {
55+
font-weight: var(--p-font-weight-medium);
56+
}
57+
58+
.semibold {
59+
font-weight: var(--p-font-weight-semibold);
60+
}
61+
62+
.bold {
63+
font-weight: var(--p-font-weight-bold);
64+
}
65+
66+
.visuallyHidden {
67+
@include visually-hidden;
68+
}
69+
70+
.displaySm {
71+
font-size: var(--p-font-size-12);
72+
line-height: var(--p-line-height-5);
73+
}
74+
75+
.displayMd {
76+
font-size: 32px;
77+
line-height: 40px;
78+
}
79+
80+
.displayLg {
81+
font-size: 40px;
82+
font-weight: var(--p-font-weight-semibold);
83+
line-height: 48px;
84+
}
85+
86+
.headingSm {
87+
font-size: var(--p-font-size-1);
88+
line-height: var(--p-line-height-1);
89+
}
90+
91+
.headingMd {
92+
font-size: var(--p-font-size-3);
93+
line-height: var(--p-line-height-2);
94+
}
95+
96+
.headingLg {
97+
font-size: var(--p-font-size-5);
98+
line-height: var(--p-line-height-3);
99+
}
100+
101+
.headingXl {
102+
font-size: var(--p-font-size-7);
103+
line-height: var(--p-line-height-3);
104+
}
105+
106+
.bodySm {
107+
font-size: var(--p-font-size-1);
108+
line-height: var(--p-line-height-1);
109+
}
110+
111+
.bodyMd {
112+
font-size: var(--p-font-size-3);
113+
line-height: var(--p-line-height-2);
114+
}
115+
116+
.bodyLg {
117+
font-size: var(--p-font-size-5);
118+
line-height: var(--p-line-height-3);
119+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React, {ReactNode} from 'react';
2+
3+
import {classNames} from '../../utilities/css';
4+
5+
import styles from './Text.scss';
6+
7+
type Element = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'span';
8+
9+
type Variant =
10+
| 'displaySm'
11+
| 'displayMd'
12+
| 'displayLg'
13+
| 'headingSm'
14+
| 'headingMd'
15+
| 'headingLg'
16+
| 'headingXl'
17+
| 'bodySm'
18+
| 'bodyMd'
19+
| 'bodyLg';
20+
21+
type Alignment = 'inherit' | 'start' | 'center' | 'end' | 'justify';
22+
23+
type FontWeight = 'regular' | 'medium' | 'semibold' | 'bold';
24+
25+
type Color = 'success' | 'critical' | 'warning' | 'subdued';
26+
27+
export interface TextProps {
28+
/** Adjust horizontal alignment of text */
29+
alignment?: Alignment;
30+
/** The element type */
31+
as: Element;
32+
/** Text to display */
33+
children: ReactNode;
34+
/** Adjust color of text */
35+
color?: Color;
36+
/** Adjust weight of text */
37+
fontWeight?: FontWeight;
38+
/** Truncate text overflow with ellipsis */
39+
truncate?: boolean;
40+
/** Typographic style of text */
41+
variant: Variant;
42+
/** Visually hide the text */
43+
visuallyHidden?: boolean;
44+
}
45+
46+
export const Text = ({
47+
alignment = 'inherit',
48+
as,
49+
children,
50+
color,
51+
fontWeight = 'regular',
52+
truncate = false,
53+
variant,
54+
visuallyHidden = false,
55+
}: TextProps) => {
56+
const Component = as || (visuallyHidden ? 'span' : 'p');
57+
58+
const className = classNames(
59+
styles.root,
60+
styles[variant],
61+
fontWeight && styles[fontWeight],
62+
(alignment || truncate) && styles.block,
63+
alignment && styles[alignment],
64+
color && styles[color],
65+
truncate && styles.truncate,
66+
visuallyHidden && styles.visuallyHidden,
67+
);
68+
69+
return <Component className={className}>{children}</Component>;
70+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Text';

polaris-react/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ export type {TabsProps} from './components/Tabs';
339339
export {Tag} from './components/Tag';
340340
export type {TagProps} from './components/Tag';
341341

342+
export {Text} from './components/Text';
343+
export type {TextProps} from './components/Text';
344+
342345
export {TextContainer} from './components/TextContainer';
343346
export type {TextContainerProps} from './components/TextContainer';
344347

0 commit comments

Comments
 (0)