Walkthroughs

Applying Custom Formatting

3min

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".

QWEQWRQWR QRQW RQWR RQW

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:

Text


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:

Text


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:

Text


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.

Text


Now, if you try selecting a piece of text and hitting Ctrl-B you should see it turn bold! Magic!

Updated 09 May 2024
Doc contributor
Doc contributor
Doc contributor
Did this page help you?