|
| 1 | +# Text |
| 2 | + |
| 3 | +The `Text` component is used to display text content with various typographic styles and options. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Labels display text throughout the interface, in buttons, menu items, and views, helping people understand the current context and what they can do next. |
| 8 | + |
| 9 | +The term label refers to uneditable text that can appear in various places. For example: |
| 10 | + |
| 11 | +- Within a button, a label generally conveys what the button does, such as Edit, Cancel, or Send. |
| 12 | +- Within many lists, a label can describe each item, often accompanied by a symbol or an image. |
| 13 | +- Within a view, a label might provide additional context by introducing a control or describing a common action or task that people can perform in the view. |
| 14 | + |
| 15 | +## Best practices |
| 16 | + |
| 17 | +Use a label to display a small amount of text that people don’t need to edit. If you need to let people edit a small amount of text, use a text field. If you need to display a large amount of text, and optionally let people edit it, use a **text view**. |
| 18 | + |
| 19 | +Prefer system fonts. A label can display plain or styled text, and it supports Dynamic Type (where available) by default. If you adjust the style of a label or use custom fonts, make sure the text remains legible. |
| 20 | + |
| 21 | +Use system-provided label colors to communicate relative importance. The system defines four label colors that vary in appearance to help you give text different levels of visual importance. For additional guidance, see **Color**. |
| 22 | + |
| 23 | +## Theming |
| 24 | + |
| 25 | +- Inherits `variation`, `gutterBottomSpace`, and `maxLength` from your theme via `useThemeTextConfigSelector`. |
| 26 | +- Uses font families and weights from your font theme via `useThemeFontSelector`. |
| 27 | +- Supports dark/light mode detection using `useColorScheme`. |
| 28 | + |
| 29 | +## Internal Logic |
| 30 | + |
| 31 | +- **Max Length Handling**: If `maxLength` is passed and `children` isn't a string, throws an error to prevent unintended truncation. |
| 32 | +- **Memoized Children**: Prevents unnecessary re-renders if `children` or `maxLength` haven't changed. |
| 33 | +- **Restyle Support**: Dynamically extracts props via `useRestyle()` and merges those styles into the final text style array. |
| 34 | + |
| 35 | +## Error Handling |
| 36 | + |
| 37 | +- If `maxLength` is passed and children is not a `string`: |
| 38 | + |
| 39 | +```tsx |
| 40 | +throw new Error('maxLength props must be used with string'); |
| 41 | +``` |
| 42 | + |
| 43 | +## Props |
| 44 | + |
| 45 | +| Property | type | Description | |
| 46 | +| --------------- | ---------------------------- | -------------------------------------------------------------------------------- | |
| 47 | +| `children` | ReactNode | The content to be displayed within the text component. | |
| 48 | +| `sx?` | [Styles](/utils/base-styles) | Custom styles to be applied to the text. | |
| 49 | +| `variation?` | String | Variation of the text, such as `body1`, `caption`, `h1`, etc. | |
| 50 | +| `gutterBottom?` | Boolean | Specifies whether to add a bottom margin to the text component. | |
| 51 | +| `maxLength?` | Number | Maximum length of the text content. Used for truncating or limiting text length. | |
| 52 | +| `error?` | Boolean | Specifies if the text component is in an error state. | |
| 53 | +| `errorColor?` | ColorValue | Color value for the text when in an error state. | |
| 54 | +| `isActive?` | Boolean | Specifies if the text component is in an active state. | |
| 55 | +| `activeColor?` | ColorValue | Color value for the text when in an active state. | |
| 56 | +| `disabled?` | Boolean | Specifies if the text component is disabled. | |
| 57 | +| `mode?` | String | `dark` and `light` Mode used for text light and dark variation color. | |
| 58 | +| `color` | ColorValue | prop allows dynamic text color changes | |
| 59 | + |
| 60 | +## Example |
| 61 | + |
| 62 | +```tsx |
| 63 | +<Text variation="body1">Lorem ipsum dolor sit</Text> |
| 64 | +``` |
| 65 | + |
| 66 | +## Related Utilities |
| 67 | + |
| 68 | +- `generateTextStyles` - Generates theme-based styles for the text component. |
| 69 | +- `maxLength` - Utility to truncate string with ellipsis (...) if needed. |
| 70 | +- `useRestyle` - Hook to map style props (margin, padding, etc.) into style. |
0 commit comments