@ada-cx/lovelace 1.21.4 → 1.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/components/AgentMessage/AgentMessage.css.js.map +1 -1
- package/dist/components/Button/Button.css.d.ts +3 -0
- package/dist/components/Button/Button.css.js +1 -0
- package/dist/components/Button/Button.css.js.map +1 -1
- package/dist/components/Chip/Chip.css.js +1 -0
- package/dist/components/Chip/Chip.css.js.map +1 -1
- package/dist/components/Chip/Chip.d.ts +2 -1
- package/dist/components/Chip/Chip.js.map +1 -1
- package/dist/components/IconButton/IconButton.css.d.ts +3 -0
- package/dist/components/IconButton/IconButton.css.js +1 -0
- package/dist/components/IconButton/IconButton.css.js.map +1 -1
- package/dist/components/Input/Input.css.d.ts +5 -4
- package/dist/components/Input/Input.css.js +1 -0
- package/dist/components/Input/Input.css.js.map +1 -1
- package/dist/components/MessageProse/MessageProse.css.d.ts +5 -6
- package/dist/components/MessageProse/MessageProse.css.js.map +1 -1
- package/dist/components/MessageRow/MessageRow.css.js.map +1 -1
- package/dist/components/PictureMessage/PictureMessage.d.ts +6 -1
- package/dist/components/PictureMessage/PictureMessage.js +10 -10
- package/dist/components/PictureMessage/PictureMessage.js.map +1 -1
- package/dist/components/ScrollMarker/ScrollMarker.css.d.ts +3 -0
- package/dist/components/ScrollMarker/ScrollMarker.css.js +1 -0
- package/dist/components/ScrollMarker/ScrollMarker.css.js.map +1 -1
- package/dist/components/SurveySelect/SurveySelect.d.ts +1 -1
- package/dist/components/SurveySelect/SurveySelect.js.map +1 -1
- package/dist/components/Toast/Toast.css.d.ts +6 -0
- package/dist/components/Toast/Toast.css.js +2 -2
- package/dist/components/Toast/Toast.css.js.map +1 -1
- package/dist/components/Toast/Toast.d.ts +2 -3
- package/dist/components/Toast/Toast.js.map +1 -1
- package/dist/components/Toast/ToastAnnouncer.d.ts +21 -0
- package/dist/components/Toast/ToastAnnouncer.js +104 -0
- package/dist/components/Toast/ToastAnnouncer.js.map +1 -0
- package/dist/components/Toast/ToastContainer.d.ts +11 -8
- package/dist/components/Toast/ToastContainer.js +46 -51
- package/dist/components/Toast/ToastContainer.js.map +1 -1
- package/dist/components/Toast/ToastItem.d.ts +7 -1
- package/dist/components/UserMessage/UserMessage.css.js.map +1 -1
- package/dist/components/VideoMessage/VideoMessage.d.ts +8 -1
- package/dist/components/VideoMessage/VideoMessage.js +9 -9
- package/dist/components/VideoMessage/VideoMessage.js.map +1 -1
- package/dist/runtime/LovelaceProvider.js +29 -26
- package/dist/runtime/LovelaceProvider.js.map +1 -1
- package/dist/runtime/branding.d.ts +71 -19
- package/dist/runtime/branding.js +97 -10
- package/dist/runtime/branding.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/styles/state-layer.css.d.ts +19 -0
- package/dist/styles/state-layer.css.js +23 -0
- package/dist/styles/state-layer.css.js.map +1 -0
- package/dist/tokens/generated/tokens.d.ts +7 -1
- package/package.json +1 -1
|
@@ -1,45 +1,48 @@
|
|
|
1
1
|
import { tokenName as e } from "../tokens/tokens.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { accentStateLayers as t } from "./branding.js";
|
|
3
|
+
import { accentStateLayerScope as n } from "../styles/state-layer.css.js";
|
|
4
|
+
import { jsx as r } from "react/jsx-runtime";
|
|
5
|
+
import { useEffect as i, useMemo as a, useState as o } from "react";
|
|
6
|
+
import { UNSAFE_PortalProvider as s } from "react-aria";
|
|
5
7
|
//#region src/runtime/LovelaceProvider.tsx
|
|
6
|
-
var
|
|
7
|
-
function
|
|
8
|
-
return typeof window > "u" || typeof window.matchMedia != "function" ? !1 : window.matchMedia(
|
|
8
|
+
var c = "(prefers-color-scheme: dark)";
|
|
9
|
+
function l() {
|
|
10
|
+
return typeof window > "u" || typeof window.matchMedia != "function" ? !1 : window.matchMedia(c).matches;
|
|
9
11
|
}
|
|
10
|
-
function
|
|
11
|
-
let [t,
|
|
12
|
-
return
|
|
12
|
+
function u(e) {
|
|
13
|
+
let [t, n] = o(() => e === "auto" ? l() ? "dark" : "light" : e);
|
|
14
|
+
return i(() => {
|
|
13
15
|
if (e !== "auto") {
|
|
14
|
-
|
|
16
|
+
n(e);
|
|
15
17
|
return;
|
|
16
18
|
}
|
|
17
19
|
if (typeof window > "u" || typeof window.matchMedia != "function") return;
|
|
18
|
-
let t = window.matchMedia(
|
|
19
|
-
|
|
20
|
+
let t = window.matchMedia(c), r = (e) => {
|
|
21
|
+
n(e ? "dark" : "light");
|
|
20
22
|
};
|
|
21
|
-
|
|
22
|
-
let i = (e) =>
|
|
23
|
+
r(t.matches);
|
|
24
|
+
let i = (e) => r(e.matches);
|
|
23
25
|
return t.addEventListener("change", i), () => t.removeEventListener("change", i);
|
|
24
26
|
}, [e]), t;
|
|
25
27
|
}
|
|
26
|
-
function
|
|
27
|
-
let
|
|
28
|
-
return /* @__PURE__ */
|
|
29
|
-
ref:
|
|
30
|
-
"
|
|
31
|
-
|
|
28
|
+
function d({ theme: i = "auto", override: c, children: l }) {
|
|
29
|
+
let d = u(i), [f, p] = o(null), m = t(c);
|
|
30
|
+
return /* @__PURE__ */ r("div", {
|
|
31
|
+
ref: p,
|
|
32
|
+
className: Object.keys(m).map((e) => n[e][m[e]]).join(" "),
|
|
33
|
+
"data-theme": d,
|
|
34
|
+
style: a(() => {
|
|
32
35
|
let t = { display: "contents" };
|
|
33
|
-
for (let [n, r] of Object.entries(
|
|
36
|
+
for (let [n, r] of Object.entries(c ?? {})) r != null && (t[e(n)] = r);
|
|
34
37
|
return t;
|
|
35
|
-
}, [
|
|
36
|
-
children:
|
|
37
|
-
getContainer: () =>
|
|
38
|
-
children:
|
|
38
|
+
}, [c]),
|
|
39
|
+
children: f != null && /* @__PURE__ */ r(s, {
|
|
40
|
+
getContainer: () => f,
|
|
41
|
+
children: l
|
|
39
42
|
})
|
|
40
43
|
});
|
|
41
44
|
}
|
|
42
45
|
//#endregion
|
|
43
|
-
export {
|
|
46
|
+
export { d as LovelaceProvider, u as useResolvedTheme };
|
|
44
47
|
|
|
45
48
|
//# sourceMappingURL=LovelaceProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LovelaceProvider.js","names":[],"sources":["../../src/runtime/LovelaceProvider.tsx"],"sourcesContent":["import type { CSSProperties, ReactNode } from \"react\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { UNSAFE_PortalProvider } from \"react-aria\";\nimport type { LovelaceOverridableToken } from \"../tokens/tokens.js\";\nimport { tokenName } from \"../tokens/tokens.js\";\n\nexport type LovelaceTheme = \"light\" | \"dark\" | \"auto\";\n\nexport interface LovelaceProviderProps {\n\t/** Color scheme. `auto` resolves via `prefers-color-scheme`. Defaults to `auto`. */\n\ttheme?: LovelaceTheme;\n\t/**\n\t * Type-safe token overrides applied to the provider root and cascaded to every\n\t * Lovelace component inside. Keys are overridable (semantic / component) tokens;\n\t * values are CSS values — typically a primitive reference via `t()` (e.g.\n\t * `{ \"font-size-body\": t(\"font-size-14\") }`) or a raw color.\n\t */\n\toverride?: Partial<Record<LovelaceOverridableToken, string>>;\n\tchildren?: ReactNode;\n}\n\nconst DARK_QUERY = \"(prefers-color-scheme: dark)\";\n\nfunction readPrefersDark(): boolean {\n\tif (\n\t\ttypeof window === \"undefined\" ||\n\t\ttypeof window.matchMedia !== \"function\"\n\t) {\n\t\treturn false;\n\t}\n\treturn window.matchMedia(DARK_QUERY).matches;\n}\n\n/**\n * Resolves `auto` against `prefers-color-scheme`, updating live. Exported so a\n * consumer deciding something per-surface (e.g. a contrast guard on a token\n * override) resolves the scheme through the same code path that stamps\n * `data-theme` — two independent media queries would disagree wherever\n * `matchMedia` is unavailable, since each falls back to light on its own.\n */\nexport function useResolvedTheme(theme: LovelaceTheme): \"light\" | \"dark\" {\n\tconst [resolved, setResolved] = useState<\"light\" | \"dark\">(() => {\n\t\tif (theme !== \"auto\") {\n\t\t\treturn theme;\n\t\t}\n\t\treturn readPrefersDark() ? \"dark\" : \"light\";\n\t});\n\n\tuseEffect(() => {\n\t\tif (theme !== \"auto\") {\n\t\t\tsetResolved(theme);\n\t\t\treturn;\n\t\t}\n\t\tif (\n\t\t\ttypeof window === \"undefined\" ||\n\t\t\ttypeof window.matchMedia !== \"function\"\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tconst mql = window.matchMedia(DARK_QUERY);\n\t\tconst update = (matches: boolean) => {\n\t\t\tsetResolved(matches ? \"dark\" : \"light\");\n\t\t};\n\t\tupdate(mql.matches);\n\t\tconst listener = (e: MediaQueryListEvent) => update(e.matches);\n\t\tmql.addEventListener(\"change\", listener);\n\t\treturn () => mql.removeEventListener(\"change\", listener);\n\t}, [theme]);\n\n\treturn resolved;\n}\n\nexport function LovelaceProvider({\n\ttheme = \"auto\",\n\toverride,\n\tchildren,\n}: LovelaceProviderProps) {\n\tconst resolvedTheme = useResolvedTheme(theme);\n\t// Callback-ref into state (not useRef): attaching the element triggers a\n\t// re-render. Children are gated on it (below) so an overlay never mounts before\n\t// the themed portal container exists — a plain ref wouldn't re-render, and\n\t// deferring the portal to a second render would move an already-open overlay,\n\t// cancelling its entrance transition and stranding it at the `data-entering`\n\t// start state (opacity 0).\n\tconst [themeEl, setThemeEl] = useState<HTMLDivElement | null>(null);\n\n\tconst overrides = useMemo<CSSProperties>(() => {\n\t\t// `display: contents` removes this wrapper from layout so it doesn't\n\t\t// interfere with the parent's sizing — children render as if the wrapper\n\t\t// isn't there. Custom properties and data-attributes still cascade.\n\t\tconst vars: Record<string, string> = { display: \"contents\" };\n\t\tfor (const [name, value] of Object.entries(override ?? {})) {\n\t\t\tif (value != null) {\n\t\t\t\tvars[tokenName(name as LovelaceOverridableToken)] = value;\n\t\t\t}\n\t\t}\n\t\treturn vars as CSSProperties;\n\t}, [override]);\n\n\t// Component tokens are declared on `[data-theme]` (this wrapper) so their var()\n\t// chain re-evaluates in the active theme. React Aria overlays (Dialog/Sheet/\n\t// Tooltip) portal to `document.body` by default — outside this element — where\n\t// they'd lose those aliases. Default the portal container to the themed wrapper\n\t// so overlays stay inside the theme boundary; a consumer can still nest its own\n\t// `UNSAFE_PortalProvider` deeper to override (as the app scopes to the chat window).\n\t//\n\t// Children render only once `themeEl` is attached so any overlay open on first\n\t// mount portals straight into the themed container — never to `document.body`\n\t// and then moved (which would cancel its entrance transition). The callback ref's\n\t// setState runs in the commit phase (before paint), so the extra commit causes no\n\t// visible flash. It does mean an SSR render would emit an empty themed div until\n\t// hydration — fine today since every lovelace tree is client-mounted.\n\treturn (\n\t\t<div
|
|
1
|
+
{"version":3,"file":"LovelaceProvider.js","names":[],"sources":["../../src/runtime/LovelaceProvider.tsx"],"sourcesContent":["import type { CSSProperties, ReactNode } from \"react\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { UNSAFE_PortalProvider } from \"react-aria\";\nimport { accentStateLayerScope } from \"../styles/state-layer.css.js\";\nimport type { LovelaceOverridableToken } from \"../tokens/tokens.js\";\nimport { tokenName } from \"../tokens/tokens.js\";\nimport { accentStateLayers } from \"./branding.js\";\n\nexport type LovelaceTheme = \"light\" | \"dark\" | \"auto\";\n\nexport interface LovelaceProviderProps {\n\t/** Color scheme. `auto` resolves via `prefers-color-scheme`. Defaults to `auto`. */\n\ttheme?: LovelaceTheme;\n\t/**\n\t * Type-safe token overrides applied to the provider root and cascaded to every\n\t * Lovelace component inside. Keys are overridable (semantic / component) tokens;\n\t * values are CSS values — typically a primitive reference via `t()` (e.g.\n\t * `{ \"font-size-body\": t(\"font-size-14\") }`) or a raw color.\n\t */\n\toverride?: Partial<Record<LovelaceOverridableToken, string>>;\n\tchildren?: ReactNode;\n}\n\nconst DARK_QUERY = \"(prefers-color-scheme: dark)\";\n\nfunction readPrefersDark(): boolean {\n\tif (\n\t\ttypeof window === \"undefined\" ||\n\t\ttypeof window.matchMedia !== \"function\"\n\t) {\n\t\treturn false;\n\t}\n\treturn window.matchMedia(DARK_QUERY).matches;\n}\n\n/**\n * Resolves `auto` against `prefers-color-scheme`, updating live. Exported so a\n * consumer deciding something per-surface (e.g. a contrast guard on a token\n * override) resolves the scheme through the same code path that stamps\n * `data-theme` — two independent media queries would disagree wherever\n * `matchMedia` is unavailable, since each falls back to light on its own.\n */\nexport function useResolvedTheme(theme: LovelaceTheme): \"light\" | \"dark\" {\n\tconst [resolved, setResolved] = useState<\"light\" | \"dark\">(() => {\n\t\tif (theme !== \"auto\") {\n\t\t\treturn theme;\n\t\t}\n\t\treturn readPrefersDark() ? \"dark\" : \"light\";\n\t});\n\n\tuseEffect(() => {\n\t\tif (theme !== \"auto\") {\n\t\t\tsetResolved(theme);\n\t\t\treturn;\n\t\t}\n\t\tif (\n\t\t\ttypeof window === \"undefined\" ||\n\t\t\ttypeof window.matchMedia !== \"function\"\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tconst mql = window.matchMedia(DARK_QUERY);\n\t\tconst update = (matches: boolean) => {\n\t\t\tsetResolved(matches ? \"dark\" : \"light\");\n\t\t};\n\t\tupdate(mql.matches);\n\t\tconst listener = (e: MediaQueryListEvent) => update(e.matches);\n\t\tmql.addEventListener(\"change\", listener);\n\t\treturn () => mql.removeEventListener(\"change\", listener);\n\t}, [theme]);\n\n\treturn resolved;\n}\n\nexport function LovelaceProvider({\n\ttheme = \"auto\",\n\toverride,\n\tchildren,\n}: LovelaceProviderProps) {\n\tconst resolvedTheme = useResolvedTheme(theme);\n\t// Callback-ref into state (not useRef): attaching the element triggers a\n\t// re-render. Children are gated on it (below) so an overlay never mounts before\n\t// the themed portal container exists — a plain ref wouldn't re-render, and\n\t// deferring the portal to a second render would move an already-open overlay,\n\t// cancelling its entrance transition and stranding it at the `data-entering`\n\t// start state (opacity 0).\n\tconst [themeEl, setThemeEl] = useState<HTMLDivElement | null>(null);\n\n\tconst stateLayers = accentStateLayers(override);\n\tconst stateLayerClassName = (\n\t\tObject.keys(stateLayers) as (keyof typeof stateLayers)[]\n\t)\n\t\t.map((target) => accentStateLayerScope[target][stateLayers[target]])\n\t\t.join(\" \");\n\n\tconst overrides = useMemo<CSSProperties>(() => {\n\t\t// `display: contents` removes this wrapper from layout so it doesn't\n\t\t// interfere with the parent's sizing — children render as if the wrapper\n\t\t// isn't there. Custom properties and data-attributes still cascade.\n\t\tconst vars: Record<string, string> = { display: \"contents\" };\n\t\tfor (const [name, value] of Object.entries(override ?? {})) {\n\t\t\tif (value != null) {\n\t\t\t\tvars[tokenName(name as LovelaceOverridableToken)] = value;\n\t\t\t}\n\t\t}\n\t\treturn vars as CSSProperties;\n\t}, [override]);\n\n\t// Component tokens are declared on `[data-theme]` (this wrapper) so their var()\n\t// chain re-evaluates in the active theme. React Aria overlays (Dialog/Sheet/\n\t// Tooltip) portal to `document.body` by default — outside this element — where\n\t// they'd lose those aliases. Default the portal container to the themed wrapper\n\t// so overlays stay inside the theme boundary; a consumer can still nest its own\n\t// `UNSAFE_PortalProvider` deeper to override (as the app scopes to the chat window).\n\t//\n\t// Children render only once `themeEl` is attached so any overlay open on first\n\t// mount portals straight into the themed container — never to `document.body`\n\t// and then moved (which would cancel its entrance transition). The callback ref's\n\t// setState runs in the commit phase (before paint), so the extra commit causes no\n\t// visible flash. It does mean an SSR render would emit an empty themed div until\n\t// hydration — fine today since every lovelace tree is client-mounted.\n\treturn (\n\t\t<div\n\t\t\tref={setThemeEl}\n\t\t\tclassName={stateLayerClassName}\n\t\t\tdata-theme={resolvedTheme}\n\t\t\tstyle={overrides}\n\t\t>\n\t\t\t{themeEl != null && (\n\t\t\t\t<UNSAFE_PortalProvider getContainer={() => themeEl}>\n\t\t\t\t\t{children}\n\t\t\t\t</UNSAFE_PortalProvider>\n\t\t\t)}\n\t\t</div>\n\t);\n}\n"],"mappings":";;;;;;;AAuBA,IAAM,IAAa;AAEnB,SAAS,IAA2B;CAOnC,OALC,OAAO,SAAW,OAClB,OAAO,OAAO,cAAe,aAEtB,KAED,OAAO,WAAW,CAAU,CAAC,CAAC;AACtC;AASA,SAAgB,EAAiB,GAAwC;CACxE,IAAM,CAAC,GAAU,KAAe,QAC3B,MAAU,SAGP,EAAgB,IAAI,SAAS,UAF5B,CAGR;CAuBD,OArBA,QAAgB;EACf,IAAI,MAAU,QAAQ;GACrB,EAAY,CAAK;GACjB;EACD;EACA,IACC,OAAO,SAAW,OAClB,OAAO,OAAO,cAAe,YAE7B;EAED,IAAM,IAAM,OAAO,WAAW,CAAU,GAClC,KAAU,MAAqB;GACpC,EAAY,IAAU,SAAS,OAAO;EACvC;EACA,EAAO,EAAI,OAAO;EAClB,IAAM,KAAY,MAA2B,EAAO,EAAE,OAAO;EAE7D,OADA,EAAI,iBAAiB,UAAU,CAAQ,SAC1B,EAAI,oBAAoB,UAAU,CAAQ;CACxD,GAAG,CAAC,CAAK,CAAC,GAEH;AACR;AAEA,SAAgB,EAAiB,EAChC,WAAQ,QACR,aACA,eACyB;CACzB,IAAM,IAAgB,EAAiB,CAAK,GAOtC,CAAC,GAAS,KAAc,EAAgC,IAAI,GAE5D,IAAc,EAAkB,CAAQ;CAiC9C,OACC,kBAAC,OAAD;EACC,KAAK;EACL,WAlCD,OAAO,KAAK,CAAW,CAAC,CAEvB,KAAK,MAAW,EAAsB,EAAO,CAAC,EAAY,GAAQ,CAAC,CACnE,KAAK,GA+BM;EACX,cAAY;EACZ,OA/BgB,QAA6B;GAI9C,IAAM,IAA+B,EAAE,SAAS,WAAW;GAC3D,KAAK,IAAM,CAAC,GAAM,MAAU,OAAO,QAAQ,KAAY,CAAC,CAAC,GACxD,AAAI,KAAS,SACZ,EAAK,EAAU,CAAgC,KAAK;GAGtD,OAAO;EACR,GAAG,CAAC,CAAQ,CAoBH;YAEN,KAAW,QACX,kBAAC,GAAD;GAAuB,oBAAoB;GACzC;EACqB,CAAA;CAEpB,CAAA;AAEP"}
|
|
@@ -24,8 +24,9 @@ export type BrandCornerStyle = "round" | "rectangular";
|
|
|
24
24
|
export interface BrandSettings {
|
|
25
25
|
/**
|
|
26
26
|
* The brand accent color. Painted unconditionally as the accent fill
|
|
27
|
-
* (`color-background-accent`)
|
|
28
|
-
*
|
|
27
|
+
* (`color-background-accent`). Also used as accent *text*
|
|
28
|
+
* (`color-content-accent`), darkened on light or lightened on dark until it
|
|
29
|
+
* reads at 4.5:1 on the rendered surface.
|
|
29
30
|
*/
|
|
30
31
|
tintColor?: string | null;
|
|
31
32
|
/**
|
|
@@ -52,7 +53,7 @@ export interface BrandSettings {
|
|
|
52
53
|
}
|
|
53
54
|
/**
|
|
54
55
|
* Lovelace's `color-background-default` per scheme (`tokens.css`). The surface
|
|
55
|
-
* every
|
|
56
|
+
* behind every chat control.
|
|
56
57
|
*/
|
|
57
58
|
export declare const LOVELACE_SURFACES: {
|
|
58
59
|
readonly light: "#ffffff";
|
|
@@ -61,9 +62,8 @@ export declare const LOVELACE_SURFACES: {
|
|
|
61
62
|
/**
|
|
62
63
|
* WCAG contrast ratio between two hex colors, or `null` when either cannot be parsed.
|
|
63
64
|
*
|
|
64
|
-
* Exported so a caller that needs
|
|
65
|
-
*
|
|
66
|
-
* a second copy. Judging *which* colors to compare is still the caller's job.
|
|
65
|
+
* Exported so a caller that needs its own floor reuses this colorimetry instead of
|
|
66
|
+
* writing a second copy. Judging *which* colors to compare is still the caller's job.
|
|
67
67
|
*/
|
|
68
68
|
export declare function contrastRatioBetween(a: string, b: string): number | null;
|
|
69
69
|
/**
|
|
@@ -87,15 +87,62 @@ export declare function readableTextOnAccent(accent: string, fallback: string):
|
|
|
87
87
|
*
|
|
88
88
|
* Both clauses are evaluated against `surface` and nothing else, so a caller that
|
|
89
89
|
* paints the tint over a *composite* of itself and the surface gets a verdict that
|
|
90
|
-
* overstates the real contrast
|
|
91
|
-
* than the fallback it declined. `FileDropZone`'s 15% accent wash is the one such
|
|
92
|
-
* caller today; see the theming spec's auto-contrast row for the measured shortfall.
|
|
90
|
+
* overstates the real contrast.
|
|
93
91
|
*/
|
|
94
92
|
export declare function isLegibleAgainst(color: string, surface: string, fallback: string): boolean;
|
|
95
93
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
|
|
94
|
+
* The colour a control on the accent fill paints its hover, focus and pressed state
|
|
95
|
+
* layer in. `content` is the control's own text colour.
|
|
96
|
+
*/
|
|
97
|
+
export type AccentStateLayer = "content" | "lighten" | "darken";
|
|
98
|
+
/** The layer colours that move the accent fill away from its text. */
|
|
99
|
+
export declare const ACCENT_STATE_LAYER_COLORS: {
|
|
100
|
+
readonly lighten: "#ffffff";
|
|
101
|
+
readonly darken: "#000000";
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Pick the state-layer colour for text painted on an accent fill.
|
|
105
|
+
*
|
|
106
|
+
* The default layer is the text colour, so each state moves the fill toward the text.
|
|
107
|
+
* On a mid-tone fill that layer drops the text below 4.5:1, so the layer moves the fill
|
|
108
|
+
* away from the text instead. A light or dark fill with black or white text keeps the
|
|
109
|
+
* default, because it has little room to move away from its text. A custom text colour can
|
|
110
|
+
* pass at rest on a black or white fill and fail under the default layer. Then the layer
|
|
111
|
+
* keeps 4.5:1 and cannot change that fill. This function does not repair text that fails at
|
|
112
|
+
* rest. A value the hex parser cannot read keeps the default.
|
|
113
|
+
*/
|
|
114
|
+
export declare function accentStateLayer(fill: string | undefined, content: string | undefined): AccentStateLayer;
|
|
115
|
+
/**
|
|
116
|
+
* The controls that paint a state layer on an accent fill, with the tokens of their fill
|
|
117
|
+
* and their text. Each list starts with the token the control reads and ends with the
|
|
118
|
+
* semantic token that token defaults to.
|
|
119
|
+
*/
|
|
120
|
+
declare const ACCENT_STATE_LAYER_TOKENS: {
|
|
121
|
+
readonly accent: {
|
|
122
|
+
readonly fill: readonly ["color-background-accent"];
|
|
123
|
+
readonly content: readonly ["color-content-on-accent"];
|
|
124
|
+
};
|
|
125
|
+
readonly buttonPrimary: {
|
|
126
|
+
readonly fill: readonly ["button-primary-background", "color-background-accent"];
|
|
127
|
+
readonly content: readonly ["button-primary-content", "color-content-on-accent"];
|
|
128
|
+
};
|
|
129
|
+
readonly chipSelected: {
|
|
130
|
+
readonly fill: readonly ["chip-background-selected", "color-background-accent"];
|
|
131
|
+
readonly content: readonly ["color-content-on-accent"];
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
/** A group of controls that read the same fill and text tokens on the accent fill. */
|
|
135
|
+
export type AccentStateLayerTarget = keyof typeof ACCENT_STATE_LAYER_TOKENS;
|
|
136
|
+
/**
|
|
137
|
+
* Pick the state-layer colour of each group of accent controls from the fill and the text
|
|
138
|
+
* that `override` gives that group. A component token in `override` changes the decision
|
|
139
|
+
* for its own group only.
|
|
140
|
+
*/
|
|
141
|
+
export declare function accentStateLayers(override: BrandOverrides | undefined): Record<AccentStateLayerTarget, AccentStateLayer>;
|
|
142
|
+
/**
|
|
143
|
+
* Whether `color` holds WCAG 1.4.3's 4.5:1 as accent text on `scheme`'s Lovelace
|
|
144
|
+
* surfaces, at rest and in the strongest state. A value the hex parser cannot read
|
|
145
|
+
* fails.
|
|
99
146
|
*/
|
|
100
147
|
export declare function isLegibleOnSurface(color: string, scheme: "light" | "dark"): boolean;
|
|
101
148
|
/**
|
|
@@ -114,9 +161,11 @@ export declare function isLegibleOnSurface(color: string, scheme: "light" | "dar
|
|
|
114
161
|
* shift the four-step type scale down/up one step).
|
|
115
162
|
* 2. `tintColor` paints `color-background-accent` unconditionally, and
|
|
116
163
|
* `color-content-on-accent` becomes whichever of black or white contrasts
|
|
117
|
-
* more with the tint. The tint is also
|
|
118
|
-
* (`color-content-accent`)
|
|
119
|
-
*
|
|
164
|
+
* more with the tint. The tint is also standalone accent *text*
|
|
165
|
+
* (`color-content-accent`). A tint that falls below 4.5:1 on the rendered
|
|
166
|
+
* surface, at rest or in any state, is darkened on light or lightened on
|
|
167
|
+
* dark until it holds 4.5:1. A tint the hex parser cannot read gets the
|
|
168
|
+
* accent text that `color-accent-500` gets.
|
|
120
169
|
* 3. With `advancedColorsEnabled`, `textOverAccentColor` replaces the derived
|
|
121
170
|
* on-accent text color verbatim.
|
|
122
171
|
* 4. With `headerColorEnabled`, `headerColor` paints `header-background`.
|
|
@@ -125,9 +174,12 @@ export declare function isLegibleOnSurface(color: string, scheme: "light" | "dar
|
|
|
125
174
|
* 4.5:1 floor against `headerColor`; without a configured foreground the
|
|
126
175
|
* maximum-contrast black or white is derived from the background.
|
|
127
176
|
*
|
|
128
|
-
* Legibility guards measure hex colors only.
|
|
129
|
-
* (named
|
|
130
|
-
*
|
|
131
|
-
*
|
|
177
|
+
* Legibility guards measure hex colors only. A value the hex parser cannot read
|
|
178
|
+
* (a named color, `rgba()`, `var()`, …) is not repaired where the mapping keeps
|
|
179
|
+
* it: repairing what cannot be measured would trade one unknown rendering for
|
|
180
|
+
* another. An unparseable tint is the exception. It still paints
|
|
181
|
+
* `color-background-accent` as configured, but its accent text is
|
|
182
|
+
* `color-accent-500` adjusted to hold 4.5:1, as step 2 says.
|
|
132
183
|
*/
|
|
133
184
|
export declare function createBrandOverrides(settings: BrandSettings, scheme: "light" | "dark"): BrandOverrides;
|
|
185
|
+
export {};
|
package/dist/runtime/branding.js
CHANGED
|
@@ -41,14 +41,101 @@ function d(e, t, r) {
|
|
|
41
41
|
let l = a(r);
|
|
42
42
|
return l !== null && s >= c(l, o);
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
var f = 4.5, p = [
|
|
45
|
+
.08,
|
|
46
|
+
.1,
|
|
47
|
+
.1
|
|
48
|
+
], m = (e) => Math.round(e * 255) / 255;
|
|
49
|
+
function h(e, t, n) {
|
|
50
|
+
let r = (r) => {
|
|
51
|
+
let i = t[r] + (e[r] - t[r]) * n;
|
|
52
|
+
return e[r] < t[r] ? Math.floor(i) : Math.ceil(i);
|
|
53
|
+
};
|
|
54
|
+
return [
|
|
55
|
+
r(0),
|
|
56
|
+
r(1),
|
|
57
|
+
r(2)
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
function g(e, t, n) {
|
|
61
|
+
return Math.min(c(e, t), ...p.map((r) => c(e, h(n, t, m(r)))));
|
|
62
|
+
}
|
|
63
|
+
function _(e, t) {
|
|
64
|
+
let n = e === void 0 ? null : a(e), r = t === void 0 ? null : a(t);
|
|
65
|
+
return !n || !r || g(r, n, r) >= f ? "content" : s(r) < s(n) ? "lighten" : "darken";
|
|
66
|
+
}
|
|
67
|
+
var v = {
|
|
68
|
+
accent: {
|
|
69
|
+
fill: ["color-background-accent"],
|
|
70
|
+
content: ["color-content-on-accent"]
|
|
71
|
+
},
|
|
72
|
+
buttonPrimary: {
|
|
73
|
+
fill: ["button-primary-background", "color-background-accent"],
|
|
74
|
+
content: ["button-primary-content", "color-content-on-accent"]
|
|
75
|
+
},
|
|
76
|
+
chipSelected: {
|
|
77
|
+
fill: ["chip-background-selected", "color-background-accent"],
|
|
78
|
+
content: ["color-content-on-accent"]
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
function y(e) {
|
|
82
|
+
let t = (t) => t.map((t) => e?.[t]).find((e) => e != null), n = (e) => _(t(v[e].fill), t(v[e].content));
|
|
83
|
+
return {
|
|
84
|
+
accent: n("accent"),
|
|
85
|
+
buttonPrimary: n("buttonPrimary"),
|
|
86
|
+
chipSelected: n("chipSelected")
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
var b = 4.5, x = {
|
|
90
|
+
light: [t.light, "#f9fafb"],
|
|
91
|
+
dark: [t.dark, "#1f2937"]
|
|
92
|
+
}, S = {
|
|
93
|
+
light: [
|
|
94
|
+
0,
|
|
95
|
+
0,
|
|
96
|
+
0
|
|
97
|
+
],
|
|
98
|
+
dark: [
|
|
99
|
+
255,
|
|
100
|
+
255,
|
|
101
|
+
255
|
|
102
|
+
]
|
|
103
|
+
}, C = 255;
|
|
104
|
+
function w(e, t) {
|
|
105
|
+
let n = x[t].map(a).filter((e) => e !== null);
|
|
106
|
+
return Math.min(g(e, n[0], e), ...n.slice(1).map((t) => c(e, t)));
|
|
107
|
+
}
|
|
108
|
+
function T(e, t) {
|
|
109
|
+
let n = a(e);
|
|
110
|
+
return n !== null && w(n, t) >= b;
|
|
111
|
+
}
|
|
112
|
+
function E(e, t, n) {
|
|
113
|
+
let r = (r) => Math.round(e[r] + (t[r] - e[r]) * n);
|
|
114
|
+
return [
|
|
115
|
+
r(0),
|
|
116
|
+
r(1),
|
|
117
|
+
r(2)
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
function D(e) {
|
|
121
|
+
return `#${e.map((e) => e.toString(16).padStart(2, "0")).join("")}`;
|
|
122
|
+
}
|
|
123
|
+
function O(e, t) {
|
|
124
|
+
let n = a(e);
|
|
125
|
+
if (!n) return O(i, t);
|
|
126
|
+
if (w(n, t) >= b) return e;
|
|
127
|
+
let r = S[t];
|
|
128
|
+
for (let e = 1; e < C; e += 1) {
|
|
129
|
+
let i = E(n, r, e / C);
|
|
130
|
+
if (w(i, t) >= b) return D(i);
|
|
131
|
+
}
|
|
132
|
+
return D(r);
|
|
46
133
|
}
|
|
47
|
-
function
|
|
134
|
+
function k(e, t) {
|
|
48
135
|
let n = l(e, t);
|
|
49
136
|
return n === null || n >= r ? t : u(e, t);
|
|
50
137
|
}
|
|
51
|
-
var
|
|
138
|
+
var A = {
|
|
52
139
|
default: {},
|
|
53
140
|
small: {
|
|
54
141
|
"font-size-caption": e("font-size-12"),
|
|
@@ -70,7 +157,7 @@ var m = {
|
|
|
70
157
|
"font-size-title-large": e("font-size-22"),
|
|
71
158
|
"line-height-title-large": e("font-line-height-28")
|
|
72
159
|
}
|
|
73
|
-
},
|
|
160
|
+
}, j = {
|
|
74
161
|
round: {},
|
|
75
162
|
rectangular: {
|
|
76
163
|
"border-radius-message": e("border-radius-10"),
|
|
@@ -79,14 +166,14 @@ var m = {
|
|
|
79
166
|
"border-radius-container": e("border-radius-10")
|
|
80
167
|
}
|
|
81
168
|
};
|
|
82
|
-
function
|
|
169
|
+
function M(e, t) {
|
|
83
170
|
let n = {
|
|
84
|
-
...
|
|
85
|
-
...
|
|
171
|
+
...j[e.cornerStyle ?? "round"],
|
|
172
|
+
...A[e.textSize ?? "default"]
|
|
86
173
|
};
|
|
87
|
-
return e.tintColor && (n["color-background-accent"] = e.tintColor, n["color-content-on-accent"] = u(e.tintColor, "#ffffff"),
|
|
174
|
+
return e.tintColor && (n["color-background-accent"] = e.tintColor, n["color-content-on-accent"] = u(e.tintColor, "#ffffff"), n["color-content-accent"] = O(e.tintColor, t)), e.advancedColorsEnabled && e.textOverAccentColor && (n["color-content-on-accent"] = e.textOverAccentColor), e.headerColorEnabled && e.headerColor && (n["header-background"] = e.headerColor, e.advancedColorsEnabled && e.headerTextColor ? n["header-content"] = k(e.headerColor, e.headerTextColor) : l(e.headerColor, "#000000") !== null && (n["header-content"] = u(e.headerColor, "#000000"))), n;
|
|
88
175
|
}
|
|
89
176
|
//#endregion
|
|
90
|
-
export { t as LOVELACE_SURFACES, l as contrastRatioBetween,
|
|
177
|
+
export { t as LOVELACE_SURFACES, _ as accentStateLayer, y as accentStateLayers, l as contrastRatioBetween, M as createBrandOverrides, d as isLegibleAgainst, T as isLegibleOnSurface, u as readableTextOnAccent };
|
|
91
178
|
|
|
92
179
|
//# sourceMappingURL=branding.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"branding.js","names":[],"sources":["../../src/runtime/branding.ts"],"sourcesContent":["/**\n * How Ada applies a customer's brand colors — the canonical mapping from\n * dashboard brand settings to Lovelace token overrides, plus the WCAG\n * colorimetry that keeps the result legible.\n *\n * `createBrandOverrides` is the single source of truth: the embedded widget,\n * the docs theme playground, and the theming documentation all describe the\n * same function. The output is a plain token→value map intended for\n * `<LovelaceProvider override={...}>`.\n */\n\nimport type { LovelaceOverridableToken } from \"../tokens/tokens.js\";\nimport { t } from \"../tokens/tokens.js\";\n\n/** Token overrides consumable by `LovelaceProvider`'s `override` prop. */\nexport type BrandOverrides = Partial<Record<LovelaceOverridableToken, string>>;\n\n/** Text-size preset. `default` keeps Lovelace's own type scale. */\nexport type BrandTextSize = \"default\" | \"small\" | \"large\";\n\n/** Corner-style preset. `round` keeps Lovelace's own radii. */\nexport type BrandCornerStyle = \"round\" | \"rectangular\";\n\n/**\n * Customer brand settings, as configured in the Ada dashboard.\n *\n * Every field is optional: an absent (or `null`) value means \"not configured\"\n * and contributes no overrides, so `createBrandOverrides({}, scheme)` is `{}`.\n */\nexport interface BrandSettings {\n\t/**\n\t * The brand accent color. Painted unconditionally as the accent fill\n\t * (`color-background-accent`); used as accent *text* (`color-content-accent`)\n\t * only when it reads against the rendered surface.\n\t */\n\ttintColor?: string | null;\n\t/**\n\t * Advanced-colors override for text painted on the accent fill\n\t * (`color-content-on-accent`). Ignored unless `advancedColorsEnabled`.\n\t */\n\ttextOverAccentColor?: string | null;\n\t/** Gate for `textOverAccentColor` and `headerTextColor`. */\n\tadvancedColorsEnabled?: boolean;\n\t/** Gate for `headerColor`/`headerTextColor`. */\n\theaderColorEnabled?: boolean;\n\t/** Header background (`header-background`). */\n\theaderColor?: string | null;\n\t/**\n\t * Header foreground (`header-content`). Ignored unless\n\t * `advancedColorsEnabled`; repaired to black or white when it provably\n\t * falls below the WCAG AA 4.5:1 floor against `headerColor`.\n\t */\n\theaderTextColor?: string | null;\n\t/** Type-scale preset. Defaults to `default`. */\n\ttextSize?: BrandTextSize;\n\t/** Corner-radius preset. Defaults to `round`. */\n\tcornerStyle?: BrandCornerStyle;\n}\n\ntype Rgb = readonly [number, number, number];\n\n/**\n * Lovelace's `color-background-default` per scheme (`tokens.css`). The surface\n * every accent-as-text legibility judgement is made against.\n */\nexport const LOVELACE_SURFACES = {\n\tlight: \"#ffffff\",\n\tdark: \"#181a1e\",\n} as const satisfies Record<\"light\" | \"dark\", string>;\n\n/**\n * An anti-invisibility floor, not an AA claim: the tinted labels this guards render at\n * 13–16px, which WCAG 1.4.3 would hold to 4.5:1. Neither design system's own withheld\n * fallback clears 4.5 on both surfaces, and 4.5 would strip the brand color from most\n * mid-tone tints, so this catches only the vanishing case.\n */\nconst MIN_CONTRAST_RATIO = 3;\n\n/** WCAG AA floor for the normal-size title and controls painted in the header. */\nconst MIN_HEADER_CONTENT_CONTRAST = 4.5;\n\n/**\n * What `color-content-accent` resolves to when the override is withheld —\n * `color-accent-500` (tokens.css). A scheme-independent primitive, so it is only\n * 2.9:1 on the dark surface: withholding is not itself a legibility guarantee.\n */\nconst FALLBACK_ACCENT = \"#4e53d1\";\n\n/**\n * Hex only, deliberately: enumerating what we can measure keeps the guards\n * honest. CSS may understand values this parser does not; those pass through\n * the mapping byte-for-byte rather than being judged blind.\n */\nfunction parseHexColor(color: string): Rgb | null {\n\tconst match = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());\n\tif (!match) {\n\t\treturn null;\n\t}\n\n\tconst hex = match[1];\n\tconst full =\n\t\thex.length === 3\n\t\t\t? hex\n\t\t\t\t\t.split(\"\")\n\t\t\t\t\t.map((c) => c + c)\n\t\t\t\t\t.join(\"\")\n\t\t\t: hex;\n\n\treturn [\n\t\tNumber.parseInt(full.slice(0, 2), 16),\n\t\tNumber.parseInt(full.slice(2, 4), 16),\n\t\tNumber.parseInt(full.slice(4, 6), 16),\n\t] as const;\n}\n\nfunction channelLuminance(channel: number): number {\n\tconst srgb = channel / 255;\n\n\treturn srgb <= 0.03928 ? srgb / 12.92 : ((srgb + 0.055) / 1.055) ** 2.4;\n}\n\nfunction relativeLuminance([r, g, b]: Rgb): number {\n\treturn (\n\t\t0.2126 * channelLuminance(r) +\n\t\t0.7152 * channelLuminance(g) +\n\t\t0.0722 * channelLuminance(b)\n\t);\n}\n\nfunction contrastRatio(a: Rgb, b: Rgb): number {\n\tconst la = relativeLuminance(a);\n\tconst lb = relativeLuminance(b);\n\n\treturn (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);\n}\n\n/**\n * WCAG contrast ratio between two hex colors, or `null` when either cannot be parsed.\n *\n * Exported so a caller that needs a different floor than this module's 3:1 — an AA\n * 4.5:1 check on the default palette, say — reuses this colorimetry instead of writing\n * a second copy. Judging *which* colors to compare is still the caller's job.\n */\nexport function contrastRatioBetween(a: string, b: string): number | null {\n\tconst rgbA = parseHexColor(a);\n\tconst rgbB = parseHexColor(b);\n\n\treturn rgbA && rgbB ? contrastRatio(rgbA, rgbB) : null;\n}\n\n/**\n * Pick black or white for ordinary text painted on a customer-provided accent fill.\n *\n * One of the two always clears WCAG AA against a valid opaque hex color (their\n * luminances sit at opposite ends of the scale). An unparseable color keeps the\n * caller's existing fallback: CSS may understand a value this deliberately-small\n * parser does not, and silently changing its text color without measuring it would\n * only trade one unknown rendering for another.\n */\nexport function readableTextOnAccent(accent: string, fallback: string): string {\n\tconst whiteRatio = contrastRatioBetween(\"#ffffff\", accent);\n\tconst blackRatio = contrastRatioBetween(\"#000000\", accent);\n\tif (whiteRatio === null || blackRatio === null) {\n\t\treturn fallback;\n\t}\n\treturn whiteRatio >= blackRatio ? \"#ffffff\" : \"#000000\";\n}\n\n/**\n * Whether `color` is safe to use as accent foreground on `surface`.\n *\n * Kept when it clears the floor, and also kept when it is merely below the floor but\n * still no worse than `fallback` — the color withholding would leave in place — so the\n * guard can never trade a tint for a *less* legible replacement. A value it cannot\n * parse fails closed: an unreadable `surface` means the judgement cannot be made, and\n * an unparseable `fallback` only forfeits that second chance.\n *\n * Both clauses are evaluated against `surface` and nothing else, so a caller that\n * paints the tint over a *composite* of itself and the surface gets a verdict that\n * overstates the real contrast — and can therefore keep a tint that is genuinely worse\n * than the fallback it declined. `FileDropZone`'s 15% accent wash is the one such\n * caller today; see the theming spec's auto-contrast row for the measured shortfall.\n */\nexport function isLegibleAgainst(\n\tcolor: string,\n\tsurface: string,\n\tfallback: string,\n): boolean {\n\tconst rgb = parseHexColor(color);\n\tconst surfaceRgb = parseHexColor(surface);\n\tif (!rgb || !surfaceRgb) {\n\t\treturn false;\n\t}\n\n\tconst tintRatio = contrastRatio(rgb, surfaceRgb);\n\tif (tintRatio >= MIN_CONTRAST_RATIO) {\n\t\treturn true;\n\t}\n\n\tconst fallbackRgb = parseHexColor(fallback);\n\n\treturn (\n\t\tfallbackRgb !== null && tintRatio >= contrastRatio(fallbackRgb, surfaceRgb)\n\t);\n}\n\n/**\n * Whether `color` is safe to use as accent text on `scheme`'s default Lovelace\n * surface. The comparison fallback is `color-accent-500`, the value the token\n * resolves to when the override is withheld.\n */\nexport function isLegibleOnSurface(\n\tcolor: string,\n\tscheme: \"light\" | \"dark\",\n): boolean {\n\treturn isLegibleAgainst(color, LOVELACE_SURFACES[scheme], FALLBACK_ACCENT);\n}\n\n/**\n * Keep a customer's configured pair when it is readable, and repair only a\n * valid pair that we can prove falls below AA. Named colors, alpha colors and\n * other values outside the shared hex parser stay byte-for-byte unchanged: CSS\n * may understand them, but choosing a replacement without measuring them would\n * exchange one unknown rendering for another.\n */\nfunction resolveHeaderContentColor(\n\tbackground: string,\n\tcontent: string,\n): string {\n\tconst ratio = contrastRatioBetween(background, content);\n\tif (ratio === null || ratio >= MIN_HEADER_CONTENT_CONTRAST) {\n\t\treturn content;\n\t}\n\treturn readableTextOnAccent(background, content);\n}\n\n/**\n * Text-size and corner style are consumer concerns: Lovelace ships only its\n * default scale, so the mapping re-points the semantic/component tokens at the\n * right Lovelace *primitives* per preset (values stay owned by the token sheet;\n * only the mapping lives here). Only tokens that differ from the default are listed.\n */\nconst TEXT_SIZE_TOKEN_OVERRIDES = {\n\tdefault: {},\n\tsmall: {\n\t\t\"font-size-caption\": t(\"font-size-12\"),\n\t\t\"line-height-caption\": t(\"font-line-height-16\"),\n\t\t\"font-size-body\": t(\"font-size-14\"),\n\t\t\"line-height-body\": t(\"font-line-height-18\"),\n\t\t\"font-size-title-small\": t(\"font-size-16\"),\n\t\t\"line-height-title-small\": t(\"font-line-height-22\"),\n\t\t\"font-size-title-large\": t(\"font-size-18\"),\n\t\t\"line-height-title-large\": t(\"font-line-height-24\"),\n\t},\n\tlarge: {\n\t\t\"font-size-caption\": t(\"font-size-16\"),\n\t\t\"line-height-caption\": t(\"font-line-height-22\"),\n\t\t\"font-size-body\": t(\"font-size-18\"),\n\t\t\"line-height-body\": t(\"font-line-height-24\"),\n\t\t\"font-size-title-small\": t(\"font-size-20\"),\n\t\t\"line-height-title-small\": t(\"font-line-height-26\"),\n\t\t\"font-size-title-large\": t(\"font-size-22\"),\n\t\t\"line-height-title-large\": t(\"font-line-height-28\"),\n\t},\n} satisfies Record<BrandTextSize, BrandOverrides>;\n\nconst CORNER_STYLE_TOKEN_OVERRIDES = {\n\tround: {},\n\trectangular: {\n\t\t\"border-radius-message\": t(\"border-radius-10\"),\n\t\t\"border-radius-composer\": t(\"border-radius-10\"),\n\t\t\"border-radius-media-preview\": t(\"border-radius-10\"),\n\t\t\"border-radius-container\": t(\"border-radius-10\"),\n\t},\n} satisfies Record<BrandCornerStyle, BrandOverrides>;\n\n/**\n * Map customer brand settings to Lovelace token overrides — exactly how the Ada\n * widget applies your brand colors. Feed the result to\n * `<LovelaceProvider override={...}>`.\n *\n * `scheme` is the *resolved* light/dark surface being painted (resolve `auto`\n * via `useResolvedTheme`), not the configured theme — the accent-text contrast\n * decision has to be made against the surface the label actually lands on.\n *\n * The mapping, in order:\n *\n * 1. `cornerStyle` / `textSize` re-point radius and typography tokens at\n * Lovelace primitives (`rectangular` flattens radii to 10px; `small`/`large`\n * shift the four-step type scale down/up one step).\n * 2. `tintColor` paints `color-background-accent` unconditionally, and\n * `color-content-on-accent` becomes whichever of black or white contrasts\n * more with the tint. The tint is also used as standalone accent *text*\n * (`color-content-accent`) only when it reads on the rendered surface —\n * a tint near that background would render the label invisible.\n * 3. With `advancedColorsEnabled`, `textOverAccentColor` replaces the derived\n * on-accent text color verbatim.\n * 4. With `headerColorEnabled`, `headerColor` paints `header-background`.\n * The header foreground is `headerTextColor` (advanced colors only),\n * repaired to black or white when it measurably falls below the WCAG AA\n * 4.5:1 floor against `headerColor`; without a configured foreground the\n * maximum-contrast black or white is derived from the background.\n *\n * Legibility guards measure hex colors only. Values the hex parser cannot read\n * (named colors, `rgba()`, `var()`, …) pass through unrepaired where they are\n * kept at all: repairing what cannot be measured would trade one unknown\n * rendering for another.\n */\nexport function createBrandOverrides(\n\tsettings: BrandSettings,\n\tscheme: \"light\" | \"dark\",\n): BrandOverrides {\n\tconst overrides: BrandOverrides = {\n\t\t...CORNER_STYLE_TOKEN_OVERRIDES[settings.cornerStyle ?? \"round\"],\n\t\t...TEXT_SIZE_TOKEN_OVERRIDES[settings.textSize ?? \"default\"],\n\t};\n\tif (settings.tintColor) {\n\t\toverrides[\"color-background-accent\"] = settings.tintColor;\n\t\toverrides[\"color-content-on-accent\"] = readableTextOnAccent(\n\t\t\tsettings.tintColor,\n\t\t\t\"#ffffff\",\n\t\t);\n\t\t// Only as text when it reads on the surface being painted — a tint near that\n\t\t// background renders the label invisible, and nothing downstream can recover\n\t\t// it (`textOverAccentColor` only covers text *on* the accent fill).\n\t\tif (isLegibleOnSurface(settings.tintColor, scheme)) {\n\t\t\toverrides[\"color-content-accent\"] = settings.tintColor;\n\t\t}\n\t}\n\tif (settings.advancedColorsEnabled && settings.textOverAccentColor) {\n\t\toverrides[\"color-content-on-accent\"] = settings.textOverAccentColor;\n\t}\n\tif (settings.headerColorEnabled && settings.headerColor) {\n\t\toverrides[\"header-background\"] = settings.headerColor;\n\t\tif (settings.advancedColorsEnabled && settings.headerTextColor) {\n\t\t\toverrides[\"header-content\"] = resolveHeaderContentColor(\n\t\t\t\tsettings.headerColor,\n\t\t\t\tsettings.headerTextColor,\n\t\t\t);\n\t\t} else if (contrastRatioBetween(settings.headerColor, \"#000000\") !== null) {\n\t\t\t// The dashboard derives the header foreground from a configured background\n\t\t\t// unless advanced colours explicitly supply one. The parse guard above\n\t\t\t// makes this fallback unreachable, so select the maximum-contrast black\n\t\t\t// or white foreground without implying a color-scheme preference.\n\t\t\toverrides[\"header-content\"] = readableTextOnAccent(\n\t\t\t\tsettings.headerColor,\n\t\t\t\t\"#000000\",\n\t\t\t);\n\t\t}\n\t}\n\treturn overrides;\n}\n"],"mappings":";;AAiEA,IAAa,IAAoB;CAChC,OAAO;CACP,MAAM;AACP,GAQM,I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eAAe;EACxD,GAAG,EAA0B,EAAS,YAAY;CACnD;CAmCA,OAlCI,EAAS,cACZ,EAAU,6BAA6B,EAAS,WAChD,EAAU,6BAA6B,EACtC,EAAS,WACT,SACD,GAII,EAAmB,EAAS,WAAW,CAAM,MAChD,EAAU,0BAA0B,EAAS,aAG3C,EAAS,yBAAyB,EAAS,wBAC9C,EAAU,6BAA6B,EAAS,sBAE7C,EAAS,sBAAsB,EAAS,gBAC3C,EAAU,uBAAuB,EAAS,aACtC,EAAS,yBAAyB,EAAS,kBAC9C,EAAU,oBAAoB,EAC7B,EAAS,aACT,EAAS,eACV,IACU,EAAqB,EAAS,aAAa,SAAS,MAAM,SAKpE,EAAU,oBAAoB,EAC7B,EAAS,aACT,SACD,KAGK;AACR"}
|
|
1
|
+
{"version":3,"file":"branding.js","names":[],"sources":["../../src/runtime/branding.ts"],"sourcesContent":["/**\n * How Ada applies a customer's brand colors — the canonical mapping from\n * dashboard brand settings to Lovelace token overrides, plus the WCAG\n * colorimetry that keeps the result legible.\n *\n * `createBrandOverrides` is the single source of truth: the embedded widget,\n * the docs theme playground, and the theming documentation all describe the\n * same function. The output is a plain token→value map intended for\n * `<LovelaceProvider override={...}>`.\n */\n\nimport type { LovelaceOverridableToken } from \"../tokens/tokens.js\";\nimport { t } from \"../tokens/tokens.js\";\n\n/** Token overrides consumable by `LovelaceProvider`'s `override` prop. */\nexport type BrandOverrides = Partial<Record<LovelaceOverridableToken, string>>;\n\n/** Text-size preset. `default` keeps Lovelace's own type scale. */\nexport type BrandTextSize = \"default\" | \"small\" | \"large\";\n\n/** Corner-style preset. `round` keeps Lovelace's own radii. */\nexport type BrandCornerStyle = \"round\" | \"rectangular\";\n\n/**\n * Customer brand settings, as configured in the Ada dashboard.\n *\n * Every field is optional: an absent (or `null`) value means \"not configured\"\n * and contributes no overrides, so `createBrandOverrides({}, scheme)` is `{}`.\n */\nexport interface BrandSettings {\n\t/**\n\t * The brand accent color. Painted unconditionally as the accent fill\n\t * (`color-background-accent`). Also used as accent *text*\n\t * (`color-content-accent`), darkened on light or lightened on dark until it\n\t * reads at 4.5:1 on the rendered surface.\n\t */\n\ttintColor?: string | null;\n\t/**\n\t * Advanced-colors override for text painted on the accent fill\n\t * (`color-content-on-accent`). Ignored unless `advancedColorsEnabled`.\n\t */\n\ttextOverAccentColor?: string | null;\n\t/** Gate for `textOverAccentColor` and `headerTextColor`. */\n\tadvancedColorsEnabled?: boolean;\n\t/** Gate for `headerColor`/`headerTextColor`. */\n\theaderColorEnabled?: boolean;\n\t/** Header background (`header-background`). */\n\theaderColor?: string | null;\n\t/**\n\t * Header foreground (`header-content`). Ignored unless\n\t * `advancedColorsEnabled`; repaired to black or white when it provably\n\t * falls below the WCAG AA 4.5:1 floor against `headerColor`.\n\t */\n\theaderTextColor?: string | null;\n\t/** Type-scale preset. Defaults to `default`. */\n\ttextSize?: BrandTextSize;\n\t/** Corner-radius preset. Defaults to `round`. */\n\tcornerStyle?: BrandCornerStyle;\n}\n\ntype Rgb = readonly [number, number, number];\n\n/**\n * Lovelace's `color-background-default` per scheme (`tokens.css`). The surface\n * behind every chat control.\n */\nexport const LOVELACE_SURFACES = {\n\tlight: \"#ffffff\",\n\tdark: \"#181a1e\",\n} as const satisfies Record<\"light\" | \"dark\", string>;\n\n/**\n * The `isLegibleAgainst` floor: WCAG 1.4.11's 3:1 for a non-text mark such as a\n * focus ring.\n */\nconst MIN_CONTRAST_RATIO = 3;\n\n/** WCAG AA floor for the normal-size title and controls painted in the header. */\nconst MIN_HEADER_CONTENT_CONTRAST = 4.5;\n\n/**\n * `color-accent-500` (tokens.css), the token default for `color-content-accent`. It is\n * only 2.9:1 on the dark surface, so a tint the hex parser cannot read gets this colour\n * adjusted the same way as a hex tint.\n */\nconst FALLBACK_ACCENT = \"#4e53d1\";\n\n/**\n * Hex only, deliberately: enumerating what we can measure keeps the guards\n * honest. CSS may understand values this parser does not; those pass through\n * the mapping byte-for-byte rather than being judged blind.\n */\nfunction parseHexColor(color: string): Rgb | null {\n\tconst match = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim());\n\tif (!match) {\n\t\treturn null;\n\t}\n\n\tconst hex = match[1];\n\tconst full =\n\t\thex.length === 3\n\t\t\t? hex\n\t\t\t\t\t.split(\"\")\n\t\t\t\t\t.map((c) => c + c)\n\t\t\t\t\t.join(\"\")\n\t\t\t: hex;\n\n\treturn [\n\t\tNumber.parseInt(full.slice(0, 2), 16),\n\t\tNumber.parseInt(full.slice(2, 4), 16),\n\t\tNumber.parseInt(full.slice(4, 6), 16),\n\t] as const;\n}\n\nfunction channelLuminance(channel: number): number {\n\tconst srgb = channel / 255;\n\n\treturn srgb <= 0.03928 ? srgb / 12.92 : ((srgb + 0.055) / 1.055) ** 2.4;\n}\n\nfunction relativeLuminance([r, g, b]: Rgb): number {\n\treturn (\n\t\t0.2126 * channelLuminance(r) +\n\t\t0.7152 * channelLuminance(g) +\n\t\t0.0722 * channelLuminance(b)\n\t);\n}\n\nfunction contrastRatio(a: Rgb, b: Rgb): number {\n\tconst la = relativeLuminance(a);\n\tconst lb = relativeLuminance(b);\n\n\treturn (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);\n}\n\n/**\n * WCAG contrast ratio between two hex colors, or `null` when either cannot be parsed.\n *\n * Exported so a caller that needs its own floor reuses this colorimetry instead of\n * writing a second copy. Judging *which* colors to compare is still the caller's job.\n */\nexport function contrastRatioBetween(a: string, b: string): number | null {\n\tconst rgbA = parseHexColor(a);\n\tconst rgbB = parseHexColor(b);\n\n\treturn rgbA && rgbB ? contrastRatio(rgbA, rgbB) : null;\n}\n\n/**\n * Pick black or white for ordinary text painted on a customer-provided accent fill.\n *\n * One of the two always clears WCAG AA against a valid opaque hex color (their\n * luminances sit at opposite ends of the scale). An unparseable color keeps the\n * caller's existing fallback: CSS may understand a value this deliberately-small\n * parser does not, and silently changing its text color without measuring it would\n * only trade one unknown rendering for another.\n */\nexport function readableTextOnAccent(accent: string, fallback: string): string {\n\tconst whiteRatio = contrastRatioBetween(\"#ffffff\", accent);\n\tconst blackRatio = contrastRatioBetween(\"#000000\", accent);\n\tif (whiteRatio === null || blackRatio === null) {\n\t\treturn fallback;\n\t}\n\treturn whiteRatio >= blackRatio ? \"#ffffff\" : \"#000000\";\n}\n\n/**\n * Whether `color` is safe to use as accent foreground on `surface`.\n *\n * Kept when it clears the floor, and also kept when it is merely below the floor but\n * still no worse than `fallback` — the color withholding would leave in place — so the\n * guard can never trade a tint for a *less* legible replacement. A value it cannot\n * parse fails closed: an unreadable `surface` means the judgement cannot be made, and\n * an unparseable `fallback` only forfeits that second chance.\n *\n * Both clauses are evaluated against `surface` and nothing else, so a caller that\n * paints the tint over a *composite* of itself and the surface gets a verdict that\n * overstates the real contrast.\n */\nexport function isLegibleAgainst(\n\tcolor: string,\n\tsurface: string,\n\tfallback: string,\n): boolean {\n\tconst rgb = parseHexColor(color);\n\tconst surfaceRgb = parseHexColor(surface);\n\tif (!rgb || !surfaceRgb) {\n\t\treturn false;\n\t}\n\n\tconst tintRatio = contrastRatio(rgb, surfaceRgb);\n\tif (tintRatio >= MIN_CONTRAST_RATIO) {\n\t\treturn true;\n\t}\n\n\tconst fallbackRgb = parseHexColor(fallback);\n\n\treturn (\n\t\tfallbackRgb !== null && tintRatio >= contrastRatio(fallbackRgb, surfaceRgb)\n\t);\n}\n\n/** WCAG 1.4.3 AA floor for the normal-size labels painted on the accent fill. */\nconst MIN_ON_ACCENT_CONTENT_CONTRAST = 4.5;\n\n/**\n * The hover, focus and pressed opacities of the state layer a control paints over its\n * fill. They mirror `opacity-state-*`, which `tokens.css` defines and this module cannot\n * read.\n */\nconst STATE_LAYER_OPACITIES = [0.08, 0.1, 0.1] as const;\n\n/**\n * The alpha a browser paints for a CSS opacity. Chromium stores the opacity in 8 bits, so\n * `0.1` paints as 26/255, which is stronger than 0.1.\n */\nconst paintedAlpha = (opacity: number) => Math.round(opacity * 255) / 255;\n\n/**\n * The colour a control on the accent fill paints its hover, focus and pressed state\n * layer in. `content` is the control's own text colour.\n */\nexport type AccentStateLayer = \"content\" | \"lighten\" | \"darken\";\n\n/** The layer colours that move the accent fill away from its text. */\nexport const ACCENT_STATE_LAYER_COLORS = {\n\tlighten: \"#ffffff\",\n\tdarken: \"#000000\",\n} as const satisfies Record<Exclude<AccentStateLayer, \"content\">, string>;\n\n/**\n * Round each channel toward the layer. 8-bit blending can land on either side of the\n * exact value, and this side gives the lower contrast against a layer in the text colour.\n */\nfunction blendTowardLayer(layer: Rgb, fill: Rgb, alpha: number): Rgb {\n\tconst channel = (i: 0 | 1 | 2) => {\n\t\tconst blended = fill[i] + (layer[i] - fill[i]) * alpha;\n\t\treturn layer[i] < fill[i] ? Math.floor(blended) : Math.ceil(blended);\n\t};\n\n\treturn [channel(0), channel(1), channel(2)] as const;\n}\n\n/** The lowest contrast `text` has on `fill` at rest and in each state of a `layer`. */\nfunction lowestStateContrast(text: Rgb, fill: Rgb, layer: Rgb): number {\n\treturn Math.min(\n\t\tcontrastRatio(text, fill),\n\t\t...STATE_LAYER_OPACITIES.map((opacity) =>\n\t\t\tcontrastRatio(text, blendTowardLayer(layer, fill, paintedAlpha(opacity))),\n\t\t),\n\t);\n}\n\n/**\n * Pick the state-layer colour for text painted on an accent fill.\n *\n * The default layer is the text colour, so each state moves the fill toward the text.\n * On a mid-tone fill that layer drops the text below 4.5:1, so the layer moves the fill\n * away from the text instead. A light or dark fill with black or white text keeps the\n * default, because it has little room to move away from its text. A custom text colour can\n * pass at rest on a black or white fill and fail under the default layer. Then the layer\n * keeps 4.5:1 and cannot change that fill. This function does not repair text that fails at\n * rest. A value the hex parser cannot read keeps the default.\n */\nexport function accentStateLayer(\n\tfill: string | undefined,\n\tcontent: string | undefined,\n): AccentStateLayer {\n\tconst fillRgb = fill === undefined ? null : parseHexColor(fill);\n\tconst contentRgb = content === undefined ? null : parseHexColor(content);\n\tif (!fillRgb || !contentRgb) {\n\t\treturn \"content\";\n\t}\n\n\tif (\n\t\tlowestStateContrast(contentRgb, fillRgb, contentRgb) >=\n\t\tMIN_ON_ACCENT_CONTENT_CONTRAST\n\t) {\n\t\treturn \"content\";\n\t}\n\n\treturn relativeLuminance(contentRgb) < relativeLuminance(fillRgb)\n\t\t? \"lighten\"\n\t\t: \"darken\";\n}\n\n/**\n * The controls that paint a state layer on an accent fill, with the tokens of their fill\n * and their text. Each list starts with the token the control reads and ends with the\n * semantic token that token defaults to.\n */\nconst ACCENT_STATE_LAYER_TOKENS = {\n\taccent: {\n\t\tfill: [\"color-background-accent\"],\n\t\tcontent: [\"color-content-on-accent\"],\n\t},\n\tbuttonPrimary: {\n\t\tfill: [\"button-primary-background\", \"color-background-accent\"],\n\t\tcontent: [\"button-primary-content\", \"color-content-on-accent\"],\n\t},\n\tchipSelected: {\n\t\tfill: [\"chip-background-selected\", \"color-background-accent\"],\n\t\tcontent: [\"color-content-on-accent\"],\n\t},\n} as const satisfies Record<\n\tstring,\n\tRecord<\"fill\" | \"content\", readonly LovelaceOverridableToken[]>\n>;\n\n/** A group of controls that read the same fill and text tokens on the accent fill. */\nexport type AccentStateLayerTarget = keyof typeof ACCENT_STATE_LAYER_TOKENS;\n\n/**\n * Pick the state-layer colour of each group of accent controls from the fill and the text\n * that `override` gives that group. A component token in `override` changes the decision\n * for its own group only.\n */\nexport function accentStateLayers(\n\toverride: BrandOverrides | undefined,\n): Record<AccentStateLayerTarget, AccentStateLayer> {\n\tconst effective = (tokens: readonly LovelaceOverridableToken[]) =>\n\t\ttokens.map((token) => override?.[token]).find((value) => value != null);\n\tconst layerFor = (target: AccentStateLayerTarget) =>\n\t\taccentStateLayer(\n\t\t\teffective(ACCENT_STATE_LAYER_TOKENS[target].fill),\n\t\t\teffective(ACCENT_STATE_LAYER_TOKENS[target].content),\n\t\t);\n\n\treturn {\n\t\taccent: layerFor(\"accent\"),\n\t\tbuttonPrimary: layerFor(\"buttonPrimary\"),\n\t\tchipSelected: layerFor(\"chipSelected\"),\n\t};\n}\n\n/** WCAG 1.4.3 AA floor for the normal-size labels painted in the accent colour. */\nconst MIN_ACCENT_TEXT_CONTRAST = 4.5;\n\n/**\n * The surfaces accent text sits on: `color-background-default` and\n * `color-background-secondary` (tokens.css). The CSAT action button paints its accent\n * glyph on the second one in hover.\n */\nconst ACCENT_TEXT_SURFACES = {\n\tlight: [LOVELACE_SURFACES.light, \"#f9fafb\"],\n\tdark: [LOVELACE_SURFACES.dark, \"#1f2937\"],\n} as const satisfies Record<\"light\" | \"dark\", readonly string[]>;\n\n/** The colour accent text moves toward when it is too close to the surface. */\nconst ACCENT_TEXT_TARGETS = {\n\tlight: [0, 0, 0],\n\tdark: [255, 255, 255],\n} as const satisfies Record<\"light\" | \"dark\", Rgb>;\n\nconst MIX_STEPS = 255;\n\n/**\n * The lowest contrast `text` has as accent text on `scheme`. A control that paints\n * accent text, such as `PillButton`, paints its state layer in that text colour on the\n * default surface, so each state is one of the backgrounds.\n */\nfunction accentTextContrast(text: Rgb, scheme: \"light\" | \"dark\"): number {\n\tconst surfaces = ACCENT_TEXT_SURFACES[scheme]\n\t\t.map(parseHexColor)\n\t\t.filter((rgb): rgb is Rgb => rgb !== null);\n\n\treturn Math.min(\n\t\tlowestStateContrast(text, surfaces[0], text),\n\t\t...surfaces.slice(1).map((surface) => contrastRatio(text, surface)),\n\t);\n}\n\n/**\n * Whether `color` holds WCAG 1.4.3's 4.5:1 as accent text on `scheme`'s Lovelace\n * surfaces, at rest and in the strongest state. A value the hex parser cannot read\n * fails.\n */\nexport function isLegibleOnSurface(\n\tcolor: string,\n\tscheme: \"light\" | \"dark\",\n): boolean {\n\tconst rgb = parseHexColor(color);\n\n\treturn (\n\t\trgb !== null && accentTextContrast(rgb, scheme) >= MIN_ACCENT_TEXT_CONTRAST\n\t);\n}\n\nfunction mixToward(color: Rgb, target: Rgb, amount: number): Rgb {\n\tconst channel = (i: 0 | 1 | 2) =>\n\t\tMath.round(color[i] + (target[i] - color[i]) * amount);\n\n\treturn [channel(0), channel(1), channel(2)] as const;\n}\n\nfunction toHexColor(rgb: Rgb): string {\n\treturn `#${rgb.map((c) => c.toString(16).padStart(2, \"0\")).join(\"\")}`;\n}\n\n/**\n * Return `color` as accent text that holds 4.5:1 on `scheme`. A colour that holds it\n * already comes back byte-for-byte. Other colours mix toward black on light or white\n * on dark, in the smallest step that holds it, which keeps the hue. A value the hex\n * parser cannot read gets the same treatment applied to `color-accent-500`.\n *\n * No single colour holds 4.5:1 on both the light and the dark surface, so the result\n * depends on the scheme.\n */\nfunction accentTextOnSurface(color: string, scheme: \"light\" | \"dark\"): string {\n\tconst rgb = parseHexColor(color);\n\tif (!rgb) {\n\t\treturn accentTextOnSurface(FALLBACK_ACCENT, scheme);\n\t}\n\tif (accentTextContrast(rgb, scheme) >= MIN_ACCENT_TEXT_CONTRAST) {\n\t\treturn color;\n\t}\n\n\tconst target = ACCENT_TEXT_TARGETS[scheme];\n\tfor (let step = 1; step < MIX_STEPS; step += 1) {\n\t\tconst mixed = mixToward(rgb, target, step / MIX_STEPS);\n\t\tif (accentTextContrast(mixed, scheme) >= MIN_ACCENT_TEXT_CONTRAST) {\n\t\t\treturn toHexColor(mixed);\n\t\t}\n\t}\n\n\treturn toHexColor(target);\n}\n\n/**\n * Keep a customer's configured pair when it is readable, and repair only a\n * valid pair that we can prove falls below AA. Named colors, alpha colors and\n * other values outside the shared hex parser stay byte-for-byte unchanged: CSS\n * may understand them, but choosing a replacement without measuring them would\n * exchange one unknown rendering for another.\n */\nfunction resolveHeaderContentColor(\n\tbackground: string,\n\tcontent: string,\n): string {\n\tconst ratio = contrastRatioBetween(background, content);\n\tif (ratio === null || ratio >= MIN_HEADER_CONTENT_CONTRAST) {\n\t\treturn content;\n\t}\n\treturn readableTextOnAccent(background, content);\n}\n\n/**\n * Text-size and corner style are consumer concerns: Lovelace ships only its\n * default scale, so the mapping re-points the semantic/component tokens at the\n * right Lovelace *primitives* per preset (values stay owned by the token sheet;\n * only the mapping lives here). Only tokens that differ from the default are listed.\n */\nconst TEXT_SIZE_TOKEN_OVERRIDES = {\n\tdefault: {},\n\tsmall: {\n\t\t\"font-size-caption\": t(\"font-size-12\"),\n\t\t\"line-height-caption\": t(\"font-line-height-16\"),\n\t\t\"font-size-body\": t(\"font-size-14\"),\n\t\t\"line-height-body\": t(\"font-line-height-18\"),\n\t\t\"font-size-title-small\": t(\"font-size-16\"),\n\t\t\"line-height-title-small\": t(\"font-line-height-22\"),\n\t\t\"font-size-title-large\": t(\"font-size-18\"),\n\t\t\"line-height-title-large\": t(\"font-line-height-24\"),\n\t},\n\tlarge: {\n\t\t\"font-size-caption\": t(\"font-size-16\"),\n\t\t\"line-height-caption\": t(\"font-line-height-22\"),\n\t\t\"font-size-body\": t(\"font-size-18\"),\n\t\t\"line-height-body\": t(\"font-line-height-24\"),\n\t\t\"font-size-title-small\": t(\"font-size-20\"),\n\t\t\"line-height-title-small\": t(\"font-line-height-26\"),\n\t\t\"font-size-title-large\": t(\"font-size-22\"),\n\t\t\"line-height-title-large\": t(\"font-line-height-28\"),\n\t},\n} satisfies Record<BrandTextSize, BrandOverrides>;\n\nconst CORNER_STYLE_TOKEN_OVERRIDES = {\n\tround: {},\n\trectangular: {\n\t\t\"border-radius-message\": t(\"border-radius-10\"),\n\t\t\"border-radius-composer\": t(\"border-radius-10\"),\n\t\t\"border-radius-media-preview\": t(\"border-radius-10\"),\n\t\t\"border-radius-container\": t(\"border-radius-10\"),\n\t},\n} satisfies Record<BrandCornerStyle, BrandOverrides>;\n\n/**\n * Map customer brand settings to Lovelace token overrides — exactly how the Ada\n * widget applies your brand colors. Feed the result to\n * `<LovelaceProvider override={...}>`.\n *\n * `scheme` is the *resolved* light/dark surface being painted (resolve `auto`\n * via `useResolvedTheme`), not the configured theme — the accent-text contrast\n * decision has to be made against the surface the label actually lands on.\n *\n * The mapping, in order:\n *\n * 1. `cornerStyle` / `textSize` re-point radius and typography tokens at\n * Lovelace primitives (`rectangular` flattens radii to 10px; `small`/`large`\n * shift the four-step type scale down/up one step).\n * 2. `tintColor` paints `color-background-accent` unconditionally, and\n * `color-content-on-accent` becomes whichever of black or white contrasts\n * more with the tint. The tint is also standalone accent *text*\n * (`color-content-accent`). A tint that falls below 4.5:1 on the rendered\n * surface, at rest or in any state, is darkened on light or lightened on\n * dark until it holds 4.5:1. A tint the hex parser cannot read gets the\n * accent text that `color-accent-500` gets.\n * 3. With `advancedColorsEnabled`, `textOverAccentColor` replaces the derived\n * on-accent text color verbatim.\n * 4. With `headerColorEnabled`, `headerColor` paints `header-background`.\n * The header foreground is `headerTextColor` (advanced colors only),\n * repaired to black or white when it measurably falls below the WCAG AA\n * 4.5:1 floor against `headerColor`; without a configured foreground the\n * maximum-contrast black or white is derived from the background.\n *\n * Legibility guards measure hex colors only. A value the hex parser cannot read\n * (a named color, `rgba()`, `var()`, …) is not repaired where the mapping keeps\n * it: repairing what cannot be measured would trade one unknown rendering for\n * another. An unparseable tint is the exception. It still paints\n * `color-background-accent` as configured, but its accent text is\n * `color-accent-500` adjusted to hold 4.5:1, as step 2 says.\n */\nexport function createBrandOverrides(\n\tsettings: BrandSettings,\n\tscheme: \"light\" | \"dark\",\n): BrandOverrides {\n\tconst overrides: BrandOverrides = {\n\t\t...CORNER_STYLE_TOKEN_OVERRIDES[settings.cornerStyle ?? \"round\"],\n\t\t...TEXT_SIZE_TOKEN_OVERRIDES[settings.textSize ?? \"default\"],\n\t};\n\tif (settings.tintColor) {\n\t\toverrides[\"color-background-accent\"] = settings.tintColor;\n\t\toverrides[\"color-content-on-accent\"] = readableTextOnAccent(\n\t\t\tsettings.tintColor,\n\t\t\t\"#ffffff\",\n\t\t);\n\t\toverrides[\"color-content-accent\"] = accentTextOnSurface(\n\t\t\tsettings.tintColor,\n\t\t\tscheme,\n\t\t);\n\t}\n\tif (settings.advancedColorsEnabled && settings.textOverAccentColor) {\n\t\toverrides[\"color-content-on-accent\"] = settings.textOverAccentColor;\n\t}\n\tif (settings.headerColorEnabled && settings.headerColor) {\n\t\toverrides[\"header-background\"] = settings.headerColor;\n\t\tif (settings.advancedColorsEnabled && settings.headerTextColor) {\n\t\t\toverrides[\"header-content\"] = resolveHeaderContentColor(\n\t\t\t\tsettings.headerColor,\n\t\t\t\tsettings.headerTextColor,\n\t\t\t);\n\t\t} else if (contrastRatioBetween(settings.headerColor, \"#000000\") !== null) {\n\t\t\t// The dashboard derives the header foreground from a configured background\n\t\t\t// unless advanced colours explicitly supply one. The parse guard above\n\t\t\t// makes this fallback unreachable, so select the maximum-contrast black\n\t\t\t// or white foreground without implying a color-scheme preference.\n\t\t\toverrides[\"header-content\"] = readableTextOnAccent(\n\t\t\t\tsettings.headerColor,\n\t\t\t\t\"#000000\",\n\t\t\t);\n\t\t}\n\t}\n\treturn overrides;\n}\n"],"mappings":";;AAkEA,IAAa,IAAoB;CAChC,OAAO;CACP,MAAM;AACP,GAMM,IAAqB,GAGrB,IAA8B,KAO9B,IAAkB;AAOxB,SAAS,EAAc,GAA2B;CACjD,IAAM,IAAQ,gCAAgC,KAAK,EAAM,KAAK,CAAC;CAC/D,IAAI,CAAC,GACJ,OAAO;CAGR,IAAM,IAAM,EAAM,IACZ,IACL,EAAI,WAAW,IACZ,EACC,MAAM,EAAE,CAAC,CACT,KAAK,MAAM,IAAI,CAAC,CAAC,CACjB,KAAK,EAAE,IACR;CAEJ,OAAO;EACN,OAAO,SAAS,EAAK,MAAM,GAAG,CAAC,GAAG,EAAE;EACpC,OAAO,SAAS,EAAK,MAAM,GAAG,CAAC,GAAG,EAAE;EACpC,OAAO,SAAS,EAAK,MAAM,GAAG,CAAC,GAAG,EAAE;CACrC;AACD;AAEA,SAAS,EAAiB,GAAyB;CAClD,IAAM,IAAO,IAAU;CAEvB,OAAO,KAAQ,SAAU,IAAO,UAAU,IAAO,QAAS,UAAU;AACrE;AAEA,SAAS,EAAkB,CAAC,GAAG,GAAG,IAAiB;CAClD,OACC,QAAS,EAAiB,CAAC,IAC3B,QAAS,EAAiB,CAAC,IAC3B,QAAS,EAAiB,CAAC;AAE7B;AAEA,SAAS,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