Documentation

Rich text editor for React

Build a toolbar for your app. Yurich handles formatting and lets you save and load the document as HTML.

Live demo

Type or select text to try the toolbar.

Key features

Overview

Quick start

Install Yurich in a React 18+ app, then render the example component.

npm install yurich
Submit HTML and restore drafts
Live example
Example result
import { Bold, EditorContent, EditorProvider, useEditor } from 'yurich';

export function BasicEditor() {
  const editor = useEditor({
    extensions: [Bold],
    initialValue: "<p>First draft, meet Yurich.</p>",
  });

  return (
    <EditorProvider editor={editor}>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}

Select text and press Cmd+B on macOS or Ctrl+B on Windows and Linux. Add a Bold button.

Submit HTML and restore drafts

When a form is submitted, use getHTML() to read the editor content. When someone resumes editing a saved draft, use setHTML() to load it into the editor.

Submit HTML from a form

Select Friday, click Bold and submit with Submit article. In Output, look for <strong>Friday</strong> in the HTML passed to onSave.

Run the standalone article form to try formatting, submission, and draft restoration together.

Live example
Example result
import {
  Bold,
  EditorContent,
  EditorProvider,
  useEditor,
  useEditorContext,
  useIsActive,
} from 'yurich';

const labels = {
  "body": "Article",
  "toolbar": "Formatting",
  "bold": "Bold",
  "submit": "Submit article"
};

function BoldButton({ label }: { label: string }) {
  const editor = useEditorContext();
  const active = useIsActive(editor, 'toggleBold');

  return (
    <button
      type="button"
      aria-pressed={active}
      onMouseDown={event => event.preventDefault()}
      onClick={() => editor.execute('toggleBold')}
    >
      {label}
    </button>
  );
}

export function ArticleEditor({
  onSave,
}: {
  onSave: (html: string) => void;
}) {
  const editor = useEditor({
    extensions: [Bold],
    initialValue: "<p>The toolbar audition is on Friday.</p>",
  });

  return (
    <form onSubmit={event => {
      event.preventDefault();
      onSave(editor.getHTML());
    }}>
      <EditorProvider editor={editor}>
        <div role="toolbar" aria-label={labels.toolbar}>
          <BoldButton label={labels.bold} />
        </div>
        <EditorContent className="article-editor" ariaLabel={labels.body} />
      </EditorProvider>
      <button type="submit">{labels.submit}</button>
    </form>
  );
}

Resume a saved draft

Edit the draft and click Save draft. Check Saved HTML, then change the text again. Click Load saved draft to go back to the version saved in this browser. Click Delete saved draft to clear the saved copy and keep editing the text in the editor.

Current draft
Saved HTML
import { useEffect, useState } from 'react';
import { Bold, EditorContent, EditorProvider, useEditor } from 'yurich';

const storageKey = 'yurich:article-draft';
const labels = {
  "draft": "Current draft",
  "savedHTML": "Saved HTML",
  "save": "Save draft",
  "restore": "Load saved draft",
  "empty": "No saved draft.",
  "saved": "Draft saved in this browser.",
  "restored": "Saved draft loaded.",
  "remove": "Delete saved draft",
  "removed": "Saved draft deleted.",
  "unsaved": "The current draft differs from the saved version.",
  "unavailable": "This browser could not access draft storage."
};
const initialHTML = "<p>The draft can wait until after coffee.</p>";

function SavedHTML({ html }: { html: string | null }) {
  return html === null ? <p>{labels.empty}</p> : <pre aria-label={labels.savedHTML}>{html}</pre>;
}

export function DraftEditor() {
  const [html, setHTML] = useState(initialHTML);
  const [savedHTML, setSavedHTML] = useState<string | null>(null);
  const [status, setStatus] = useState<'saved' | 'restored' | 'removed' | 'unavailable'>();
  const editor = useEditor({
    extensions: [Bold],
    initialValue: initialHTML,
    onChange: setHTML,
  });

  useEffect(() => {
    try {
      setSavedHTML(localStorage.getItem(storageKey));
    } catch {
      setStatus('unavailable');
    }
  }, []);

  function save() {
    const draft = editor.getHTML();
    try {
      localStorage.setItem(storageKey, draft);
      setHTML(draft);
      setSavedHTML(draft);
      setStatus('saved');
    } catch {
      setStatus('unavailable');
    }
  }

  function restore() {
    if (savedHTML === null) return;
    editor.setHTML(savedHTML, { history: false });
    setHTML(editor.getHTML());
    setStatus('restored');
  }

  function removeSavedDraft() {
    try {
      localStorage.removeItem(storageKey);
      setSavedHTML(null);
      setStatus('removed');
    } catch {
      setStatus('unavailable');
    }
  }

  const hasUnsavedChanges = savedHTML !== null && html !== savedHTML;
  const message = status !== 'unavailable' && hasUnsavedChanges
    ? labels.unsaved
    : status && labels[status];

  return (
    <EditorProvider editor={editor}>
      <EditorContent ariaLabel={labels.draft} />
      <button type="button" className="primary-action" onClick={save}>{labels.save}</button>
      <button type="button" onClick={restore} disabled={savedHTML === null}>{labels.restore}</button>
      <button type="button" onClick={removeSavedDraft} disabled={savedHTML === null}>{labels.remove}</button>
      <p role="status">{message}</p>
      <SavedHTML html={savedHTML} />
    </EditorProvider>
  );
}
  • initialValue is read when the editor is created. Use setHTML when a draft arrives after mount or when switching documents.
  • Read the document with getHTML() in your save handler. You can use the same pattern with an API instead of browser storage.
  • With { history: false }, restoring starts a fresh undo history. Omit the option to make replacing the document an undoable edit.
  • Restoring HTML can call onChange, even if the HTML is identical. Keep saving and loading as separate actions and avoid writing back into the editor from the change callback.