diff --git a/CLAUDE.md b/CLAUDE.md index a3e7a9d..f9c3db2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -66,7 +66,7 @@ In dev, Vite proxies `/api` to the target set in `vite.config.ts`, so the API mu - **Imports need explicit extensions** (`.ts`, `.tsx`, `.js`) everywhere. - **Vite resolves `.js` before `.ts`**, so a `.js` and `.ts` file with the same base name will import the wrong one. Give new hooks distinct names. - **Don't use `fallback: EMPTY` in api.ts hooks.** It silently returns empty arrays and hides server errors; let the error state surface. -- **Colours are tokens, not hex.** Use the utilities from `src/index.css` (`text-ink`, `text-body`, `text-muted`, `text-link`, `bg-surface`, `bg-tint`, `bg-action`, `border-brand/20`...) so dark mode works; a literal only for something that must look the same in both modes (white on a dark band, a platform's colour). Data colours (accents, event and region colours) go through `withAlpha`, `accentText` and `readableOn` in `src/lib/color.ts`, never `${color}55`. +- **Colours are tokens, not hex.** Admins can override any token (Panel → Site settings → Colours); the defaults live in three places that must agree: `src/index.css`, `THEME_DEFAULTS` in `server/src/theme.js`, `THEME_COLORS` in `src/lib/adminSchema/siteSettings.ts`. Use the utilities from `src/index.css` (`text-ink`, `text-body`, `text-muted`, `text-link`, `bg-surface`, `bg-tint`, `bg-action`, `border-brand/20`...) so dark mode works; a literal only for something that must look the same in both modes (white on a dark band, a platform's colour). Data colours (accents, event and region colours) go through `withAlpha`, `accentText` and `readableOn` in `src/lib/color.ts`, never `${color}55`. - **No em-dashes** (U+2014) anywhere: UI text, help text, comments, docs, commit messages. Use a colon, comma or parentheses. An empty table cell shows `-`. - **Admin editor text:** blank select options read `Default: …` (what blank means), examples in help text start with `eg.`, and record ids are called "URL ID". Cut-off preview text uses `preview()` from `src/lib/text.ts`, which ends it with an ellipsis. See `docs/updating-data-in-code.md`. @@ -74,6 +74,7 @@ In dev, Vite proxies `/api` to the target set in `vite.config.ts`, so the API mu Descriptor-driven: `server/src/admin-crud.js` and `server/src/admin-schema.js` (server) and `src/lib/adminSchema/` (client, one file per entity, registered in `adminSchema.ts`) generate SQL and form fields from declarative entity configs. Adding an entity should mean adding a descriptor, not new CRUD code. - Child collections are deleted and reinserted wholesale. Unsafe for entities referenced by foreign keys elsewhere. - `reindex: false` prevents cross-entity sort order collisions. +- `check: (db, id) => void` runs after a save has written everything, inside its transaction; throwing an `HttpError` refuses the save (Site settings uses it for the colour contrast check, `server/src/theme.js`). - The `OMIT` sentinel distinguishes unsent fields from deliberate clears. - `admin-schema-sync.js` runs at boot and throws if descriptors don't match live `PRAGMA table_info`. If boot fails after a schema change, update the descriptor or migration so they agree. - `admin-cli.js` imports `ROLES` and `destroyAllSessionsFor` from `auth.js`. Keep it that way to prevent drift. diff --git a/docs/updating-data-in-code.md b/docs/updating-data-in-code.md index 881389f..b66302b 100644 --- a/docs/updating-data-in-code.md +++ b/docs/updating-data-in-code.md @@ -274,7 +274,7 @@ The numbers band's "Number Source" dropdown works the same way: | History page decade headers | `src/data/historyDecades.ts` | Title, tagline and blurb per decade. | | Chapter map tiles and area names | `src/data/mapGrid.ts` | `AREAS` (tile positions) and `AREA_NAMES`. Which region paints which tile is in the database (Admin → Data → Organizations → a region → Map areas). | | Feedback form types | `src/data/feedbackTypes.ts` **and** `TYPES` in `server/src/routes/feedback.js` | Keep the ids identical in both; the server stores unknown ids as `general`. Labels and hints are client-only. `DATA_REQUEST` ('privacy') isn't a tile: the Privacy page's form sends it, and the server requires an email for it. | -| The site's colours, light and dark | `src/index.css` (`@theme` tokens and the dark overrides under them) | Change a token's value in both modes and every use follows. Use the token utilities (`text-muted`, `bg-surface`, `border-brand/20`) or `var(--color-…)`, not hex. A colour that arrives as data (a page accent, an event's or region's colour) goes through `src/lib/color.ts`: `withAlpha` for tints, `accentText` for text in it, `readableOn` for text on it. | +| The site's colours, light and dark | the database over `src/index.css` | Admin → Panel → Site settings → Colours (superadmins): pick a colour, set its light and/or dark value; saving refuses anything that makes text hard to read. The defaults are the `@theme` tokens and dark block in `src/index.css`, and must stay the same in `THEME_DEFAULTS` (`server/src/theme.js`) and `THEME_COLORS` (`src/lib/adminSchema/siteSettings.ts`). Change a token's value in both modes and every use follows. Use the token utilities (`text-muted`, `bg-surface`, `border-brand/20`) or `var(--color-…)`, not hex. A colour that arrives as data (a page accent, an event's or region's colour) goes through `src/lib/color.ts`: `withAlpha` for tints, `accentText` for text in it, `readableOn` for text on it. | | Privacy Policy and Terms wording | `src/pages/Privacy.tsx`, `src/pages/Terms.tsx` | The `PARTS` list, and `LAST_UPDATED` whenever the wording changes. The Privacy page must stay true of the code: what the feedback form stores, what the browser keeps, which embeds wait for a click. | | Event type labels and plurals (public site) | `src/lib/eventTypes.ts` | Also the order of the type filter chips. | | Event and person role labels (public site) | `ROLE_LABEL` in `src/pages/EventDetail.tsx` and `src/pages/PersonDetail.tsx` | | diff --git a/server/src/admin-crud.js b/server/src/admin-crud.js index a56f75d..19220c1 100644 --- a/server/src/admin-crud.js +++ b/server/src/admin-crud.js @@ -316,6 +316,7 @@ export function createRow(db, entity, payload) { writeExtensions(db, entity, newId, payload, values); writeChildren(db, entity, newId, payload, values); + entity.check?.(db, newId); }), ); @@ -365,6 +366,9 @@ export function updateRow(db, entity, rawId, payload) { writeExtensions(db, entity, id, payload, merged); writeChildren(db, entity, id, payload, merged); + // The descriptor's own rule over the saved result, children + // included. Throwing rolls the whole save back. + entity.check?.(db, id); }), ); diff --git a/server/src/admin-schema.js b/server/src/admin-schema.js index b25e2aa..92d63be 100644 --- a/server/src/admin-schema.js +++ b/server/src/admin-schema.js @@ -15,6 +15,9 @@ children ordered collections, replaced wholesale on save singleton the one id this entity ever has; the engine refuses create and delete (see front_page) + check (db, id) => void, run after a save has written + everything, inside its transaction: throw an + HttpError to refuse it (site settings' colours) Replacing children wholesale is only safe because nothing has a foreign key INTO these tables. That is the dividing line, and @@ -35,6 +38,8 @@ what the other side owns rather than blanking it. ═══════════════════════════════════════════════════════════════ */ +import { THEME_TOKENS, checkTheme } from "./theme.js"; + /* ── Column helpers ──────────────────────────────────────────── */ const text = (name, opts = {}) => ({ name, type: "text", ...opts }); @@ -945,7 +950,21 @@ const siteSettings = { order: "sort_order", columns: [text("path", { required: true }), text("title"), text("description")], }, + { + key: "colors", + table: "site_setting_colors", + owner: { column: "settings_id" }, + order: "sort_order", + columns: [ + enumeration("token", THEME_TOKENS, { required: true }), + text("light"), + text("dark"), + ], + }, ], + + // Refuses a palette that would make text hard to read (theme.js). + check: checkTheme, }; export const ENTITIES = { diff --git a/server/src/migrations/041_site_colors.sql b/server/src/migrations/041_site_colors.sql new file mode 100644 index 0000000..30cc48f --- /dev/null +++ b/server/src/migrations/041_site_colors.sql @@ -0,0 +1,28 @@ +-- ═══════════════════════════════════════════════════════════════ +-- 041 SITE COLOURS +-- +-- The admin's palette (Panel → Site settings → Colours): one row per +-- colour token that differs from the site's own (src/index.css), with +-- a light and a dark value. Blank keeps that mode's default. The +-- server writes them into every page (server/src/theme.js) and +-- refuses a save that would make text hard to read. +-- +-- token one of the tokens in THEME_DEFAULTS (theme.js), which +-- the admin's dropdown offers +-- light '#rrggbb', or null for the default +-- dark '#rrggbb', or null for the default +-- ═══════════════════════════════════════════════════════════════ + +CREATE TABLE site_setting_colors ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + settings_id TEXT NOT NULL REFERENCES site_settings (id) ON DELETE CASCADE, + sort_order INTEGER NOT NULL DEFAULT 0, + token TEXT NOT NULL CHECK (token IN ( + 'ink', 'body', 'muted', 'link', 'brand', 'action', 'action-hover', + 'surface', 'tint', 'tint-soft', 'danger', 'danger-tint')), + light TEXT CONSTRAINT "A colour must be written #rrggbb, eg. #0f7183." + CHECK (light IS NULL OR (length(light) = 7 AND light GLOB '#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]')), + dark TEXT CONSTRAINT "A dark colour must be written #rrggbb, eg. #5ccbdc." + CHECK (dark IS NULL OR (length(dark) = 7 AND dark GLOB '#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]')), + UNIQUE (settings_id, token) +) STRICT; diff --git a/server/src/pageMeta.js b/server/src/pageMeta.js index d0a21c8..3c167af 100644 --- a/server/src/pageMeta.js +++ b/server/src/pageMeta.js @@ -42,6 +42,7 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; import { loadBlocks, paragraphs } from "./shape.js"; +import { themeCss } from "./theme.js"; const here = path.dirname(fileURLToPath(import.meta.url)); @@ -103,6 +104,9 @@ export function siteFor(db) { index: row.indexing ? row.indexing === "on" : base.robots?.index !== false, }, privacyEmail: row.privacy_email || base.privacyEmail || "", + // The admin's colours (theme.js): only #rrggbb values and fixed + // text, so safe inside `, ].filter(Boolean); // A blocked site (Site settings indexing off, or site.json's diff --git a/server/src/theme.js b/server/src/theme.js new file mode 100644 index 0000000..789be80 --- /dev/null +++ b/server/src/theme.js @@ -0,0 +1,135 @@ +/* ═══════════════════════════════════════════════════════════════ + THEME + + The admin's palette (Panel → Site settings → Colours, table + site_setting_colors) over the site's own colour tokens. + + THEME_DEFAULTS must match @theme and the dark block in + src/index.css: the same tokens, the same light and dark values. + A blank override keeps the default. + + themeCss() is written into every page's
(pageMeta.js). + It goes into the same cascade layers as index.css (theme for + light, base for dark), after it, so an override replaces exactly + the value it names: a light-only change leaves dark mode alone. + + checkTheme() runs when Site settings is saved (the descriptor's + `check`, inside the save's transaction), and refuses a palette in + which any text would fall under WCAG AA (4.5:1) in either mode, so + an admin can't make the site unreadable. + ═══════════════════════════════════════════════════════════════ */ + +import { HttpError } from "./admin-crud.js"; + +// light dark +export const THEME_DEFAULTS = { + ink: ["#073d4a", "#eaf6f8"], + body: ["#26454c", "#cde0e4"], + muted: ["#4a6b72", "#9fb9bf"], + link: ["#0f7183", "#5ccbdc"], + brand: ["#138ba0", "#2aa7bc"], + action: ["#0f7183", "#0f7183"], + "action-hover": ["#0b5c6a", "#0b5c6a"], + surface: ["#ffffff", "#0c1d21"], + tint: ["#eef9fb", "#11292e"], + "tint-soft": ["#f6fbfc", "#0f2428"], + danger: ["#b3261e", "#ff8a80"], + "danger-tint": ["#fdf3f2", "#3b1714"], +}; + +export const THEME_TOKENS = Object.keys(THEME_DEFAULTS); + +const HEX = /^#[0-9a-f]{6}$/i; +const MODES = ["light", "dark"]; + +function overrides(db) { + const rows = db + .prepare(`SELECT token, light, dark FROM site_setting_colors WHERE settings_id = 'site'`) + .all(); + // Checked again here, not only by the table's CHECK: this goes + // straight into a