Slate Rich Text

The Slate editor has been supported by Payload since we released our initial beta. It's battle-tested and will continue to be supported into the future.

If you are migrating a Payload project from 1.x or earlier, you can continue to use the Slate editor as long as you'd like.

To use the Slate editor, first you need to install it:

1
npm install --save @payloadcms/richtext-slate

After installation, you can pass it to your top-level Payload config:

1
import { buildConfig } from 'payload/config'
2
import { slateEditor } from '@payloadcms/richtext-slate'
3
4
export default buildConfig({
5
collections: [
6
// your collections here
7
],
8
// Pass the Slate editor to the root config
9
editor: slateEditor({})
10
})

And here's an example for how to install the Slate editor on a field-by-field basis, while customizing its options:

1
import { CollectionConfig } from 'payload/types'
2
import { slateEditor } from '@payloadcms/richtext-slate'
3
4
export const Pages: CollectionConfig = {
5
slug: 'pages',
6
fields: [
7
{
8
name: 'content',
9
type: 'richText',
10
// Pass the Slate editor here and configure it accordingly
11
editor: slateEditor({
12
admin: {
13
elements: [
14
// customize elements allowed in Slate editor here
15
],
16
leaves: [
17
// customize leaves allowed in Slate editor here
18
]
19
}
20
})
21
}
22
]
23
}

Admin Options

elements

The elements property is used to specify which built-in or custom SlateJS elements should be made available to the field within the admin panel.

The default elements available in Payload are:

leaves

The leaves property specifies built-in or custom SlateJS leaves to be enabled within the Admin panel.

The default leaves available in Payload are:

  • bold
  • code
  • italic
  • strikethrough
  • underline

link.fields

This allows fields to be saved as extra fields on a link inside the Rich Text Editor. When this is present, the fields will render inside a modal that can be opened by clicking the "edit" button on the link element.

link.fields may either be an array of fields (in which case all fields defined in it will be appended below the default fields) or a function that accepts the default fields as only argument and returns an array defining the entirety of fields to be used (thus providing a mechanism of overriding the default fields).

RichText link fields RichText link with custom fields

upload.collections[collection-name].fields

This allows fields to be saved as meta data on an upload field inside the Rich Text Editor. When this is present, the fields will render inside a modal that can be opened by clicking the "edit" button on the upload element.

RichText upload element RichText field using the upload element

RichText upload element modal RichText upload element modal displaying fields from the config

Relationship element

The built-in relationship element is a powerful way to reference other Documents directly within your Rich Text editor.

Upload element

Similar to the relationship element, the upload element is a user-friendly way to reference Upload-enabled collections with a UI specifically designed for media / image-based uploads.

Relationship and Upload elements are populated dynamically into your Rich Text field' content. Within the REST and Local APIs, any present RichText relationship or upload elements will respect the depth option that you pass, and will be populated accordingly. In GraphQL, each richText field accepts an argument of depth for you to utilize.

TextAlign element

Text Alignment is not included by default and can be added to a Rich Text Editor by adding textAlign to the list of elements. TextAlign will alter the existing element to include a new textAlign field in the resulting JSON. This field can be used in combination with other elements and leaves to position content to the left, center or right.

Specifying which elements and leaves to allow

To specify which default elements or leaves should be allowed to be used for this field, define arrays that contain string names for each element or leaf you wish to enable. To specify a custom element or leaf, pass an object with all corresponding properties as outlined below. View the example to reference how this all works.

Building custom elements and leaves

You can design and build your own Slate elements and leaves to extend the editor with your own functionality. To do so, first start by reading the SlateJS documentation and looking at the Slate examples to familiarize yourself with the SlateJS editor as a whole.

Once you're up to speed with the general concepts involved, you can pass in your own elements and leaves to your field's admin config.

Both custom elements and leaves are defined via the following config:

PropertyDescription
name *The default name to be used as a type for this element.
Button *A React component to be rendered in the Rich Text toolbar.
pluginsAn array of plugins to provide to the Rich Text editor.
typeA type that overrides the default type used by name

Custom Elements also require the Element property set to a React component to be rendered as the Element within the rich text editor itself.

Custom Leaf objects follow a similar pattern but require you to define the Leaf property instead.

Specifying custom Types let you extend your custom elements by adding additional fields to your JSON object.

Example

collections/ExampleCollection.ts

1
import type { CollectionConfig } from 'payload/types'
2
3
import { slateEditor } from '@payloadcms/richtext-slate'
4
5
export const ExampleCollection: CollectionConfig = {
6
slug: 'example-collection',
7
fields: [
8
{
9
name: 'content', // required
10
type: 'richText', // required
11
defaultValue: [
12
{
13
children: [{ text: 'Here is some default content for this field' }],
14
},
15
],
16
required: true,
17
editor: slateEditor({
18
admin: {
19
elements: [
20
'h2',
21
'h3',
22
'h4',
23
'link',
24
'blockquote',
25
{
26
name: 'cta',
27
Button: CustomCallToActionButton,
28
Element: CustomCallToActionElement,
29
plugins: [
30
// any plugins that are required by this element go here
31
],
32
},
33
],
34
leaves: [
35
'bold',
36
'italic',
37
{
38
name: 'highlight',
39
Button: CustomHighlightButton,
40
Leaf: CustomHighlightLeaf,
41
plugins: [
42
// any plugins that are required by this leaf go here
43
],
44
},
45
],
46
link: {
47
// Inject your own fields into the Link element
48
fields: [
49
{
50
name: 'rel',
51
label: 'Rel Attribute',
52
type: 'select',
53
hasMany: true,
54
options: ['noopener', 'noreferrer', 'nofollow'],
55
},
56
],
57
},
58
upload: {
59
collections: {
60
media: {
61
fields: [
62
// any fields that you would like to save
63
// on an upload element in the `media` collection
64
],
65
},
66
},
67
},
68
},
69
}),
70
},
71
],
72
}

Generating HTML

As the Rich Text field saves its content in a JSON format, you'll need to render it as HTML yourself. Here is an example for how to generate JSX / HTML from Rich Text content:

1
import React, { Fragment } from "react";
2
import escapeHTML from "escape-html";
3
import { Text } from "slate";
4
5
const serialize = (children) =>
6
children.map((node, i) => {
7
if (Text.isText(node)) {
8
let text = (
9
<span dangerouslySetInnerHTML={{ __html: escapeHTML(node.text) }} />
10
);
11
12
if (node.bold) {
13
text = <strong key={i}>{text}</strong>;
14
}
15
16
if (node.code) {
17
text = <code key={i}>{text}</code>;
18
}
19
20
if (node.italic) {
21
text = <em key={i}>{text}</em>;
22
}
23
24
if (node.text === '') {
25
text = <br />;
26
}
27
28
// Handle other leaf types here...
29
30
return <Fragment key={i}>{text}</Fragment>;
31
}
32
33
if (!node) {
34
return null;
35
}
36
37
switch (node.type) {
38
case "h1":
39
return <h1 key={i}>{serialize(node.children)}</h1>;
40
// Iterate through all headings here...
41
case "h6":
42
return <h6 key={i}>{serialize(node.children)}</h6>;
43
case "blockquote":
44
return <blockquote key={i}>{serialize(node.children)}</blockquote>;
45
case "ul":
46
return <ul key={i}>{serialize(node.children)}</ul>;
47
case "ol":
48
return <ol key={i}>{serialize(node.children)}</ol>;
49
case "li":
50
return <li key={i}>{serialize(node.children)}</li>;
51
case "link":
52
return (
53
<a href={escapeHTML(node.url)} key={i}>
54
{serialize(node.children)}
55
</a>
56
);
57
58
default:
59
return <p key={i}>{serialize(node.children)}</p>;
60
}
61
});

Built-in SlateJS Plugins

Payload comes with a few built-in SlateJS plugins which can be extended to make developing your own elements and leaves a bit easier.

shouldBreakOutOnEnter

Payload's built-in heading elements all allow a "hard return" to "break out" of the currently active element. For example, if you hit enter while typing an h1, the h1 will be "broken out of" and you'll be able to continue writing as the default paragraph element.

If you want to utilize this functionality within your own custom elements, you can do so by adding a custom plugin to your element like the following "large body" element example:

customLargeBodyElement.js:

1
import Button from './Button'
2
import Element from './Element'
3
import withLargeBody from './plugin'
4
5
export default {
6
name: 'large-body',
7
Button,
8
Element,
9
plugins: [
10
(incomingEditor) => {
11
const editor = incomingEditor
12
const { shouldBreakOutOnEnter } = editor
13
14
editor.shouldBreakOutOnEnter = (element) =>
15
element.type === 'large-body' ? true : shouldBreakOutOnEnter(element)
16
17
return editor
18
},
19
],
20
}

Above, you can see that we are creating a custom SlateJS element with a name of large-body. This might render a slightly larger body copy on the frontend of your app(s). We pass it a name, button, and element—but additionally, we pass it a plugins array containing a single SlateJS plugin.

The plugin itself extends Payload's built-in shouldBreakOutOnEnter Slate function to add its own element name to the list of elements that should "break out" when the enter key is pressed.

TypeScript

If you are building your own custom Rich Text elements or leaves, you may benefit from importing the following types:

1
import type { RichTextCustomElement, RichTextCustomLeaf } from '@payloadcms/richtext-slate'
Next

Lexical Rich Text