@tonycodes-ui/mortar 0.16.0 → 0.16.2

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.
@@ -0,0 +1,7 @@
1
+ export type Rgb = [number, number, number];
2
+ /** sRGB channels in 0–1 as #rrggbb, clipped to the sRGB gamut. */
3
+ export declare function hex(rgb: Rgb): string;
4
+ /** sRGB channels in 0–1; throws for anything but opaque literal syntax. */
5
+ export declare function parseColour(value: string): Rgb;
6
+ /** color-mix(in oklab, a p%, b) as #rrggbb. */
7
+ export declare function mix(a: string, b: string, percentA: number): string;
package/dist/colour.js ADDED
@@ -0,0 +1,100 @@
1
+ /* Colour maths for brands resolved outside a browser (email, native apps):
2
+ the opaque colour syntax a brand can resolve without one, converted to sRGB
3
+ and written as #rrggbb, and color-mix(in oklab, …) as tokens.css uses it.
4
+ tests/email.test.mjs checks the conversions against Chromium's. */
5
+ const linear = (v) => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
6
+ const gamma = (v) => v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
7
+ function toOklab([r, g, b]) {
8
+ const [lr, lg, lb] = [r, g, b].map(linear);
9
+ const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb);
10
+ const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb);
11
+ const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb);
12
+ return [
13
+ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
14
+ 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
15
+ 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
16
+ ];
17
+ }
18
+ function fromOklab([L, a, b]) {
19
+ const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
20
+ const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
21
+ const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
22
+ return [
23
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
24
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
25
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
26
+ ].map(gamma);
27
+ }
28
+ /** sRGB channels in 0–1 as #rrggbb, clipped to the sRGB gamut. */
29
+ export function hex(rgb) {
30
+ return `#${rgb
31
+ .map((v) => Math.round(Math.min(1, Math.max(0, v)) * 255)
32
+ .toString(16)
33
+ .padStart(2, "0"))
34
+ .join("")}`;
35
+ }
36
+ /** A number, or a percentage of `whole`. */
37
+ function amount(value, whole) {
38
+ const n = parseFloat(value);
39
+ if (!/^[+-]?(?:\d+\.?\d*|\.\d+)%?$/.test(value) || Number.isNaN(n))
40
+ throw new Error();
41
+ return value.endsWith("%") ? (n / 100) * whole : n;
42
+ }
43
+ function hue(value) {
44
+ const m = /^([+-]?(?:\d+\.?\d*|\.\d+))(deg|turn|rad)?$/.exec(value);
45
+ if (!m)
46
+ throw new Error();
47
+ const n = parseFloat(m[1]);
48
+ return m[2] === "turn" ? n * 360 : m[2] === "rad" ? (n * 180) / Math.PI : n;
49
+ }
50
+ function hslToRgb(h, s, l) {
51
+ const k = (n) => (n + h / 30) % 12;
52
+ const a = s * Math.min(l, 1 - l);
53
+ return [0, 8, 4].map((n) => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1)));
54
+ }
55
+ /** sRGB channels in 0–1; throws for anything but opaque literal syntax. */
56
+ export function parseColour(value) {
57
+ const text = value.trim().toLowerCase();
58
+ if (text === "white")
59
+ return [1, 1, 1];
60
+ if (text === "black")
61
+ return [0, 0, 0];
62
+ const short = /^#([0-9a-f])([0-9a-f])([0-9a-f])$/.exec(text);
63
+ if (short)
64
+ return short.slice(1).map((d) => parseInt(d + d, 16) / 255);
65
+ const long = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/.exec(text);
66
+ if (long)
67
+ return long.slice(1).map((d) => parseInt(d, 16) / 255);
68
+ const fn = /^(rgba?|hsla?|oklab|oklch)\(([^()]*)\)$/.exec(text);
69
+ if (!fn)
70
+ throw new Error();
71
+ const [channels, alpha] = fn[2].split("/");
72
+ const parts = channels.trim().split(/[\s,]+/);
73
+ const extraAlpha = parts.length === 4 ? parts.pop() : undefined;
74
+ const opacity = alpha ?? extraAlpha;
75
+ if (opacity !== undefined && amount(opacity.trim(), 1) !== 1)
76
+ throw new Error();
77
+ if (parts.length !== 3)
78
+ throw new Error();
79
+ const [x, y, z] = parts;
80
+ switch (fn[1]) {
81
+ case "rgb":
82
+ case "rgba":
83
+ return [x, y, z].map((v) => amount(v, 255) / 255);
84
+ case "hsl":
85
+ case "hsla":
86
+ return hslToRgb(hue(x), amount(y, 100) / 100, amount(z, 100) / 100);
87
+ case "oklab":
88
+ return fromOklab([amount(x, 1), amount(y, 0.4), amount(z, 0.4)]);
89
+ default: {
90
+ const c = amount(y, 0.4), h = (hue(z) * Math.PI) / 180;
91
+ return fromOklab([amount(x, 1), c * Math.cos(h), c * Math.sin(h)]);
92
+ }
93
+ }
94
+ }
95
+ /** color-mix(in oklab, a p%, b) as #rrggbb. */
96
+ export function mix(a, b, percentA) {
97
+ const t = percentA / 100;
98
+ const [x, y] = [toOklab(parseColour(a)), toOklab(parseColour(b))];
99
+ return hex(fromOklab(x.map((v, i) => v * t + y[i] * (1 - t))));
100
+ }
package/dist/display.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type ImageFallback } from "./image.js";
1
2
  import { type ReactNode } from "react";
2
3
  import type { Tone } from "./primitives.js";
3
4
  import { type HeadingLevel } from "./heading.js";
@@ -88,10 +89,11 @@ export interface GridListItem {
88
89
  /** Avatar or icon shown beside the title. */
89
90
  media?: ReactNode;
90
91
  /** Image across the top of the card. Use alt="" when the title already describes it. */
92
+ /** A cover image. `fallbackSrc` and `fallback` cover a missing or broken image. */
91
93
  cover?: {
92
94
  src: string;
93
95
  alt: string;
94
- };
96
+ } & ImageFallback;
95
97
  /** Badges, dates or counts under the description. */
96
98
  meta?: ReactNode;
97
99
  /** Term/value pairs under the body, e.g. a client card's last invoice and amount. */
package/dist/display.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { KitImage, useImageFallback } from "./image.js";
2
3
  import { useId } from "react";
3
4
  import { KitLink } from "./navigation.js";
4
5
  import { Check, Close, Paperclip, Person } from "./icons.js";
@@ -13,12 +14,14 @@ function initials(name) {
13
14
  .join("");
14
15
  }
15
16
  export function Avatar({ name, src, size = "md", shape = "circle", status, statusPosition = "bottom", placeholder = "initials", decorative = false, className, }) {
17
+ // A broken or expired photo falls back to the placeholder.
18
+ const image = useImageFallback(src ?? "");
16
19
  return (_jsxs("span", { ...componentRoot("Avatar"), className: cx(`m-avatar m-avatar-${size}`, className), "data-shape": shape === "circle" ? undefined : shape, ...(decorative
17
20
  ? { "aria-hidden": true }
18
21
  : {
19
22
  role: "img",
20
23
  "aria-label": status ? `${name}, ${status}` : name,
21
- }), children: [src ? (_jsx("img", { src: src, alt: "" })) : placeholder === "icon" ? (_jsx(Person, { className: "m-avatar-placeholder" })) : (_jsx("span", { "aria-hidden": "true", children: initials(name) })), status && (_jsx("span", { className: "m-avatar-status", "data-status": status, "data-position": statusPosition === "top" ? "top" : undefined, "aria-hidden": "true" }))] }));
24
+ }), children: [image.current ? (_jsx("img", { ref: image.ref, src: image.current, alt: "", onError: image.onError })) : placeholder === "icon" ? (_jsx(Person, { className: "m-avatar-placeholder" })) : (_jsx("span", { "aria-hidden": "true", children: initials(name) })), status && (_jsx("span", { className: "m-avatar-status", "data-status": status, "data-position": statusPosition === "top" ? "top" : undefined, "aria-hidden": "true" }))] }));
22
25
  }
23
26
  export function AvatarGroup({ label, people, max = 4, className, }) {
24
27
  const shown = people.slice(0, max), rest = people.length - shown.length;
@@ -67,7 +70,7 @@ export function DescriptionList({ title, description, headingLevel = 2, items, l
67
70
  */
68
71
  export function GridList({ label, items, columns = 3, itemHeadingLevel = 3, actionsLayout = "inline", className, }) {
69
72
  const Heading = headingTag(itemHeadingLevel);
70
- return (_jsx("ul", { ...componentRoot("GridList"), className: cx("m-grid-list", className), "aria-label": label, "data-columns": columns === 3 ? undefined : columns, "data-actions": actionsLayout === "split" ? "split" : undefined, children: items.map((item) => (_jsxs("li", { children: [item.cover && (_jsx("div", { className: "m-grid-list-cover", children: _jsx("img", { src: item.cover.src, alt: item.cover.alt, loading: "lazy" }) })), _jsxs("div", { className: "m-grid-list-body", children: [item.media && (_jsx("span", { className: "m-grid-list-media", children: item.media })), _jsxs("div", { className: "m-grid-list-text", children: [_jsx(Heading, { className: "m-grid-list-title", children: item.href ? (_jsx(KitLink, { href: item.href, children: item.title })) : (item.title) }), item.description && _jsx("p", { children: item.description }), item.meta && _jsx("div", { className: "m-grid-list-meta", children: item.meta })] })] }), item.details && item.details.length > 0 && (_jsx(TermList, { className: "m-grid-list-details", items: item.details })), item.actions && (_jsx("div", { className: "m-grid-list-actions", children: item.actions }))] }, item.id))) }));
73
+ return (_jsx("ul", { ...componentRoot("GridList"), className: cx("m-grid-list", className), "aria-label": label, "data-columns": columns === 3 ? undefined : columns, "data-actions": actionsLayout === "split" ? "split" : undefined, children: items.map((item) => (_jsxs("li", { children: [item.cover && (_jsx("div", { className: "m-grid-list-cover", children: _jsx(KitImage, { ...item.cover, loading: "lazy" }) })), _jsxs("div", { className: "m-grid-list-body", children: [item.media && (_jsx("span", { className: "m-grid-list-media", children: item.media })), _jsxs("div", { className: "m-grid-list-text", children: [_jsx(Heading, { className: "m-grid-list-title", children: item.href ? (_jsx(KitLink, { href: item.href, children: item.title })) : (item.title) }), item.description && _jsx("p", { children: item.description }), item.meta && _jsx("div", { className: "m-grid-list-meta", children: item.meta })] })] }), item.details && item.details.length > 0 && (_jsx(TermList, { className: "m-grid-list-details", items: item.details })), item.actions && (_jsx("div", { className: "m-grid-list-actions", children: item.actions }))] }, item.id))) }));
71
74
  }
72
75
  /** Files attached to a record, each with a size and a named download link. */
73
76
  export function AttachmentList({ files, label = "Attachments", className, }) {
@@ -1,7 +1,8 @@
1
+ import { type ImageFallback } from "./image.js";
1
2
  import { type ReactNode } from "react";
2
3
  import { type ProgressStep } from "./application.js";
3
4
  import { type HeadingLevel } from "./heading.js";
4
- export interface ProductImage {
5
+ export interface ProductImage extends ImageFallback {
5
6
  src: string;
6
7
  /** Describe the product shown; images are the main product information. */
7
8
  alt: string;
package/dist/ecommerce.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { KitImage } from "./image.js";
2
3
  import { useEffect, useId, useRef, useState, } from "react";
3
4
  import { KitLink } from "./navigation.js";
4
5
  import { ActionLink, Button, Checkbox, EmptyState, Modal, } from "./primitives.js";
@@ -13,7 +14,7 @@ export function ProductList({ label, products, columns = 4, itemHeadingLevel = 3
13
14
  const overlay = variant === "overlay";
14
15
  return (_jsx("ul", { id: id, ...componentRoot("ProductList"), className: cx("m-product-list", className), "aria-label": label, "data-columns": columns, "data-variant": variant === "plain" ? undefined : variant, children: products.map((product) => {
15
16
  const text = (_jsxs(_Fragment, { children: [_jsxs("div", { className: "m-product-info", children: [_jsx(Heading, { className: "m-product-name", children: _jsx(KitLink, { href: product.href, children: product.name }) }), product.meta && _jsx("p", { className: "m-product-meta", children: product.meta }), product.description && (_jsx("p", { className: "m-product-summary", children: product.description }))] }), _jsx("p", { className: "m-product-price", children: product.price })] }));
16
- return (_jsxs("li", { children: [_jsxs("div", { className: "m-product-image", children: [_jsx("img", { src: product.image.src, alt: product.image.alt, loading: "lazy" }), product.badge && (_jsx("span", { className: "m-product-badge", children: product.badge })), overlay && _jsx("div", { className: "m-product-overlay", children: text })] }), !overlay && text, product.swatches && product.swatches.length > 0 && (_jsx("ul", { className: "m-product-swatches", "aria-label": `${product.name} colours`, children: product.swatches.map((colour) => (_jsx("li", { children: _jsx(Swatch, { color: colour.swatch, label: colour.label }) }, colour.label))) })), product.action && (_jsx("div", { className: "m-product-card-action", children: product.action }))] }, product.id));
17
+ return (_jsxs("li", { children: [_jsxs("div", { className: "m-product-image", children: [_jsx(KitImage, { ...product.image, loading: "lazy" }), product.badge && (_jsx("span", { className: "m-product-badge", children: product.badge })), overlay && _jsx("div", { className: "m-product-overlay", children: text })] }), !overlay && text, product.swatches && product.swatches.length > 0 && (_jsx("ul", { className: "m-product-swatches", "aria-label": `${product.name} colours`, children: product.swatches.map((colour) => (_jsx("li", { children: _jsx(Swatch, { color: colour.swatch, label: colour.label }) }, colour.label))) })), product.action && (_jsx("div", { className: "m-product-card-action", children: product.action }))] }, product.id));
17
18
  }) }));
18
19
  }
19
20
  /** Product page body: gallery, price, rating, option pickers, add to bag and details. */
@@ -29,7 +30,7 @@ export function ProductOverview({ name, price, description, images, rating, opti
29
30
  const group = useId();
30
31
  const Heading = headingTag(headingLevel);
31
32
  const shown = images[current] ?? images[0];
32
- return (_jsxs("div", { id: id, ...componentRoot("ProductOverview"), className: cx("m-product-overview", className), children: [gallery === "grid" ? (_jsx("div", { className: "m-product-gallery", "data-gallery": "grid", children: images.map((image) => (_jsx("div", { className: "m-product-image", children: _jsx("img", { src: image.src, alt: image.alt }) }, image.src))) })) : (_jsxs("div", { className: "m-product-gallery", children: [shown && (_jsx("div", { className: "m-product-image", children: _jsx("img", { src: shown.src, alt: shown.alt }) })), images.length > 1 && (_jsx("div", { className: "m-product-thumbs", role: "group", "aria-label": "Product images", children: images.map((image, index) => (_jsx("button", { type: "button", "aria-pressed": index === current, "aria-label": `Show image ${index + 1} of ${images.length}: ${image.alt}`, onClick: () => setCurrent(index), children: _jsx("img", { src: image.src, alt: "" }) }, image.src))) }))] })), _jsxs("div", { className: "m-product-details", children: [!hideName && _jsx(Heading, { className: "m-product-name", children: name }), _jsx("p", { className: "m-product-price", children: price }), rating && (_jsxs("div", { className: "m-rating", children: [_jsx(Stars, { value: rating.value, label: `Rated ${rating.value} out of 5 from ${rating.count} reviews` }), rating.href ? (_jsxs(KitLink, { href: rating.href, children: [rating.count, " reviews"] })) : (_jsxs("span", { "aria-hidden": "true", children: [rating.count, " reviews"] }))] })), description && (_jsx("div", { className: "m-product-description", children: description })), _jsxs("form", { className: "m-product-form", onSubmit: (event) => {
33
+ return (_jsxs("div", { id: id, ...componentRoot("ProductOverview"), className: cx("m-product-overview", className), children: [gallery === "grid" ? (_jsx("div", { className: "m-product-gallery", "data-gallery": "grid", children: images.map((image) => (_jsx("div", { className: "m-product-image", children: _jsx(KitImage, { ...image }) }, image.src))) })) : (_jsxs("div", { className: "m-product-gallery", children: [shown && (_jsx("div", { className: "m-product-image", children: _jsx(KitImage, { ...shown }) })), images.length > 1 && (_jsx("div", { className: "m-product-thumbs", role: "group", "aria-label": "Product images", children: images.map((image, index) => (_jsx("button", { type: "button", "aria-pressed": index === current, "aria-label": `Show image ${index + 1} of ${images.length}: ${image.alt}`, onClick: () => setCurrent(index), children: _jsx(KitImage, { ...image, alt: "" }) }, image.src))) }))] })), _jsxs("div", { className: "m-product-details", children: [!hideName && _jsx(Heading, { className: "m-product-name", children: name }), _jsx("p", { className: "m-product-price", children: price }), rating && (_jsxs("div", { className: "m-rating", children: [_jsx(Stars, { value: rating.value, label: `Rated ${rating.value} out of 5 from ${rating.count} reviews` }), rating.href ? (_jsxs(KitLink, { href: rating.href, children: [rating.count, " reviews"] })) : (_jsxs("span", { "aria-hidden": "true", children: [rating.count, " reviews"] }))] })), description && (_jsx("div", { className: "m-product-description", children: description })), _jsxs("form", { className: "m-product-form", onSubmit: (event) => {
33
34
  event.preventDefault();
34
35
  if (!loading)
35
36
  onAdd?.();
@@ -75,7 +76,7 @@ function CartLines({ items, onQuantityChange, onRemove, label = "Items in your b
75
76
  return (_jsx("ul", { id: id, ...componentRoot("ShoppingCart", isRoot), className: cx("m-cart", className), "aria-label": label, ref: root, children: items.map((item, index) => {
76
77
  const quantityId = `${base}-${item.id}`;
77
78
  const choices = Array.from({ length: Math.max(maxQuantity, item.quantity) }, (_, i) => i + 1);
78
- return (_jsxs("li", { children: [_jsx("div", { className: "m-product-image", children: _jsx("img", { src: item.image.src, alt: item.image.alt, loading: "lazy" }) }), _jsxs("div", { className: "m-cart-info", children: [_jsx(Heading, { className: "m-cart-item-title", children: _jsx(KitLink, { href: item.href, children: item.name }) }), item.meta && _jsx("p", { className: "m-product-meta", children: item.meta }), _jsx("p", { className: "m-product-price", children: item.price }), item.stock && _jsx("p", { className: "m-cart-stock", children: item.stock })] }), _jsxs("div", { className: "m-cart-controls", children: [_jsxs("label", { className: "m-sr-only", htmlFor: quantityId, children: ["Quantity, ", item.name] }), _jsxs("div", { className: "m-select-control", children: [_jsx("select", { id: quantityId, value: item.quantity, onChange: (event) => onQuantityChange(item.id, Number(event.target.value)), children: choices.map((n) => (_jsx("option", { value: n, children: n }, n))) }), _jsx(ChevronDown, {})] }), _jsxs("button", { type: "button", className: "m-cart-remove", onClick: () => {
79
+ return (_jsxs("li", { children: [_jsx("div", { className: "m-product-image", children: _jsx(KitImage, { ...item.image, loading: "lazy" }) }), _jsxs("div", { className: "m-cart-info", children: [_jsx(Heading, { className: "m-cart-item-title", children: _jsx(KitLink, { href: item.href, children: item.name }) }), item.meta && _jsx("p", { className: "m-product-meta", children: item.meta }), _jsx("p", { className: "m-product-price", children: item.price }), item.stock && _jsx("p", { className: "m-cart-stock", children: item.stock })] }), _jsxs("div", { className: "m-cart-controls", children: [_jsxs("label", { className: "m-sr-only", htmlFor: quantityId, children: ["Quantity, ", item.name] }), _jsxs("div", { className: "m-select-control", children: [_jsx("select", { id: quantityId, value: item.quantity, onChange: (event) => onQuantityChange(item.id, Number(event.target.value)), children: choices.map((n) => (_jsx("option", { value: n, children: n }, n))) }), _jsx(ChevronDown, {})] }), _jsxs("button", { type: "button", className: "m-cart-remove", onClick: () => {
79
80
  removing.current = { index, id: item.id };
80
81
  onRemove(item.id);
81
82
  }, children: ["Remove", _jsxs("span", { className: "m-sr-only", children: [" ", item.name] })] })] })] }, item.id));
@@ -172,6 +173,6 @@ export function OrderHistory({ orders, headingLevel = 2, empty = {
172
173
  return (_jsxs("section", { className: "m-order", "aria-labelledby": titleId, children: [_jsxs("header", { className: "m-order-header", children: [_jsxs(Heading, { id: titleId, className: "m-order-title", children: ["Order ", order.number] }), _jsx(TermList, { items: [
173
174
  { term: "Date placed", details: order.date },
174
175
  { term: "Total", details: order.total },
175
- ] }), _jsxs(KitLink, { href: order.href, children: ["View order", _jsxs("span", { className: "m-sr-only", children: [" ", order.number] })] }), order.actions && (_jsx("div", { className: "m-order-actions", children: order.actions }))] }), _jsx("p", { className: "m-order-status", children: order.status }), order.progress && order.progress.length > 0 && (_jsx(ProgressSteps, { className: "m-order-progress", steps: order.progress, variant: "bar", navigation: false, label: `Order ${order.number} progress` })), _jsx("ul", { className: "m-order-items", children: order.items.map((item) => (_jsxs("li", { children: [_jsx("div", { className: "m-product-image", children: _jsx("img", { src: item.image.src, alt: item.image.alt, loading: "lazy" }) }), _jsxs("div", { children: [_jsx("p", { className: "m-order-item-name", children: item.href ? (_jsx(KitLink, { href: item.href, children: item.name })) : (item.name) }), _jsxs("p", { className: "m-product-meta", children: ["Qty ", item.quantity] })] }), _jsx("p", { className: "m-product-price", children: item.price }), item.actions && (_jsx("div", { className: "m-order-item-actions", children: item.actions }))] }, item.id))) })] }, order.id));
176
+ ] }), _jsxs(KitLink, { href: order.href, children: ["View order", _jsxs("span", { className: "m-sr-only", children: [" ", order.number] })] }), order.actions && (_jsx("div", { className: "m-order-actions", children: order.actions }))] }), _jsx("p", { className: "m-order-status", children: order.status }), order.progress && order.progress.length > 0 && (_jsx(ProgressSteps, { className: "m-order-progress", steps: order.progress, variant: "bar", navigation: false, label: `Order ${order.number} progress` })), _jsx("ul", { className: "m-order-items", children: order.items.map((item) => (_jsxs("li", { children: [_jsx("div", { className: "m-product-image", children: _jsx(KitImage, { ...item.image, loading: "lazy" }) }), _jsxs("div", { children: [_jsx("p", { className: "m-order-item-name", children: item.href ? (_jsx(KitLink, { href: item.href, children: item.name })) : (item.name) }), _jsxs("p", { className: "m-product-meta", children: ["Qty ", item.quantity] })] }), _jsx("p", { className: "m-product-price", children: item.price }), item.actions && (_jsx("div", { className: "m-order-item-actions", children: item.actions }))] }, item.id))) })] }, order.id));
176
177
  }) }));
177
178
  }
@@ -6,6 +6,7 @@
6
6
  (color-mix(in oklab, …)) are computed in JS. tests/email.test.mjs checks
7
7
  the defaults against tokens.css. */
8
8
  import { isBrandFont } from "./brand.js";
9
+ import { hex, mix, parseColour } from "./colour.js";
9
10
  /* The light defaults in tokens.css. */
10
11
  const DEFAULTS = {
11
12
  paper: "#ffffff",
@@ -62,95 +63,6 @@ function fontStack(value, key) {
62
63
  });
63
64
  return [...names, fallback].join(", ");
64
65
  }
65
- const linear = (v) => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
66
- const gamma = (v) => v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
67
- function toOklab([r, g, b]) {
68
- const [lr, lg, lb] = [r, g, b].map(linear);
69
- const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb);
70
- const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb);
71
- const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb);
72
- return [
73
- 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
74
- 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
75
- 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
76
- ];
77
- }
78
- function fromOklab([L, a, b]) {
79
- const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
80
- const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
81
- const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
82
- return [
83
- 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
84
- -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
85
- -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
86
- ].map(gamma);
87
- }
88
- /** sRGB channels in 0–1 as #rrggbb, clipped to the sRGB gamut. */
89
- function hex(rgb) {
90
- return `#${rgb
91
- .map((v) => Math.round(Math.min(1, Math.max(0, v)) * 255)
92
- .toString(16)
93
- .padStart(2, "0"))
94
- .join("")}`;
95
- }
96
- /** A number, or a percentage of `whole`. */
97
- function amount(value, whole) {
98
- const n = parseFloat(value);
99
- if (!/^[+-]?(?:\d+\.?\d*|\.\d+)%?$/.test(value) || Number.isNaN(n))
100
- throw new Error();
101
- return value.endsWith("%") ? (n / 100) * whole : n;
102
- }
103
- function hue(value) {
104
- const m = /^([+-]?(?:\d+\.?\d*|\.\d+))(deg|turn|rad)?$/.exec(value);
105
- if (!m)
106
- throw new Error();
107
- const n = parseFloat(m[1]);
108
- return m[2] === "turn" ? n * 360 : m[2] === "rad" ? (n * 180) / Math.PI : n;
109
- }
110
- function hslToRgb(h, s, l) {
111
- const k = (n) => (n + h / 30) % 12;
112
- const a = s * Math.min(l, 1 - l);
113
- return [0, 8, 4].map((n) => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1)));
114
- }
115
- function parseColour(value) {
116
- const text = value.trim().toLowerCase();
117
- if (text === "white")
118
- return [1, 1, 1];
119
- if (text === "black")
120
- return [0, 0, 0];
121
- const short = /^#([0-9a-f])([0-9a-f])([0-9a-f])$/.exec(text);
122
- if (short)
123
- return short.slice(1).map((d) => parseInt(d + d, 16) / 255);
124
- const long = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/.exec(text);
125
- if (long)
126
- return long.slice(1).map((d) => parseInt(d, 16) / 255);
127
- const fn = /^(rgba?|hsla?|oklab|oklch)\(([^()]*)\)$/.exec(text);
128
- if (!fn)
129
- throw new Error();
130
- const [channels, alpha] = fn[2].split("/");
131
- const parts = channels.trim().split(/[\s,]+/);
132
- const extraAlpha = parts.length === 4 ? parts.pop() : undefined;
133
- const opacity = alpha ?? extraAlpha;
134
- if (opacity !== undefined && amount(opacity.trim(), 1) !== 1)
135
- throw new Error();
136
- if (parts.length !== 3)
137
- throw new Error();
138
- const [x, y, z] = parts;
139
- switch (fn[1]) {
140
- case "rgb":
141
- case "rgba":
142
- return [x, y, z].map((v) => amount(v, 255) / 255);
143
- case "hsl":
144
- case "hsla":
145
- return hslToRgb(hue(x), amount(y, 100) / 100, amount(z, 100) / 100);
146
- case "oklab":
147
- return fromOklab([amount(x, 1), amount(y, 0.4), amount(z, 0.4)]);
148
- default: {
149
- const c = amount(y, 0.4), h = (hue(z) * Math.PI) / 180;
150
- return fromOklab([amount(x, 1), c * Math.cos(h), c * Math.sin(h)]);
151
- }
152
- }
153
- }
154
66
  function colour(value, key) {
155
67
  const text = value?.trim();
156
68
  if (!text)
@@ -162,12 +74,6 @@ function colour(value, key) {
162
74
  throw new Error(`Mortar email colours must be literal and opaque, for light.${key}: "${text}". Use a hex value, white, black, or rgb(), hsl(), oklab() or oklch().`);
163
75
  }
164
76
  }
165
- /** color-mix(in oklab, a p%, b) as #rrggbb. */
166
- function mix(a, b, percentA) {
167
- const t = percentA / 100;
168
- const [x, y] = [toOklab(parseColour(a)), toOklab(parseColour(b))];
169
- return hex(fromOklab(x.map((v, i) => v * t + y[i] * (1 - t))));
170
- }
171
77
  /**
172
78
  * The email theme for a brand, in its light appearance: emails render light,
173
79
  * since most clients ignore or override a dark one. Without a brand, Mortar's
package/dist/icons.d.ts CHANGED
@@ -48,3 +48,7 @@ export declare function Eye(): import("react").JSX.Element;
48
48
  export declare function EyeOff(): import("react").JSX.Element;
49
49
  /** Hollow circle: a requirement that is not met yet. */
50
50
  export declare function Circle(): import("react").JSX.Element;
51
+ /** A picture frame, for an image that failed to load. */
52
+ export declare function ImageMissing({ className }: {
53
+ className?: string;
54
+ }): import("react").JSX.Element;
package/dist/icons.js CHANGED
@@ -68,3 +68,7 @@ export function EyeOff() {
68
68
  export function Circle() {
69
69
  return (_jsx(Icon, { children: _jsx(Stroke, { d: "M10 15a5 5 0 1 0 0-10 5 5 0 0 0 0 10z", width: "1.5" }) }));
70
70
  }
71
+ /** A picture frame, for an image that failed to load. */
72
+ export function ImageMissing({ className }) {
73
+ return (_jsxs(Icon, { className: className, stroke: "currentColor", strokeWidth: "1.5", children: [_jsx("rect", { x: "2.75", y: "3.75", width: "14.5", height: "12.5", rx: "2" }), _jsx("circle", { cx: "7.25", cy: "8", r: "1.5" }), _jsx("path", { d: "m3 14.5 4.25-4 3 2.75 2.5-2.25 4.25 3.75" })] }));
74
+ }
@@ -0,0 +1,32 @@
1
+ import { type ReactNode } from "react";
2
+ /** Optional fallbacks on an image a component renders from data. */
3
+ export interface ImageFallback {
4
+ /** Shown once if `src` fails or is empty. Not retried if it fails too. */
5
+ fallbackSrc?: string;
6
+ /**
7
+ * Shown when every image fails, in the image's reserved box, instead of the
8
+ * default placeholder icon. It is visual only: the image's `alt` names it.
9
+ */
10
+ fallback?: ReactNode;
11
+ }
12
+ /**
13
+ * Which source to show: `src`, then `fallbackSrc` once, then none. Each step
14
+ * happens at most once per `src`, so a failing fallback can't loop, and a new
15
+ * `src` starts over. An error that fired before hydration attached `onError`
16
+ * is caught on mount from the image's own state.
17
+ */
18
+ export declare function useImageFallback(src: string, fallbackSrc?: string): {
19
+ current: string | undefined;
20
+ ref: import("react").RefObject<HTMLImageElement | null>;
21
+ onError: () => void;
22
+ };
23
+ /**
24
+ * An image with the kit's fallback: `fallbackSrc` once, then `fallback` or a
25
+ * placeholder icon in the same reserved box (the container sets the size), so
26
+ * nothing shifts. The placeholder carries the image's accessible name.
27
+ */
28
+ export declare function KitImage({ src, alt, fallbackSrc, fallback, loading, }: ImageFallback & {
29
+ src: string;
30
+ alt: string;
31
+ loading?: "lazy";
32
+ }): import("react").JSX.Element;
package/dist/image.js ADDED
@@ -0,0 +1,42 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ // Images the kit renders from data (covers, product photos, avatars, logos),
3
+ // with a one-shot fallback when the image fails. Internal: not exported.
4
+ import { useLayoutEffect, useRef, useState } from "react";
5
+ import { ImageMissing } from "./icons.js";
6
+ /**
7
+ * Which source to show: `src`, then `fallbackSrc` once, then none. Each step
8
+ * happens at most once per `src`, so a failing fallback can't loop, and a new
9
+ * `src` starts over. An error that fired before hydration attached `onError`
10
+ * is caught on mount from the image's own state.
11
+ */
12
+ export function useImageFallback(src, fallbackSrc) {
13
+ const [failed, setFailed] = useState(null);
14
+ const second = fallbackSrc && fallbackSrc !== src ? fallbackSrc : undefined;
15
+ let stage = failed?.src === src ? failed.stage : 0;
16
+ if (stage === 0 && !src)
17
+ stage = second ? 1 : 2;
18
+ if (stage === 1 && !second)
19
+ stage = 2;
20
+ const current = stage === 0 ? src : stage === 1 ? second : undefined;
21
+ const fail = () => setFailed({ src, stage: stage === 0 && second ? 1 : 2 });
22
+ const ref = useRef(null);
23
+ useLayoutEffect(() => {
24
+ const img = ref.current;
25
+ if (img?.complete && img.naturalWidth === 0 && img.getAttribute("src"))
26
+ fail();
27
+ // `fail` reads this render's stage; re-run only when the source changes.
28
+ // eslint-disable-next-line react-hooks/exhaustive-deps
29
+ }, [current]);
30
+ return { current, ref, onError: fail };
31
+ }
32
+ /**
33
+ * An image with the kit's fallback: `fallbackSrc` once, then `fallback` or a
34
+ * placeholder icon in the same reserved box (the container sets the size), so
35
+ * nothing shifts. The placeholder carries the image's accessible name.
36
+ */
37
+ export function KitImage({ src, alt, fallbackSrc, fallback, loading, }) {
38
+ const image = useImageFallback(src, fallbackSrc);
39
+ if (image.current)
40
+ return (_jsx("img", { ref: image.ref, src: image.current, alt: alt, loading: loading, onError: image.onError }));
41
+ return (_jsx("span", { className: "m-image-fallback", ...(alt ? { role: "img", "aria-label": alt } : { "aria-hidden": true }), children: fallback ? _jsx("span", { "aria-hidden": "true", children: fallback }) : _jsx(ImageMissing, {}) }));
42
+ }
package/dist/marketing.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useImageFallback } from "./image.js";
2
3
  import { IconLinks, KitLink, } from "./navigation.js";
3
4
  import { useId, useState } from "react";
4
5
  import { DialogPanel, DialogTitle } from "@headlessui/react";
@@ -71,9 +72,14 @@ export function Testimonial({ id, className, quote, name, role, logo, avatar, })
71
72
  const person = (_jsxs(_Fragment, { children: [_jsx("strong", { children: name }), role && _jsx("span", { children: role })] }));
72
73
  return (_jsxs("figure", { id: id, className: cx("m-testimonial", className), ...componentRoot("Testimonial"), children: [logo && _jsx("div", { className: "m-testimonial-logo", children: logo }), _jsx("blockquote", { children: quote }), _jsx("figcaption", { children: avatar ? (_jsxs(_Fragment, { children: [_jsx("span", { className: "m-testimonial-avatar", "aria-hidden": "true", children: avatar }), _jsx("span", { className: "m-testimonial-person", children: person })] })) : (person) })] }));
73
74
  }
75
+ /** A logo, or its name as text when there is no image or it fails to load. */
76
+ function Logo({ name, image }) {
77
+ const logo = useImageFallback(image ?? "");
78
+ return logo.current ? (_jsx("img", { ref: logo.ref, src: logo.current, alt: name, onError: logo.onError })) : (_jsx("span", { children: name }));
79
+ }
74
80
  export function LogoCloud({ id, className, label, items, layout = "inline", title, description, actions = [], headingLevel = 2, }) {
75
81
  const Heading = headingTag(headingLevel);
76
- const list = (_jsx("ul", { children: items.map((item) => (_jsx("li", { children: item.image ? (_jsx("img", { src: item.image, alt: item.name })) : (_jsx("span", { children: item.name })) }, item.name))) }));
82
+ const list = (_jsx("ul", { children: items.map((item) => (_jsx("li", { children: _jsx(Logo, { name: item.name, image: item.image }) }, item.name))) }));
77
83
  if (layout === "split") {
78
84
  return (_jsxs("section", { id: id, className: cx("m-logo-cloud", className), "aria-label": label, ...componentRoot("LogoCloud"), "data-layout": "split", children: [_jsxs("div", { className: "m-logo-cloud-copy", children: [title ? (_jsx(Heading, { className: "m-section-title", children: title })) : (_jsx("p", { children: label })), description && (_jsx("p", { className: "m-section-description", children: description })), actions.length > 0 && _jsx(ActionLinks, { actions: actions })] }), list] }));
79
85
  }
@@ -0,0 +1,128 @@
1
+ import { type BrandDefinition } from "./brand.js";
2
+ /** One appearance's colours, as #rrggbb. */
3
+ export interface NativePalette {
4
+ /** The page background. */
5
+ paper: string;
6
+ /** Cards, panels and raised controls. */
7
+ surface: string;
8
+ /** Recessed areas: wells, muted bands and hover fills. */
9
+ sunken: string;
10
+ line: string;
11
+ /** Control borders and other lines that must reach 3:1. */
12
+ lineStrong: string;
13
+ ink: string;
14
+ secondary: string;
15
+ muted: string;
16
+ success: string;
17
+ warning: string;
18
+ danger: string;
19
+ info: string;
20
+ /** Text on a filled status colour. */
21
+ onFeedback: string;
22
+ primary: string;
23
+ /** Pressed-and-held and pointer hover. */
24
+ primaryHover: string;
25
+ /** Pressed. */
26
+ primaryActive: string;
27
+ onPrimary: string;
28
+ accent: string;
29
+ onAccent: string;
30
+ focus: string;
31
+ link: string;
32
+ /** A tint of primary on the paper, for selected rows and items. */
33
+ selected: string;
34
+ codeBackground: string;
35
+ codeInk: string;
36
+ codeDim: string;
37
+ }
38
+ /** A brand font: its family names, then the generic family to fall back to. */
39
+ export interface NativeFont {
40
+ /** Family names in order of preference; empty for the system font. */
41
+ families: string[];
42
+ generic: "sans-serif" | "serif" | "monospace";
43
+ }
44
+ /** Literal colours for both appearances, and fonts, for a native app. */
45
+ export interface NativeTheme {
46
+ light: NativePalette;
47
+ dark: NativePalette;
48
+ fonts: {
49
+ body: NativeFont;
50
+ heading: NativeFont;
51
+ mono: NativeFont;
52
+ };
53
+ }
54
+ /**
55
+ * The native theme for a brand: literal light and dark colours and its fonts.
56
+ * Without a brand, Mortar's neutral defaults, the values in the generated
57
+ * Swift and Kotlin token files. Brand colours must be literal (`var()`,
58
+ * `color-mix()` and `light-dark()` need a browser) and opaque; the accent
59
+ * gradient is not used, so accents use the flat `accent`.
60
+ */
61
+ export declare function nativeTheme(brand?: BrandDefinition): NativeTheme;
62
+ /** Appearance-independent scales from tokens.css, in points (dp on Android). */
63
+ export declare const NATIVE_SCALES: {
64
+ readonly space: {
65
+ readonly 1: 4;
66
+ readonly 2: 8;
67
+ readonly 3: 12;
68
+ readonly 4: 16;
69
+ readonly 6: 24;
70
+ readonly 8: 32;
71
+ readonly 10: 40;
72
+ readonly 12: 48;
73
+ };
74
+ readonly radius: {
75
+ readonly xs: 2;
76
+ readonly sm: 4;
77
+ readonly md: 8;
78
+ readonly lg: 12;
79
+ readonly xl: 16;
80
+ readonly full: 999;
81
+ };
82
+ readonly weight: {
83
+ readonly regular: 400;
84
+ readonly medium: 500;
85
+ readonly semibold: 600;
86
+ readonly bold: 700;
87
+ };
88
+ /** Letter spacing, in em. */
89
+ readonly tracking: {
90
+ readonly display: -0.04;
91
+ readonly heading: -0.02;
92
+ readonly body: 0;
93
+ readonly caps: 0.04;
94
+ };
95
+ /** Line height, as a multiple of the font size. */
96
+ readonly leading: {
97
+ readonly display: 1.1;
98
+ readonly heading: 1.3;
99
+ readonly body: 1.6;
100
+ };
101
+ /** Control height, padding, font size and row padding per density. */
102
+ readonly density: {
103
+ readonly compact: {
104
+ readonly control: 30;
105
+ readonly pad: 10;
106
+ readonly fontSize: 13;
107
+ readonly rowPad: 10;
108
+ };
109
+ readonly comfortable: {
110
+ readonly control: 36;
111
+ readonly pad: 14;
112
+ readonly fontSize: 14;
113
+ readonly rowPad: 14;
114
+ };
115
+ readonly spacious: {
116
+ readonly control: 44;
117
+ readonly pad: 18;
118
+ readonly fontSize: 15;
119
+ readonly rowPad: 18;
120
+ };
121
+ };
122
+ readonly icon: 16;
123
+ readonly focusOffset: 3;
124
+ /** The minimum hit size on touch screens. */
125
+ readonly touchTarget: 44;
126
+ /** The opacity of the black scrim behind sheets and dialogs. */
127
+ readonly scrimOpacity: 0.5;
128
+ };