|
| 1 | +import { Canvas } from "@site/src/components/Canvas"; |
| 2 | + |
| 3 | +# Typography |
| 4 | + |
| 5 | +Bento type scale organizes styles into five roles, semantically named to describe their purposes: |
| 6 | + |
| 7 | +- [Display](../components/Display): reserved for short, important text or numerals. Usually, they work best on large screens. |
| 8 | +- [Headline](../components/Headline): best suited for short, high-emphasis text. These styles can be suitable for marking primary text passages or important content regions. |
| 9 | +- [Title](../components/Title): used to divide secondary text passages or content regions. |
| 10 | +- [Body](../components/Body): used for longer passages of text and paragraphs. |
| 11 | +- [Label](../components/Label): utilitarian styles used for text inside of components or very small supporting text in the content body, like captions. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +You can use any of the typography components like this: |
| 16 | + |
| 17 | +<Canvas path="Typography/All" /> |
| 18 | + |
| 19 | +## Font weight |
| 20 | + |
| 21 | +All typography components support a single (configurable) font weight, with the exception of `Body`, which has two weights: `"default"` and `"strong"`. |
| 22 | + |
| 23 | +`"default"` is the default weight, and `"strong"` is a heavier weight that can be used to emphasize certain parts of the text. |
| 24 | + |
| 25 | +<Canvas path="Typography/BodyWeights" /> |
| 26 | + |
| 27 | +## Truncation |
| 28 | + |
| 29 | +All typography components support an optional `ellipsis` prop (defaulted to `false`), which causes the text to be truncated with an ellipsis when it overflows its container. |
| 30 | + |
| 31 | +<Canvas path="Typography/Ellipsis" /> |
| 32 | + |
| 33 | +This feature uses standard CSS, so the truncation will be accessible (the entire text is still visible in the DOM node) and standard browser feature work as expected (e.g. try copy-pasting the truncated text and you'll see you'll get the full text). |
| 34 | + |
| 35 | +## Semantic HTML |
| 36 | + |
| 37 | +All typography components render as `<span>` by default. This behavior can be customized using the optional `as` prop. |
| 38 | + |
| 39 | +For example, you may want to render `Body` as a `<p>`: |
| 40 | + |
| 41 | +<Canvas path="Typography/BodyP" /> |
| 42 | + |
| 43 | +Another example is using `Title` as a `<h2>`: |
| 44 | + |
| 45 | +<Canvas path="Typography/TitleH2" /> |
| 46 | + |
| 47 | +## Localization and rich formatting |
| 48 | + |
| 49 | +Typography components accept either a `LocalizedString` (see [Type-safe localization](../Advanced/strict-localization)) or a complex React Node as children. |
| 50 | + |
| 51 | +If you need rich formatting, you can typically rely on the localization library you are using to create a React children node. |
| 52 | + |
| 53 | +For example, if you are using [react-i18next](https://react.i18next.com/), you can use the `Trans` component to create a React node: |
| 54 | + |
| 55 | +```tsx |
| 56 | +<Body size="medium"> |
| 57 | + <Trans i18nKey="My.Localization.Key" /> |
| 58 | +</Body> |
| 59 | +``` |
| 60 | + |
| 61 | +where the string matching `My.Localization.Key` can be along the lines of `Hello <b>World</b>!` |
| 62 | + |
| 63 | +You can read more about `Trans` component in the official [react-i18next documentation](https://react.i18next.com/latest/trans-component) |
0 commit comments