# Editor42 in a Svelte app

> Live demo of the @editor42/editor42-svelte component: a WYSIWYG editor in a Svelte 5 app, with its full source to copy or fork on CodePlay.

Source: https://editor42.com/examples/svelte  
Section: Demo > Integrations > Svelte  
Every page of editor42.com has a Markdown copy at its URL + `.md` (`index.md` after a URL that ends in `/`); all of them are in one file: https://editor42.com/llms-full.txt

## When to use

When you are building with Svelte 5 and want the editor as a component, with its content bindable like any other prop.

## How it works

The component from `@editor42/editor42-svelte`. In a `.svelte` file you write `<Editor bind:value={content}/>`; a demo page has no component file to compile, so the same component is mounted from JavaScript with Svelte's own `mount()` and reports changes through its `change` and `keyup` props. Editor options go in `conf`.

## How to start using

Install the component with `npm install @editor42/editor42-svelte` and bind it with `bind:value`. Svelte 5 is required — the component is written with runes.

See the [Svelte install page](https://editor42.com/docs/install/svelte) for self-hosting and the move from `@tinymce/tinymce-svelte`.

- [Download ZIP](https://codeplay.democdn.com/pens/editor42/jQVNIId/editor42-example-svelte.zip)
- [Open in CodePlay](https://codeplay.democdn.com/editor42/jQVNIId)

*The page shows these as tabs, one at a time: Live Demo, JavaScript, HTML, SASS.*

## Live Demo

[Live demo: Editor42 in a Svelte app](https://run.codeplay.democdn.com/pens/editor42/jQVNIId/dist/index.html)

## JavaScript

```js
// In a Svelte app this is a component in your markup:

// <script>
//   import Editor from '@editor42/editor42-svelte';
//   let content = $state('<p>Hello from Editor42</p>');
// </script>
//
// <Editor bind:value={content} conf={{height: 420}}/>

// A demo page has no .svelte file to compile, so the same component is mounted from JS:

import {mount} from 'svelte';
import Editor from '@editor42/editor42-svelte';

const INITIAL = `
<h2>Editor42 in a Svelte app</h2>
<p>This is the component from <code>@editor42/editor42-svelte</code>. In a real app you bind it with <code>bind:value</code>; here it is mounted from plain JavaScript and reports changes through the <code>change</code> and <code>keyup</code> props.</p>
<ul>
  <li><code>conf</code> holds the Editor42 options</li>
  <li><code>value</code> is the content, bindable in a .svelte file</li>
  <li>Every editor event is a prop: <code>change</code>, <code>keyup</code>, <code>init</code>, …</li>
</ul>
`;

const output = document.getElementById('output');
const show = (event, editor) => {
  output.textContent = editor.getContent().trim();
};

mount(Editor, {
  target: document.getElementById('app'),
  props: {
    value: INITIAL,
    change: show,
    keyup: show,
    conf: {
      height: 420,
      menubar: false,
      plugins: 'lists link code',
      toolbar: 'undo redo | bold italic | bullist numlist | link code',
      content_style: 'body { font-family: Helvetica, Arial, sans-serif; font-size: 16px; line-height: 1.6; }'
    }
  }
});
```

## HTML

```html
<h1>Editor42 in Svelte</h1>
<p class="hint">The <code>@editor42/editor42-svelte</code> component, mounted with Svelte 5.</p>

<div id="app"></div>

<h2 class="h6 mt-3">Your content, live</h2>
<pre id="output" class="state"></pre>
```

## SASS

```scss
.hint {
  margin-bottom: 20px;
  color: var(--demo-muted-color);
}

.state {
  max-height: 160px;
  overflow: auto;
  padding: 12px;
  border-radius: 6px;
  background: #fff;
  border: 1px solid #ddd;
  font-size: 13px;
  white-space: pre-wrap;
}
```
