|
| 1 | +--- |
| 2 | +sidebar_position: 7 |
| 3 | +--- |
| 4 | + |
| 5 | +import InteractiveExample from '@site/src/components/InteractiveExample'; |
| 6 | +import TextShortcutsEditor from '@site/src/examples/TextShortcutsEditor'; |
| 7 | +import TextShortcutsEditorSrc from '!!raw-loader!@site/src/examples/TextShortcutsEditor'; |
| 8 | + |
| 9 | +# Text shortcuts |
| 10 | + |
| 11 | +Text shortcuts let users format as they type, the way Markdown editors do - |
| 12 | +typing `#` turns a line into a heading, wrapping a word in `**` makes it bold. |
| 13 | +You configure them with the `textShortcuts` prop, an array that maps a `trigger` |
| 14 | +string to a `style`: |
| 15 | + |
| 16 | +```tsx |
| 17 | +<EnrichedTextInput |
| 18 | + textShortcuts={[ |
| 19 | + { trigger: '# ', style: 'h1' }, |
| 20 | + { trigger: '**', style: 'bold' }, |
| 21 | + ]} |
| 22 | + /* ... */ |
| 23 | +/>; |
| 24 | +``` |
| 25 | + |
| 26 | +The `style` values are the same rich text styles you met in |
| 27 | +[Basic styles](/rich-text-formatting/basic-styles) - and the inline vs. paragraph |
| 28 | +distinction is exactly what decides how a shortcut fires. |
| 29 | + |
| 30 | +## Paragraph vs. inline shortcuts |
| 31 | + |
| 32 | +The two families of styles trigger differently, because they format different |
| 33 | +things: |
| 34 | + |
| 35 | +- **Paragraph shortcuts** (`h1`–`h6`, `blockquote`, `codeblock`, |
| 36 | + `unordered_list`, `ordered_list`, `checkbox_list`) fire at the **start of a |
| 37 | + paragraph**. The trigger is the prefix you type before the line's content - |
| 38 | + e.g. `#` for a heading, `-` for a bulleted list. Since a paragraph can only hold |
| 39 | + one paragraph style, these only fire on a **plain** paragraph: if the line is |
| 40 | + already a heading or a list item, typing the prefix does nothing. |
| 41 | + |
| 42 | +- **Inline shortcuts** (`bold`, `italic`, `underline`, `strikethrough`, |
| 43 | + `inline_code`) fire when you type a **closing delimiter** around some text, so |
| 44 | + typing `**word**` bolds `word`. |
| 45 | + |
| 46 | +:::note |
| 47 | + |
| 48 | +The usual [style rules](/rich-text-formatting/basic-styles#paragraph-styles) still |
| 49 | +apply to shortcut-triggered styles. If the target style is **blocked** by an |
| 50 | +active one (e.g. bold inside a code block). |
| 51 | + |
| 52 | +::: |
| 53 | + |
| 54 | +## Defaults |
| 55 | + |
| 56 | +Even without the prop, two shortcuts are active out of the box: |
| 57 | + |
| 58 | +```ts |
| 59 | +[ |
| 60 | + { trigger: '- ', style: 'unordered_list' }, |
| 61 | + { trigger: '1. ', style: 'ordered_list' }, |
| 62 | +]; |
| 63 | +``` |
| 64 | + |
| 65 | +Passing your own array replaces these defaults entirely, and passing an empty |
| 66 | +array (`textShortcuts={[]}`) disables shortcuts altogether. |
| 67 | + |
| 68 | +## Try it out |
| 69 | + |
| 70 | +The editor below wires up one shortcut of each kind - a paragraph one (`#` → H1) |
| 71 | +and an inline one (`**` → bold). Start a line with `#` and watch it become a |
| 72 | +heading, then wrap a word in `**stars**` to bold it. |
| 73 | + |
| 74 | +<InteractiveExample src={TextShortcutsEditorSrc} component={TextShortcutsEditor} /> |
| 75 | + |
| 76 | +:::info |
| 77 | + |
| 78 | +On Web, the same styles are also reachable through |
| 79 | +[keyboard shortcuts](/api-reference/enriched-text-input) like ⌘B / Ctrl+B, |
| 80 | +independent of the `textShortcuts` prop. |
| 81 | + |
| 82 | +::: |
0 commit comments