Installing Slate
Slate is a monorepo divided up into multiple npm packages, so to install it you do:
1
yarn add slate slate-react
Copied!
You'll also need to be sure to install Slate's peer dependencies:
1
yarn add react react-dom
Copied!
Note, if you'd rather use a pre-bundled version of Slate, you can yarn add slate and retrieve the bundled dist/slate.js file! Check out the Using the Bundled Source guide for more information.
Once you've installed Slate, you'll need to import it.
1
// Import React dependencies.
2
import React, { useMemo, useState } from 'react'
3
// Import the Slate editor factory.
4
import { createEditor } from 'slate'
5
6
// Import the Slate components and React plugin.
7
import { Slate, Editable, withReact } from 'slate-react'
Copied!
Before we use those imports, let's start with an empty <App> component:
1
// Define our app...
2
const App = () => {
3
return null
4
}
Copied!
The next step is to create a new Editor object. We want the editor to be stable across renders, so we use the useMemo hook:
1
const App = () => {
2
// Create a Slate editor object that won't change across renders.
3
const editor = useMemo(() => withReact(createEditor()), [])
4
return null
5
}
Copied!
Of course we haven't rendered anything, so you won't see any changes.
If you are using TypeScript, you will also need to extend the Editor with ReactEditor and add annotations as per the documentation on TypeScript. The example below also includes the custom types required for the rest of this example.
1
// TypeScript users only add this code
2
import { BaseEditor, Descendant } from 'slate'
3
import { ReactEditor } from 'slate-react'
4
5
type CustomElement = { type: 'paragraph'; children: CustomText[] }
6
type CustomText = { text: string }
7
8
declare module 'slate' {
9
interface CustomTypes {
10
Editor: BaseEditor & ReactEditor
11
Element: CustomElement
12
Text: CustomText
13
}
14
}
Copied!
1
// Also you must annotate `useState<Descendant[]>` and the editor's initial value.
2
const App = () => {
3
const initialValue: CustomElement[] = []
4
const [value, setValue] = useState<Descendant[]>(initialValue)
5
return (
6
<Slate value={value} onChange={setValue}>
7
...
8
</Slate>
9
)
10
}
Copied!
Next we want to create state for value:
1
const App = () => {
2
const editor = useMemo(() => withReact(createEditor()), [])
3
4
// Keep track of state for the value of the editor.
5
const [value, setValue] = useState([])
6
return null
7
}
Copied!
Next up is to render a <Slate> context provider.
The provider component keeps track of your Slate editor, its plugins, its value, its selection, and any changes that occur. It must be rendered above any <Editable> components. But it can also provide the editor state to other components like toolbars, menus, etc. using the useSlate hook.
1
const App = () => {
2
const editor = useMemo(() => withReact(createEditor()), [])
3
const [value, setValue] = useState([])
4
// Render the Slate context.
5
return (
6
<Slate
7
editor={editor}
8
value={value}
9
onChange={newValue => setValue(newValue)}
10
/>
11
)
12
}
Copied!
You can think of the <Slate> component as providing a "controlled" context to every component underneath it.
This is a slightly different mental model than things like <input> or <textarea>, because richtext documents are more complex. You'll often want to include toolbars, or live previews, or other complex components next to your editable content.
By having a shared context, those other components can execute commands, query the editor's state, etc.
Okay, so the next step is to render the <Editable> component itself:
1
const App = () => {
2
const editor = useMemo(() => withReact(createEditor()), [])
3
const [value, setValue] = useState([])
4
return (
5
// Add the editable component inside the context.
6
<Slate
7
editor={editor}
8
value={value}
9
onChange={newValue => setValue(newValue)}
10
>
11
<Editable />
12
</Slate>
13
)
14
}
Copied!
The <Editable> component acts like contenteditable. Anywhere you render it will render an editable richtext document for the nearest editor context.
There's only one last step. So far we've been using an empty [] array as the initial value of the editor, so it has no content. Let's fix that by defining an initial value.
The value is just plain JSON. Here's one containing a single paragraph block with some text in it:
1
const App = () => {
2
const editor = useMemo(() => withReact(createEditor()), [])
3
// Add the initial value when setting up our state.
4
const [value, setValue] = useState([
5
{
6
type: 'paragraph',
7
children: [{ text: 'A line of text in a paragraph.' }],
8
},
9
])
10
11
return (
12
<Slate
13
editor={editor}
14
value={value}
15
onChange={newValue => setValue(newValue)}
16
>
17
<Editable />
18
</Slate>
19
)
20
}
Copied!
There you have it!
That's the most basic example of Slate. If you render that onto the page, you should see a paragraph with the text A line of text in a paragraph. And when you type, you should see the text change!
Last modified 14d ago
Copy link