Theme editor service

@dash/theme-editor is an optional administration service. Like @dash/cms, it provides a page module, an explicit API handler, and request-scoped administrator checks. Installing it does not mount routes or give development users admin access. The editor client is a separate, lazy-loaded entry point; the core client and @dash/devtools no longer contain a theme editor.

Mount the service

ts
// src/theme-editor.ts
import { resolve } from "node:path";
import { createThemeEditorService, createFileThemeStore } from "@dash/theme-editor/server";
import { createDefaultDataUserResolver } from "@dash/server";

export const themeEditor = createThemeEditorService({
  store: createFileThemeStore({ filePath: resolve("themes/registry.json") }),
  getUser: createDefaultDataUserResolver({ publicOwnerId: false }),
  // For a separate local Vite origin, explicitly allow that origin:
  allowedOrigins: ["http://localhost:3001"],
});
Reuse the app's trusted identity resolver. Admission defaults to access.admin; canAccess(user) can supply a different editor-role policy. Local demo admin access is an app decision and must not promote anonymous production users. allowedOrigins supplements same-origin requests and should contain only trusted frontend origins. Page queries and every administration request check access.Merge themeEditor.queryResolvers into dash.config.ts alongside the CMS resolvers. Expose these file-based routes:
File
Exports
src/app/theme-editor.tsxroute and default from createThemeEditorPage()
src/app/api/theme-editor.tsGET and POST = themeEditor.handleApi
src/app/api/themes.tsGET = themeEditor.handlePublic
createThemeEditorPage() is independent of service initialization, so codegen can load it without connecting storage. The same module is available as themeEditor.pages.index. Options apiPath, publicPath, and backHref must match the app's routes. API paths default to /api/theme-editor and /api/themes. There is no required URL for the editor page itself.

Register the browser editor

Register the editor before starting the Dash client. Pass the app's generated stylesheet for the isolated component preview:
tsx
// src/main.tsx
import { lazy } from "react";
import { DefaultRegistry, setApiBaseUrl } from "@dash/client";
import { loadPublishedThemes } from "@dash/theme-editor/runtime";

setApiBaseUrl(import.meta.env.VITE_API_BASE_URL ?? "");
DefaultRegistry.ThemeEditor = lazy(async () => {
  const [{ createThemeEditorComponent }, { default: css }] = await Promise.all([
    import("@dash/theme-editor/client"),
    import("@dash/generated/theme.css?raw"),
  ]);
  return { default: createThemeEditorComponent(css) };
});

async function start() {
  try {
    await loadPublishedThemes();
  } catch (error) {
    console.warn("Using built-in themes", error);
  }
  await import("@dash/client/entry-router");
}
void start();
For a custom registry, register the component there instead. The page uses the ClientOnly renderer node: its children render during SSR and the first client render; after mounting, the registered component renders inside Suspense. This keeps optional browser code out of SSR and avoids hydration mismatches. The TUI cannot edit themes; use a browser for this page.loadPublishedThemes() loads only the saved public registry, validates it, generates token CSS through @dash/ui/themes/css, and calls configureThemeRegistry from @dash/ui/themes. It has a five-second request limit. On failure the app can start with its bundled defaults. Saved themes are applied before client rendering; initial server-rendered markup still uses the bundled stylesheet. Explicit per-browser theme and appearance preferences take precedence over the app default when their theme still exists. Other already-open shop tabs receive saved changes on their next reload.

Storage and request contract

The file store keeps app configuration as JSON, never executable TypeScript. It does not modify the framework checkout, installed packages, the UI registry source, or generated .dash files. The default initial themes come from @dash/ui/themes/registry; an initial registry may be supplied. Those initial presets are protected from editor updates and deletion; duplicate them to create an editable app theme.createFileThemeStore writes one complete registry atomically through a temporary file and rename. A short-lived exclusive .lock file coordinates writers across API/SSR processes. Every mutation supplies the last saved revision; stale writes and busy writers return 409 rather than overwriting another session. Invalid storage and write failures remain errors, and corrupt catalogs are never silently replaced. If a process is terminated mid-write, remove its abandoned .lock only after confirming no writer is still running. Keep the JSON file on persistent storage in production. Multiple hosts need a shared durable store with equivalent compare-and-swap behavior, supplied through the ThemeStore interface.Both endpoints use MessagePack. Admin GET returns { themes, defaultTheme, protectedIds, revision }. Admin POST accepts one of:
  • { op: "duplicate", sourceId, id, name, revision }
  • { op: "save", id, theme, revision }
  • { op: "setDefault", id, revision }
  • { op: "delete", id, revision }
Success returns the new saved snapshot. The public GET returns only { themes, defaultTheme }; it never accepts writes. All responses use no-store. Identifiers use letters, digits, hyphens, and underscores and start with a letter. CSS values are bounded strings that cannot escape declarations or contain URLs; the editor additionally validates values with the browser's CSS parser. The active default cannot be deleted until a replacement is selected.Saving updates the saved theme immediately. Unsaved changes exist only in the browser: there is no separate published revision. Choosing “Use as shop default” affects new visitors; editing a non-default theme does not select it for visitors.

Editing behavior

The editor uses shared Dash buttons, inputs, selects, and dialogs, and follows the host’s active theme and appearance. Its preview keeps separate tokens so editing an application theme cannot change the surrounding console.The editor provides a theme library, grouped and searchable tokens, per-token reset, undo/redo, discard, a save shortcut, and explicit unsaved-change prompts. Save failures and conflicts leave the draft intact. Reloading a conflicting catalog retains local edits for review. Edits made during an in-flight save remain dirty after that save completes. Duplicate/delete dialogs stay open on errors.Light/dark previews and desktop/tablet/mobile widths use real Dash components in an isolated iframe. The preview covers visual components, grouped by type, including major variants, disabled states, forms, tables, and interactive overlays. Runtime control-flow nodes have no themed appearance and are excluded. Its category links navigate within the gallery. Overlays stay inside the iframe, and its appearance chooser uses temporary preview preferences without writing the administrator's browser preferences. Hex color pairs show a text contrast ratio. Built-in presets and default-theme deletion constraints are reflected in the controls as well as enforced on the service.

Migration from devtools

Remove imports from @dash/devtools/theme-editor and handleDevThemeApi. The implicit /_dev/theme screen and /_dev/api/theme* routes are removed. Mount the service and register the editor explicitly in each consumer app. Existing custom TypeScript definitions can be provided as the store's initial registry; no framework source files are rewritten or deleted by the service.Run bun run check in the framework repo. The service handles authorization, persistence, protected themes, default selection, malformed input, concurrent writers, stale revisions, storage errors, HTTP errors, and draft preservation; there is no automated test suite, so verify a change through Admin's Themes tool.

Multiple applications

Use the central admin console to host administration separately and select among multiple applications through one authenticated endpoint. Each application explicitly enables its services through its connector.

Client-owned themes

createLocalThemeApi({ storage }) from @dash/theme-editor/local implements the same editor API using browser storage. Use it as ThemeEditor's api, with applyToHost={false} to avoid fetching an application theme endpoint. Consumers can apply the resulting CSS and registry locally; Admin does not include a personal theme editor. The portable definition and validation types live in @dash/shared. See Administration for the distinction between application theme services and personal appearance.