|
1 | | -import { watchDevframeTheme } from 'devframe/client' |
| 1 | +import type { DevframeBranding } from '../packages/hub-ui/src/types' |
| 2 | +import { setupDevframeConnection } from 'devframe/client' |
2 | 3 |
|
3 | | -// Match the reference dock's primary ramp. Only accent tokens change; semantic |
4 | | -// success/error/warning colors and each SPA's chart palettes stay independent. |
5 | | -const primaryStops = { |
6 | | - DEFAULT: 0, |
7 | | - 50: 95, |
8 | | - 100: 90, |
9 | | - 200: 75, |
10 | | - 600: 9, |
11 | | - 500: 18, |
12 | | - 400: 34, |
13 | | - 300: 54, |
14 | | - 700: -8, |
15 | | - 800: -25, |
16 | | - 900: -45, |
17 | | - 950: -70, |
18 | | -} as const |
| 4 | +/** Apply startup branding to a built-in devframe's iframe SPA. */ |
| 5 | +export function applyPanelBranding(): () => void { |
| 6 | + if (window.parent === window) |
| 7 | + return () => {} |
19 | 8 |
|
20 | | -/** Opt a built-in devframe SPA into the hub UI provider's primary accent. */ |
21 | | -export function syncPanelTheme(): () => void { |
22 | | - const style = document.documentElement.style |
23 | | - const previous = Object.keys(primaryStops).map((stop) => { |
24 | | - const name = `--colors-primary-${stop}` |
25 | | - return { name, value: style.getPropertyValue(name), priority: style.getPropertyPriority(name) } |
26 | | - }) |
27 | | - function restore() { |
28 | | - for (const { name, value, priority } of previous) { |
29 | | - if (value) |
30 | | - style.setProperty(name, value, priority) |
31 | | - else |
32 | | - style.removeProperty(name) |
33 | | - } |
34 | | - } |
35 | | - const stop = watchDevframeTheme(({ primaryColor }) => { |
36 | | - if (!primaryColor || !CSS.supports('color', primaryColor)) { |
37 | | - restore() |
| 9 | + const root = document.documentElement |
| 10 | + const previous = root.style.getPropertyValue('--devframe-primary') |
| 11 | + const priority = root.style.getPropertyPriority('--devframe-primary') |
| 12 | + let disposed = false |
| 13 | + let applied = false |
| 14 | + |
| 15 | + void setupDevframeConnection().then(({ connectionMeta }) => { |
| 16 | + const configs = connectionMeta.configs as { ui?: { branding?: DevframeBranding } } | undefined |
| 17 | + const color = configs?.ui?.branding?.primaryColor |
| 18 | + if (disposed || !color || !CSS.supports('color', color)) |
38 | 19 | return |
39 | | - } |
40 | | - for (const [stop, white] of Object.entries(primaryStops)) { |
41 | | - style.setProperty(`--colors-primary-${stop}`, white |
42 | | - ? `color-mix(in oklab, ${primaryColor}, ${white > 0 ? 'white' : 'black'} ${Math.abs(white)}%)` |
43 | | - : primaryColor) |
44 | | - } |
| 20 | + root.style.setProperty('--devframe-primary', color) |
| 21 | + applied = true |
| 22 | + }).catch(() => { |
| 23 | + // Connection failures leave the SPA's default palette intact. |
45 | 24 | }) |
| 25 | + |
46 | 26 | return () => { |
47 | | - stop() |
48 | | - restore() |
| 27 | + disposed = true |
| 28 | + if (!applied) |
| 29 | + return |
| 30 | + if (previous) |
| 31 | + root.style.setProperty('--devframe-primary', previous, priority) |
| 32 | + else |
| 33 | + root.style.removeProperty('--devframe-primary') |
49 | 34 | } |
50 | 35 | } |
0 commit comments