The Text component provides styles and semantic meaning for text and headings in a manner consistent with other components.
Examples #
Import Syntax #
import Text from 'mineral-ui/Text';
Basic Usage #
Text renders content with the correct styles and semantics. Text can include complex content, such as an Icon, and can be rendered as any HTML element.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo, in suscipit orci. Donec enim augue, vehicula vel enim a, vestibulum tempus nunc.
<Text> Lorem ipsum <IconCloud size="large" /> dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo, in suscipit orci. Donec enim augue, vehicula vel enim a, vestibulum tempus nunc. </Text>
Headings #
Create a heading with Text by using the element
prop to
apply the appropriate styles and semantics. If you need a heading with styles
that vary from the semantic meaning, e.g. an h1
that looks like an h3
,
you can use the appearance
prop.
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
<div> <Text as="h1">Heading 1</Text> <Text as="h2">Heading 2</Text> <Text as="h3">Heading 3</Text> <Text as="h4">Heading 4</Text> <Text as="h5">Heading 5</Text> <Text as="h6">Heading 6</Text> </div>
Appearance #
Apply predefined styles with the appearance
prop.
Heading 1 which looks like a Heading 3
Prose (Long Form) Paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum. Praesent gravida metus at scelerisque ultrices.
Mousetype Paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum. Praesent gravida metus at scelerisque ultrices. Suspendisse at facilisis massa. Integer eleifend eleifend nibh posuere interdum.
<div> <Text as="h1" appearance="h3">Heading 1 which looks like a Heading 3</Text> <Text appearance="prose"> Prose (Long Form) Paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum. Praesent gravida metus at scelerisque ultrices. </Text> <Text appearance="mouse"> Mousetype Paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum. Praesent gravida metus at scelerisque ultrices. Suspendisse at facilisis massa. Integer eleifend eleifend nibh posuere interdum. </Text> </div>
Margin #
Text applies appropriate margins to text and headings.
You may remove them with the noMargins
prop.
Heading 1
<Text as="h1" noMargins>Heading 1</Text>
Alignment #
Set the horizontal alignment of your text content with the
align
prop.
Align End
Align Center
Align Justify: justified text should stretch the width of the container for each separate line. This example contains words of many lengths to display that lines are justified correctly. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mattis luctus nunc, in posuere odio viverra et. Quisque aliquam tincidunt ante ac mollis. Vestibulum et massa eget velit ultricies feugiat non non augue. Nulla nisl augue, ullamcorper ac dui.
<div> <Text align="end">Align End</Text> <Text align="center">Align Center</Text> <Text align="justify"> Align Justify: justified text should stretch the width of the container for each separate line. This example contains words of many lengths to display that lines are justified correctly. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mattis luctus nunc, in posuere odio viverra et. Quisque aliquam tincidunt ante ac mollis. Vestibulum et massa eget velit ultricies feugiat non non augue. Nulla nisl augue, ullamcorper ac dui. </Text> </div>
Truncate #
You can truncate Text with an ellipsis. Passing true
will
apply a max-width of '100%'
, passing a number will apply it in pixels, and
strings ('50%', '15em'
) are passed directly. Note that the element
must
be block-level (like the default 'p'
) for this to display correctly.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo.
<div> <Text truncate> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo. </Text> <Text truncate={200}> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo. </Text> <Text truncate="50%"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non purus id dolor maximus eleifend nec in nunc. Integer quis lacinia leo. </Text> </div>
Font Weight #
Text is available in the following font weights. You can apply a custom font weight by passing in a number.
Semi-bold Text
Bold Text
Extra-bold Text
Light Text
<div> <Text fontWeight="semiBold">Semi-bold Text</Text> <Text fontWeight="bold">Bold Text</Text> <Text fontWeight="extraBold">Extra-bold Text</Text> <Text fontWeight={300}>Light Text</Text> </div>
Color #
Text can be any color, but be sure to use a color/background-color combination with an adequate contrast ratio.
Success Text
Magenta Text
Custom Text
<div> <Text color={theme.color_success}>Success Text</Text> <Text color={palette.magenta_60}>Magenta Text</Text> <DemoPanel backgroundColor="#421429"> <Text color="#bfe7d4" noMargins>Custom Text</Text> </DemoPanel> </div>
Nested Components #
Text components nested inside other Text components inherit
styles and semantics from the parent(s) by default. Text components inside
non-Text components won't inherit styles and semantics by default. You can
toggle inheritance in either case with the inherit
prop.
Note
For illustrative purposes, a purple background color and padding have been applied to the Text components below.
This non-nested Text has opted in to inheriting those custom styles.
This nested Text has opted out of inheriting those custom styles.
() => { const CustomDiv = styled('div')({ backgroundColor: palette.sky_10, color: palette.sky_80, fontFamily: 'serif', fontSize: '1.25rem', padding: theme.space_inset_sm }); return ( <DemoLayout> For non-nested Text components, you can opt <em>in</em> to style inheritance by passing true to the inherit prop. <CustomDiv> This is a non-nested div with custom styles. <Text inherit> This non-nested Text has opted in to inheriting those custom styles. </Text> </CustomDiv> <Text as="div"> For nested Text components (inside another Text), you can opt <em>out</em> of style inheritance by passing false to the inherit prop. <CustomDiv> This is a nested div with custom styles. <Text inherit={false}> This nested Text has opted out of inheriting those custom styles. </Text> </CustomDiv> </Text> </DemoLayout> ); }
Bidirectionality #
Text supports right-to-left (RTL) languages; alignment will be reversed
when the direction
theme variable is set to rtl
.
محاذاة البداية
محاذاة النهاية
<div dir="rtl"> <ThemeProvider theme={{ direction: 'rtl' }}> <div> <Text>محاذاة البداية</Text> <Text align="end">محاذاة النهاية</Text> </div> </ThemeProvider> </div>
API & Theme #
Text Props #
The Text component takes the following React props.
Name | Type | Default | Description |
---|---|---|---|
align | 'start' | 'end' | 'center' | 'justify' | Available horizontal alignments | |
appearance | 'p' | Available styles | |
children | React$Node | required | Rendered content |
color | string | Color of text | |
fontWeight | Available font weights | ||
inherit | boolean | Inherit all styles from parent | |
noMargins | boolean | Remove top & bottom margins | |
truncate | boolean | number | string | Force display to one line and truncate with ellipsis at given max-width |
Text Theme Variables #
These variables can be used as hooks to override this component's
style at either a
local
or global
level. The theme
referenced below is whatever theme is available
from props to the instance of this component.
Variable | Value |
---|---|
Text_color | theme.color |
Text_color_h1 | theme.h1_color |
Text_color_h2 | theme.h2_color |
Text_color_h3 | theme.h3_color |
Text_color_h4 | theme.h4_color |
Text_color_h5 | theme.h5_color |
Text_color_h6 | theme.h6_color |
Text_color_mouse | theme.color_mouse |
Text_fontSize | theme.fontSize_ui |
Text_fontSize_h1 | theme.h1_fontSize |
Text_fontSize_h2 | theme.h2_fontSize |
Text_fontSize_h3 | theme.h3_fontSize |
Text_fontSize_h4 | theme.h4_fontSize |
Text_fontSize_h5 | theme.h5_fontSize |
Text_fontSize_h6 | theme.h6_fontSize |
Text_fontSize_mouse | theme.fontSize_mouse |
Text_fontSize_prose | theme.fontSize_prose |
Text_fontWeight_h1 | theme.h1_fontWeight |
Text_fontWeight_h2 | theme.h2_fontWeight |
Text_fontWeight_h3 | theme.h3_fontWeight |
Text_fontWeight_h4 | theme.h4_fontWeight |
Text_fontWeight_h5 | theme.h5_fontWeight |
Text_fontWeight_h6 | theme.h6_fontWeight |
Text_lineHeight | theme.lineHeight_prose |
Text_lineHeight_heading | theme.lineHeight_heading |
Text_lineHeight_headingSmall | |
Text_marginBottom | theme.space_stack_md |
Text_marginBottom_heading | theme.space_stack_sm |
Usage #
When/How to Use #
Use Text when displaying text-based content, particularly headings or content that wraps to multiple lines, in your app to ensure proper font sizes, colors, weights, and spacing relative to other components.
The semantic meaning of your content should inform the as
prop.
Best Practices #
Use a logical order for the headings in your app.
Heading 1
This is a paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum.
Heading 2
This is a paragraph. Praesent gravida metus at scelerisque ultrices. Suspendisse at facilisis massa.
Use the prose appearance
for long blocks of text content,
to improve the reading experience.
Title
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum. Praesent gravida metus at scelerisque ultrices. Suspendisse at facilisis massa. Integer eleifend eleifend nibh posuere interdum. Sed eu nisl eu lectus condimentum ultricies id et sapien.
Mauris vulputate justo nec velit fringilla, sed convallis elit hendrerit. Sed at viverra nibh. Maecenas pharetra turpis tempor velit viverra, ac commodo elit iaculis. Sed finibus ultricies nisl non dapibus. Nunc ut ullamcorper nulla, sit amet tempus turpis.
Use the mouse appearance
for brief blocks of content when
you need to conserve space or display minor information.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum. Praesent gravida metus at scelerisque ultrices.
Suspendisse at facilisis massa. Integer eleifend eleifend nibh posuere interdum. Sed eu nisl eu lectus condimentum ultricies id et sapien.
Don't use font weight in place of semantic meaning. Use a
heading element
or a 'strong' element
instead.
Not a real heading
This is a paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque molestie eros at nisl rhoncus, et condimentum dui elementum.