This is a fun series about building a rich text editor with React and Slate. Here’s part one where we left off. You’ll want to read it first if you haven’t already!
The editor has to look sharp. Who wants to stare at and use a scruffy editor? So why don’t we add some quick CSS to make it look more spectacular.
In a traditional create-react-app, we have the
index.css file inside the
index.css is used for applying “global” styles for our app. By global styles I’m keeping in mind DOM elements which apply to all pages/documents. For example
Applying system fonts, background colors, paddings, margins, colors and the width for our editor.
If you’re curious about what kind of “shenanigans” are going on with the
font-family — listen up. We are defaulting to the font(s) the user has installed on their computer. This has many performance boosts since there is no need to download the fonts.
Check out this clever explanation by
css-tricks for more in detail.
We should end up with something smarter. 😅
What are some of the key features a rich text editor has? Hmm — let’s see.
- Code highlighting
- Embedded Videos
- Text formatting (alignment, bullet list, ordered list, etc)
- Save the text to a database for safe keeping
Let’s start by adding the images.
Rich text editors have a toolbar which eases the efforts of editing. Take a look at Medium’s toolbar.
Medium’s toolbar for formatting
When we click on a selected text, a popup toolbar pops up from the ashes which allows us to format our text.
It makes sense to start implementing the toolbar first and then hook it up with the appropriate functionality.
Let’s grab a small library which gives us access to a huge array of icons.
The icons we want to use are feather icons.
I propose creating a component which holds all the formatting logic and styles in one place. Call it
FormatToolbar.js and place it inside
FormatToolbar is a stateless component which renders its content thanks to using
A stateless component is a component which doesn’t make use of the
this.state mechanism, but rather lets its parent figure out the state.
Think of it this way: we want to separate concerns as much as possible. A stateless component handles rendering the view (
images) and only the view.
A stateful container takes care of its internal
state and passes it as
props to the stateless components.
props.children allows you nest components inside a component.
If you’re unsure about
props.children — this article is good at explaining the concept behind children props in React. ✌️
Next up, we will import
FormatToolbar.js component to our
Importing our icons to the TextEditor component
And finally, consume our
FormatToolbar like this:
Passing our toolbar component to the editor render function
Oh and I also added some styles to make it look neat. Remember, the
.format-toolbar is our container component which holds all the icons. We set it to flex so they’re all nicely aligned in a row and start from the left. We also removed the default border from our button since we’re going for the clean/minimalist approach.
Applying styles to our tooltip
toolbar is the full row and the
tooltip is a single item/collection.
What gets rendered on the screen
Woah! Starting to look like an actual rich text editor. Well done! Allow yourself a fresh coffee and let’s get right back to tackling our app.
There’s no shortage of remarkable ideas, what’s missing is the will to execute them. — Seth Godin
Clicking on the icons has no affect since we didn’t add a single event listener to our icons.
Time to change that. One of the goals of this tutorial is to teach you how to approach problems and how to solve them. Solving complex problems is very challenging. An ubiquitous approach to problem solving is breaking the problem apart into small solvable pieces.
Think about it — what are we missing? We have the formatting functionality in place, the icons, and the styling.
You guessed it, we need to bundle icons and text together. By clicking on the icon, we want to format our text.
Let’s start by adding a event listener on the button. Which event listener should we use? There are a bazillion options out there like
onMouseDown, touchend and so on.
So, what’s the best choice? Good question and I’m glad you asked. We’re going to use a new event listener which was introduced in React version 16.4. The event listener has a particular name —
I wrote this brief introduction to pointer events. In a nutshell, instead of writing an event listener for the mouse, touch surface, and the pen, we use a different approach. That approach uses the 3-in-1 solution, compatible with all devices.
The pointer is like any event, just attach it to your button like so:
Adding pointer events to our buttons
onPointerDown is fired just like
Notice we added a callback with the name onMarkClick. The first argument is the event object and the second argument type is the formatting we want to apply.
Don’t be startled, the comments make it seem bloated. The only functionality we applied was the following:
1. Cancel the browser’s default behavior
2. Get the
this.state.value — remember this is the initial value we passed to Slate. The slate model has built in functions, just like mongoose.
3. Apply the formatting on the selected text with the desired format.
4. Finally call an update on the component to display the newly made changes.
Clicking on the icon changes our text
Let’s finish up by adding some final touches. We’ll be adding code formatting, lists, and underlining functionality. Try following our examples and finish it up.
onKeyDown the left and renderMark the right
And finally, creating our elements inside the
Woohoo! We made it. Time for the long-awaited dopamine hit.
The editor already looks like a tool we use everyday. Notice the elements being placed and removed in the vDOM — React takes care of this for you.
Congratulations for making it to the end. If you would like me to add more features, let me know on Twitter or in the discussion section.
Thanks for reading! ❤