@softure-ai/theme-kit
v0.1.10
Published
SOFTURE AI theme kit: the theme contract (defineTheme), the theme CSS generator and build command, cn and the scoped preflight every shadcn/ui theme package shares.
Readme
@softure-ai/theme-kit
The standard every SOFTURE theme package is built on. A theme is plain shadcn/ui (Radix, Tailwind CSS v4, lucide
icons) with its own tokens and component deltas; the kit holds everything that does not change from theme to theme:
the token contract, the way a theme is declared, the CSS generator, the build command and the shared helpers. A new
theme is a new themes/theme-<name> folder that follows the contract below; nothing in the kit changes.
Apps install a theme package (e.g. @softure-ai/theme-notion), which depends on the kit.
An app imports the kit directly only for useIsMobile, the app icon (/next) or to build its own variant of a theme. Its stylesheets
(preflight.css, base.css, maplibre.css) reach the app through the theme: every theme re-exports them under its
own name (@softure-ai/theme-notion/base.css), so the app imports CSS only from the package it declared.
Design
ThemeDefinition (data) ──defineTheme──▶ theme (checked) ──buildThemeCss──▶ tokens CSS
▲ │
extendTheme(base, overrides) └──softure-theme-css + softureTheme──▶ dist/theme.css- One contract.
ThemeDefinitionsays what a theme decides: colours (light and dark, or an alias of another colour), shadows, fonts, named text styles, radii, layout sizes, whether Tailwind's default palette is cleared, and optionally its list densities.SHADCN_COLOR_ROLESare the colour roles shadcn/ui components read; every theme defines them all, so a component written for one theme renders in any other. A theme adds its own tokens on top (green-tag,primary-hover). - Declared and checked in one place.
defineTheme(definition)checks the theme where it is written and throws with every problem listed: a missing colour role, a name that is not kebab-case, an empty value or one that could break out of the CSS, an alias of a colour the theme does not define.checkThemereturns the same problems as a list. The literal types stay, sotheme.colors.primaryis known to the compiler. - Built the same way. A theme package has no build script of its own. It declares its stylesheet as data
(
softureThemeinpackage.json) and its build runs the kit'ssofture-theme-css, which writesdist/theme.cssfrom the package'stheme, copies its extra stylesheets and re-exports the kit's. - Variants, not copies.
extendTheme(base, overrides)gives a theme that differs from another in a few tokens: each token group of the overrides is laid over the base's (a token is replaced whole), the result is checked. - Checked in the repository.
tests/repo/themes.test.tsruns the contract over everythemes/theme-*package, so a new theme is covered by being created.
What it holds
| Export | What it does |
| --- | --- |
| ThemeDefinition, SHADCN_COLOR_ROLES, findMissingColorRoles | The token contract |
| DENSITIES, Density, DensityScale, DEFAULT_DENSITY, LIST_DENSITY_VARIABLES | The list densities, compact or roomy, picked with data-density (below) |
| defineTheme(definition) | Declares a theme: checks it, returns it with its literal types, throws with every problem |
| checkTheme(definition) | The contract problems as messages; empty when the theme is complete |
| extendTheme(base, overrides) | A checked variant of a theme: a new name, per group only the tokens that change |
| buildThemeCss(definition) | The theme's tokens as CSS for Tailwind CSS v4 (below) |
| getThemeColor(theme, name, scheme) | A colour's value in one scheme (light by default), aliases followed, for code drawing outside the page |
| createCn(options), createThemeCn(definition) | shadcn/ui's cn, with tailwind-merge told the theme's named text styles, shadows and sizes (text-body and text-muted-foreground both stay; a later size-touch-target replaces size-7) |
| useIsMobile(), MOBILE_BREAKPOINT (/react) | The sidebar's breakpoint (768 px), on useSyncExternalStore; the server renders the desktop layout |
| drawAppIcon(options), APP_ICON_SIZE, APPLE_ICON_SIZE, MANIFEST_ICON_SIZE, MANIFEST_LARGE_ICON_SIZE (/next) | The app's tab, home screen and web app manifest icons in the theme's colours, as a next/og image (below) |
| preflight.css | Tailwind's preflight scoped to [data-slot] elements, in @layer base with zero specificity, plus prefers-reduced-motion |
| base.css | The page base: html with text-size-adjust: 100%, as Tailwind's full preflight sets it, so mobile Safari does not enlarge the text of a block wider than the viewport (a table that scrolls sideways); body without the browser's margin, on the theme's background, foreground and sans font (Radix portals on body inherit it); a border without a colour class in the theme's border and outlines in a half-transparent ring, as shadcn/ui's globals.css does, where Tailwind's preflight leaves currentColor; all in @layer base |
| maplibre.css | The theme's sans font on MapLibre maps (body .maplibregl-map, unlayered, so it beats maplibre-gl.css in any load order) |
| softure-theme-css [packageDir] | Builds a theme package's stylesheets after tsc (the contract below) |
| /build: parseThemePackageConfig, buildThemeStylesheet, writeThemeStylesheets, buildThemePackage | The code behind the command, for tests and tools (Node only) |
| /build: KIT_STYLESHEETS, buildKitStylesheetReexport(name) | The kit stylesheets every theme re-exports, and the text of a theme's re-export |
base.css and maplibre.css read the font with --theme(), so they are imported through the app's Tailwind, after
the theme's theme.css. An app imports them from its theme (@softure-ai/theme-<name>/base.css). They read only contract tokens, so they work with every theme.
App icon
@softure-ai/theme-kit/next draws the app's icon from any theme's tokens, so apps on one domain get tabs that read
apart without theme code of their own. next (16) is an optional peer, needed only for this subpath.
// app/icon.tsx
import { APP_ICON_SIZE, drawAppIcon } from "@softure-ai/theme-kit/next";
import { theme } from "@softure-ai/theme-notion";
import { Wallet } from "lucide";
export const size = APP_ICON_SIZE;
export const contentType = "image/png";
export default function Icon() {
return drawAppIcon({ theme, icon: Wallet });
}// app/apple-icon.tsx: full bleed, iOS rounds it itself
import { APPLE_ICON_SIZE, drawAppIcon } from "@softure-ai/theme-kit/next";
import { theme } from "@softure-ai/theme-notion";
import { Wallet } from "lucide";
export const size = APPLE_ICON_SIZE;
export const contentType = "image/png";
export default function AppleIcon() {
return drawAppIcon({ theme, icon: Wallet, size: APPLE_ICON_SIZE, shape: "square" });
}| Option | Default | What it does |
| --- | --- | --- |
| theme | | Any theme that meets the contract; its colours are read as values, aliases followed |
| icon | | The glyph: a 24 × 24 stroke icon as data (IconNode), or { svg }, one standalone <svg> drawn in currentColor |
| size | APP_ICON_SIZE (32 × 32) | The image size; pass the same constant as the file's size export |
| tone | primary | The pair of contract roles: primary, secondary, accent (each on its -foreground), inverse (background on foreground) |
| scheme | light | The scheme the colours come from: a favicon is one image for both |
| shape | rounded | rounded (corners at 22 % of the side), square (full bleed) or maskable (full bleed, the glyph inside the safe zone) |
| glyphScale | 0.625; 0.5 for maskable | The glyph's side as a share of the icon's side, above 0 and at most 1; for maskable at most MASKABLE_SAFE_ZONE / √2 (≈ 0.566), so its corners stay inside the safe zone circle |
| strokeWidth | 2 (lucide's) | The stroke of an icon-data glyph in its 24-unit grid, e.g. a theme's own icon stroke plus 0.5 for a heavier line at tab size; { svg } markup draws its own stroke and refuses it |
An option out of range throws, naming the value and the limit: it is a bug in the icon file, found at build time.
Manifest icons
An installable app lists its icons in app/manifest.ts: 192 and 512 for launchers, and a maskable 512, which
Android crops to a circle or squircle. A maskable icon has no corners of its own and keeps the glyph inside the
centred circle of 80 % of its side (MASKABLE_SAFE_ZONE). These are not Next metadata files, so one route handler
serves them; drawAppIcon returns a Response.
// app/manifest.ts
import type { MetadataRoute } from "next";
export default function manifest(): MetadataRoute.Manifest {
return {
name: "Example",
start_url: "/",
display: "standalone",
icons: [
{ src: "/icons/192", sizes: "192x192", type: "image/png" },
{ src: "/icons/512", sizes: "512x512", type: "image/png" },
{ src: "/icons/maskable", sizes: "512x512", type: "image/png", purpose: "maskable" },
],
};
}// app/icons/[variant]/route.ts
import { drawAppIcon, MANIFEST_ICON_SIZE, MANIFEST_LARGE_ICON_SIZE, type AppIconOptions } from "@softure-ai/theme-kit/next";
import { theme } from "@softure-ai/theme-notion";
import { Wallet } from "lucide";
const VARIANTS = {
"192": { size: MANIFEST_ICON_SIZE, shape: "square" },
"512": { size: MANIFEST_LARGE_ICON_SIZE, shape: "square" },
maskable: { size: MANIFEST_LARGE_ICON_SIZE, shape: "maskable" },
} satisfies Record<string, Pick<AppIconOptions, "size" | "shape">>;
export const dynamicParams = false;
export function generateStaticParams() {
return Object.keys(VARIANTS).map((variant) => ({ variant }));
}
function isVariant(variant: string): variant is keyof typeof VARIANTS {
return Object.hasOwn(VARIANTS, variant);
}
export async function GET(_request: Request, { params }: { params: Promise<{ variant: string }> }) {
const { variant } = await params;
if (!isVariant(variant)) return new Response(null, { status: 404 });
return drawAppIcon({ theme, icon: Wallet, ...VARIANTS[variant] });
}The glyph is data, not a component. A metadata route renders on the server, and lucide-react's icons are client
modules that call useContext: next/og runs no hooks, and Next refuses react-dom/server in app code. lucide's
framework-free package, lucide, exports the same icons as data (import { Wallet } from "lucide"), with the same
names as lucide-react; add it next to lucide-react. Another icon set goes in as its SVG markup ({ svg: "<svg …>" }).
buildAppIconElement(options) returns the element without drawing it, and renderIconSvg(icon, color) the glyph's
markup, for an image of the app's own.
The theme package contract
Every themes/theme-<name> package (named @softure-ai/theme-<name> by its folder):
src/tokens.tsexportstheme, declared withdefineTheme. It may also export a named alias (NOTION_THEME).src/index.tsexportstheme,cn(createThemeCn(theme)) and the components. The same names in every theme, so an app moves to another theme by changing the import path.package.json:depends on
@softure-ai/theme-kit; takesreactandtailwindcss^4.0.0as peers;"build": "tsc -p tsconfig.build.json && softure-theme-css";exports
./theme.css(./dist/theme.css), each extra stylesheet (./<name>.css→./dist/<name>.css) and the kit's stylesheets (./preflight.css,./base.css,./maplibre.css→./dist/<name>.css);declares the stylesheet:
"softureTheme": { "sources": ["./ui", "./notion"], "imports": ["tw-animate-css"], "stylesheets": ["sft-bridge.css"] }sources: the component folders undersrc/(anddist/), written intotheme.cssas@source, so the app's Tailwind generates their classes insidenode_modules.imports: CSS packages the components need, imported first.stylesheets: plain CSS files insrc/copied todist/as they are; a kit stylesheet's name is refused. Each defaults to none. The command also writesdist/preflight.css,dist/base.cssanddist/maplibre.css, each one line,@import "@softure-ai/theme-kit/<name>.css";: a bundler resolves it from the theme's folder, so the app gets the kit version the theme depends on even when the kit is not hoisted (pnpm, a nested second version).
A component file that creates a React context or calls a hook starts with
"use client"(as does any source file that callscreateContext), so the package root stays importable from a Server Component. Plain.tsmodules that only define a hook (useIsMobile) need no directive. A"use client"module exports only components and hooks: in a Server Component its exports are client references, so a helper or a constant from it throws when called or read. Helpers live in a plain module (theme-notion'sformatFileSize).
theme.css is then @import lines, @source lines and the generated tokens, in that order.
The generated CSS
buildThemeCss writes, in this order:
@custom-variant dark: Tailwind'sdark:follows the same rule as the tokens below.:root, .light, [data-theme="light"]: the light values.@media (prefers-color-scheme: dark)→:root:not(.light, [data-theme="light"]): the dark values by the system setting, unless the page asks for light..dark, [data-theme="dark"]: the dark values by choice. Works withnext-themes(attribute="class") and with@softure-ai/ui'sThemeScript/ThemeSwitch(data-themeon<html>); a section can take a fixed scheme.:root, [data-density="compact"]and[data-density="roomy"]: the list densities' lengths (below).@theme inline:--color-<name>,--font-*,--text-<style>(size, line height, weight, tracking),--radius-*,--shadow-*and the sizes as--spacing-*, sobg-primary,text-page-title,rounded-xl,shadow-menuandw-sidebar-widthexist. WithclearDefaultPalette,--color-*: initialfirst.
It throws on a name that is not kebab-case, a value holding ;, {, }, <, > or a line break, and an alias of
a colour the theme does not define.
List density
A list's rows are either compact (tight rows, no line between them: the default) or roomy (a quiet line between
rows and more room around each). An app picks the density with an attribute, on <html> for the whole app (a user
setting), or on a page, a section or one panel; the nearest attribute wins, so a compact panel can sit in a roomy page:
<html data-density="roomy"> {/* or <section data-density="roomy"> */}The theme's CSS carries each density as three inherited variables, which a theme's list components read:
| Variable | What it is | compact | roomy |
| --- | --- | --- | --- |
| --list-row-y | a row's vertical padding | 0.25rem | 0.5rem |
| --list-item-y | the room between a row and the lines around it, on the list item | 0px | 0.25rem |
| --list-divider-width | the line between two rows (0px: none) | 0px | 1px |
The values are the kit's DEFAULT_DENSITY; a theme sets its own with density in its definition
({ compact: DensityScale, roomy: DensityScale }, checked like any other token). DENSITIES and the Density type
name the densities for an app's own setting, LIST_DENSITY_VARIABLES the variables. Every theme gets the variables
through the kit; a theme's components decide which of its lists follow them (@softure-ai/theme-notion: List,
ListItem, ListRow).
Overriding in an app
A few tokens: redefine the CSS variables after the theme's
theme.css, for both schemes:@import "@softure-ai/theme-notion/theme.css"; :root { --primary: #e16259; } .dark, [data-theme="dark"] { --primary: #df5452; }A variant of its own: build it from the theme and write the CSS in the app's build, then import the app's file instead of the theme's tokens:
import { writeFileSync } from "node:fs"; import { buildThemeCss, extendTheme } from "@softure-ai/theme-kit"; import { theme } from "@softure-ai/theme-notion"; const brand = extendTheme(theme, { name: "Brand", colors: { primary: { light: "#e16259", dark: "#df5452" } } }); writeFileSync("app/brand-theme.css", buildThemeCss(brand));cnstays the theme's while the variant adds no text styles, shadows or sizes; otherwise usecreateThemeCn(brand).
Adding a theme
- Create
themes/theme-<name>/with the workspace package shape (templates/package/forpackage.json, thetsconfigfiles andCHANGELOG.md) and the contract above. src/tokens.ts:export const theme = defineTheme({ name: "<Name>", colors: { … }, … }). Every shadcn colour role is required;defineThemenames the missing ones. A theme close to an existing one can beextendTheme(otherTheme, { … }). Keep a snapshot of the design system's tokens intests/fixtures/and a test that the definition equals it.src/lib/utils.ts:export const cn = createThemeCn(theme).src/ui/: the shadcn/ui components with the theme's deltas (sizes, radii, shadows, variants), listed in the README; keep shadcn's file names and formatting, so a laternpx shadcn@latest adddiff stays readable. Add compositions in a folder of their own and name every folder insoftureTheme.sources.- Run
npm test:tests/repo/themes.test.tschecks the package against the contract.
