Skip to content
On this page Release channels

Getting started

Installation

Install @codeatlasdev/ui and wire the theme into SvelteKit, Astro or plain Vite without a flash of the wrong theme.

CodeAtlas UI needs Svelte 5 (runes mode). In a SvelteKit or Astro app, bunx @codeatlasdev/ui init does the whole setup below for you (see The init command). Setup is three things: import the stylesheet once, inject the theme script before first paint, and start the colorScheme store in your root layout.

bun add @codeatlasdev/ui

The core bundle stays small. Heavy features are separate entry points with optional peer dependencies, so install only what you use:

Feature Import from Also install
Charts @codeatlasdev/ui/charts echarts
DataTable @codeatlasdev/ui/table @tanstack/svelte-table
CodeSnippet @codeatlasdev/ui/code shiki
Masked and money fields @codeatlasdev/ui/forms nothing
Accessibility test helpers @codeatlasdev/ui/testing axe-core
Icons in your own code (KpiCard, Chip, Button and nav items take icon components) @lucide/svelte @lucide/svelte
Brand icons (caBrandPlugin) @codeatlasdev/ui/vite @resvg/resvg-js and png-to-ico
migrate command bunx @codeatlasdev/ui migrate --from <major> --to <major> typescript (resolved from the project; without it the command prints the install hint and exits 1)

@sveltejs/kit and astro are optional peers too: install the one your app uses. Icons are @lucide/svelte components. The package depends on it for its own icons, but Bun’s isolated linker (and pnpm) do not expose a dependency’s dependency to your app, so import { ArrowRight } from "@lucide/svelte" fails until you run bun add @lucide/svelte yourself.

Release channels

Releases are published by version tag (@codeatlasdev/[email protected]), not from branches. The prerelease id in the version is the npm dist-tag.

Version Install When to use it
X.Y.Z bun add @codeatlasdev/ui (dist-tag latest) Production. Semver: breaking changes only in a major.
X.Y.Z-beta.N bun add @codeatlasdev/ui@beta A prerelease of the next version. It never moves latest. Try it before it ships.

A prerelease with another id (X.Y.Z-alpha.N, for example) is published under a dist-tag with that id. Pin an exact version (bun add @codeatlasdev/[email protected]) to keep a prerelease stable.

Every version is published from CI with an npm provenance statement, so you can check which commit and workflow built it (npm audit signatures).

The init command

bunx @codeatlasdev/ui init [--write] [--dir <path>] [--with lint,e2e,pwa]

init detects the framework from package.json (@sveltejs/kit means SvelteKit, otherwise astro means Astro) and prints Detected SvelteKit. or Detected Astro.. With neither it prints an error and exits with code 1. Then it goes through the files of that framework, one line each:

Line Meaning
create <file> The file was missing and is now written (--write).
would create <file> The file is missing. Nothing is written until you re-run with --write.
ok <file> (already set up) Nothing to change. Running init again on a finished setup prints only ok lines.
diff <file> (exists, not modified) The file exists and needs a change. A unified diff follows; nothing is written until you re-run with --write.
patch <file> The change was applied (--write).
manual <file>: could not patch safely. init could not edit the file safely (no plugins: [...] array, an existing handle of your own, no </body>). It says what to add by hand and exits with code 1.
create AGENTS.md / patch AGENTS.md The rules an agent needs to work in this app, between <!-- codeatlas-ui:start --> and <!-- codeatlas-ui:end -->. An existing AGENTS.md keeps everything outside the markers; running init again replaces only the block.
skip <file> (not found) The file does not exist and init does not create it. On Astro this is the Astro config; SvelteKit creates every file it needs.
skip <file>: the app already imports its theme from "<module>" vite.config.ts, hooks.server.ts (or the Astro config) already import theme from somewhere else, for example a shared workspace package. init does not create src/lib/theme.ts (src/theme.ts on Astro) and wires the files it adds to that import.
Flag Effect
--write Create the missing files and apply the proposed changes. Without it init is a dry run: nothing touches the disk.
--dir <path> Project directory. Defaults to the current directory.
--with <list> Opt-in extras, comma separated: lint (UI guard scripts), e2e (Playwright matrix and axe spec), pwa (SvelteKit: manifest, service worker, offline page, robots.txt, sitemap.xml; see --with pwa). Without --with none of them is written.

When anything is pending it ends with N change(s) not applied. Re-run with --write to create the files and apply the diffs above. A clean run ends with Done. Run your dev server: the theme paints on the first frame.

SvelteKit

@codeatlasdev/ui supports SvelteKit 2 (^2.12) and SvelteKit 3. The steps below are the same on both; the differences are in SvelteKit 3. init reads the installed Kit major (node_modules/@sveltejs/kit, or the range in package.json before install ran; anything it cannot parse counts as 2) and writes the right theme import.

What init writes

For each file below, init creates it when it is missing, or patches it when it exists. Without --write nothing is written: init prints would create for a missing file and a diff for an existing one. A file that already exists is never overwritten: its diff is applied only with --write. Re-running it changes nothing once the setup is complete. A project with nothing but package.json and @sveltejs/kit works: init creates the config files it needs.

File What init puts there If the file exists
package.json "scripts": { "prepare": "svelte-kit sync" } and "imports": { "#lib/*": "./src/lib/*" } (Kit 2 and 3), so the files below can import #lib/theme.ts and #lib/messages.ts ($lib was removed in Kit 3; Kit 2 resolves #lib too) adds only what is missing: a prepare script you already have is kept as it is, and so is the rest of imports
src/lib/theme.ts the shared theme object (defaultPreset: "amber", defaultScheme: "system") left alone
src/lib/messages.ts the app copy the scaffold shows (export const m = { appTitle }): templates read {m.appTitle}, so codeatlas-ui lint stays clean. --with pwa adds the offline keys the offline page reads. See app copy left alone; --with pwa adds offline only to the file init wrote itself
vite.config.ts caThemePlugin(theme) and caErrorPlugin({ scaffold: true, theme }), appended to plugins, in one @codeatlasdev/ui/vite import (plus import { theme } from "./src/lib/theme.ts") adds only the plugin that is missing
src/hooks.server.ts handle = sequence(caThemeHandle(theme)) and handleError = createErrorHandler() adds only what is missing; reports manual if you already define a handle of your own; never touches your handleError
src/routes/+layout.svelte the stylesheet import, colorScheme.init() in onMount and a default <Seo> (title from m.appTitle, canonical from page.url) adds only what is missing; an existing <Seo> is kept

That layout <Seo> is a placeholder, not a default a page overrides. Each <Seo> writes its own tags into <head>; only the <title> is replaced by the page’s (the last one rendered wins). The description, canonical, og:* and twitter:* tags of the layout and of a page that has its own <Seo> would both be in the document. Delete the layout <Seo> as soon as the pages carry theirs, or keep it and give the pages no <Seo> of their own (see SEO). | src/app.html | the viewport meta with viewport-fit=cover, interactive-widget=resizes-content | rewrites only the viewport meta; reports manual when there is none | | tsconfig.json | see a bare app; with @sveltejs/adapter-static, the adapter output folder in exclude | adds only what is missing | | src/routes/+layout.ts | only with @sveltejs/adapter-static: export const prerender = true; | adds the line when missing |

The service worker, manifest, robots.txt, sitemap.xml and offline page are opt-in (--with pwa). With @sveltejs/adapter-static, see static deploys.

caErrorPlugin({ scaffold: true, theme }) then creates src/routes/+error.svelte and src/error.html the first time Vite runs (dev or build), and never overwrites them. src/error.html takes its colors, default scheme and preset from the theme you pass (the same object as caThemePlugin), carries the theme init script in <head>, and has no inline handler or javascript: URL, so it works under a strict CSP. Its copy and lang follow locale (caErrorPlugin({ scaffold: true, theme, locale: "pt-BR" }); default en, with a build warning until you set it). The result is the setup below, which you can also write by hand.

Full setup

  1. Register both Vite plugins. caThemePlugin bundles the ESM-only dependencies for SSR (@tanstack/svelte-table, shiki) and warns at build time if the theme script is not injected. caErrorPlugin creates the error pages when they are missing (scaffold: true), or only warns without it. Import the theme with its .ts extension: Vite loads the config with Node’s native loader, which does not resolve extensionless imports (Vite 8 warns about it). That needs "allowImportingTsExtensions": true in tsconfig.json. Kit 3’s $app/tsconfig sets it, as long as your tsconfig.json keeps "extends": "$app/tsconfig" and does not override compilerOptions; Kit 2’s generated .svelte-kit/tsconfig.json does not, so set it (with noEmit and skipLibCheck) yourself. init writes them into the tsconfig.json it creates and adds the missing ones to a Kit 2 file you have.

    // vite.config.ts
    import { sveltekit } from "@sveltejs/kit/vite";
    import { caErrorPlugin, caThemePlugin } from "@codeatlasdev/ui/vite";
    import { defineConfig } from "vite";
    import { theme } from "./src/lib/theme.ts";
    
    export default defineConfig({
      plugins: [sveltekit(), caThemePlugin(theme), caErrorPlugin({ scaffold: true, theme })],
    });
  2. Inject the theme script and sanitize server errors in the server hook. caThemeHandle puts the brand presets and the init script right after <head> of every SSR and prerendered page, so the first paint already has the right scheme and preset. createErrorHandler turns every unexpected server error into { message, code, id } without leaking the message of a 5xx; pass a locale or a report callback when you need them.

    // src/hooks.server.ts
    import { sequence } from "@sveltejs/kit/hooks";
    import { caThemeHandle, createErrorHandler } from "@codeatlasdev/ui/kit";
    import { theme } from "#lib/theme.ts";
    
    export const handle = sequence(caThemeHandle(theme));
    export const handleError = createErrorHandler();
  3. Define the theme once. Both the hook and the plugin take the same object. An empty object is valid and gives you the built-in amber preset in dark mode.

    // src/lib/theme.ts
    import type { ThemeOptions } from "@codeatlasdev/ui/vite";
    
    export const theme: ThemeOptions = {
      defaultScheme: "system",
    };
  4. Import the stylesheet and start the store in the root layout.

    <!-- src/routes/+layout.svelte -->
    <script>
      import "@codeatlasdev/ui/styles/global.css";
      import { colorScheme } from "@codeatlasdev/ui";
      import { onMount } from "svelte";
    
      let { children } = $props();
      onMount(() => colorScheme.init());
    </script>
    
    {@render children()}
  5. Set the viewport meta in src/app.html. See Mobile for what each part does.

    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, interactive-widget=resizes-content" />

SvelteKit 3

On a fresh Kit 3 app run svelte-kit sync (bunx svelte-kit sync) before svelte-check: .svelte-kit/tsconfig.json is no longer generated, and until the sync wrote .svelte-kit/ svelte-check stops with File '$app/tsconfig' not found. init adds "prepare": "svelte-kit sync" to package.json, so the install step runs it for you; vite dev and vite build sync as well.

Kit 3 changed three things this setup touches. init handles the third ($lib is gone): it adds the #lib/* map to package.json (on Kit 2 too) and imports through it. For the other two it prints a note line instead of rewriting svelte.config.js or an existing tsconfig.json: it reports svelte.config.js/.ts/.mjs when one exists, and a tsconfig.json that does not mention $app/tsconfig. On a clean Kit 3 run you get no Done. line while a note is pending.

  1. Configuration moves into the Vite plugin. svelte.config.js is ignored (config_file_unsupported) and the kit namespace is gone (config_kit_namespace). Pass the options directly to sveltekit(), next to the two plugins:

    // vite.config.ts
    import adapter from "@sveltejs/adapter-auto";
    import { sveltekit } from "@sveltejs/kit/vite";
    import { caErrorPlugin, caThemePlugin } from "@codeatlasdev/ui/vite";
    import { defineConfig } from "vite";
    import { theme } from "./src/lib/theme.ts";
    
    export default defineConfig({
      plugins: [sveltekit({ adapter: adapter() }), caThemePlugin(theme), caErrorPlugin({ scaffold: true, theme })],
    });
  2. tsconfig.json extends $app/tsconfig. .svelte-kit/tsconfig.json is no longer generated (tsconfig_extends_missing). The base config already allows .ts import extensions and skips library checks:

    { "extends": "$app/tsconfig" }
  3. $lib is removed (module_removed_lib). Use the #lib/* import that sv create adds to package.json, written with the extension. init --write adds it when it is missing and never replaces an imports map you have:

    { "imports": { "#lib/*": "./src/lib/*" } }
    // src/hooks.server.ts
    import { theme } from "#lib/theme.ts";

    Everything else (the layout, the stylesheet import, colorScheme.init() and the viewport meta) is identical to Kit 2. Kit 2 resolves the same #lib/* map, so init writes #lib/...ts imports there as well; an existing $lib import on Kit 2 is left as it is.

A bare app: files init creates

init does the whole setup, including a project with nothing but package.json and @sveltejs/kit in it. When vite.config.ts, src/app.html or tsconfig.json are missing it creates a minimal one instead of skipping:

File What init creates
vite.config.ts sveltekit(), caThemePlugin(theme) and caErrorPlugin({ scaffold: true, theme }). On Kit 3 with @sveltejs/adapter-static in package.json it also imports the adapter and passes sveltekit({ adapter: adapter() }). On Kit 2 the adapter belongs in svelte.config.js: with @sveltejs/adapter-static in package.json init creates it (adapter plus vitePreprocess), or adds adapter: adapter() and the import to one that names no adapter. A config that already sets adapter is left alone.
src/app.html the document skeleton with the viewport meta and %sveltekit.head% / %sveltekit.body%, without the style="display: contents" wrapper that sv create adds (the UI guard forbids inline styles)
tsconfig.json Kit 3: { "extends": "$app/tsconfig" }. Kit 2: extends ./.svelte-kit/tsconfig.json plus allowImportingTsExtensions, noEmit and skipLibCheck (Kit 2’s generated config sets none of them, and vite.config.ts imports the theme with its .ts extension)

If the files exist, init patches them as before (a diff without --write). For an existing Kit 2 tsconfig.json it adds only the compiler options you have not set yourself.

Static deploys with @sveltejs/adapter-static

init detects @sveltejs/adapter-static in package.json and adds what a static build needs, so vite build and svelte-check both pass:

  • tsconfig.json exclude. The adapter writes its output (build/ by default, or the pages option of adapter({ pages }) in vite.config.ts) into the project, and svelte-check would type-check that minified JS (thousands of errors). init adds the folder to exclude.
  • src/routes/+layout.ts with export const prerender = true;. Without it adapter-static emits only the SPA fallback (200.html), not one HTML file per page. An existing +layout.ts keeps its content and gains the line.
  • The adapter major must match the Kit major, and Kit 3 needs TypeScript 6. @sveltejs/adapter-static@4 peers on SvelteKit 3 only and @sveltejs/adapter-static@3 on SvelteKit 2; the other pairing fails npm install with ERESOLVE. SvelteKit 3 also peers on typescript@^6 (SvelteKit 2 accepts ^5.3.3 || ^6), so a hand-written package.json with typescript@^5 fails the same way on Kit 3. init prints a note when the declared adapter major is the wrong one.
SvelteKit @sveltejs/adapter-static typescript
3 4 ^6
2 (^2.12) 3 ^5.3.3 || ^6

Links to routes that do not exist yet (a landing page’s /sign-in) make the prerender fail with a 404 linked from /. Create the routes, or set prerender: { handleHttpError: "warn" } (in sveltekit({ ... }) on Kit 3, under kit in svelte.config.js on Kit 2).

PWA and SEO: --with pwa

A plain init does not write a service worker, manifest, offline page, robots.txt or sitemap. A screen app does not need them, they need icons and a public origin you have to supply, and a manifest route you delete later leaves a manifest link that fails the prerender. <Seo> in the root layout carries a title (read from src/lib/messages.ts) and the page’s canonical URL, and no manifest.

bunx @codeatlasdev/ui init --write --with pwa

--with pwa writes the set below and links the manifest from <Seo manifest="/manifest.webmanifest">. It can be added to an app init already set up: the missing files are created and init’s own <Seo> gains the link (a <Seo> you wrote is left alone). --with takes a comma separated list: --with lint,e2e,pwa.

File What init --with pwa puts there
src/routes/manifest.webmanifest/+server.ts a prerendered manifest built with manifest({ name, theme, icons }), named from m.appTitle
src/routes/robots.txt/+server.ts, src/routes/sitemap.xml/+server.ts prerendered robotsTxt() (pointing at the sitemap) and sitemapXml() listing the static pages (no [param] routes, route groups dropped, no /offline)
src/service-worker.ts (Kit 2) or src/service-worker/index.ts (Kit 3), src/routes/offline/ the worker from PWA and the prerendered offline page it falls back to, with its copy in src/lib/messages.ts
vite.config.ts (created, Kit 3) sveltekit({ prerender: { handleUnseenRoutes: "ignore" } }): no page links to /robots.txt or /sitemap.xml, and Kit fails the build with prerender_unseen_routes otherwise
tsconfig.json (Kit 3) src/service-worker in exclude: the worker has its own tsconfig ($app/tsconfig/service-worker)

After creating them init prints what the app still has to provide:

  • The public origin. While prerendering Kit uses a placeholder origin, and it would end up in sitemap.xml, robots.txt and every canonical URL. On Kit 3 it is sveltekit({ paths: { origin: "https://example.com" } }) in vite.config.ts. On Kit 2 there is no paths.origin (Kit 2.70 rejects it with Unexpected option config.kit.paths.origin): the option is kit: { prerender: { origin: "https://example.com" } } in svelte.config.js.
  • prerender.handleUnseenRoutes: "ignore" when the config already existed without it (Kit 2: under kit in svelte.config.js).
  • The manifest icons (static/icons/192.png, 512.png, maskable-512.png), or generate them from one SVG with caBrandPlugin (@codeatlasdev/ui/vite).

See SEO and PWA.

Astro

Use the integration together with @astrojs/svelte. caUI puts the theme (font preloads, <style id="ca-theme">, the init script) as the first children of the <head> of every page, ahead of every bundled stylesheet, and handles the SSR bundling. Astro renders its own <style> tags before any script an integration injects, so the integration rewrites the rendered page instead; pass extra first-paint markup of your own (a boot script) as caUI(theme, { head }) so it sits in the same place. bunx @codeatlasdev/ui init sets this up for an Astro project:

File What init puts there If the file exists
src/theme.ts the shared theme object (defaultPreset: "amber", defaultScheme: "system") left alone
astro.config.mjs (or .ts, .js, .mts) caUI(theme) and svelte() first in integrations, with their imports adds each one unless caUI( / svelte( is already there; reports manual when there is no integrations: [ array. init cannot install @astrojs/svelte: when it is not in package.json it prints a note to run npx astro add svelte
the first .astro file in src/layouts that renders <html> the stylesheet import, the viewport meta and a client <script> that calls colorScheme.init() adds only what is missing; reports manual when there is no </body> for the script
src/layouts/Base.astro created only when src/layouts has no layout that renders <html> not applicable
bun add @codeatlasdev/ui @astrojs/svelte svelte
// astro.config.mjs
import svelte from "@astrojs/svelte";
import { caUI } from "@codeatlasdev/ui/astro";
import { defineConfig } from "astro/config";
import { theme } from "./src/theme.ts";

export default defineConfig({ integrations: [svelte(), caUI(theme)] });
// src/theme.ts
import type { ThemeOptions } from "@codeatlasdev/ui/astro";

export const theme: ThemeOptions = { defaultPreset: "amber", defaultScheme: "system" };

Import the stylesheet once in your base layout, add the viewport meta and start the store from a client script, so theme switches and OS scheme changes stay in sync on the page:

---
// src/layouts/Base.astro
import "@codeatlasdev/ui/styles/global.css";
---
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, interactive-widget=resizes-content" />
    <title>My app</title>
  </head>
  <body>
    <slot />
    <script>
      import { colorScheme } from "@codeatlasdev/ui";
      colorScheme.init();
    </script>
  </body>
</html>

Components are Svelte islands. Add a client directive to anything interactive (client:visible or client:load); purely visual components render on the server with no JavaScript. Call colorScheme.init() once per page, from that layout script or from one island that is always present.

With <ClientRouter /> (View Transitions) the theme survives navigation: caUI copies the injected <style id="ca-theme">, the runtime tokens, the theme-color meta and the data-preset, data-style, data-ca-scheme, data-ca-style attributes and the dark class onto each incoming page before Astro swaps it in. Nothing to configure.

Plain Vite

caThemePlugin also works without SvelteKit: it injects the theme head through transformIndexHtml.

// vite.config.ts
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { caThemePlugin } from "@codeatlasdev/ui/vite";
import { defineConfig } from "vite";

export default defineConfig({ plugins: [caThemePlugin(), svelte()] });
// src/main.ts
import "@codeatlasdev/ui/styles/global.css";
import { colorScheme } from "@codeatlasdev/ui";
import { mount } from "svelte";
import App from "./App.svelte";

colorScheme.init();
mount(App, { target: document.getElementById("app")! });

Verify

<script>
  import { Button, ThemeToggle } from "@codeatlasdev/ui";
</script>

<Button variant="filled">It works</Button>
<ThemeToggle />

Reload with the toggle on dark and check that there is no white flash. Continue with Theming.

Lint and e2e templates

init --with lint,e2e --write adds the lint:ui and check:ca scripts and ui-guard.config.json (lint), and a Playwright config with 390, 768 and 1280 px projects plus an axe spec per route in light and dark (e2e). codeatlas-ui lint src reads .svelte, .astro and .css, which Biome and ESLint do not see inside templates, and reports hex colors, raw px, inline style, :global(.ca-*) and literal text (app copy), each with file and line; a .ca-* override that must stay goes in allow with a ca-ui-workaround(#n) marker. Existing files and scripts are never overwritten. Biome cannot see template usage, so it may flag Svelte imports as unused: keep it on .ts and let svelte-check own .svelte. What init itself writes passes this lint: its copy lives in src/lib/messages.ts.

Check an existing setup with doctor

bunx @codeatlasdev/ui doctor reads the project offline and prints one line per problem with file:line and the fix. It exits 1 on an error. --fix only renames markers; the rest are hints.

Finding What it checks
peer-svelte, peer-kit Svelte is at least 5.33 and SvelteKit is 2.12 or 3.
optional-peer An app that imports /table, /charts, /code or /testing lists the optional peer that entry needs (@tanstack/svelte-table, echarts, shiki, axe-core).
wiring-vite, wiring-hook, wiring-handle-error, wiring-css caThemePlugin in the Vite config, caThemeHandle and handleError in hooks.server, and the global.css import. init --write adds all four.
viewport-fit The viewport meta carries viewport-fit=cover, so the safe-area tokens are not zero.
root-font-size App CSS does not set font-size on html or :root, which resizes every component.
marker-todo TODO(lib#n) markers; workarounds only reads ca-ui-workaround(#n). --fix renames them.
ca-selectors App CSS that targets .ca-* classes has a script running workarounds --strict.
locale-key A registerLocale group or key the catalog lacks: never read, so it stays English.
lint-guard A script that runs codeatlas-ui lint (init --with lint adds it).