@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.// 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"],
});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.tsx | route and default from createThemeEditorPage() |
| src/app/api/theme-editor.ts | GET and POST = themeEditor.handleApi |
| src/app/api/themes.ts | GET = 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.// 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();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..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 }{ 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.@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.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.