Text
Text provides an ergonomic API for rendering text. It includes built-in support for Canvas type tokens.
Component Type
Text
Platform
Web
Component
Sana Canvas
Delivery Channels
Web
Version
16.1.7Experience Surfaces
Chat Sidepanel
Usage Guidance
Examples
Basic Example
Text is built on top of Box and supports a multitude of
style props including spacing props such
as margin and padding, as well as text styling props such as fontSize and fontWeight.
Text renders a <span> element by default. You may override the rendered element using the as
prop.
Text as h4
Text styled using cs props
Text styled using type token level
import {Box} from '@workday/canvas-kit-react/layout';
import {Text} from '@workday/canvas-kit-react/text';
import {system} from '@workday/canvas-tokens-web';
export default () => (
<Box>
<Text as="h4">Text as h4</Text>
<Text
as="p"
cs={{
fontSize: system.fontSize.body.sm,
fontWeight: system.fontWeight.normal,
fontFamily: system.fontFamily.default,
}}
>
Text styled using cs props
</Text>
<Text as="p" cs={{...system.type.subtext.lg}}>
Text styled using type token level
</Text>
<Box cs={{...system.type.subtext.lg, color: system.color.fg.muted.default}}>
<Text>Text with inherited styles</Text>
</Box>
</Box>
);
Type Level and Variant
Text includes built-in support for Canvas type tokens via the typeLevel and
variant props.
typeLevel hooks into the Canvas type hierarchy.
Small Body level text
import React from 'react';
import {Text} from '@workday/canvas-kit-react/text';
export default () => (
<Text as="p" typeLevel="body.small">
Small Body level text
</Text>
);
variant allows you to layer additional styling to aid users’
understanding of the text being rendered.
Error text
import React from 'react';
import {Text} from '@workday/canvas-kit-react/text';
export default () => (
<Text as="p" variant="error">
Error text
</Text>
);
Custom Styles
Text supports custom styling via the cs prop. For more information, check our
“How To Customize Styles” .
Component API
Text
This is a generic base component intended to render any text.
It has typeLevel and variant style props that simplify navigating
our type hierarchy and using Canvas type tokens.
By default, it renders a semantic span element,
but you can adjust this as needed with the as prop.
The type hierarchy is organized into four levels, which correspond to our Canvas type levels.
title: Intended to be used for large page titles.heading: Intended to be used for headings and large text.body: Intended to be used for standard body text.subtext: Intended to be used for small subtext content or in tight spaces.
Each level has three sizes: large, medium, and small.
You can provide any level and size to the typeLevel prop, and it will apply the correct styles
accordingly. In the example below we're providing the subtext level and small size to the
component with the value subtext.small.
import { Text } from '@workday/canvas-kit-react/text';
const CustomErrorText = () => (
<Text typeLevel="subtext.small" variant="error">
Error Text
</Text>
);
Layout Component
Text supports all props from thelayout component.
Props
Props extend from span. Changing the as prop will change the element interface.
| Name | Type | Description | Default |
|---|---|---|---|
typeLevel | | Type token as string with level and size separated by dot. These values map to our Canvas type levels. | |
variant | 'error' | 'hint' | 'inverse' | Type variant token names: | |
cs | | The | |
children | ReactNode | ||
as | React.ElementType | Optional override of the default element used by the component. Any valid tag or Component. If you provided a Component, this component should forward the ref using Note: Not all elements make sense and some elements may cause accessibility issues. Change this value with care. | span |
ref | React.Ref<R = span> | Optional ref. If the component represents an element, this ref will be a reference to the real DOM element of the component. If |