Practic Base UI Theme Editor - v1.0.0
    Preparing search index...

    Practic Base UI Theme Editor - v1.0.0

    @practic/bui-theme-editor

    A visual editor for @practic/bui themes: edit any part's CSS beside live widgets, then download the theme file.

    npm types license

    A @practic/bui theme is one CSS file with a rule for each part of each component. Writing it by hand means switching between the file and the app to see each change. The theme editor shows every widget in a preview beside the rules, applies each edit as you make it, and offers a tool for every kind of value: colour pickers and mixers, sliders with units, shadow, gradient and easing builders, and font stacks. When the theme looks right, download the file and import it like any stylesheet. It is a development tool, so install it as a dev dependency.

    npm install --save-dev @practic/bui-theme-editor @practic/bui @base-ui/react react react-dom
    

    Or bun add -d, pnpm add -D, yarn add -D.

    Render the editor on a page of its own, such as a dev-only route:

    import { themeBuiltins, type ThemeEditorTheme } from '@practic/bui-theme-editor'
    import { Demo as ThemeEditor } from '@practic/bui-theme-editor/demo'
    import { useState } from 'react'

    export function ThemeEditorPage() {
    // optional: the theme to start from, such as your own file imported with `?raw`; here a copy of barebones
    const [theme, setTheme] = useState<ThemeEditorTheme>({ name: 'brand', css: themeBuiltins['barebones'] ?? '' })

    return (
    <ThemeEditor
    theme={theme}
    // optional: receives the edited file on mount and after every change, to keep in state, storage or a dev server
    setTheme={(next) => {
    setTheme(next)
    localStorage.setItem(`theme:${next.name}`, next.css)
    }}
    />
    )
    }

    Pick a theme, choose a widget in Rules for, and edit its rules. Use Download to save the file, then import it in your app in place of the theme you started from.

    Both props are optional. Without them, the editor starts on barebones and keeps your edits itself. A theme is loaded as if you had opened the file: it is added under its name and selected, with no edits, once it passes the theme template's checks; otherwise the editor shows why and keeps its theme. setTheme receives { name, css } once on mount and after every edit or theme switch. Passing back what it received does not reload anything, so useState works as shown.

    • 👀 Live preview of every widget. Each edit applies at once, inside an isolated frame, so the editor keeps its own look.
    • 🎯 Rules per widget. Choose a widget to list only its rules and scroll the preview to it.
    • 🛠️ A tool for every kind of value. Each value has a ✎ panel:
      • colours in hex, rgb(), hsl(), oklch() and more, with an alpha slider and a color-mix() mixer;
      • lengths and other numbers with a slider and units;
      • box sides, borders, shadows, gradients, transitions, easing curves, transforms and font stacks;
      • keywords and var() references to the theme's tokens.
    • 🌬️ Tailwind @apply (optional). Type utilities in a rule with suggestions; unknown classes are flagged and never break the preview.
    • 📄 Valid theme files. Edits keep the theme in template order, and values that would corrupt the file are refused.
    • 📂 Open your own theme. Load opens a .css file, and the theme prop does the same from code. Either one is checked against the theme template first, and refused with the reasons when it does not fit.
    • 🔁 State in and out. setTheme receives the edited file on mount and after every change, to keep in state, storage or a dev server.
    • 🧩 Your page, your preview. ThemeEditorPanel is the whole editor without a page around it, previewing your own components, and useThemeEditor lets you write parts of your own.
    • 🪟 Your running app as the preview. ThemeEditor.AppFrame frames your app's own pages, still usable, and each edit restyles them.
    • 🤖 Headless. themeEditCss applies edits to a theme file exactly as the editor saves it, for scripts and CI.
    • 💾 Edits survive reloads, kept per theme until you download them.
    • 📋 Copy or download the finished theme.
    Import Export Description
    @practic/bui-theme-editor/demo Demo The complete editor page: header, rules, preview, load, copy, download.
    @practic/bui-theme-editor ThemeEditorPanel The same editor without the page: fits inside one of yours.
    @practic/bui-theme-editor ThemeEditor Compound parts to build your own editor layout.
    @practic/bui-theme-editor useThemeEditor, useThemeEditorRule Read and edit the theme from parts of your own.
    @practic/bui-theme-editor/core themeEditCss, themeGetTokens, themeCheckNewSelector Edit a theme file, list its tokens and check a new selector (refused with a reason), with no UI.
    @practic/bui-theme-editor/core themeCheckFile Why a theme file breaks the theme template, or an empty list.
    @practic/bui-theme-editor/core themeBuiltins The themes @practic/bui ships, by name.
    @practic/bui-theme-editor themeEditorAppFrameCss The preview stylesheet for ThemeEditor.AppFrame: your app fills the preview.
    @practic/bui-theme-editor themeEditorLabelsEn Every text the editor shows, in English: what labels override, and the base of a translation.
    @practic/bui-theme-editor tailwindCompile, tailwindListClasses Tailwind's compiler and class list, loaded on first use.

    @practic/bui-theme-editor/core holds no React, so scripts, CI jobs and server code import it alone; the main entry exports the same functions too.

    Demo, ThemeEditorPanel and ThemeEditor.Root all take the optional theme and setTheme shown in the Quick start. A theme is { name, css }: the name it is saved under (Download writes <name>.css) and its file.

    ThemeEditorPanel and ThemeEditor.Root show English texts. To reword or translate them, pass labels with only the texts that change, such as labels={{ download: 'Save', copy: { idle: 'Copy CSS' } }}; the rest stay in English, and themeEditorLabelsEn holds them all as the base of a translation.

    The built-in preview (Demo, or ThemeEditorPanel with no preview) is the clean room. You design the theme against every widget in every state, and the preview is the theme file and nothing else: the editor draws the widgets itself, inside a frame where the edited theme is the only theme, and Rules for scrolls to each one.

    ThemeEditor.AppFrame is the fitting room. You see the same theme on your real screens, with real data, signed in, and can catch what the showcase can't show, such as how your own layouts and content wear it. Your app runs as its own page inside the preview, with its own theme still loaded under the edited one, so a property you delete still shows the old value there.

    Most themes start in the clean room and finish in the fitting room.

    The editor inside a page of your own, previewing your components

    ThemeEditorPanel registers nothing, so it fits inside an app that already set up @practic/bui. Give it a storage key of its own, the screens to preview, and the components Rules for narrows the rules to:

    import { ThemeEditorPanel } from '@practic/bui-theme-editor'
    import { Button } from '@practic/bui'
    import '@practic/bui/themes/barebones.css'

    // one of your own screens, previewed with the edited theme; its id is what Rules for scrolls to
    function SignIn() {
    return (
    <section
    id='sign-in'
    style={{ display: 'grid', gap: 8, padding: 16 }}
    >
    <h2>Sign in</h2>
    <Button>Continue</Button>
    </section>
    )
    }

    export function AppThemeEditor() {
    return (
    <ThemeEditorPanel
    data-key='my-app_theme-editor'
    data-attr='brand'
    preview={<SignIn />}
    previewCss=''
    widgets={[{ id: 'sign-in', title: 'Sign in', parts: ['Button'] }]}
    />
    )
    }
    Prop Default Description
    labels themeEditorLabelsEn Texts to show instead of the English ones: any of ThemeEditorPanelLabels.
    theme, setTheme The edited theme in and out, as in the Quick start.
    data-key, data-attr the demo's own Where the selection, edits, loaded themes and chosen widget are stored.
    themes themeBuiltins Theme files to choose from, by name.
    preview every @practic/bui widget What the preview frame shows with the edited theme.
    previewCss the showcase page's The only stylesheet in the preview frame besides the theme, for your preview's layout.
    widgets the showcase's widgets { id, title, parts }: the element Rules for scrolls to, and its part classes.
    compileTailwind, listTailwindClasses Tailwind's, loaded lazily How @apply is previewed and suggested.
    Theming your running app

    ThemeEditor.AppFrame previews your app itself: pass it as ThemeEditorPanel's preview, with themeEditorAppFrameCss as previewCss. It opens a page of your app in a frame and puts the edited theme at the end of each page's head, after the theme your app imports, so each edit restyles the real pages. The app stays usable inside it: sign in, open dialogs and move between pages, and the theme follows.

    import { ThemeEditor, ThemeEditorPanel, themeEditorAppFrameCss } from '@practic/bui-theme-editor'
    import '@practic/bui/themes/barebones.css'

    export function OwnAppThemeEditor() {
    return (
    <ThemeEditorPanel
    data-key='my-app_theme-editor'
    data-attr='brand'
    previewCss={themeEditorAppFrameCss}
    preview={
    <ThemeEditor.AppFrame
    src='/'
    title='My app, with the edited theme'
    />
    }
    />
    )
    }
    1. Give it a route of its own, for development only. Render it on a page such as /theme-editor. Serve that page only while developing: in Vite, register the route only when import.meta.env.DEV is set; in Next.js, redirect away from it in proxy.ts unless the request's Host header is localhost. Either way it is a convenience for developers, not access control.
    2. Serve it from your app's origin. The editor reaches into the framed page to style it, which the browser allows only when both share an origin. src is a path of your app, such as / or /dashboard.
    3. Let Tailwind see the editor (Tailwind 4 apps). Tailwind scans only your sources, so add @source '../node_modules/@practic/bui-theme-editor'; to the stylesheet that imports tailwindcss, with the path relative to that stylesheet.
    4. Download and swap. When the theme looks right, use Download and import the file in place of the theme your app imported, such as @practic/bui/themes/barebones.css.
    Prop Type Description
    src string The page of your app the frame opens on, of the editor's origin.
    title string Names the frame for assistive technology.
    overrides { propsFrame?: ComponentProps<'iframe'> } (optional) Props for the iframe; an onLoad given here runs after the frame's own.

    Your app's own theme stays loaded under the edited one. So the preview never shows a property you deleted in the editor: the old theme's value shows instead. The downloaded file is right. Page transitions run without their fades inside the frame, since Chromium crashes running a view transition in a frame this deeply nested. Rules for still narrows the rules list, but has no widget of yours to scroll to.

    Building your own editor with ThemeEditor parts

    ThemeEditor.Root owns the selected theme and its edits, and every other part reads them.

    Prop Type Description
    labels ThemeEditorLabelsInput Texts to show instead of the English ones; any of them.
    themes Record<string, string> Theme files by name. Defaults to themeBuiltins; pass your own to edit them.
    theme ThemeEditorTheme A theme to load, as if opened with Load.
    setTheme (theme: ThemeEditorTheme) => void Receives the edited theme on mount and after every change.
    data-key string Storage key for the selection and edits.
    data-attr string Attribute under that key; without it, edits stay in memory.
    compileTailwind (rules: string) => Promise<string> Compiles rules that use @apply for the preview.
    listTailwindClasses () => Promise<TailwindClasses> Loads the classes the Tailwind field suggests.

    Parts: ThemeSelect, Load, RemoveTheme, LoadError, Rules, RuleItem (a collapsible rule with every rule part), RuleLabel, RuleVariables, RuleProperties, RuleAddProperty, RuleApply, RuleReset, RuleRemove, RuleSource, AddRule, Style (the edited theme as a <style>), AppFrame (your running app as the preview), Output, Copy, Download, TailwindNotice, and TailwindError.

    Render ThemeEditor.Style inside an IsolatedFrame from @practic/bui/app, so the edited theme reaches only the preview. Parts of your own read the theme with useThemeEditor(): its rules, css, tokens and themeName, and setProperty, addRule, removeRule, resetRule and loadTheme, which validate as the built-in parts do. Inside ThemeEditor.Rules, useThemeEditorRule() gives each rule's selector, declarations and variables:

    import { ThemeEditor, useThemeEditor } from '@practic/bui-theme-editor'
    import { Button } from '@practic/bui'
    import { IsolatedFrame } from '@practic/bui/app'
    import { SettingNumber } from '@practic/bui/widgets'
    import { useState } from 'react'
    import '@practic/bui/themes/barebones.css'

    // one control of your own: the theme's corner radius, written into its :root tokens
    function RadiusSetting() {
    const { setProperty } = useThemeEditor()
    const [radius, setRadius] = useState(4)

    return (
    <SettingNumber
    label='Corner radius'
    value={radius}
    min={0}
    max={24}
    onChange={(next) => {
    setRadius(next)
    setProperty(':root', '--radius', `${next}px`)
    }}
    />
    )
    }

    export function RadiusControl() {
    return (
    <ThemeEditor.Root>
    <RadiusSetting />
    <IsolatedFrame
    title='Preview'
    style={{ width: 320, height: 120 }}
    >
    <ThemeEditor.Style />
    <Button>Rounded</Button>
    </IsolatedFrame>
    <ThemeEditor.Download />
    </ThemeEditor.Root>
    )
    }
    Editing themes without the UI

    themeEditCss applies the same edits the editor makes, keeping template order, and returns the file it would save. Removing a template rule (null) comments it back into its scaffold:

    import { themeBuiltins, themeCheckFile, themeEditCss } from '@practic/bui-theme-editor/core'

    // a rounder variant of barebones, made in a Bun build script rather than in the editor
    const rounded = themeEditCss(themeBuiltins['barebones'] ?? '', {
    ':root': { '--radius': '0.75rem' },
    '.Button': { padding: '0.5rem 1rem' },
    })

    console.log(themeCheckFile({ name: 'rounded', css: rounded }).length === 0 ? rounded : 'not a valid theme')
    • Peer dependencies: react and react-dom 19.2 or later (for useEffectEvent), @base-ui/react 1, and @practic/bui. tailwindcss 4 is optional, needed only to preview @apply.
    • Demo sets up storage. When it renders, Demo registers @practic/persist as @practic/bui's persistence layer, which must happen before any @practic/bui component renders. Give it its own page or entry, or register the same layer in your app. ThemeEditorPanel and ThemeEditor.Root register nothing.

    MIT © Felipe Penno. See LICENSE.