Days
Hours
Minutes
Seconds
x
Skip to content
Froala Documentation

Headless Mode Example: Build Your Own Editor Toolbar

This live example shows Froala’s headless mode in action. Froala keeps the editing engine and hands the interface to you: every button in the toolbar below is your own HTML, and each one calls a single Froala API method. Try it, then copy the code or open it in JSFiddle.

Headless Mode

NEW IN 5.5

Set the headless option to true and Froala renders a clean editable area. The built-in toolbar, popups and default editor styling step aside, so your app's own design system shapes the whole experience.

Try it yourself:


Edit in JSFiddle

HTML

<!-- Froala styles for saved content -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/froala-editor@5.5.0/css/froala_editor.pkgd.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/froala-editor@5.5.0/css/froala_style.min.css">

<!-- Your toolbar is built into this element by JavaScript -->
<div id="my-toolbar" class="my-toolbar"></div>

<!-- A small inline form for adding links -->
<div id="link-row" class="link-row" hidden>
  <input id="link-url" type="url" placeholder="https://froala.com">
  <button type="button" id="link-apply" class="tb-btn is-primary">Insert link</button>
  <button type="button" id="link-cancel" class="tb-btn">Cancel</button>
</div>

<!-- The editor itself: an editable area -->
<div id="editor">
  <p>Select some text, then try the buttons above.</p>
</div>

<pre id="output" class="output"></pre>

<!-- Froala 5.5.0 -->
<script src="https://cdn.jsdelivr.net/npm/froala-editor@5.5.0/js/froala_editor.pkgd.min.js"></script>

CSS

/* Paste into the JSFiddle CSS pane */
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: #f5f6f8; color: #1f2937; }
.demo { max-width: 1000px; margin: 0 auto; padding: 24px 16px 48px; }
.demo-head h1 { margin: 0 0 4px; font-size: 24px; }
.demo-head p { margin: 0 0 16px; color: #4b5563; }
code { background: #e5e7eb; padding: 1px 5px; border-radius: 4px; font-size: .9em; }

/* ---- Our custom toolbar ---- */
.my-toolbar { display: flex; flex-direction: column; gap: 10px; background: #fff; border: 1px solid #d1d5db;
  border-radius: 10px 10px 0 0; padding: 12px; position: sticky; top: 0; z-index: 5; }
.tb-group { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.tb-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: #6b7280; width: 110px; flex-shrink: 0; }
.tb-btn { font: inherit; font-size: 13px; padding: 6px 10px; border-radius: 6px; border: 1px solid #d1d5db; background: #f9fafb; cursor: pointer; }
.tb-btn:hover { background: #eef2ff; border-color: #818cf8; }
.tb-btn.is-active { background: #4f46e5; border-color: #4f46e5; color: #fff; }

/* ---- The editing area: in headless mode, OUR CSS decides how it looks ---- */
#editor { background: #fff; border: 1px solid #d1d5db; border-top: none; border-radius: 0 0 10px 10px; }
#editor .fr-element { padding: 20px 24px; min-height: 260px; font-size: 16px; line-height: 1.6; outline: none; }
#editor .fr-element:focus { box-shadow: inset 0 0 0 2px #c7d2fe; border-radius: 0 0 10px 10px; }
#editor table td { border: 1px solid #d1d5db; padding: 6px 8px; }
#editor blockquote { border-left: 4px solid #818cf8; margin-left: 0; padding-left: 12px; color: #4b5563; }

/* ---- Output panel ---- */
@media (max-width: 720px) { .tb-label { width: 100%; } }
.panel { margin-top: 16px; background: #fff; border: 1px solid #d1d5db; border-radius: 10px; padding: 12px 14px; min-width: 0; }
.panel h3 { margin: 0 0 8px; font-size: 15px; }
.output { margin: 0; max-height: 260px; overflow: auto; font-size: 12px; background: #111827; color: #e5e7eb; padding: 10px; border-radius: 6px; white-space: pre-wrap; word-break: break-word; }

JAVASCRIPT

// Froala 5.5.0 — headless mode demo.
// The editor runs with no built-in UI. The toolbar below is our own HTML,
// and every button simply calls a Froala API method.

const outputEl = document.getElementById('output');

function showHTML(ed) {
  outputEl.textContent = ed.html.get();
}

// Ask the user for a URL. Returns the trimmed URL, or null if they cancel.
// (A browser prompt keeps this demo short. In your app, use your own input or dialog.)
function askForURL(message, example) {
  const url = window.prompt(message, example);
  return url && url.trim() ? url.trim() : null;
}

// ---------------------------------------------------------------------------
// 1. Create the editor in headless mode
// ---------------------------------------------------------------------------
const editor = new FroalaEditor('#editor', {
  // key: 'YOUR_LICENSE_KEY',   // add your license key here if you have one
  headless: true,               // no toolbar, popups or default styling
  htmlExecuteScripts: false,    // treat pasted scripts as plain content
  placeholderText: 'Start typing…',
  events: {
    initialized: function () {
      showHTML(this);
      buildToolbar(this);
    },
    contentChanged: function () {
      showHTML(this);
    },
    // Images and videos land asynchronously, so record the undo step once they arrive.
    'image.inserted': function () { this.undo.saveStep(); },
    'video.inserted': function () { this.undo.saveStep(); },
    // Froala's own error popups are off in headless mode, so report errors yourself.
    'image.error': function () { window.alert('That image could not be loaded.'); },
    'video.linkError': function () { window.alert('That link is not a supported YouTube or Vimeo URL.'); }
  }
});

// ---------------------------------------------------------------------------
// 2. Our own buttons. Each one calls a Froala API method.
//    inline: tag to check with format.is() for the pressed state
//    block:  tag to look for around the caret for the pressed state
//    history: undo/redo move through the history, so no extra undo step
//    async:   the undo step is saved when the media lands (see events above)
// ---------------------------------------------------------------------------
const GROUPS = [
  {
    label: 'Text',
    buttons: [
      { label: 'Bold', inline: 'strong', run: e => e.commands.bold() },
      { label: 'Italic', inline: 'em', run: e => e.commands.italic() },
      { label: 'Underline', inline: 'u', run: e => e.commands.underline() },
      { label: 'Strike', inline: 's', run: e => e.commands.strikeThrough() },
      { label: 'Sub', inline: 'sub', run: e => e.commands.subscript() },
      { label: 'Sup', inline: 'sup', run: e => e.commands.superscript() },
      { label: 'Clear format', run: e => e.commands.clearFormatting() }
    ]
  },
  {
    label: 'Blocks',
    buttons: [
      { label: 'Paragraph', run: e => e.paragraphFormat.apply('N') },
      { label: 'H2', block: 'h2', run: e => e.paragraphFormat.apply('H2') },
      { label: 'H3', block: 'h3', run: e => e.paragraphFormat.apply('H3') },
      { label: '• List', block: 'ul', run: e => e.lists.format('UL') },
      { label: '1. List', block: 'ol', run: e => e.lists.format('OL') },
      { label: 'Quote +', block: 'blockquote', run: e => e.quote.apply('increase') },
      { label: 'Quote −', run: e => e.quote.apply('decrease') },
      { label: 'Indent', run: e => e.commands.indent() },
      { label: 'Outdent', run: e => e.commands.outdent() },
      { label: 'Line', run: e => e.commands.insertHR() }
    ]
  },
  {
    label: 'Align',
    buttons: [
      { label: 'Left', run: e => e.align.apply('left') },
      { label: 'Center', run: e => e.align.apply('center') },
      { label: 'Right', run: e => e.align.apply('right') },
      { label: 'Justify', run: e => e.align.apply('justify') }
    ]
  },
  {
    label: 'Style',
    buttons: [
      { label: 'Red text', run: e => e.colors.text('#DC2626') },
      { label: 'Highlight', run: e => e.colors.background('#FDE68A') },
      { label: 'Size 24px', run: e => e.fontSize.apply('24px') },
      { label: 'Georgia', run: e => e.fontFamily.apply('Georgia,serif') },
      { label: 'Custom style', run: e => e.format.applyStyle('letter-spacing', '2px') }
    ]
  },
  {
    label: 'Insert',
    buttons: [
      {
        label: 'Link…', run: e => {
          const text = e.selection.text();
          e.selection.save();              // the prompt takes focus, so remember the selection
          const url = askForURL('Enter the link URL:', 'https://froala.com');
          e.selection.restore();
          if (!url) return;
          e.link.insert(url, text || url, { target: '_blank' });   // Froala adds rel="noopener noreferrer" itself
        }
      },
      {
        label: 'Image…', async: true, run: e => {
          e.selection.save();
          const url = askForURL('Enter the image URL:', 'https://picsum.photos/600/300');
          e.selection.restore();
          if (url) e.image.insertByURL(url);
        }
      },
      {
        label: 'Video…', async: true, run: e => {
          e.selection.save();
          const url = askForURL('Enter a YouTube or Vimeo link, for example https://www.youtube.com/watch?v=…', '');
          e.selection.restore();
          if (url) e.video.insertByURL(url);
        }
      },
      { label: 'Table 3×3', run: e => e.table.insert(3, 3) }
    ]
  },
  {
    label: 'History',
    buttons: [
      { label: 'Undo', history: true, run: e => e.commands.undo() },
      { label: 'Redo', history: true, run: e => e.commands.redo() },
      { label: 'Select all', history: true, run: e => e.commands.selectAll() }
    ]
  }
];

// ---------------------------------------------------------------------------
// 3. Build the toolbar HTML and wire up the clicks
// ---------------------------------------------------------------------------
function buildToolbar(ed) {
  const toolbar = document.getElementById('my-toolbar');
  toolbar.setAttribute('role', 'toolbar');
  toolbar.setAttribute('aria-label', 'Formatting');

  GROUPS.forEach(group => {
    const row = document.createElement('div');
    row.className = 'tb-group';
    row.innerHTML = '<span class="tb-label">' + group.label + '</span>';

    group.buttons.forEach(def => {
      const btn = document.createElement('button');
      btn.type = 'button';
      btn.className = 'tb-btn';
      btn.textContent = def.label;
      if (def.inline) btn.dataset.inline = def.inline;
      if (def.block) btn.dataset.block = def.block;
      if (def.inline || def.block) btn.setAttribute('aria-pressed', 'false');

      // Keep the editor's text selection when the button is pressed
      // (Froala's own toolbar does the same thing).
      btn.addEventListener('mousedown', ev => ev.preventDefault());

      btn.addEventListener('click', () => {
        // commands.* methods record their own undo steps, but lists, align, paragraphFormat,
        // quote, colors, fontSize, fontFamily, link and table calls don't. So save a step
        // BEFORE the change (the state Undo returns to) and AFTER it (the state Redo returns to),
        // the same way Froala's own toolbar does. Froala ignores a step that matches the last one.
        // Undo/redo move through the history, and media saves its "after" step on insert.
        if (!def.history) ed.undo.saveStep();
        def.run(ed);
        if (!def.history && !def.async) ed.undo.saveStep();
        showHTML(ed);
        refreshActive(ed);
      });
      row.appendChild(btn);
    });

    toolbar.appendChild(row);
  });

  // Update pressed states when the caret moves or the content changes
  ['mouseup', 'keyup', 'contentChanged'].forEach(name => {
    ed.events.on(name, () => refreshActive(ed));
  });
}

function refreshActive(ed) {
  const el = ed.selection.element();

  document.querySelectorAll('.tb-btn[data-inline], .tb-btn[data-block]').forEach(btn => {
    let on;
    if (btn.dataset.inline) {
      on = ed.format.is(btn.dataset.inline);       // inline formatting, as in the guide
    } else {
      const match = el && el.closest ? el.closest(btn.dataset.block) : null;
      on = !!(match && ed.el.contains(match));     // block formatting around the caret
    }
    btn.classList.toggle('is-active', on);
    btn.setAttribute('aria-pressed', String(on));
  });
}

How This Example Works

  1. Create the editor with headless: true. Froala turns #editor into an editable area.
  2. Wait for initialized before building the toolbar. Editor methods are ready from this moment on.
  3. Describe buttons as data. The GROUPS list pairs a label with a function, for example e => e.commands.bold().
  4. Keep the selection. Calling preventDefault() on mousedown keeps the highlighted text selected while the user clicks your button.
  5. Read the result with editor.html.get() inside the contentChanged event.

API methods used

commands.bold() Also italic(), underline(), strikeThrough(), clearFormatting(), insertHR(), undo(), redo().
paragraphFormat.apply() Sets the block type. Accepts 'N' (normal), 'H2', 'H3' and more.
quote.apply() Pass 'increase' to wrap blocks in a blockquote.
lists.format() Pass 'UL' for bullets or 'OL' for numbers.
align.apply() 'left', 'center', 'right' or 'justify'.
link.insert() Inserts a link with (href, text).
table.insert() Inserts a table with (rows, columns).
html.get() Returns the current content as an HTML string.

Learn the full setup

This example shows headless mode in one working page. The headless editor setup guide explains each piece step by step and covers what a production app needs.

  • React setup with the official Froala wrapper.
  • Styling the editing area with your own CSS.
  • Saving and loading content with html.get() and html.set().
  • Everything that changes when headless is set to true.
  • Helper methods for making your own plugins work in headless mode.

Read the headless editor setup guide