Skip to content

Commit 7bdb5c3

Browse files
authored
docs: add clarity on toolbar preset selection logic (#886)
1 parent f57f153 commit 7bdb5c3

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

docs/how-to-customize-toolbars.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22

33
## How to customize toolbars
44

5+
### Understanding Presets
6+
7+
> **Important:** The editor uses two independent types of presets:
8+
9+
1. **Editor Preset** (`MarkdownEditorPreset`) - Defines the set of extensions and functionality available in the WYSIWYG editor. Configured via the `preset` property when initializing the editor.
10+
- Available values: `'zero'`, `'commonmark'`, `'default'`, `'yfm'`, `'full'`
11+
- Example: `useMarkdownEditor({ preset: 'default' })`
12+
13+
2. **Toolbar Preset** (`ToolbarsPreset`) - Defines which buttons appear in the toolbars and their order.
14+
- Configured via the **optional** `toolbarsPreset` property in the `MarkdownEditorView` component
15+
- Example: `<MarkdownEditorView toolbarsPreset={customToolbarPreset} />`
16+
17+
**How toolbar preset is determined:**
18+
19+
- **If `toolbarsPreset` is NOT provided** – automatically uses the toolbar preset matching the editor preset name
20+
- **If `toolbarsPreset` is provided** – uses your custom toolbar configuration
21+
22+
For example:
23+
```ts
24+
// Editor preset is 'default', toolbar preset is also 'default' (automatic)
25+
const editor = useMarkdownEditor({ preset: 'default' });
26+
<MarkdownEditorView editor={editor} />
27+
28+
// Editor preset is 'default', but toolbar preset is custom (override)
29+
const editor = useMarkdownEditor({ preset: 'default' });
30+
<MarkdownEditorView editor={editor} toolbarsPreset={myCustomToolbar} />
31+
```
32+
533
### Toolbar Types
634

735
The editor currently provides six types of toolbars:
@@ -19,14 +47,16 @@ More details can be found in [issue #508](https://github.com/gravity-ui/markdown
1947

2048
Starting from `@gravity-ui/[email protected]`, all toolbars—except the selection-based and slash-triggered toolbars—are configured using a shared dictionary of items and arrays defining the order of those items.
2149

22-
Base toolbar configurations are available in the `gravity-ui/markdown-editor` repository:
50+
Built-in **toolbar presets** are available in the `gravity-ui/markdown-editor` repository:
2351

2452
- [`zero`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L109)
2553
- [`commonmark`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L128)
2654
- [`default`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L303)
2755
- [`yfm`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L384)
2856
- [`full`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L517)
2957

58+
> **Note:** These toolbar presets have the same names as editor presets for convenience. When you don't specify `toolbarsPreset`, the editor automatically selects the toolbar preset matching your editor preset name.
59+
3060
### Configuration Details
3161

3262
1. The `items` key contains a shared dictionary used across the four main toolbars.
@@ -36,23 +66,27 @@ Base toolbar configurations are available in the `gravity-ui/markdown-editor` re
3666

3767
### Example Configuration
3868

39-
The `default` preset defines a shared `items` dictionary, specifies the display order in `orders`, and connects the corresponding extensions in the `extensions` section.
69+
The `default` **editor preset** defines a set of extensions, while the `default` **toolbar preset** defines the toolbar configuration:
4070

41-
1. Extension set:
71+
1. **Editor preset** - Extension set:
4272
[src/presets/default.ts#L10](https://github.com/gravity-ui/markdown-editor/blob/c1a23b649f2f69ad71a49989d66ba4a9224e40af/src/presets/default.ts#L10)
43-
2. Toolbar items dictionary:
73+
2. **Toolbar preset** - Toolbar items dictionary:
4474
[src/modules/toolbars/presets.ts#L308](https://github.com/gravity-ui/markdown-editor/blob/c1a23b649f2f69ad71a49989d66ba4a9224e40af/src/modules/toolbars/presets.ts#L308)
45-
3. Toolbar order definition:
75+
3. **Toolbar preset** - Toolbar order definition:
4676
[src/modules/toolbars/presets.ts#L316](https://github.com/gravity-ui/markdown-editor/blob/c1a23b649f2f69ad71a49989d66ba4a9224e40af/src/modules/toolbars/presets.ts#L316)
4777

4878
### Customizing the Toolbar
4979

50-
The library provides a set of predefined presets that cannot be overridden directly. If none of the built-in presets suit your needs, you can define a custom toolbar configuration. Below is an example of how to do that:
80+
The library provides a set of predefined toolbar presets that cannot be overridden directly. If none of the built-in toolbar presets suit your needs, you can define a custom toolbar configuration. Below is an example of how to do that:
5181

5282
- [Live demo (custom preset)](https://preview.gravity-ui.com/md-editor/?path=/story/extensions-presets--custom)
5383
- [Presets.stories.tsx#L30](https://github.com/gravity-ui/markdown-editor/blob/main/demo/stories/presets/Presets.stories.tsx#L30)
5484
- [presets.ts#L21](https://github.com/gravity-ui/markdown-editor/blob/main/demo/stories/presets/presets.ts#L21)
5585

86+
#### Step 1: Define your custom toolbar preset
87+
88+
Create a `ToolbarsPreset` object with `items` and `orders`:
89+
5690
```ts
5791
export const toolbarPresets: Record<string, ToolbarsPreset> = {
5892
custom: {
@@ -89,6 +123,29 @@ export const toolbarPresets: Record<string, ToolbarsPreset> = {
89123
};
90124
```
91125

126+
#### Step 2: Use your custom toolbar preset with the editor
127+
128+
```ts
129+
import {useMarkdownEditor, MarkdownEditorView} from '@gravity-ui/markdown-editor';
130+
import {toolbarPresets} from './your-toolbar-presets';
131+
132+
function MyEditor() {
133+
// Initialize editor with an editor preset (defines functionality)
134+
const editor = useMarkdownEditor({
135+
preset: 'default', // or 'zero', 'commonmark', 'yfm', 'full'
136+
});
137+
138+
return (
139+
<MarkdownEditorView
140+
editor={editor}
141+
toolbarsPreset={toolbarPresets.custom} // Override with custom toolbar preset
142+
/>
143+
);
144+
}
145+
```
146+
147+
> **Key point:** By providing `toolbarsPreset`, you override the default toolbar configuration. Without it, the editor would use the built-in `'default'` toolbar preset (matching the editor preset name).
148+
92149
### Conditional Toolbar Items
93150

94151
Sometimes you may want to display different sets of toolbar items depending on certain conditions—for example, user permissions. In such cases, you can implement a getter function that returns the appropriate toolbar configuration based on parameters. Example:

0 commit comments

Comments
 (0)