Rich text editor
Installation
Package depends on @mantine/core and @mantine/hooks.
Install with yarn:
Install with npm:
Demo
Usage
value and onChange props are required for component to work.
Note that even though component is controlled, you cannot force value (limitation of Quill.js library).
Configure toolbar
RichTextEditor supports these controls in toolbar:
bold,strike,italic,underline– general inline formatting.clean– removes all inline formatting.h1,h2, ...,h6– headings. Onlyh1-h4headings are displayed in the toolbar by default.link– link editor.blockquote– blockquote.sub,sup– super and sub scripts.video,image– video and image embeds.unorderedList,orderedList–ulandoltags.alignCenter,alignLeft,alignRight– controlstext-align.codeandcodeBlock– inline and block code.
You can add, remove and configure controls arrangement in toolbar with controls prop:
To configure sticky toolbar properties set following props:
sticky– set tofalseto make toolbar stay at the top.stickyOffset–topproperty, used withstickyposition. Use it to offset elements with fixed position. For example, Mantine docs website has60pxheader. In this case you should setstickyOffsetto60.
Images and videos embeds
Restrict formats
To restrict the allowed formats, set formats prop with an array of quill formats. Note that you will also need to remove toolbar
buttons. In the following example three formats are enabled: bold, italic and underline while
toolbar includes italic and underline controls. bold format can be added with Ctrl + B keyboard
shortcut. Other formats are disabled:
Images upload
RichTextEditor will handle images upload in following situations:
- Image button click in toolbar
- Image was pasted from clipboard into editor
- Image was dropped into editor
To set up images upload add onImageUpload function:
Important! If you do not provide onImageUpload all images will be converted to
base64 format. In most cases this is not a valid option to store images so make sure you provide onImageUpload
if you are planning to use images.
Mentions
RichTextEditor comes with quill-mentions plugin.
To add mentions support, provide quill-mentions configuration to mentions prop:
Extra modules
You can provide any amount of extra modules. Note that it is required to memoize modules object:
Read only
When editor is in readonly state, user cannot edit content and the toolbar is hidden:
Keyboard shortcuts
⌘ + B/Ctrl + B– toggle bold format in current selection⌘ + I/Ctrl + I– toggle italic format in current selection⌘ + U/Ctrl + U– toggle underline format in current selection⌘ + K/Ctrl + K– add link to current selection⌘ + option + 1/Ctrl + Alt + 1– toggle heading at current line, valid for 1-6 headings
Get editor ref
Render HTML content
You can use TypographyStylesProvider component
to render RichTextEditor value.
Server side rendering
Quill does not support server side rendering as it relies on browser API. To make component work on server you will need to create a wrapper component with additional checks.
General strategy:
Usage with Next.js
To make component work with Next.js use dynamic module:
Then when you want to use RichTextEditor import your component instead:
React strict mode limitations
RichTextEditor component is not compatible with React strict mode. You will experience
the following error if strict mode is enabled: TypeError: Cannot read properties of null (reading 'index').
To fix it, disable strict mode.
RichTextEditor component props
| Name | Type | Description |
|---|---|---|
| children * | ReactNode | Child editor components |
| editor * | Editor | Tiptap editor instance |
| labels | Partial<RichTextEditorLabels> | Labels that are used in controls |
| withCodeHighlightStyles | boolean | Determines whether code highlight styles should be added, true by default |
| withTypographyStyles | boolean | Determines whether typography styles should be added, true by default |
RichTextEditor component Styles API
| Name | Static selector | Description |
|---|---|---|
| root | .mantine-RichTextEditor-root | Root element |
| toolbar | .mantine-RichTextEditor-toolbar | Toolbar element |
| content | .mantine-RichTextEditor-content | Content area |
| typographyStylesProvider | .mantine-RichTextEditor-typographyStylesProvider | TypographyStylesProvider component, wraps content |
| control | .mantine-RichTextEditor-control | RichTextEditor.Control root element, used as a base for all controls |
| controlsGroup | .mantine-RichTextEditor-controlsGroup | RichTextEditor.ControlsGroup component root |
| linkEditor | .mantine-RichTextEditor-linkEditor | Link editor root element |
| linkEditorSave | .mantine-RichTextEditor-linkEditorSave | Link editor save button |
| linkEditorInput | .mantine-RichTextEditor-linkEditorInput | Link editor url input |
| linkEditorExternalControl | .mantine-RichTextEditor-linkEditorExternalControl | Link editor external button |