@tonycodes-ui/mortar 0.16.1 → 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
+ }
@@ -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
@@ -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
+ };
@@ -0,0 +1,178 @@
1
+ /* Native theme. SwiftUI and Jetpack Compose cannot read Mortar's stylesheet,
2
+ so a native app takes its colours as literal values, resolved here from a
3
+ brand for both appearances the way tokens.css resolves them: omitted values
4
+ keep Mortar's defaults or derive from primary, and derived colours
5
+ (color-mix(in oklab, …)) are computed in JS. scripts/native-tokens.mjs
6
+ writes the defaults and scales into the Swift and Kotlin token files, and
7
+ tests/native.test.mjs checks them against tokens.css. */
8
+ import { isBrandFont, } from "./brand.js";
9
+ import { hex, mix, parseColour } from "./colour.js";
10
+ /* The appearance defaults in tokens.css. */
11
+ const LIGHT = {
12
+ paper: "#ffffff",
13
+ surface: "#ffffff",
14
+ sunken: "#f5f5f7",
15
+ line: "#e5e5ea",
16
+ lineStrong: "#8b8b90",
17
+ ink: "#000000",
18
+ secondary: "#3a3a3c",
19
+ muted: "#6e6e73",
20
+ success: "#1f7a36",
21
+ warning: "#945700",
22
+ danger: "#c70014",
23
+ info: "#0060c7",
24
+ onFeedback: "#ffffff",
25
+ primary: "#000000",
26
+ onPrimary: "#ffffff",
27
+ codeBackground: "#000000",
28
+ codeInk: "#f5f5f7",
29
+ codeDim: "#8b8b90",
30
+ };
31
+ const DARK = {
32
+ paper: "#000000",
33
+ surface: "#0e0e10",
34
+ sunken: "#1c1c1e",
35
+ line: "#29292c",
36
+ lineStrong: "#727278",
37
+ ink: "#f5f5f7",
38
+ secondary: "#c7c7cc",
39
+ muted: "#98989d",
40
+ success: "#30d158",
41
+ warning: "#ffd60a",
42
+ danger: "#ff6961",
43
+ info: "#5aa9ff",
44
+ onFeedback: "#000000",
45
+ primary: "#ffffff",
46
+ onPrimary: "#000000",
47
+ codeBackground: "#0e0e10",
48
+ codeInk: "#f5f5f7",
49
+ codeDim: "#8b8b90",
50
+ };
51
+ /* The neutral brand's tuned interaction states (no brand at all). */
52
+ const NEUTRAL = {
53
+ light: {
54
+ primaryHover: "#29292c",
55
+ primaryActive: "#3a3a3c",
56
+ selected: "#f2f2f4",
57
+ },
58
+ dark: {
59
+ primaryHover: "#e5e5ea",
60
+ primaryActive: "#c7c7cc",
61
+ selected: "#1c1c1e",
62
+ },
63
+ };
64
+ function colour(value, mode, key) {
65
+ const text = value?.trim();
66
+ if (!text)
67
+ return undefined;
68
+ try {
69
+ return hex(parseColour(text));
70
+ }
71
+ catch {
72
+ throw new Error(`Mortar native colours must be literal and opaque, for ${mode}.${key}: "${text}". Use a hex value, white, black, or rgb(), hsl(), oklab() or oklch().`);
73
+ }
74
+ }
75
+ function palette(tokens, mode, branded) {
76
+ const base = mode === "light" ? LIGHT : DARK;
77
+ const own = (key) => colour(tokens?.[key], mode, key);
78
+ const paper = own("paper") ?? base.paper;
79
+ const primary = own("primary") ?? base.primary;
80
+ const onPrimary = own("onPrimary") ?? base.onPrimary;
81
+ const neutral = NEUTRAL[mode];
82
+ return {
83
+ ...base,
84
+ paper,
85
+ surface: own("surface") ?? base.surface,
86
+ sunken: own("sunken") ?? base.sunken,
87
+ primary,
88
+ primaryHover: own("primaryHover") ??
89
+ (branded ? mix(primary, base.ink, 85) : neutral.primaryHover),
90
+ primaryActive: own("primaryActive") ??
91
+ (branded ? mix(primary, base.ink, 72) : neutral.primaryActive),
92
+ onPrimary,
93
+ accent: own("accent") ?? primary,
94
+ onAccent: own("onAccent") ?? onPrimary,
95
+ focus: own("focus") ?? primary,
96
+ // Dark links default to ink: a dark primary is often too pale for text.
97
+ link: own("link") ?? (mode === "light" ? primary : base.ink),
98
+ selected: own("selected") ??
99
+ (branded
100
+ ? mix(primary, paper, mode === "light" ? 8 : 10)
101
+ : neutral.selected),
102
+ };
103
+ }
104
+ const GENERIC = {
105
+ "sans-serif": "sans-serif",
106
+ "system-ui": "sans-serif",
107
+ "ui-sans-serif": "sans-serif",
108
+ serif: "serif",
109
+ "ui-serif": "serif",
110
+ monospace: "monospace",
111
+ "ui-monospace": "monospace",
112
+ };
113
+ /**
114
+ * A brand font list as family names and a generic family. `var()` entries
115
+ * (which only a browser resolves) and the web's system aliases are dropped;
116
+ * the app bundles or registers the named families itself.
117
+ */
118
+ function font(value, key) {
119
+ const text = value?.trim();
120
+ if (!text)
121
+ return undefined;
122
+ if (!isBrandFont(text))
123
+ throw new Error(`Invalid Mortar brand font for fonts.${key}: "${text}". Use family names, quoted names and generic families separated by commas, e.g. "Inter", sans-serif.`);
124
+ let generic = "sans-serif";
125
+ const families = [];
126
+ for (const entry of text.split(",")) {
127
+ const name = entry.trim().replace(/^(["'])(.*)\1$/, "$2");
128
+ const kind = GENERIC[name.toLowerCase()];
129
+ if (kind)
130
+ generic = kind;
131
+ else if (!/^var\(/i.test(name) && !/^-apple-system$/i.test(name))
132
+ families.push(name);
133
+ }
134
+ return { families, generic };
135
+ }
136
+ const SYSTEM = { families: [], generic: "sans-serif" };
137
+ const SYSTEM_MONO = { families: [], generic: "monospace" };
138
+ /**
139
+ * The native theme for a brand: literal light and dark colours and its fonts.
140
+ * Without a brand, Mortar's neutral defaults, the values in the generated
141
+ * Swift and Kotlin token files. Brand colours must be literal (`var()`,
142
+ * `color-mix()` and `light-dark()` need a browser) and opaque; the accent
143
+ * gradient is not used, so accents use the flat `accent`.
144
+ */
145
+ export function nativeTheme(brand) {
146
+ const body = font(brand?.fonts?.body, "body") ?? SYSTEM;
147
+ return {
148
+ light: palette(brand?.light, "light", Boolean(brand)),
149
+ dark: palette(brand?.dark, "dark", Boolean(brand)),
150
+ fonts: {
151
+ body,
152
+ heading: font(brand?.fonts?.heading, "heading") ?? body,
153
+ mono: SYSTEM_MONO,
154
+ },
155
+ };
156
+ }
157
+ /** Appearance-independent scales from tokens.css, in points (dp on Android). */
158
+ export const NATIVE_SCALES = {
159
+ space: { 1: 4, 2: 8, 3: 12, 4: 16, 6: 24, 8: 32, 10: 40, 12: 48 },
160
+ radius: { xs: 2, sm: 4, md: 8, lg: 12, xl: 16, full: 999 },
161
+ weight: { regular: 400, medium: 500, semibold: 600, bold: 700 },
162
+ /** Letter spacing, in em. */
163
+ tracking: { display: -0.04, heading: -0.02, body: 0, caps: 0.04 },
164
+ /** Line height, as a multiple of the font size. */
165
+ leading: { display: 1.1, heading: 1.3, body: 1.6 },
166
+ /** Control height, padding, font size and row padding per density. */
167
+ density: {
168
+ compact: { control: 30, pad: 10, fontSize: 13, rowPad: 10 },
169
+ comfortable: { control: 36, pad: 14, fontSize: 14, rowPad: 14 },
170
+ spacious: { control: 44, pad: 18, fontSize: 15, rowPad: 18 },
171
+ },
172
+ icon: 16,
173
+ focusOffset: 3,
174
+ /** The minimum hit size on touch screens. */
175
+ touchTarget: 44,
176
+ /** The opacity of the black scrim behind sheets and dialogs. */
177
+ scrimOpacity: 0.5,
178
+ };
package/docs/native.md ADDED
@@ -0,0 +1,36 @@
1
+ # Native apps
2
+
3
+ Mortar is coming to iPhone, iPad and Android as a SwiftUI package and a Jetpack Compose library, tracked in [#95](https://github.com/tonycodes/mortar/issues/95). This page covers what exists so far: the design tokens both libraries are built on.
4
+
5
+ ## Tokens
6
+
7
+ SwiftUI and Compose cannot read `tokens.css`, so each library gets its tokens as literal values in a generated file:
8
+
9
+ | Platform | File | Colours |
10
+ | -------- | ------------------------------------------------------------------------- | -------------------------------------------------- |
11
+ | Apple | `native/apple/Sources/Mortar/MortarTokens.swift` | `UInt32`, `0xRRGGBB` |
12
+ | Android | `native/android/mortar/src/main/kotlin/codes/tony/mortar/MortarTokens.kt` | `Long`, `0xFFRRGGBB`, the form `Color(Long)` takes |
13
+
14
+ Each file holds:
15
+
16
+ - `MortarTokens.light` and `MortarTokens.dark`: a `MortarPalette` for each appearance. Its colours mirror the `--m-*` tokens: `paper`, `surface`, `sunken`, `line`, `lineStrong`, `ink`, `secondary`, `muted`, the four status colours and `onFeedback`, `primary` with `primaryHover`, `primaryActive` and `onPrimary`, `accent` and `onAccent`, `focus`, `link`, `selected`, and the code block colours.
17
+ - `MortarTokens.compact`, `comfortable` and `spacious`: a `MortarDensity` with the control height, padding, font size and row padding.
18
+ - The scales, named after their CSS properties: `space1` to `space12`, `radiusXs` to `radiusFull` (`radius` is `--m-radius`), `weightRegular` to `weightBold`, `trackingDisplay` to `trackingCaps` (in em), `leadingDisplay` to `leadingBody`, `icon`, `focusOffset`, `touchTarget` and `scrimOpacity`. Sizes are in points on Apple and dp on Android.
19
+
20
+ `touchTarget` is Mortar's 44. Material asks for 48dp on Android, so the Compose components will use the larger of the two.
21
+
22
+ ## Brands
23
+
24
+ `nativeTheme(brand)` in `src/native-theme.ts` resolves a `BrandDefinition` to literal light and dark palettes the way `tokens.css` resolves it: omitted values keep the defaults or derive from primary, and `color-mix(in oklab, …)` tints are computed in JS. Brand colours must be literal and opaque (hex, `white`, `black`, `rgb()`, `hsl()`, `oklab()` or `oklch()`). Accents use the flat `accent`, not the gradient. Fonts become family names plus a generic family; the app bundles the fonts itself.
25
+
26
+ It isn't a public entry point yet. The Swift and Kotlin libraries will load a resolved brand at runtime.
27
+
28
+ ## Changing tokens
29
+
30
+ `src/native-theme.ts` holds the defaults and scales; `tests/native.test.mjs` fails when they differ from `tokens.css`, or when a brand resolves differently from the CSS. After changing either, regenerate the files:
31
+
32
+ ```sh
33
+ npm run native:tokens
34
+ ```
35
+
36
+ The test also fails while the generated files are out of date, and compiles them with `swiftc` (Swift 6 language mode) and `kotlinc` when those are installed. CI requires both compilers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonycodes-ui/mortar",
3
- "version": "0.16.1",
3
+ "version": "0.16.2",
4
4
  "description": "Mortar: a shared design foundation, application and marketing UI for React and Tailwind.",
5
5
  "type": "module",
6
6
  "files": [
@@ -58,6 +58,7 @@
58
58
  "build:playground": "tsc --noEmit -p tsconfig.playground.json && vite build",
59
59
  "format:check": "prettier --check .",
60
60
  "release:prepare": "node scripts/release.mjs",
61
+ "native:tokens": "npm run build && node scripts/native-tokens.mjs",
61
62
  "test:visual": "playwright test --config playwright.visual.config.mjs"
62
63
  },
63
64
  "peerDependencies": {