Rich text editor
Installation
Package has peer dependencies: react, react-dom, @mantine/hooks and @mantine/core:.
Install with npm:
Install with yarn:
Demo
Welcome to Mantine Rich Text Editor
RichTextEditor component focuses on usability and is designed to be as simple as possible to bring a familiar editing experience to regular users. RichTextEditor is based on Quill.js via react-quill and supports most of its features:
- General text formatting: bold, italic, underline,
strikethrough - Headings (h1-h6)
- Sub and super scripts (<sup /> and <sub /> tags)
- Ordered and bullet lists
- Image and video embeds
- Text align
But RichTextEditor is not just a wrapper for react-quill, it comes with a bunch of extra features:
- Seamless integration with your Mantine theme – component will use font-family, font-sizes, spacing and primary color from your custom theme, defined in MantineProvider
- Dark theme support – like any other Mantine component, RichTextEditor supports dark theme out of the box
- Images uploading – specify upload function (S3 or anywhere else) that will be triggered when user pastes or drops image to editor
- Sticky toolbar will be visible when user scrolls
Usage
value
and onChange
props are required for component to work.
Note that 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 formattingclean
– removes all inline formattingh1
,h2
, ...,h6
– headings, in default toolbar only h1-h4 headings are displayedlink
– link editorblockquote
– blockquotesub
,sup
– super and sub scriptsvideo
,image
– video and image embedsunorderedList
,orderedList
– ul and ol tagsalignCenter
,alignLeft
,alignRight
– controls text-aligncode
andcodeBlock
– 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 to false to make toolbar stay at the topstickyOffset
– top property, used with sticky position, use it to offset elements with fixed position, for example, Mantine docs website has 60px header, in this case you should set stickyOffset to 60
Images and videos embeds
Embedding videos and images is simple
To embed a video click video icon and paste a link to YouTube, Vimeo or other video service which supports inserting via iframe. Images are more complex you will need to setup uploading function and then editor will handle all heavy image stuff: dnd, pasting from clipboard and inserting with image button. Try the thing out!
Image embed
YouTube video embed
Vimeo video embed
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, add 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 readonly state, user cannot edit content, 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
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: