Rich text editor component for React

npm version

CKEditor 5 consists of ready-to-use editor builds and CKEditor 5 Framework upon which the builds are based.

The easiest way to use CKEditor 5 in your React application is by choosing one of the rich text editor builds. Additionally, it is also possible to integrate CKEditor 5 built from source into your application.

Quick start

Install the CKEditor 5 WYSIWYG editor component for React and the editor build of your choice.

Assuming that you picked @ckeditor/ckeditor5-build-classic:

  1. npm install --save @ckeditor/ckeditor5-react @ckeditor/ckeditor5-build-classic

Use the <CKEditor> component inside your project:

  1. import React, { Component } from 'react';
  2. import CKEditor from '@ckeditor/ckeditor5-react';
  3. import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
  4. class App extends Component {
  5. render() {
  6. return (
  7. <div className="App">
  8. <h2>Using CKEditor 5 build in React</h2>
  9. <CKEditor
  10. editor={ ClassicEditor }
  11. data="<p>Hello from CKEditor 5!</p>"
  12. onInit={ editor => {
  13. // You can store the "editor" and use when it is needed.
  14. console.log( 'Editor is ready to use!', editor );
  15. } }
  16. onChange={ ( event, editor ) => {
  17. const data = editor.getData();
  18. console.log( { event, editor, data } );
  19. } }
  20. onBlur={ editor => {
  21. console.log( 'Blur.', editor );
  22. } }
  23. onFocus={ editor => {
  24. console.log( 'Focus.', editor );
  25. } }
  26. />
  27. </div>
  28. );
  29. }
  30. }
  31. export default App;

Component properties

The <CKEditor> component supports the following properties:

  • editor (required) – The Editor constructor to use.

  • data – The initial data for the created editor. See the Basic API guide.

  • config – The editor configuration. See the Configuration guide.

  • onChange – A function called when the editor’s data has changed. See the editor.model.document#change:data event.

The callback receives two parameters:

  • An EventInfo object,
  • An Editor instance.
    • onInit – A function called when the editor was initialized. It receives the initialized editor as a parameter.
  • onBlur – A function called when the editor was blurred. It receives the blurred editor as a parameter.

  • onFocus – A function called when the editor was focused. It receives the focused editor as a parameter.

  • disabled – A boolean. The editor is being switched to read-only mode if the property is set to true.

Customizing the builds

CKEditor 5 builds come ready to use, with a set of built-in plugins and a predefined configuration. While you can change the configuration easily by using the config property of the <CKEditor> component which allows you to change the toolbar or remove some plugins, in order to add plugins you need to rebuild the editor.

There are two main ways to do that.

This option does not require any changes in your project’s configuration. You will create a new build somewhere next to your project and include it like you included one of the existing builds. Therefore, it is the easiest way to add missing features. Read more about this method in Installing plugins.

In this approach you will include CKEditor 5 built from source — so you will choose the editor creator you want and the list of plugins, etc. It is more powerful and creates a tighter integration between your application and the WYSIWYG editor, however, it requires adjusting your webpack.config.js to CKEditor 5 needs.

Read more about this option in Integrating CKEditor 5 from source.

Note: Building for production

If you still work with create-react-app@1 or use a custom configuration for you application that still uses webpack@3, you will need to adjust the UglifyJsPlugin option to make CKEditor 5 compatible with this setup. CKEditor 5 builds use ES6 so the default JavaScript minifier of webpack@3 and create-react-app@1 is not able to digest them.

To do that, you need to first eject the configuration from the setup created via create-react-app (assuming that you use it):

  1. npm run eject

Then, you can customize UglifyJsPlugin options in the webpack configuration. Read how to do this here.

Note: Using the Document editor build

If you use the Document editor, you need to add the toolbar to the DOM manually:

  1. import DecoupledEditor from '@ckeditor/ckeditor5-build-decoupled-document';
  2. class App extends Component {
  3. render() {
  4. return (
  5. <div className="App">
  6. <h2>CKEditor 5 using a custom build - DecoupledEditor</h2>
  7. <CKEditor
  8. onInit={ editor => {
  9. console.log( 'Editor is ready to use!', editor );
  10. // Insert the toolbar before the editable area.
  11. editor.ui.getEditableElement().parentElement.insertBefore(
  12. editor.ui.view.toolbar.element,
  13. editor.ui.getEditableElement()
  14. );
  15. } }
  16. onChange={ ( event, editor ) => console.log( { event, editor } ) }
  17. editor={ DecoupledEditor }
  18. data="<p>Hello from CKEditor 5's DecoupledEditor!</p>"
  19. config={ /* the editor configuration */ }
  20. />
  21. );
  22. }
  23. }
  24. export default App;

Integrating CKEditor 5 built from source

Integrating the rich text editor from source allows you to use the full power of CKEditor 5 Framework.

This guide assumes that you are using Create React App CLI as your boilerplate and it goes through adjusting it to fit CKEditor 5 needs. If you use your custom webpack setup, please read more about including CKEditor 5 built from source.

Using create-react-app@2

The configuration needs to be ejected to make it possible to customize the webpack configuration. In order to be able to build CKEditor 5 from source, you need to tell webpack how to handle CKEditor 5’s SVG and CSS files (by adding loaders configuration). Additionally, you need to exclude the CKEditor 5 source from existing loaders.

You can see all the changes described below in this example project: https://github.com/ckeditor/ckeditor5-react-example/commits/master.

Create a sample application using create-react-app@2 first:

  1. npx create-react-app ckeditor5-react-example && cd ckeditor5-react-example

Now you can eject the configuration (you can find more information about ejecting here):

  1. yarn eject
  2. # For some strange reasons this is needed, too
  3. # (https://github.com/facebook/create-react-app/issues/6099).
  4. yarn add @babel/plugin-transform-react-jsx @babel/plugin-transform-react-jsx-self

Installing missing dependencies

Before you start modifying the webpack configuration, first install some CKEditor 5 dependencies that you will need:

  1. yarn add \
  2. raw-loader \
  3. @ckeditor/ckeditor5-dev-utils \
  4. @ckeditor/ckeditor5-theme-lark \
  5. @ckeditor/ckeditor5-react \
  6. @ckeditor/ckeditor5-editor-classic \
  7. @ckeditor/ckeditor5-essentials \
  8. @ckeditor/ckeditor5-paragraph \
  9. @ckeditor/ckeditor5-basic-styles

Modifying webpack configuration

Once you ejected the configuration and installed dependencies, you can now edit the webpack configuration (config/webpack.config.js).

First, import an object that creates the configuration for PostCSS:

  1. const { styles } = require( '@ckeditor/ckeditor5-dev-utils' );

Then, add two new elements to the exported object under the module.rules array (as new items of the oneOf array). These are SVG and CSS loaders required to handle the CKEditor 5 source:

  1. {
  2. test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
  3. use: [ 'raw-loader' ]
  4. },
  5. {
  6. test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
  7. use: [
  8. {
  9. loader: 'style-loader',
  10. options: {
  11. singleton: true
  12. }
  13. },
  14. {
  15. loader: 'postcss-loader',
  16. options: styles.getPostCssConfig( {
  17. themeImporter: {
  18. themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
  19. },
  20. minify: true
  21. } )
  22. }
  23. ]
  24. },

Now you need to exclude CSS files used by CKEditor 5 from the project’s CSS loader.

First, find a loader that starts its definition with the following code: test: cssRegex and modify it:

  1. {
  2. test: cssRegex,
  3. exclude: [
  4. cssModuleRegex,
  5. /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
  6. ],
  7. // (...)
  8. }

Below it, you will find another loader that handles CSS files. You need to disable it for CKEditor 5 CSS as well. Its definition starts with test: cssModuleRegex:

  1. {
  2. test: cssModuleRegex,
  3. exclude: [
  4. /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
  5. ],
  6. // (...)
  7. }

Finally, exclude CKEditor 5 SVG and CSS files from file-loader . Find the last item in the module.rules array which should be the file-loader configuration, and modify it so it looks like this:

  1. {
  2. loader: require.resolve('file-loader'),
  3. // Exclude `js` files to keep the "css" loader working as it injects
  4. // its runtime that would otherwise be processed through the "file" loader.
  5. // Also exclude `html` and `json` extensions so they get processed
  6. // by webpack's internal loaders.
  7. exclude: [
  8. /\.(js|mjs|jsx|ts|tsx)$/,
  9. /\.html$/,
  10. /\.json$/,
  11. /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
  12. /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/
  13. ],
  14. options: {
  15. name: 'static/media/[name].[hash:8].[ext]',
  16. }
  17. }

Using CKEditor 5 source

Once your configuration is updated, you can now use CKEditor 5 directly from source. Test it by editing src/App.js:

  1. import React, { Component } from 'react';
  2. import CKEditor from '@ckeditor/ckeditor5-react';
  3. // NOTE: Use the editor from source (not a build)!
  4. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  5. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  6. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  7. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. const editorConfiguration = {
  10. plugins: [ Essentials, Bold, Italic, Paragraph ],
  11. toolbar: [ 'bold', 'italic' ]
  12. };
  13. class App extends Component {
  14. render() {
  15. return (
  16. <div className="App">
  17. <h2>Using CKEditor 5 from source in React</h2>
  18. <CKEditor
  19. editor={ ClassicEditor }
  20. config={ editorConfiguration }
  21. data="<p>Hello from CKEditor 5!</p>"
  22. onInit={ editor => {
  23. // You can store the "editor" and use when it is needed.
  24. console.log( 'Editor is ready to use!', editor );
  25. } }
  26. onChange={ ( event, editor ) => {
  27. const data = editor.getData();
  28. console.log( { event, editor, data } );
  29. } }
  30. onBlur={ editor => {
  31. console.log( 'Blur.', editor );
  32. } }
  33. onFocus={ editor => {
  34. console.log( 'Focus.', editor );
  35. } }
  36. />
  37. </div>
  38. );
  39. }
  40. }
  41. export default App;

Finally, you can see your application live:

  1. yarn start

You can read more about using CKEditor 5 from source in the @link builds/guides/integration/advanced-setup#scenario-2-building-from-source Advanced setup guide}.

Using create-react-app@1

Install React CLI:

  1. npm install -g create-react-app@^1.0.0

Create your project using the CLI and go to the project’s directory:

  1. create-react-app ckeditor5-react-example && cd ckeditor5-react-example

Now you can eject the configuration:

  1. npm run eject

The configuration needs to be ejected to make it possible to customize the webpack configuration. To be able to build CKEditor 5 from source you must load inline SVG images and handle CKEditor 5’s CSS as well as correctly minify the ES6 source.

You can find more information about ejecting here.

Changes required in webpack’s production configuration

At this stage, if you tried to build your application with CKEditor 5 source included, you would get the following error:

  1. Failed to minify the code from this file: [31/75]
  2. <project_root>/node_modules/@ckeditor/ckeditor5-build-classic/build/ckeditor.js:5:2077

UglifyJS exported by webpack@3 cannot parse code written in ES6. You need to manually replace it with uglifyjs-webpack-plugin. These changes touch the webpack.config.prod.js file only.

After ejecting, this file is placed in <project_root>/config/webpack.config.prod.js. You need to make the following changes:

  • Install uglifyjs-webpack-plugin:
  1. npm install --save-dev uglifyjs-webpack-plugin
  • Load the installed package (at the top of the webpack.config.prod.js file):
  1. const UglifyJsWebpackPlugin = require( 'uglifyjs-webpack-plugin' );
  • Replace the webpack.optimize.UglifyJsPlugin with UglifyJsWebpackPlugin:
  1. - new webpack.optimize.UglifyJsPlugin
  2. + new UglifyJsWebpackPlugin
  • Options: compress, mangle and output cannot be passed directly to UglifyJsWebpackPlugin. You need to wrap these options in uglifyOptions: { … }.

In the end, the entire plugin definition should look as follows:

  1. // Minify the code.
  2. new UglifyJsWebpackPlugin( {
  3. uglifyOptions: {
  4. compress: {
  5. warnings: false,
  6. // Disabled because of an issue with Uglify breaking seemingly valid code:
  7. // https://github.com/facebookincubator/create-react-app/issues/2376
  8. // Pending further investigation:
  9. // https://github.com/mishoo/UglifyJS2/issues/2011
  10. comparisons: false,
  11. },
  12. mangle: {
  13. safari10: true,
  14. },
  15. output: {
  16. comments: false,
  17. // Turned on because emoji and regex is not minified properly using default
  18. // https://github.com/facebookincubator/create-react-app/issues/2488
  19. ascii_only: true,
  20. },
  21. },
  22. sourceMap: shouldUseSourceMap,
  23. } )

Changes required in both webpack configurations

In order to build your application properly, you need to modify your webpack configuration files. After ejecting they are located at:

  1. <project_root>/config/webpack.config.dev.js
  2. <project_root>/config/webpack.config.prod.js

You need to modify the webpack configuration to load CKEditor 5 SVG icons properly.

First, import an object that creates the configuration for PostCSS:

  1. const { styles } = require( '@ckeditor/ckeditor5-dev-utils' );

Then add two new elements to the exported object under the module.rules array (as new items for the oneOf array). These are SVG and CSS loaders only for CKEditor 5 code:

  1. {
  2. test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
  3. use: [ 'raw-loader' ]
  4. },
  5. {
  6. test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
  7. use: [
  8. {
  9. loader: 'style-loader',
  10. options: {
  11. singleton: true
  12. }
  13. },
  14. {
  15. loader: 'postcss-loader',
  16. options: styles.getPostCssConfig( {
  17. themeImporter: {
  18. themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
  19. },
  20. minify: true
  21. } )
  22. }
  23. ]
  24. },

Exclude CSS files used by CKEditor 5 from project’s CSS loader:

  1. {
  2. test: /\.css$/,
  3. exclude: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/,
  4. // (...)
  5. }

And exclude CKEditor 5 SVG and CSS files from file-loader because these files will be handled by the loaders added previously (usually the last item in the module.rules array is the file-loader) so it looks like this:

  1. {
  2. loader: require.resolve('file-loader'),
  3. // Exclude `js` files to keep the "css" loader working as it injects
  4. // its runtime that would otherwise be processed through the "file" loader.
  5. // Also exclude `html` and `json` extensions so they get processed
  6. // by webpack's internal loaders.
  7. exclude: [
  8. /\.(js|jsx|mjs)$/,
  9. /\.html$/,
  10. /\.json$/,
  11. /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
  12. /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css/
  13. ],
  14. options: {
  15. name: 'static/media/[name].[hash:8].[ext]'
  16. }
  17. }

Remember that the changes above should be done in both configuration files.

Next, install raw-loader, the theme for CKEditor 5, and CKEditor 5 development utilities:

  1. npm install --save-dev raw-loader @ckeditor/ckeditor5-theme-lark @ckeditor/ckeditor5-dev-utils

Finally, install the component, the specific editor and plugins you want to use:

  1. npm install --save \
  2. @ckeditor/ckeditor5-react \
  3. @ckeditor/ckeditor5-editor-classic \
  4. @ckeditor/ckeditor5-essentials \
  5. @ckeditor/ckeditor5-basic-styles \
  6. @ckeditor/ckeditor5-heading \
  7. @ckeditor/ckeditor5-paragraph

Using CKEditor 5 source

Now you can use the <CKEditor> component together with CKEditor 5 Framework:

  1. import React, { Component } from 'react';
  2. import CKEditor from '@ckeditor/ckeditor5-react';
  3. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  4. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  5. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  7. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  8. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  9. class App extends Component {
  10. render() {
  11. return (
  12. <div className="App">
  13. <h2>Using CKEditor 5 Framework in React</h2>
  14. <CKEditor
  15. onInit={ editor => console.log( 'Editor is ready to use!', editor ) }
  16. onChange={ ( event, editor ) => console.log( { event, editor } ) }
  17. config={ {
  18. plugins: [ Essentials, Paragraph, Bold, Italic, Heading ],
  19. toolbar: [ 'heading', '|', 'bold', 'italic', '|', 'undo', 'redo', ]
  20. } }
  21. editor={ ClassicEditor }
  22. data="<p>Hello from CKEditor 5!</p>"
  23. />
  24. </div>
  25. );
  26. }
  27. }
  28. export default App;

Contributing and reporting issues

The source code of rich text editor component for React is available on GitHub in https://github.com/ckeditor/ckeditor5-react.