DEV Community

Cover image for The Power of Rich Text Editing with jodit-react
Sahil Khurana
Sahil Khurana

Posted on • Originally published at innostax.com

The Power of Rich Text Editing with jodit-react

Key takeaways

react-jodit includes almost all formatting tools that one might need, and it allows you to customize the interface according to your requirements. This library is very light and uses React’s Virtual DOM for rendering. It is efficient and works smoothly, regardless of whether the content is complex or not. react-jodit is cross-browser and meets all the accessibility standards, which makes it a really good choice to use.

What is react-jodit?

If you’ve created a blog, a content management system, or even a web application where people have to format their text, you would know how crucial the right editor is. The wrong editor will annoy your users. The right editor blends seamlessly into your application, letting your users get things done without any trouble.

react-jodit allows developers to use the highly popular Jodit editor inside their React apps via an easily reusable editor component that integrates Jodit seamlessly with your React components and provides all the features that Jodit can provide.

Advantages of using React-Jodit:

Robust Formatting Features: Highlighting codes, images, tables, bold, italics, underlining, various text fonts and sizes — these features and much more are included. Almost all possible formatting features that you can think of are supported right off the bat without requiring any additional libraries for it.

Highly Customizable and Extensible: With its high degree of customization, you have full control over how the editing toolbar appears to you and your users. Additional custom plugins can be developed as required. This makes it extremely flexible for developing highly customized applications.

Lightweight and Efficient: Despite all the features that it supports, react-jodit still remains lightweight. The efficiency with which HTML content is rendered using React’s Virtual DOM makes it suitable for even complex content editing tasks without compromising on performance.

Browser Compatibility: Jodit was designed taking into consideration the compatibility issues from day one. It works flawlessly with different browsers without necessitating browser-specific hacks in your code.

Accessibility Support: react-jodit follows accessibility guidelines, keeping the editor usable for people with disabilities. This matters both for inclusivity and for applications where compliance requirements are part of the picture.

Installation and Setup:

Getting started is pretty painless. Install via npm or yarn:

npm install jodit-react --save
Enter fullscreen mode Exit fullscreen mode

or

Yarn add jodit-react
Enter fullscreen mode Exit fullscreen mode

Basic Usage:

import React from 'react';
import JoditEditor from 'react-jodit';

export const RichTextEditor = () => {
  const editor = React.useRef(null);
  const [content, setContent] = useState("");

  return (
    <JoditEditor
      ref={editor}
      value={content}
      onBlur={(content) => setContent(content)}
    />
  );
};
Enter fullscreen mode Exit fullscreen mode

content holds the editor’s initial value and setContent updates it when the editor loses focus. Clean and straightforward to drop into an existing component.

Customization:

This is where react-jodit genuinely shines. Pass a config object to control exactly which toolbar buttons appear and how the editor behaves:

import React from 'react';
import JoditEditor from 'react-jodit';

export const MyEditor = () => {
  const editor = React.useRef(null);
  const [content, setContent] = useState("");
  const config = {
    readonly: false, // Allows editing
    toolbar: true, // Show toolbar
    toolbarButtonsXml: [
      // Customize toolbar buttons
      'source',
      '|',
      'bold',
      'strikethrough',
      '|',
      'fontsize',
      'brush',
      '|',
      'align',
      '|',
      'link',
      'file',
      'image',
      '|',
      'table',
      '|',
      'undo',
      'redo',
    ],
   };

  return (
    <JoditEditor
      ref={editor}
      value={content}
      config={config}
      onBlur={(content) => setContent(content)}
    />
  );
};
Enter fullscreen mode Exit fullscreen mode

Handle Event

You can go further with image uploader settings and custom event handlers when your application needs more specific behavior:

const config = {
    readonly: false,
    uploader: {
      // Configure image uploader
      insertImageAsBase64URI: true,
    },
    events: {
      // Handle editor events
      beforeSetMode: mode => {
        console.log('Editor mode is changing to:', mode);
      },
      paste: () => {
        console.log('Handle paste');
      },
    },
  };
Enter fullscreen mode Exit fullscreen mode

This flexibility lets you shape the editor around your actual requirements rather than compromising to fit its defaults.

Why jodit-react over other editors?

**
**Effortless React Integration: react-jodit has been designed to be a React component right from its start. You will not need any additional adapters or glue code to use it in React applications.

Performance-Oriented: Content becomes heavier but performance stays high. The Virtual DOM will keep everything going with no hitches during editing.

Cross-browser Consistency: The consistent behavior of react-jodit across all browsers comes natively and isn’t a separate thing added on top of your own implementation.

Markdown Support: Both Markdown and HTML content editing are supported natively, saving lots of effort for implementing an application using both formats.

Disadvantages and Limitations:

Notable limitations worth considering as well.

Learning Curve: Easy to set up but proper configuration and development of custom plugins may require significant time investment.

Dependency Management: The react-jodit component relies on the base editor, Jodit, which requires version compatibility during updates.

Small Community: Compared to other popular editors, it lacks a big enough community base. Searching for third-party plugins or solutions for edge cases may become an issue at times.

Possible Performance Issues: Large or complicated content on old browsers or less powerful computers may still lead to poor performance.

Despite these drawbacks, which are important yet not critical for most cases, react-jodit remains a viable option for building powerful rich text editors within React applications.

Conclusion

The package react-jodit is a viable option for developers using the React JavaScript library who require a powerful, rich-text editor solution. Many format options, excellent performance, and good customization potential make it worthy of consideration when creating any kind of website where text editing will be required. While there are certain limitations, these don’t outweigh the advantages, at least not in most cases. When you’re looking for an effective solution for a rich text editor within the React environment, consider react-jodit.

About Innostax

Innostax specializes in managed engineering teams and was founded in 2014, and is headquartered in Framingham, Massachusetts. We establish engineering teams with accountability as a priority for both startups and enterprises, helping them achieve consistent software velocity with no customer churn.

Read more: The Power of Rich Text Editing with jodit-react

Top comments (0)