Files
loki/scripts/generate-widget-theme.ts
T
CatoandClaude Opus 5.5 9d5d0c4694 feat(widget): Loki's own look, in Loki's light or dark mode, with one scroll (#1227)
* feat(widget): Loki's own look, in Loki's light or dark mode, with one scroll

The site widget took its colours from the fleet's shared tokens, whose
accent is OrangeCat's warm orange, and was dark only: on a client's light
site the owner saw an orange-ringed black panel and asked why Loki's widget
looked like OrangeCat.

- Themes are generated from Loki's own globals.css, light and dark. The
  accent is the ink; the warm colour is only the focus ring and a picked
  element's outline on the host page.
- Every widget stylesheet reads var(--lk-*), defined for both modes on the
  shadow host; widget/theme-mode.ts picks one. The owner gets the mode they
  chose in Loki (user_preferences.theme, saved by ThemeSync via
  PUT /api/me/theme, returned with their changes and remembered per site);
  everyone else gets their device's setting, followed live.
- The panel is one column with one scroll area (the thread) between a fixed
  header and composer. The whole card used to scroll too, which hid the
  composer below a laptop's fold and nested scrolls on phones.
- The desktop card no longer greys out the site it invites you to use; the
  phone sheet opens at two thirds of the screen with a lighter scrim.
- About is a segmented control that fits a 320px phone on one line.
- The walkthrough card follows the same mode; its ring uses the highlight
  so it reads on light and dark sites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LMe5Uv6mfWJibAiCaNbbk

* fix(widget): the launcher is findable — Loki's mark at full strength

On the owner's phone, signed out of the owner pass, AOZ showed a grey
pencil at 38% opacity and 85% size, and the owner said the widget was gone.
Without the launcher there is no way to the sign-in that brings owner mode
back. The resting launcher is now Loki's spiral at full strength with a
hairline and a soft lift off the page, on phones and desktop; it still
fades while the page scrolls.

Also: the user_theme migration is renumbered 0107 after main's 0106.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LMe5Uv6mfWJibAiCaNbbk

* fix(widget): keep the shared chat's thread bounded however chatkit nests it

CI's widget-kit-browser (needs @bitbaum/chatkit, so it only runs there)
failed one check: the chat thread no longer had a max-height once the panel
became a flex column. The flex layout bounds it only when the wrap is a
direct child, so the cap is back as a backstop, and the test now pins the
outcome — the thread scrolls and ends inside a card that fits the screen —
instead of the mechanism.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018LMe5Uv6mfWJibAiCaNbbk

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-15 10:40:11 -07:00

168 lines
6.5 KiB
TypeScript

/**
* The feedback widget's two themes — light and dark — derived from LOKI'S OWN
* design tokens (src/app/globals.css, `:root` and `.dark`).
*
* The widget floats on other people's sites and cannot read our CSS
* variables, so it needs the values themselves. They used to come from the
* fleet's shared @bitbaum/design-tokens, whose accent is the warm orange
* OrangeCat and the public pages spend on calls to action — so the widget
* wore orange labels, orange rings and orange buttons, and the owner asked why
* Loki's widget looked like OrangeCat (2026-10-15). Loki is monochrome: its
* accent is the ink (near-black on light, near-white on dark), and the warm
* colour is kept for the focus ring. globals.css is where that decision lives,
* so that is what this reads. A test regenerates and compares, so a token
* change that was not regenerated fails the build instead of shipping a stale
* widget.
*
* npx tsx scripts/generate-widget-theme.ts # write
* npx tsx scripts/generate-widget-theme.ts --check # exit 1 if stale
*
* Values are passed through as written (oklch()): every browser Loki itself
* supports reads them, and converting would only add a place to be wrong.
* Fonts and radii still come from the fleet tokens — Loki's own font is a
* next/font variable with no family name a foreign page could use.
*/
import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { createRequire } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const LOKI_CSS = join(here, "..", "src", "app", "globals.css");
const FLEET_TOKENS_CSS = require.resolve("@bitbaum/design-tokens/tokens.css");
const OUT = join(here, "..", "src", "lib", "widget-theme.generated.ts");
type Vars = Record<string, string>;
/** `:root { … }` and `.dark { … }` as flat maps of custom property → raw value. */
export function parseTokenBlocks(css: string): { root: Vars; dark: Vars } {
const block = (selector: string): Vars => {
const start = css.indexOf(`\n${selector} {`);
if (start === -1) throw new Error(`no "${selector} {" block`);
const end = css.indexOf("\n}", start + 1);
const body = css.slice(start, end).replace(/\/\*[\s\S]*?\*\//g, "");
const vars: Vars = {};
for (const m of body.matchAll(/--([a-z0-9-]+)\s*:\s*([^;]+);/g)) {
vars[m[1]] = m[2].trim().replace(/\s+/g, " ");
}
return vars;
};
return { root: block(":root"), dark: block(".dark") };
}
/** A colour the widget can use as written: a literal, never a var() it cannot resolve. */
function literal(key: string, value: string): string {
if (value.includes("var(")) throw new Error(`--${key} is "${value}": the widget needs a literal`);
return value;
}
function remToPx(rem: string): string {
const m = rem.trim().match(/^([\d.]+)rem$/);
if (!m) return rem.trim();
return `${Math.round(Number(m[1]) * 16)}px`;
}
/** One theme from one block, the light block filling what the dark one does not redefine. */
function themeFrom(own: Vars, root: Vars, fleet: Vars) {
const c = (key: string): string => {
const v = own[key] ?? root[key];
if (!v) throw new Error(`globals.css is missing --${key}`);
return literal(key, v);
};
const f = (key: string): string => {
const v = fleet[key];
if (!v) throw new Error(`design tokens are missing --${key}`);
return v;
};
return {
// Loki's accent is the ink: the Send fill, a selected chip, a live dot.
accent: c("accent-primary"),
accentHover: c("accent-hover"),
accentMuted: c("accent-muted"),
inkOnAccent: c("text-inverted"),
text: c("text-primary"),
textSecondary: c("text-secondary"),
textTertiary: c("text-tertiary"),
textMuted: c("text-muted"),
/** The panel itself, then a step up (cards, bubbles), then a step further (hover). */
surface: c("surface-base"),
surfaceRaised: c("surface-raised"),
surfaceSubtle: c("surface-overlay"),
border: c("border-subtle"),
borderStrong: c("border-default"),
borderDark: c("border-strong"),
success: c("status-positive"),
warning: c("status-warning"),
error: c("status-negative"),
errorSurface: c("status-negative-subtle"),
/** Loki's focus ring — the one place the warm colour appears. */
focus: c("ring"),
/** What a picked element on the HOST page is outlined in: it must read on any site. */
highlight: c("accent-warm"),
black: c("surface-public"),
white: "#ffffff",
radiusControl: remToPx(f("radius-control")),
radiusSurface: remToPx(f("radius-surface")),
fontSans: f("font-sans").replace(/\s+/g, " "),
fontMono: f("font-mono").replace(/\s+/g, " "),
} as const;
}
export function buildWidgetThemes(lokiCss: string, fleetCss: string) {
const { root, dark } = parseTokenBlocks(lokiCss);
const fleet = parseTokenBlocks(fleetCss).root;
return { light: themeFrom(root, root, fleet), dark: themeFrom(dark, root, fleet) };
}
export function renderModule(themes: ReturnType<typeof buildWidgetThemes>): string {
const block = (t: Record<string, string>) =>
Object.entries(t)
.map(([k, v]) => ` ${k}: ${JSON.stringify(v)},`)
.join("\n");
return `// GENERATED by scripts/generate-widget-theme.ts from src/app/globals.css
// — do not edit; change the tokens and regenerate. scripts/test/widget-theme-from-tokens.ts
// fails when this file is stale.
export const WIDGET_THEMES = {
light: {
${block(themes.light)}
},
dark: {
${block(themes.dark)}
},
} as const;
/** The dark theme — what a surface that is always dark (the preview gate) uses. */
export const WIDGET_THEME = WIDGET_THEMES.dark;
export type WidgetThemeShape = (typeof WIDGET_THEMES)["light" | "dark"];
`;
}
export function generate(): string {
return renderModule(
buildWidgetThemes(readFileSync(LOKI_CSS, "utf8"), readFileSync(FLEET_TOKENS_CSS, "utf8")),
);
}
const invokedDirectly =
process.argv[1] &&
fileURLToPath(import.meta.url) === require("node:path").resolve(process.argv[1]);
if (invokedDirectly) {
const next = generate();
const current = existsSync(OUT) ? readFileSync(OUT, "utf8") : "";
if (process.argv.includes("--check")) {
if (current !== next) {
console.error("widget theme is stale — run: npx tsx scripts/generate-widget-theme.ts");
process.exit(1);
}
console.log("widget theme matches src/app/globals.css");
} else if (current !== next) {
writeFileSync(OUT, next);
console.log(`wrote ${OUT}`);
} else {
console.log("widget theme unchanged");
}
}