The Text
component is a code-only component used to apply specific predefined HDS typographic styles to a block of text or content.
How to use this component
The Text
component is available in three different variants with a one-to-one association between the namespace (eg. ::Display
) and the corresponding style in Figma.
<Hds::Text::Display>Text as "Display" variant</Hds::Text::Display>
<Hds::Text::Body>Text as "Body" variant</Hds::Text::Body>
<Hds::Text::Code>Text as "Code" variant</Hds::Text::Code>
When no @tag
argument is provided, the text element is rendered as a <span>
by default.
HTML tag
To specify which HTML tag to use to render the element, use the @tag
argument:
Text as "Display" variant
Text as "Body" variant
Text as "Code" variant
<Hds::Text::Display @tag="h1">Text as "Display" variant</Hds::Text::Display>
<Hds::Text::Body @tag="p">Text as "Body" variant</Hds::Text::Body>
<Hds::Text::Code @tag="pre">Text as "Code" variant</Hds::Text::Code>
Size
To specify which size to apply to the text, use the @size
argument:
Text as "Display-500" variant
Text as "Body-300" variant
Text as "Code-100" variant
<Hds::Text::Display @tag="h1" @size="500">Text as "Display-500" variant</Hds::Text::Display>
<Hds::Text::Body @tag="p" @size="300">Text as "Body-300" variant</Hds::Text::Body>
<Hds::Text::Code @tag="pre" @size="100">Text as "Code-100" variant</Hds::Text::Code>
Different style variants support different sizes: refer to the Component API section for details.
Weight
To apply a specific font weight to the text, use the @weight
argument:
"Display" text with "medium" font weight
"Body" text with "semibold" font weight
"Copy" text with "bold" font weight
<Hds::Text::Display @tag="h4" @size="300" @weight="medium">"Display" text with "medium" font weight</Hds::Text::Display>
<Hds::Text::Body @tag="p" @weight="semibold">"Body" text with "semibold" font weight</Hds::Text::Body>
<Hds::Text::Code @tag="pre" @weight="bold">"Copy" text with "bold" font weight</Hds::Text::Code>
Alignment
To specify a text alignment, use the @align
argument:
This text is right-aligned
This text is center-aligned
This text is left-aligned
<Hds::Text::Display @tag="h1" @align="right">This text is right-aligned</Hds::Text::Display>
<Hds::Text::Body @tag="p" @align="center">This text is center-aligned</Hds::Text::Body>
<Hds::Text::Code @tag="pre" @align="left">This text is left-aligned</Hds::Text::Code>
Color
It is possible to apply a specific foreground color to the text (from a predefined list) using the @color
argument:
This text has a "strong" color applied
This text has an "action-active" color applied
This text has a "highlight-high-contrast" color applied
<Hds::Text::Body @tag="p" @color="strong">This text has a "strong" color applied</Hds::Text::Body>
<Hds::Text::Body @tag="p" @color="action-active">This text has an "action-active" color applied</Hds::Text::Body>
<Hds::Text::Body @tag="p" @color="highlight-high-contrast">This text has a "highlight-high-contrast" color applied</Hds::Text::Body>
For the list of possible foreground colors supported, refer to the Component API section for details.
It’s also possible to provide a CSS color as string (in this case the color will be applied as inline style). The string can be a CSS var()
that uses one of the predefined color tokens:
This text has a "blue-400" color applied
<Hds::Text::Body @tag="p" @color="var(--token-color-palette-blue-400)">This text has a "blue-400" color applied</Hds::Text::Body>
Or it can be one of the standard CSS color formats (hex, rgb, rgba, hsl, etc.):
This text has a "#FF0000" (red) color applied
<Hds::Text::Body @tag="p" @color="#FF0000">This text has a "#FF0000" (red) color applied</Hds::Text::Body>
Structured content
Since the component is not prescriptive on the tags that can be used to render the text, it can be used to apply a typographic style not only to plain text but also to structured content:
This text contains some strong and em tags, a link.
<Hds::Text::Body @tag="p" @size="300">This text contains some <strong>strong</strong> and <em>em</em> tags, a <Hds::Link::Inline @href="#">link</Hds::Link::Inline>.</Hds::Text::Body>
We can imagine even more complex examples where we may want to apply a typographic style to an entire block of content:
This is some generic text in a paragraph.
- this list item contains some strong and em tags
- this list item contains a link
This is also some generic text in a paragraph.
<Hds::Text::Body @tag="div" @size="200" @color="strong">
<p>This is some generic text in a paragraph.</p>
<ul>
<li>this list item contains some <strong>strong</strong> and <em>em</em> tags</li>
<li>this list item contains a <Hds::Link::Inline @href="#">link</Hds::Link::Inline></li>
</ul>
<p>This is also some generic text in a paragraph.</p>
</Hds::Text::Body>
Component API
The Text
component is used through these specialized variants: Hds::Text::Display
, Hds::Text::Body
, and Hds::Text::Code
.
Text::Display
size
string
- 500
- 400
- 300
- 200 (default)
- 100
Display
text style.
tag
string
- span (default)
weight
string
- medium
- semibold
- bold
@weight
argument is provided, the component will inherit its weight from the parent container/context.Notice: not all the font weights can be used for all the font sizes (refer to the Typography section for details.). Below are the allowed combinations:
Display-500
→ bold
Display-400
→ medium
, semibold
, bold
Display-300
→ medium
, semibold
, bold
Display-200
→ semibold
Display-100
→ medium
align
string
- left
- center
- right
@align
argument is provided, the component will inherit its alignment from the parent container/context.
color
string | CSS color
- primary
- strong
- faint
- disabled
- high-contrast
- action
- action-hover
- action-active
- highlight
- highlight-on-surface
- highlight-high-contrast
- success
- success-on-surface
- success-high-contrast
- warning
- warning-on-surface
- warning-high-contrast
- critical
- critical-on-surface
- critical-high-contrast
@color
argument is provided, the component will inherit its color from the parent container/context.
yield
<@tag>
HTML element.
…attributes
...attributes
.
Text::Body
size
string
- 300
- 200 (default)
- 100
Body
text style.
tag
string
- span (default)
weight
string
- regular (default)
- medium
- semibold
@weight
argument is provided, the component will inherit its weight from the parent container/context.
align
string
- left
- center
- right
@align
argument is provided, the component will inherit its alignment from the parent container/context.
color
string | CSS color
- primary
- strong
- faint
- disabled
- high-contrast
- action
- action-hover
- action-active
- highlight
- highlight-on-surface
- highlight-high-contrast
- success
- success-on-surface
- success-high-contrast
- warning
- warning-on-surface
- warning-high-contrast
- critical
- critical-on-surface
- critical-high-contrast
@color
argument is provided, the component will inherit its color from the parent container/context.
yield
<@tag>
HTML element.
…attributes
...attributes
.
Text::Code
size
string
- 300
- 200 (default)
- 100
Code
text style.
tag
string
- span (default)
weight
string
- regular (default)
- bold
@weight
argument is provided, the component will inherit its weight from the parent container/context.
align
string
- left
- center
- right
@align
argument is provided, the component will inherit its alignment from the parent container/context.
color
string | CSS color
- primary
- strong
- faint
- disabled
- high-contrast
- action
- action-hover
- action-active
- highlight
- highlight-on-surface
- highlight-high-contrast
- success
- success-on-surface
- success-high-contrast
- warning
- warning-on-surface
- warning-high-contrast
- critical
- critical-on-surface
- critical-high-contrast
@color
argument is provided, the component will inherit its color from the parent container/context.
yield
<@tag>
HTML element.
…attributes
...attributes
.
Conformance rating
Since this component is nothing more than a wrapper around generic HTML tags with custom typographic styles applied, any conformance issues that may arise will depend on how this component is used in the consumer’s code, in the same way as normal HTML tags and text would.
Support
If any accessibility issues have been found within this component, let us know by submitting an issue.