Applying Custom Formatting
In the previous guide we learned how to create custom block types that render chunks of text inside different containers. But Slate allows for more than just "blocks".
In this guide, we'll show you how to add custom formatting options, like bold, italic, code or strikethrough.
So we start with our app from earlier:
1
const renderElement = props => {
2
switch (props.element.type) {
3
case 'code':
4
return <CodeElement {...props} />
5
default:
6
return <DefaultElement {...props} />
7
}
8
}
9
10
const initialValue = [
11
{
12
type: 'paragraph',
13
children: [{ text: 'A line of text in a paragraph.' }],
14
},
15
]
16
17
const App = () => {
18
const editor = useMemo(() => withReact(createEditor()), [])
19
20
return (
21
<Slate editor={editor} value={initialValue}>
22
<Editable
23
renderElement={renderElement}
24
onKeyDown={event => {
25
if (event.key === '`' && event.ctrlKey) {
26
event.preventDefault()
27
const [match] = Editor.nodes(editor, {
28
match: n => n.type === 'code',
29
})
30
Transforms.setNodes(
31
editor,
32
{ type: match ? 'paragraph' : 'code' },
33
{ match: n => Editor.isBlock(editor, n) }
34
)
35
}
36
}}
37
/>
38
</Slate>
39
)
40
}
Copied!
And now, we'll edit the onKeyDown handler to make it so that when you press control-B, it will add a bold format to the currently selected text:
1
const initialValue = [
2
{
3
type: 'paragraph',
4
children: [{ text: 'A line of text in a paragraph.' }],
5
},
6
]
7
8
const App = () => {
9
const editor = useMemo(() => withReact(createEditor()), [])
10
11
const renderElement = useCallback(props => {
12
switch (props.element.type) {
13
case 'code':
14
return <CodeElement {...props} />
15
default:
16
return <DefaultElement {...props} />
17
}
18
}, [])
19
20
return (
21
<Slate editor={editor} value={initialValue}>
22
<Editable
23
renderElement={renderElement}
24
onKeyDown={event => {
25
if (!event.ctrlKey) {
26
return
27
}
28
29
switch (event.key) {
30
// When "`" is pressed, keep our existing code block logic.
31
case '`': {
32
event.preventDefault()
33
const [match] = Editor.nodes(editor, {
34
match: n => n.type === 'code',
35
})
36
Transforms.setNodes(
37
editor,
38
{ type: match ? 'paragraph' : 'code' },
39
{ match: n => Editor.isBlock(editor, n) }
40
)
41
break
42
}
43
44
// When "B" is pressed, bold the text in the selection.
45
case 'b': {
46
event.preventDefault()
47
Transforms.setNodes(
48
editor,
49
{ bold: true },
50
// Apply it to text nodes, and split the text node up if the
51
// selection is overlapping only part of it.
52
{ match: n => Text.isText(n), split: true }
53
)
54
break
55
}
56
}
57
}}
58
/>
59
</Slate>
60
)
61
}
Copied!
Okay, so we've got the hotkey handler setup... but! If you happen to now try selecting text and hitting Ctrl-B, you won't notice any change. That's because we haven't told Slate how to render a "bold" mark.
For every format you add, Slate will break up the text content into "leaves", and you need to tell Slate how to read it, just like for elements. So let's define a Leaf component:
1
// Define a React component to render leaves with bold text.
2
const Leaf = props => {
3
return (
4
<span
5
{...props.attributes}
6
style={{ fontWeight: props.leaf.bold ? 'bold' : 'normal' }}
7
>
8
{props.children}
9
</span>
10
)
11
}
Copied!
Pretty familiar, right?
And now, let's tell Slate about that leaf. To do that, we'll pass in the renderLeaf prop to our editor.
1
const initialValue = [
2
{
3
type: 'paragraph',
4
children: [{ text: 'A line of text in a paragraph.' }],
5
},
6
]
7
8
const App = () => {
9
const editor = useMemo(() => withReact(createEditor()), [])
10
11
const renderElement = useCallback(props => {
12
switch (props.element.type) {
13
case 'code':
14
return <CodeElement {...props} />
15
default:
16
return <DefaultElement {...props} />
17
}
18
}, [])
19
20
// Define a leaf rendering function that is memoized with `useCallback`.
21
const renderLeaf = useCallback(props => {
22
return <Leaf {...props} />
23
}, [])
24
25
return (
26
<Slate editor={editor} value={initialValue}>
27
<Editable
28
renderElement={renderElement}
29
// Pass in the `renderLeaf` function.
30
renderLeaf={renderLeaf}
31
onKeyDown={event => {
32
if (!event.ctrlKey) {
33
return
34
}
35
36
switch (event.key) {
37
case '`': {
38
event.preventDefault()
39
const [match] = Editor.nodes(editor, {
40
match: n => n.type === 'code',
41
})
42
Transforms.setNodes(
43
editor,
44
{ type: match ? null : 'code' },
45
{ match: n => Editor.isBlock(editor, n) }
46
)
47
break
48
}
49
50
case 'b': {
51
event.preventDefault()
52
Transforms.setNodes(
53
editor,
54
{ bold: true },
55
{ match: n => Text.isText(n), split: true }
56
)
57
break
58
}
59
}
60
}}
61
/>
62
</Slate>
63
)
64
}
65
66
const Leaf = props => {
67
return (
68
<span
69
{...props.attributes}
70
style={{ fontWeight: props.leaf.bold ? 'bold' : 'normal' }}
71
>
72
{props.children}
73
</span>
74
)
75
}
Copied!
Now, if you try selecting a piece of text and hitting Ctrl-B you should see it turn bold! Magic!
Copy link