Theme: admin and site colours follow Site design, rendered server-side

- Admin primary/secondary now follow the site's Primary/Accent colours
  (owner's request), replacing the fixed admin sage. Text on them is picked
  automatically (white or dark, whichever contrasts more), which keeps the
  earlier low-contrast problem from coming back with light brand colours;
  brand colour is no longer used as text (active nav item, placeholders).
- New $shared/theme renders the colours as a <style> in <head> during
  SSR. The public site used to apply them in onMount, so every page load
  flashed the default colours first.
- DaisyUI `secondary` now maps to the site Accent everywhere — the
  button block's "Secondary" variant was DaisyUI's default pink.
- Colours are admin-entered text placed inside <style>, so only plain
  colour values are accepted (hex, named, rgb()/hsl()/oklch()…); anything
  else falls back to the default.
- Saving colours in Site design refreshes the admin theme immediately.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
agent 2026-10-06 11:32:21 +00:00
parent 015ce2a63f
commit 3399daf951
7 changed files with 98 additions and 40 deletions

View file

@ -4,6 +4,7 @@
import { afterNavigate } from '$app/navigation';
import { page } from '$app/state';
import { createBasketStore, setBasketContext } from '$lib/basket.svelte';
import { brandThemeVars, rootStyleTag } from '$shared/theme';
let { children, data } = $props();
@ -12,6 +13,9 @@
const BARE_ROUTES = ['/login', '/preview'];
let bare = $derived(BARE_ROUTES.includes(page.url.pathname));
// The active item is marked with a tint + bar in the brand colour, never
// brand-coloured text: the site's primary may be too light to read as text.
// Sidebar navigation, grouped by what the studio is doing. Labels are
// user-facing names; hrefs keep the original route names.
const navGroups = [
@ -55,6 +59,8 @@
<svelte:head>
<link rel="icon" href={favicon} />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Admin buttons/highlights use the site's Primary and Accent colours (sanitised; readable text picked automatically). -->
{@html rootStyleTag(brandThemeVars(data.colors))}
</svelte:head>
{#if bare}
@ -86,7 +92,7 @@
<li class="menu-title mt-3 first:mt-0">{group.title}</li>
{#each group.links as link (link.href)}
<li>
<a href={link.href} class={isActive(link.href) ? 'bg-primary/10 font-semibold text-primary' : ''} aria-current={isActive(link.href) ? 'page' : undefined}>{link.label}</a>
<a href={link.href} class={isActive(link.href) ? 'bg-primary/20 font-semibold shadow-[inset_3px_0_0_var(--color-primary)]' : ''} aria-current={isActive(link.href) ? 'page' : undefined}>{link.label}</a>
</li>
{/each}
{/each}

View file

@ -128,7 +128,7 @@
<div class="grid gap-2 sm:grid-cols-2">
{#each emailPlaceholders as placeholder}
<button type="button" class="btn btn-ghost btn-sm justify-start font-normal" onclick={() => copyPlaceholder(placeholder.key)}>
<code class="text-primary">{`{{${placeholder.key}}}`}</code>
<code class="font-semibold">{`{{${placeholder.key}}}`}</code>
<span class="text-base-content/60">{copied === placeholder.key ? 'Copied!' : placeholder.description}</span>
</button>
{/each}
@ -142,7 +142,7 @@
<div class="rounded-box bg-base-100 p-4 shadow">
<div class="dropdown w-full">
<button class="btn btn-primary w-full" type="button" tabindex="0">Add block</button>
<ul class="menu dropdown-content z-10 mt-2 w-full rounded-box bg-base-100 p-2 shadow">
<ul class="menu dropdown-content z-20 mt-2 w-full rounded-box bg-base-100 p-2 shadow-lg">
{#each emailBlockTypes as type}
<li><button type="button" onclick={() => addBlock(type)}>{emailBlockLabels[type]}</button></li>
{/each}

View file

@ -37,7 +37,8 @@
const submit: SubmitFunction = ({ formData }) => {
saving = String(formData.get('section'));
return async ({ update }) => {
await update({ reset: false, invalidateAll: false });
// Re-run loads so the admin theme picks up saved colours immediately.
await update({ reset: false });
saving = '';
};
};

View file

@ -2,25 +2,13 @@
import { onMount } from 'svelte';
import BlockRenderer from '$shared/blocks/BlockRenderer.svelte';
import type { ContentBlock } from '$lib/types';
import { rootStyleTag, siteThemeVars } from '$shared/theme';
let { data } = $props();
let blocks = $state<ContentBlock[]>([]);
let error = $state('');
onMount(() => {
// Apply the site's colors so the preview matches the public site. Only
// here: these variable names overlap DaisyUI's theme (--color-primary,
// --color-accent), so setting them in the admin shell restyled the admin.
if (data.colors) {
const root = document.documentElement;
root.style.setProperty('--color-bg', data.colors.background);
root.style.setProperty('--color-heading', data.colors.heading);
root.style.setProperty('--color-subheading', data.colors.subheading);
root.style.setProperty('--color-text', data.colors.text);
root.style.setProperty('--color-primary', data.colors.primary);
root.style.setProperty('--color-accent', data.colors.accent);
}
const raw = sessionStorage.getItem('cms-preview-blocks');
if (!raw) return;
try {
@ -34,6 +22,8 @@
<svelte:head>
<title>Preview</title>
<!-- Full site colours so the draft looks like the public site. -->
{@html rootStyleTag(siteThemeVars(data.colors))}
</svelte:head>
{#if blocks.length}