"""Design templates.
- design.design_system: Color swatches, typography scale, spacing tokens.
- design.component_variants: Grid of component states/sizes/intents.
"""
from __future__ import annotations
import composer as c
from . import register
# --------------------------------------------------------------------------- #
# design_system #
# --------------------------------------------------------------------------- #
@register(
"design.design_system",
summary="Design system reference: color swatches, type scale, spacing tokens.",
spec_keys={
"title": "Page title.",
"subtitle": "Optional sub-headline.",
"colors": "Dict[group_name -> List[{name, hex, token?}]] — e.g. {'Brand': [{'name':'Clay','hex':'#D97757','token':'--clay'}]}.",
"typography": "List[{name, font_family, size, weight?, line_height?, sample?}].",
"spacing": "List[{name, px}] — rendered as a horizontal ruler.",
"radii": "Optional list[{name, px}].",
},
)
def design_system(spec: dict) -> dict:
sections = []
colors = spec.get("colors") or {}
if colors:
groups = []
for group, items in colors.items():
chips = []
for item in items:
hex_v = c.css_color(item.get("hex"), default="#000")
chips.append(
f'
'
f'
{c.esc(item.get("name"))}
'
f'
{c.esc(hex_v)}
' # display value (escaped)
+ (f'
{c.esc(item.get("token"))}
' if item.get("token") else "")
+ '
'
)
groups.append(
f''
f'
{c.esc(group)}
'
f'
{"".join(chips)}
'
f'
'
)
sections.append(c.section("Color", body="".join(groups)))
typography = spec.get("typography") or []
if typography:
rows = []
for t in typography:
style = (f'font-family:{c.esc(t.get("font_family","var(--sans)"))};'
f'font-size:{c.esc(t.get("size","16px"))};'
f'line-height:{c.esc(t.get("line_height","1.4"))};'
f'font-weight:{c.esc(t.get("weight","400"))};')
sample = c.esc(t.get("sample", "The quick brown fox jumps over the lazy dog."))
rows.append(
f''
f'
{sample}
'
f'
'
f'
{c.esc(t.get("name"))}
'
f'
{c.esc(t.get("size",""))} / {c.esc(t.get("weight",""))}
'
)
sections.append(c.section("Typography",
body=f'{"".join(rows)}
'))
spacing = spec.get("spacing") or []
if spacing:
bars = []
for s in spacing:
px = int(s.get("px", 0))
bars.append(
f''
f'
'
f'
{c.esc(s.get("name"))}'
f'
{px}px
'
)
sections.append(c.section("Spacing",
body=f'{"".join(bars)}
'))
radii = spec.get("radii") or []
if radii:
chips = []
for r in radii:
px = int(r.get("px", 0))
chips.append(
f''
f'
'
f'
{c.esc(r.get("name"))}'
f'
{px}px
'
)
sections.append(c.section("Radius",
body=f'{"".join(chips)}
'))
return {
"title": spec.get("title", "Design system"),
"subtitle": spec.get("subtitle"),
"eyebrow_text": spec.get("eyebrow", "DESIGN SYSTEM"),
"body": "".join(sections),
}
# --------------------------------------------------------------------------- #
# component_variants #
# --------------------------------------------------------------------------- #
@register(
"design.component_variants",
summary="Component variants grid: every size/state/intent of a UI component.",
spec_keys={
"title": "Page title.",
"subtitle": "Optional sub-headline.",
"components": "List[{name, description?, axes: {axis_name: [labels...]}, "
"cells: [{coords: {axis_name: label,...}, html}]}]. "
"Cells are rendered as a grid; axes drive row/column headers.",
},
)
def component_variants(spec: dict) -> dict:
sections = []
for comp in spec.get("components", []):
axes = comp.get("axes") or {}
cells = comp.get("cells") or []
axis_names = list(axes.keys())
if len(axis_names) >= 2:
rows_axis, cols_axis = axis_names[0], axis_names[1]
row_labels, col_labels = axes[rows_axis], axes[cols_axis]
lookup: dict[tuple, str] = {}
for cell in cells:
co = cell.get("coords") or {}
lookup[(co.get(rows_axis), co.get(cols_axis))] = cell.get("html", "")
head = " | " + "".join(f"{c.esc(cl)} | " for cl in col_labels) + "
"
body_rows = []
for rl in row_labels:
tds = "".join(
f'{lookup.get((rl, cl), "")} | '
for cl in col_labels
)
body_rows.append(f'| {c.esc(rl)} | {tds}
')
grid_html = f'{head}{"".join(body_rows)}
'
else:
grid_html = c.grid([
c.card(
cell.get("html", ""),
title=", ".join(f"{k}: {v}" for k, v in (cell.get("coords") or {}).items()) or None,
)
for cell in cells
], cols="auto")
sections.append(c.section(
comp.get("name"),
body=(f'{c.esc(comp.get("description"))}
'
if comp.get("description") else "") + grid_html,
))
return {
"title": spec.get("title", "Component variants"),
"subtitle": spec.get("subtitle"),
"eyebrow_text": spec.get("eyebrow", "COMPONENTS"),
"body": "".join(sections),
"page_class": "page page--wide",
}