|
| 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 | +}; |
0 commit comments