From eae1fb786a9d46711e108326a4ec9728335b44fe Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 5 Oct 2026 21:27:48 +0200 Subject: [PATCH] feat(web-svc-design): harden palette contrast, add frame tokens The palette derives the link color and the four status colors against all five surfaces (the three surface levels plus their hover and active states) instead of the two content surfaces, and a status color also reaches AA on its own subtle tint. The subtle and border tints of the status colors move from a CSS color-mix into the palette, so both modes carry them as plain values. New tokens --design-frame, --design-on-frame, --design-frame-hover and --design-frame-active give large brand surfaces such as a sidebar a deep tone with AAA text in light and dark mode. The default focus outline reads --design-focus and falls back to --design-link, so a frame can keep its focus indicator visible. The design lookup exposes the generated icon under urls.favicon_ico, because a role may declare a slot named favicon. Slot names must be kebab-case; a violation fails the lookup. resolve_branding returns a bootstrap flag whose default false lives in web-svc-design. Co-Authored-By: Claude Opus 5.5 (1M context) --- plugins/lookup/design.py | 12 ++--- .../templates/css/default.css.j2 | 9 +--- roles/web-app-dashboard/vars/main.yml | 2 +- roles/web-svc-design/meta/services.yml | 1 + .../unit/python/utils/design/test_branding.py | 43 ++++++++++++++++- .../unit/python/utils/design/test_palette.py | 46 ++++++++++++++++++- utils/design/branding.py | 25 +++++++++- utils/design/palette.py | 34 ++++++++++++-- 8 files changed, 148 insertions(+), 24 deletions(-) diff --git a/plugins/lookup/design.py b/plugins/lookup/design.py index 5cdea8419c..689edf184e 100644 --- a/plugins/lookup/design.py +++ b/plugins/lookup/design.py @@ -7,7 +7,7 @@ from ansible.errors import AnsibleError from ansible.plugins.loader import lookup_loader from ansible.plugins.lookup import LookupBase -from utils.design.branding import resolve_branding +from utils.design.branding import asset_urls, resolve_branding ASSET_DIR = "design" @@ -24,7 +24,7 @@ class LookupModule(LookupBase): slots {name: {width, height, text_only}} domain canonical domain of the role dest CDN directory the generated assets are written to - urls {"favicon": url, "": {"png": url, "svg": url}} + urls {"favicon_ico": url, "": {"png": url, "svg": url}} """ def _sub(self, name: str, terms: list, variables: dict[str, Any]) -> Any: @@ -52,18 +52,12 @@ class LookupModule(LookupBase): cdn = self._sub("cdn", [application_id], variables) base_url = f"{cdn['urls']['role']['release']['img'].rstrip('/')}/{ASSET_DIR}" - urls: dict[str, Any] = {"favicon": f"{base_url}/favicon.ico"} - for slot in branding["slots"]: - urls[slot] = { - "png": f"{base_url}/{slot}.png", - "svg": f"{base_url}/{slot}.svg", - } return [ { **branding, "domain": self._sub("domain", [application_id], variables), "dest": str(Path(cdn["role"]["release"]["img"]) / ASSET_DIR), - "urls": urls, + "urls": asset_urls(base_url, branding["slots"]), } ] diff --git a/roles/sys-front-inj-design/templates/css/default.css.j2 b/roles/sys-front-inj-design/templates/css/default.css.j2 index 63d6440ca2..2a7dbe9afa 100644 --- a/roles/sys-front-inj-design/templates/css/default.css.j2 +++ b/roles/sys-front-inj-design/templates/css/default.css.j2 @@ -35,10 +35,6 @@ --design-shadow-3: 0 8px 24px rgba(var(--design-shadow-color)); --design-focus-ring: 0 0 0 3px color-mix(in srgb, var(--design-link) 45%, transparent); --design-focus-ring-inset: inset 0 0 0 1px var(--design-link); -{% for status in ['success', 'warning', 'danger', 'info'] %} - --design-{{ status }}-subtle: color-mix(in srgb, var(--design-{{ status }}) 12%, var(--design-surface-2)); - --design-{{ status }}-border: color-mix(in srgb, var(--design-{{ status }}) 40%, var(--design-surface-2)); -{% endfor %} } :root { --bs-font-sans-serif: var(--design-font); @@ -106,7 +102,6 @@ --bs-table-striped-bg: var(--design-surface-3); --bs-table-hover-bg: rgba(var(--design-primary-rgb), 0.06); } -/* A cascade layer loses to every unlayered rule, so an app's own design system always wins over these element defaults. */ @layer infinito-design { :root { color-scheme: light; @@ -139,7 +134,7 @@ color: var(--bs-link-hover-color); } :focus-visible { - outline: 2px solid var(--design-link); + outline: 2px solid var(--design-focus, var(--design-link)); outline-offset: 2px; } button { @@ -174,7 +169,7 @@ input:focus-visible, textarea:focus-visible, select:focus-visible { - border-color: var(--design-link); + border-color: var(--design-focus, var(--design-link)); outline-offset: -2px; } input:user-invalid, diff --git a/roles/web-app-dashboard/vars/main.yml b/roles/web-app-dashboard/vars/main.yml index 777e94d884..d1b9922421 100644 --- a/roles/web-app-dashboard/vars/main.yml +++ b/roles/web-app-dashboard/vars/main.yml @@ -43,7 +43,7 @@ DASHBOARD_PLATFORM_LOGO_SOURCE: "{{ lookup('design', DASHBOARD_APPLICATION_ID).u | string }}" DASHBOARD_PLATFORM_LOGO_URL: "{{ ('https:' ~ DASHBOARD_PLATFORM_LOGO_SOURCE) if DASHBOARD_PLATFORM_LOGO_SOURCE.startswith('//') else DASHBOARD_PLATFORM_LOGO_SOURCE }}" -DASHBOARD_PLATFORM_FAVICON_SOURCE: "{{ lookup('design', DASHBOARD_APPLICATION_ID).urls.favicon if DASHBOARD_DESIGN_LOGO_ENABLED | bool else service_provider.platform.favicon +DASHBOARD_PLATFORM_FAVICON_SOURCE: "{{ lookup('design', DASHBOARD_APPLICATION_ID).urls.favicon_ico if DASHBOARD_DESIGN_LOGO_ENABLED | bool else service_provider.platform.favicon | string }}" DASHBOARD_PLATFORM_FAVICON_URL: "{{ ('https:' ~ DASHBOARD_PLATFORM_FAVICON_SOURCE) if DASHBOARD_PLATFORM_FAVICON_SOURCE.startswith('//') else DASHBOARD_PLATFORM_FAVICON_SOURCE }}" diff --git a/roles/web-svc-design/meta/services.yml b/roles/web-svc-design/meta/services.yml index 8a1ef376f9..39e1db5b18 100644 --- a/roles/web-svc-design/meta/services.yml +++ b/roles/web-svc-design/meta/services.yml @@ -20,6 +20,7 @@ design: type: 'system-ui, -apple-system, "Segoe UI", Roboto, "Liberation Sans", Arial, sans-serif' logo: assets/img/logo.png title: true + bootstrap: false slots: icon: width: 512 diff --git a/tests/unit/python/utils/design/test_branding.py b/tests/unit/python/utils/design/test_branding.py index 332f5390de..4c538a3173 100644 --- a/tests/unit/python/utils/design/test_branding.py +++ b/tests/unit/python/utils/design/test_branding.py @@ -2,7 +2,7 @@ import tempfile import unittest from pathlib import Path -from utils.design.branding import is_disabled, resolve_branding +from utils.design.branding import asset_urls, is_disabled, resolve_branding GLOBAL_SLOTS = {"icon": {"width": 512, "height": 512}} @@ -31,6 +31,7 @@ class TestResolveBranding(unittest.TestCase): design = { "logo": "assets/logo.png", "title": True, + "bootstrap": False, "slots": GLOBAL_SLOTS, **global_extra, } @@ -52,6 +53,25 @@ class TestResolveBranding(unittest.TestCase): ) self.assertEqual(result["title"], "Platform") + def test_bootstrap_mapping_is_off_unless_a_role_or_the_platform_opts_in( + self, + ) -> None: + self.assertFalse( + resolve_branding(self._apps({}, {}), "web-app-a", self.root)["bootstrap"] + ) + self.assertTrue( + resolve_branding( + self._apps({}, {"bootstrap": True}), "web-app-a", self.root + )["bootstrap"] + ) + self.assertFalse( + resolve_branding( + self._apps({"bootstrap": True}, {"bootstrap": False}), + "web-app-a", + self.root, + )["bootstrap"] + ) + def test_role_override_wins_over_global(self) -> None: apps = self._apps({"title": "Platform"}, {"title": "Own", "logo": "custom.png"}) result = resolve_branding(apps, "web-app-a", self.root) @@ -112,6 +132,27 @@ class TestResolveBranding(unittest.TestCase): with self.assertRaisesRegex(ValueError, "positive"): resolve_branding(apps, "web-app-a", self.root) + def test_slot_name_outside_kebab_case_fails_loudly(self) -> None: + for name in ("favicon_ico", "Logo", "wide.banner", ""): + with self.subTest(name=name): + apps = self._apps({}, {"slots": {name: {"width": 10, "height": 10}}}) + with self.assertRaisesRegex(ValueError, "kebab-case"): + resolve_branding(apps, "web-app-a", self.root) + + +class TestAssetUrls(unittest.TestCase): + def test_a_slot_named_favicon_keeps_the_icon_url(self) -> None: + urls = asset_urls("https://cdn.test/design", {"favicon": {}, "logo": {}}) + self.assertEqual(urls["favicon_ico"], "https://cdn.test/design/favicon.ico") + self.assertEqual( + urls["favicon"], + { + "png": "https://cdn.test/design/favicon.png", + "svg": "https://cdn.test/design/favicon.svg", + }, + ) + self.assertEqual(set(urls), {"favicon_ico", "favicon", "logo"}) + class TestIsDisabled(unittest.TestCase): def test_disabled_spellings(self) -> None: diff --git a/tests/unit/python/utils/design/test_palette.py b/tests/unit/python/utils/design/test_palette.py index 3c529a6a5f..bfe11204ed 100644 --- a/tests/unit/python/utils/design/test_palette.py +++ b/tests/unit/python/utils/design/test_palette.py @@ -50,7 +50,9 @@ class TestPaletteContrast(unittest.TestCase): self._assert_min( t["--design-text-muted"], surfaces, CONTRAST_TEXT, "muted" ) - self._assert_min(t["--design-link"], content, CONTRAST_TEXT, "link") + self._assert_min( + t["--design-link"], surfaces, CONTRAST_TEXT, "link" + ) self._assert_min( t["--design-primary"], content, CONTRAST_UI, "primary" ) @@ -65,7 +67,12 @@ class TestPaletteContrast(unittest.TestCase): ) for status in STATUS_HUES: color = t[f"--design-{status}"] - self._assert_min(color, content, CONTRAST_TEXT, status) + self._assert_min( + color, + [*surfaces, t[f"--design-{status}-subtle"]], + CONTRAST_TEXT, + status, + ) self._assert_min( t[f"--design-on-{status}"], [color], @@ -89,6 +96,10 @@ class TestPaletteContrast(unittest.TestCase): self._assert_min( t["--design-text-muted"], states, CONTRAST_TEXT, "muted" ) + for name in ("link", *STATUS_HUES): + self._assert_min( + t[f"--design-{name}"], states, CONTRAST_TEXT, name + ) for fill in ("primary", *STATUS_HUES): self._assert_min( t[f"--design-on-{fill}"], @@ -97,6 +108,37 @@ class TestPaletteContrast(unittest.TestCase): f"on-{fill}", ) + def test_frame_is_a_deep_brand_surface_in_both_modes(self) -> None: + for base in self.BASES: + palette = build_palette(base) + for mode in self.MODES: + with self.subTest(base=base, mode=mode): + t = palette[mode] + frame = t["--design-frame"] + self._assert_min( + t["--design-on-frame"], + [ + frame, + t["--design-frame-hover"], + t["--design-frame-active"], + ], + CONTRAST_BODY_TEXT, + "on-frame", + ) + self.assertLess( + relative_luminance(frame), + relative_luminance(palette["light"]["--design-surface-3"]), + "the frame must stay darker than every light surface", + ) + self.assertGreaterEqual( + contrast(frame, t["--design-surface-1"]), + 1.2, + "the frame must stand out against the page", + ) + + def test_light_frame_keeps_a_deep_base_unchanged(self) -> None: + self.assertEqual(build_palette("#001f3f")["light"]["--design-frame"], "#001f3f") + def test_light_primary_keeps_a_compliant_base_unchanged(self) -> None: self.assertEqual( build_palette("#001f3f")["light"]["--design-primary"], "#001f3f" diff --git a/utils/design/branding.py b/utils/design/branding.py index e62d68a0be..190776baed 100644 --- a/utils/design/branding.py +++ b/utils/design/branding.py @@ -9,6 +9,7 @@ from utils.cache.files import read_text DESIGN_ROLE = "web-svc-design" _H1_RE = re.compile(r"^#\s+(.+?)\s*$", re.MULTILINE) +_SLOT_NAME_RE = re.compile(r"^[a-z0-9]+(-[a-z0-9]+)*$") def is_disabled(value: object) -> bool: @@ -43,9 +44,28 @@ def role_title(roles_dir: Path, application_id: str) -> str: return match.group(1) +def asset_urls(base_url: str, slots: dict[str, dict]) -> dict[str, object]: + """Map the generated assets of one role onto their CDN URLs. + + Args: + base_url: CDN URL of the directory the assets are written to. + slots: Resolved slots of the role. + + Returns: + ``{"favicon_ico": url, "": {"png": url, "svg": url}}``. Slot + names are kebab-case, so none can take the ``favicon_ico`` key. + """ + urls: dict[str, object] = {"favicon_ico": f"{base_url}/favicon.ico"} + for slot in slots: + urls[slot] = {"png": f"{base_url}/{slot}.png", "svg": f"{base_url}/{slot}.svg"} + return urls + + def _slots(raw: dict) -> dict[str, dict]: slots = {} for name, spec in raw.items(): + if not _SLOT_NAME_RE.match(str(name)): + raise ValueError(f"design slot {name!r} must be named in kebab-case") width, height = int(spec["width"]), int(spec["height"]) if width <= 0 or height <= 0: raise ValueError(f"design slot {name!r} needs a positive width and height") @@ -74,9 +94,11 @@ def resolve_branding( Returns: ``{"logo": | False, "title": | False, "name": , "label": , + "bootstrap": , "slots": {name: {"width", "height", "text_only"}}}``. ``label`` serves installers that require a site name even when the title replacement - is disabled. + is disabled. ``bootstrap`` tells whether the role links the shared + Bootstrap component mapping. Raises: ValueError: When the logo file is missing, a slot is malformed or the @@ -112,5 +134,6 @@ def resolve_branding( "title": title, "name": name, "label": title or name, + "bootstrap": str(pick("bootstrap")).strip().lower() == "true", "slots": _slots({**global_cfg["slots"], **(role_cfg.get("slots") or {})}), } diff --git a/utils/design/palette.py b/utils/design/palette.py index 9cf5b76a30..b661756d1b 100644 --- a/utils/design/palette.py +++ b/utils/design/palette.py @@ -8,6 +8,10 @@ from __future__ import annotations import math import re +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from collections.abc import Callable SCALE_STEPS: tuple[int, ...] = (50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950) _SCALE_LIGHTNESS = (0.97, 0.94, 0.88, 0.80, 0.71, 0.62, 0.54, 0.46, 0.38, 0.30, 0.22) @@ -37,7 +41,9 @@ _SURFACE_STATE_LIGHTNESS = { "light": {"hover": 0.905, "active": 0.875}, "dark": {"hover": 0.30, "active": 0.34}, } +_FRAME_LIGHTNESS = {"light": 0.38, "dark": 0.30} _FILL_STATE_SHIFT = {"hover": 0.04, "active": 0.08} +_STATUS_TINT_SHARE = {"subtle": 0.12, "border": 0.40} _HEX_RE = re.compile(r"^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$") _LIGHTNESS_STEP = 0.005 @@ -171,6 +177,13 @@ def contrast(first: str, second: str) -> float: return (high + 0.05) / (low + 0.05) +def _mix(color: str, surface: str, share: float) -> str: + channels = zip(parse_hex(color), parse_hex(surface), strict=True) + return "#" + "".join( + f"{round((share * c + (1 - share) * s) * 255):02x}" for c, s in channels + ) + + def _ensure_contrast( lightness: float, chroma: float, @@ -178,10 +191,12 @@ def _ensure_contrast( against: list[str], target: float, step: float, + tint: Callable[[str], str] | None = None, ) -> str: while 0.0 <= lightness <= 1.0: candidate = oklch_to_hex(lightness, chroma, hue) - if min(contrast(candidate, surface) for surface in against) >= target: + backgrounds = [*against, tint(candidate)] if tint else against + if min(contrast(candidate, surface) for surface in backgrounds) >= target: return candidate lightness += step raise ValueError(f"no lightness reaches contrast {target} for hue {hue:.1f}") @@ -286,11 +301,21 @@ def _mode_tokens( away, ), **_fill_tokens("primary", primary), + **_fill_tokens( + "frame", + oklch_to_hex( + min(base_l, _FRAME_LIGHTNESS[mode]) + if mode == "light" + else _FRAME_LIGHTNESS[mode], + base_c, + hue, + ), + ), "--design-link": _ensure_contrast( min(base_l, 0.55) if mode == "light" else max(base_l, 0.7), base_c, hue, - content_surfaces, + text_surfaces, CONTRAST_TEXT, away, ), @@ -300,11 +325,14 @@ def _mode_tokens( _STATUS_LIGHTNESS[mode], status_c, status_hue, - content_surfaces, + text_surfaces, CONTRAST_TEXT, away, + tint=lambda fill: _mix(fill, surfaces[1], _STATUS_TINT_SHARE["subtle"]), ) tokens.update(_fill_tokens(name, color)) + for tint, share in _STATUS_TINT_SHARE.items(): + tokens[f"--design-{name}-{tint}"] = _mix(color, surfaces[1], share) return tokens