|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +import InteractiveExample from '@site/src/components/InteractiveExample'; |
| 6 | +import BasicStylesEditor from '@site/src/examples/BasicStylesEditor'; |
| 7 | +import BasicStylesEditorSrc from '!!raw-loader!@site/src/examples/BasicStylesEditor'; |
| 8 | + |
| 9 | +# Basic styles |
| 10 | + |
| 11 | +In [Your first editor](/fundamentals/your-first-editor) you wired up a single |
| 12 | +**Bold** button. Every other formatting style works exactly the same way: a |
| 13 | +`toggle` method on the `ref` changes the text, and `onChangeState` reports back |
| 14 | +whether the style is active. The only exceptions to that rule are **mentions**, |
| 15 | +**links** and **inline images**, which work a bit differently. They cannot be toggled |
| 16 | +on/off on any text - they require additional data to be valid,but you'll learn more |
| 17 | +about them in further sections. This page walks through the rest of the basic rich |
| 18 | +text styles and the one core distinction that shapes how they behave - **inline** |
| 19 | +versus **paragraph** formatting. |
| 20 | + |
| 21 | +## Inline styles |
| 22 | + |
| 23 | +Inline styles wrap a range of characters. Toggling one applies it to exactly the |
| 24 | +characters you have selected, or - if nothing is selected - arms it so the next |
| 25 | +characters you type come out styled. This is the family the bold belongs to: |
| 26 | + |
| 27 | +- **Bold** - `toggleBold()` |
| 28 | +- **Italic** - `toggleItalic()` |
| 29 | +- **Underline** - `toggleUnderline()` |
| 30 | +- **Strikethrough** - `toggleStrikeThrough()` |
| 31 | +- **Inline code** - `toggleInlineCode()` |
| 32 | + |
| 33 | +They combine freely, so a word can be bold and italic and underlined at |
| 34 | +once. |
| 35 | + |
| 36 | +:::note |
| 37 | + |
| 38 | +There are two more inline styles, which you cannot toggle quite like the others - **links** and **mentions**. You'll learn about them in their respective sections, once you finish this chapter. |
| 39 | + |
| 40 | +::: |
| 41 | + |
| 42 | +## Paragraph styles |
| 43 | + |
| 44 | +Paragraph styles apply to a whole paragraph at once. Toggling one affects the entire paragraph the |
| 45 | +cursor sits in (or every paragraph the selection touches), no matter how much of |
| 46 | +it is actually selected. The common ones are: |
| 47 | + |
| 48 | +- **Headings** - `toggleH1()` through `toggleH6()` |
| 49 | +- **Blockquote** - `toggleBlockQuote()` |
| 50 | +- **Code block** - `toggleCodeBlock()` |
| 51 | +- **Unordered list** - `toggleUnorderedList()` |
| 52 | +- **Ordered list** - `toggleOrderedList()` |
| 53 | +- **Checkbox list** - `toggleCheckboxList(checked: boolean)` |
| 54 | + |
| 55 | +:::note |
| 56 | + |
| 57 | +We will not touch list styles for now - we'll cover them in-depth in their [own section](/rich-text-formatting/lists). |
| 58 | + |
| 59 | +::: |
| 60 | + |
| 61 | +The key difference from inline styles is that **a paragraph can only have one |
| 62 | +paragraph style at a time**. A line can't be both a heading and a blockquote, so |
| 63 | +toggling a second paragraph style replaces the first rather than stacking on top. |
| 64 | +The editor surfaces this through the `isConflicting` flag in `onChangeState`. |
| 65 | + |
| 66 | +Code blocks go one step further: they **block** inline styles entirely. You |
| 67 | +can't make text bold inside a code block, and `onChangeState` reports bold as |
| 68 | +`isBlocking` there so you can grey the button out. |
| 69 | + |
| 70 | +:::tip |
| 71 | + |
| 72 | +The full table of what every style blocks or conflicts with is available in |
| 73 | +[Supported tags](/fundamentals/html-format-and-supported-tags) and the `onChangeState` behavior is described in [Style state model](/fundamentals/core-concepts#the-style-state-model). |
| 74 | + |
| 75 | +::: |
| 76 | + |
| 77 | +## Building the toolbar |
| 78 | + |
| 79 | +A toolbar with many styles is just the [first editor](/fundamentals/your-first-editor) |
| 80 | +loop repeated. Each button reads its slice of the `onChangeState` payload for |
| 81 | +`isActive` (to highlight it) and `isBlocking` (to disable it), and calls the |
| 82 | +matching `toggle` method on press: |
| 83 | + |
| 84 | +```tsx |
| 85 | +const inlineButtons = [ |
| 86 | + { label: 'Bold', state: state?.bold, onPress: () => ref.current?.toggleBold() }, |
| 87 | + { label: 'Italic', state: state?.italic, onPress: () => ref.current?.toggleItalic() }, |
| 88 | + { label: 'Underline', state: state?.underline, onPress: () => ref.current?.toggleUnderline() }, |
| 89 | + { label: 'Strike', state: state?.strikeThrough, onPress: () => ref.current?.toggleStrikeThrough() }, |
| 90 | +]; |
| 91 | + |
| 92 | +const paragraphButtons = [ |
| 93 | + { label: 'H1', state: state?.h1, onPress: () => ref.current?.toggleH1() }, |
| 94 | + { label: 'H2', state: state?.h2, onPress: () => ref.current?.toggleH2() }, |
| 95 | + { label: 'Quote', state: state?.blockQuote, onPress: () => ref.current?.toggleBlockQuote() }, |
| 96 | + { label: 'Code', state: state?.codeBlock, onPress: () => ref.current?.toggleCodeBlock() }, |
| 97 | +]; |
| 98 | +``` |
| 99 | + |
| 100 | +## Try it out |
| 101 | + |
| 102 | +And just like that we've expanded our editor with several new styles to choose |
| 103 | +from a small toolbar. Select some text and toggle inline styles, or place the cursor |
| 104 | +on a line and try the paragraph ones - notice how switching from **H1** to **Quote** |
| 105 | +swaps the style instead of adding to it, and how the inline buttons grey out once |
| 106 | +you're inside a **Code** block. |
| 107 | + |
| 108 | +<InteractiveExample src={BasicStylesEditorSrc} component={BasicStylesEditor} /> |
| 109 | + |
| 110 | +:::info |
| 111 | + |
| 112 | +This page is about _what_ each style does, using the built-in defaults. To change |
| 113 | +how they look - heading sizes, blockquote borders, code block colors - see |
| 114 | +[Styling the input](/core-functionalities/styling-the-input). |
| 115 | + |
| 116 | +::: |
0 commit comments