ContentBox.js · Guide

Adding a Custom Top Bar for ContentBox.js

ContentBox.js can show your own top bar above the editor, with your logo, your buttons and the controls your users need. In this guide we add one with undo and redo, save, device views, preview, HTML and a button to show or hide the edit panel. It builds on the ContentBox.js React guide, so start from that project.

ContentBox already styles the top bar for you:

Placement

An element with the custom-topbar class appears at the top of the editor when topSpace is on, and the editor moves down to make room.

Layout

It has three groups, left (your logo or site name), middle and right. Buttons, separators and the selected state are styled by ContentBox.

Icons

ContentBox's built-in icons can be used directly, so you need no icon package.

In this guide

  1. Create the top bar
  2. Use it in the editor
  3. Add your own sidebar buttons (optional)
  4. Try it

Step 1

Create the top bar

Create src/Topbar.tsx:

import { useState } from 'react'

type TopbarProps = {
    title: string
    onUndo: () => void
    onRedo: () => void
    onSave: () => void
    onViewPage: () => void
    onScreenMode: (mode: string) => void
    onPreview: () => void
    onViewHtml: () => void
    onTogglePanel: () => void
}

// The device views on the right: [screen mode, icon, label]
const SCREEN_MODES = [
    ['desktop-lg', 'icon-device-desktop', 'Large desktop'],
    ['desktop', 'icon-device-laptop', 'Desktop'],
    ['tablet', 'icon-device-tablet', 'Tablet'],
    ['mobile', 'icon-device-mobile', 'Mobile'],
    ['fullview', 'icon-fullview', 'Full view'],
]

// One of ContentBox's built-in icons
function Icon({ name }: { name: string }) {
    return <svg><use href={'#' + name} /></svg>
}

export default function Topbar(props: TopbarProps) {
    // ContentBox remembers the last device view, so start from it
    const [screenMode, setScreenMode] = useState(() => localStorage.getItem('_screenmode') || 'fullview')

    const changeScreenMode = (mode: string) => {
        setScreenMode(mode)
        props.onScreenMode(mode)
    }

    return (
        <div className="builder-ui keep-selection custom-topbar" data-tooltip>
            {/* Left: your logo or site name */}
            <div>{props.title}</div>

            {/* Middle: editing actions */}
            <div>
                <button onClick={props.onUndo} title="Undo"><Icon name="icon-undo" /></button>
                <button onClick={props.onRedo} title="Redo"><Icon name="icon-redo" /></button>
                <div className="separator"></div>
                <button onClick={props.onSave} title="Save"><Icon name="icon-save" /><span>Save</span></button>
                <button onClick={props.onViewPage} title="View page"><Icon name="icon-eye" /><span>View page</span></button>
            </div>

            {/* Right: device views, preview, HTML and the edit panel */}
            <div>
                {SCREEN_MODES.map(([mode, icon, label]) => (
                    <button key={mode} onClick={() => changeScreenMode(mode)} title={label} aria-pressed={screenMode === mode}>
                        <Icon name={icon} />
                    </button>
                ))}
                <div className="separator"></div>
                <button onClick={props.onPreview} title="Preview"><Icon name="icon-window-maximize" /></button>
                <button onClick={props.onViewHtml} title="HTML"><Icon name="icon-code" /></button>
                <div className="separator"></div>
                <button onClick={props.onTogglePanel} title="Edit panel"><Icon name="icon-settings" /></button>
            </div>
        </div>
    )
}

The classes on the top bar each have a job:

  • custom-topbar gives it the ContentBox top bar look and position.
  • builder-ui gives its buttons the editor's own interface styling.
  • keep-selection keeps what you are editing selected when you click a button on the bar.
  • data-tooltip shows each button's title as a tooltip.
  • aria-pressed highlights the device view in use.

Step 2

Use it in the editor

Replace src/App.tsx. It is the editor page from the React guide, with its three simple buttons replaced by the top bar:

import { useEffect, useRef } from 'react'
import ContentBox from '@innovastudio/contentbox'
import '@innovastudio/contentbuilder/public/contentbuilder/contentbuilder.css'
import '@innovastudio/contentbox/public/contentbox/contentbox.css'
import Topbar from './Topbar'

const DEFAULT_CONTENT = `
<div class="is-section is-box type-system-ui">
    <div class="is-overlay"></div>
    <div class="is-container v2 leading-14 size-18">
        <div class="row">
            <div class="column">
                <h1>Hello World</h1>
                <p>Start editing your page here.</p>
            </div>
        </div>
    </div>
</div>`

// Upload handler: send the file to your server and return { url }.
// Without a server, the file can be embedded as a data URL, as here.
async function upload(file: File) {
    const url = await new Promise<string | ArrayBuffer | null>((resolve, reject) => {
        const reader = new FileReader()
        reader.onload = () => resolve(reader.result)
        reader.onerror = reject
        reader.readAsDataURL(file)
    })
    return { url }
}

function App() {
    const builderRef = useRef<ContentBox | null>(null)

    useEffect(() => {
        // Create the editor
        const builder = new ContentBox({
            controlPanel: true,
            iframeSrc: '/blank.html', // the page you edit
            snippetUrl: '/assets/minimalist-blocks/content-plugins.js', // the blocks users can drag in
            topSpace: true,           // shows the top bar and leaves room for it
            upload,                   // handles image, video, audio and file uploads
        })
        builderRef.current = builder

        // Load the saved page, or the default the first time
        builder.loadHtml(localStorage.getItem('savedHtml') || DEFAULT_CONTENT)
        builder.loadStyles(
            localStorage.getItem('savedMainCss') || '',
            localStorage.getItem('savedSectionCss') || ''
        )

        return () => {
            builder.destroy()
            builderRef.current = null
        }
    }, [])

    const save = () => {
        const builder = builderRef.current
        if (!builder) return
        localStorage.setItem('savedHtml', builder.html())
        localStorage.setItem('savedMainCss', builder.mainCss())
        localStorage.setItem('savedSectionCss', builder.sectionCss())
    }

    return (
        <Topbar
            title="My Site"
            onUndo={() => builderRef.current?.undo()}
            onRedo={() => builderRef.current?.redo()}
            onSave={() => { save(); alert('Page saved!') }}
            onViewPage={() => { save(); window.open('/view.html', '_blank') }}
            onScreenMode={(mode) => builderRef.current?.setScreenMode(mode)}
            onPreview={() => builderRef.current?.openPagePreview()}
            onViewHtml={() => builderRef.current?.viewHtml()}
            onTogglePanel={() => builderRef.current?.toggleEditPanel()}
        />
    )
}

export default App

What each button does:

  • Undo and Redo: undo() and redo().
  • Save and View page: your own save function, as in the React guide. View page saves, then opens the viewing page.
  • Device views: setScreenMode() shows the page at that screen size. Full view uses the whole editor.
  • Preview: openPagePreview() shows the page at any width, using public/preview.html.
  • HTML: viewHtml() opens the code view.
  • Edit panel: toggleEditPanel() shows or hides the panel on the right.
ContentBox.js with a custom top bar: site name, undo and redo, save, device views, preview, HTML and the edit panel

The custom top bar, with the two new sidebar buttons on the left.

With topSpace on, ContentBox leaves out its own undo, redo and device buttons, since your top bar has them now.

Step 3

Add your own sidebar buttons (optional)

You can also add buttons to the editor's left sidebar. In src/App.tsx, add these lines after builderRef.current = builder:

        // Your own buttons on the left sidebar
        builder.addButton({
            pos: 2, // position in the sidebar
            title: 'Animate Section',
            html: '<svg><use xlink:href="#icon-wand"></use></svg>',
            onClick: () => builder.openMotionCascade(),
        })
        builder.addButton({
            pos: 3,
            title: 'Motion Timeline',
            html: '<svg><use xlink:href="#icon-anim-timeline"></use></svg>',
            onClick: () => builder.openMotionDock(),
        })

Animate Section opens the panel for animating a section on scroll, and Motion Timeline opens the timeline editor for those animations.

Step 4

Try it

Run npm run dev and open the printed URL (usually http://localhost:5173). The top bar shows your site name on the left and the buttons in the middle and on the right. Switch between the device views, open the preview and the HTML, and hide or show the edit panel.

ContentBox.js in the tablet view, chosen from the custom top bar

The tablet view, chosen from the top bar.

Resources

© 2026 InnovaStudio

This site is built with Site Builder Kit