Sign up (with export icon)

Integrating CKEditor 5 with Svelte from npm

Contribute to this guide Show the table of contents

Svelte is a modern JavaScript compiler that builds highly optimized, reactive web applications. Unlike traditional frameworks, Svelte shifts most of the work from runtime to build time, resulting in highly efficient applications. CKEditor 5 rich-text editor can be easily integrated with Svelte applications, providing powerful rich text editing capabilities to your projects.

Create your own CKEditor 5

Check out our interactive Builder to quickly get a taste of CKEditor 5. It offers an easy-to-use user interface to help you configure, preview, and download the editor suited to your needs.

  • editor type,
  • the features you need,
  • the preferred framework (React, Angular, Vue or Vanilla JS),
  • the preferred distribution method.

You get ready-to-use code tailored to your needs!

Check out our interactive Builder

Quick start

Copy link

This guide will show you how to integrate CKEditor 5 into a Svelte application using the npm distribution. If you are new to Svelte, check out their official tutorial.

Setting up a Svelte project

Copy link

First, create a new Svelte project using Vite:

npm create vite@latest ckeditor-svelte -- --template svelte
cd ckeditor-svelte
npm install
Copy code

Installing CKEditor 5

Copy link

Next, install the ckeditor5 and the ckeditor5-premium-features packages:

npm install ckeditor5 ckeditor5-premium-features
Copy code
Note

The premium features package is optional and used in this guide to demonstrate a complete integration. You can use just the open-source features if you prefer.

Project structure

Copy link

When completed, the folder structure of your project should resemble this one:

├── node_modules/
├── public/
├── src/
│   ├── lib/
│   │   └── Editor.svelte
│   ├── App.svelte
│   ├── main.js
│   └── ...
├── index.html
├── package.json
├── vite.config.js
├── svelte.config.js
└── ...
Copy code

The integration requires two key Svelte components:

  • src/lib/Editor.svelte – the component that wraps CKEditor functionality
  • src/App.svelte – the main application component that uses the Editor component

Let’s implement these components next.

Implementing the Editor component

Copy link

Create a new file src/lib/Editor.svelte with the following content:

<script>
    import { onMount, onDestroy } from 'svelte';
    
    import { ClassicEditor, Essentials, Bold, Italic, Font, Paragraph } from 'ckeditor5';
    import { FormatPainter } from 'ckeditor5-premium-features';
    
    import 'ckeditor5/ckeditor5.css';
    import 'ckeditor5-premium-features/ckeditor5-premium-features.css';

    let editorContainer;
    let editorInstance = null;

    onMount( () => {
        ClassicEditor
            .create( editorContainer, {
                licenseKey: '<YOUR_LICENSE_KEY>', // Replace with your license key or 'GPL'
                plugins: [ Essentials, Bold, Italic, Font, Paragraph, FormatPainter ],
                toolbar: [
                    'undo', 'redo', '|', 'bold', 'italic', '|',
                    'fontSize', 'fontFamily', 'fontColor', 'fontBackgroundColor', '|',
                    'formatPainter'
                ]
            } )
            .then( editor => {
                editorInstance = editor;
            } )
            .catch( error => {
                console.error( 'Error initializing CKEditor:', error );
            } );
    } );

    onDestroy( () => {
        if ( editorInstance ) {
            editorInstance.destroy().catch( err => console.error( err ) );
        }
    } );
</script>

<div bind:this={editorContainer}>
    <p>Hello from CKEditor 5!</p>
</div>
Copy code

Using the Editor component

Copy link

Now, modify the main App.svelte file to use our editor component:

<script>
    import Editor from './lib/Editor.svelte'
</script>

<main>
    <h1>CKEditor 5 + Svelte</h1>

    <div class="editor-wrapper">
        <Editor />
    </div>
</main>

<style>
    .editor-wrapper {
        width: 800px;
    }

    :global(.ck.ck-editor__editable) {
        height: 200px;
        background-color: white;
        color: #333;
    }
</style>
Copy code

You can now run the dev server to see the editor in action:

npm run dev
Copy code

Component structure

Copy link

The Svelte HTML editor integration follows these key steps:

  1. Import dependencies: The required CKEditor 5 modules and styles are imported
  2. Editor initialization: The editor is created with the specified configuration when the component mounts
  3. Cleanup: Resources are properly released when the component is destroyed

Styling

Copy link

Basic styling is provided in the App.svelte component to ensure proper display in various environments, especially when using dark themes:

.editor-wrapper {
    width: 800px;
}

:global(.ck.ck-editor__editable) {
    height: 200px;
    background-color: white;
    color: #333;
}
Copy code

Next steps

Copy link