@nulib/canopy 0.0.0 → 2.0.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.
Files changed (141) hide show
  1. package/bin/canopy.js +16 -0
  2. package/lib/AGENTS.md +107 -0
  3. package/lib/base-path.js +67 -0
  4. package/lib/build/appearance.js +76 -0
  5. package/lib/build/assets.js +30 -0
  6. package/lib/build/build.js +350 -0
  7. package/lib/build/dev.js +1355 -0
  8. package/lib/build/devtoast.config.json +6 -0
  9. package/lib/build/devtoast.css +14 -0
  10. package/lib/build/facets.js +19 -0
  11. package/lib/build/fonts.js +105 -0
  12. package/lib/build/iiif.js +2990 -0
  13. package/lib/build/log.js +31 -0
  14. package/lib/build/mdx.js +1967 -0
  15. package/lib/build/package-names.js +111 -0
  16. package/lib/build/pages.js +420 -0
  17. package/lib/build/rehype-task-list-labels.js +54 -0
  18. package/lib/build/runtimes.js +141 -0
  19. package/lib/build/search-index.js +55 -0
  20. package/lib/build/search-text.js +160 -0
  21. package/lib/build/search-workflow.js +72 -0
  22. package/lib/build/search.js +302 -0
  23. package/lib/build/sitemap.js +197 -0
  24. package/lib/build/styles.js +140 -0
  25. package/lib/build/theme-tokens.js +78 -0
  26. package/lib/build/ui-styles.js +25 -0
  27. package/lib/build/upgrade-notices.js +121 -0
  28. package/lib/cli.js +64 -0
  29. package/lib/common.js +580 -0
  30. package/lib/components/bibliography.js +197 -0
  31. package/lib/components/featured.js +237 -0
  32. package/lib/components/hero-slider-runtime.js +215 -0
  33. package/lib/components/image-story-runtime-entry.js +249 -0
  34. package/lib/components/map-runtime.js +114 -0
  35. package/lib/components/metadata-index.js +32 -0
  36. package/lib/components/nav-place.js +227 -0
  37. package/lib/components/navigation.js +381 -0
  38. package/lib/components/referenced.js +217 -0
  39. package/lib/components/slider-runtime-entry.js +106 -0
  40. package/lib/components/timeline-runtime.js +80 -0
  41. package/lib/components/viewer-runtime-entry.js +88 -0
  42. package/lib/config-path.js +78 -0
  43. package/lib/default-locale.js +113 -0
  44. package/lib/discovery/browser-loader.js +107 -0
  45. package/lib/discovery/browser.js +115 -0
  46. package/lib/discovery/config.js +25 -0
  47. package/lib/discovery/envelope.js +124 -0
  48. package/lib/discovery/export.js +226 -0
  49. package/lib/discovery/ingestion.js +62 -0
  50. package/lib/discovery/pagination.js +66 -0
  51. package/lib/discovery/provenance.js +79 -0
  52. package/lib/discovery/search.js +81 -0
  53. package/lib/discovery/tools.js +155 -0
  54. package/lib/discovery/validation.js +121 -0
  55. package/lib/head.js +154 -0
  56. package/lib/iiif/thumbnail.js +402 -0
  57. package/lib/index.js +9 -0
  58. package/lib/locale-runtime.js +69 -0
  59. package/lib/locales.js +203 -0
  60. package/lib/orchestrator.js +231 -0
  61. package/lib/page-context.js +24 -0
  62. package/lib/search/bundle.js +25 -0
  63. package/lib/search/data.js +202 -0
  64. package/lib/search/engine.js +86 -0
  65. package/lib/search/index-cache.js +95 -0
  66. package/lib/search/search-app.jsx +835 -0
  67. package/lib/search/search-engine-runtime.js +44 -0
  68. package/lib/search/search-form-runtime.js +462 -0
  69. package/lib/search/search.js +676 -0
  70. package/package.json +109 -3
  71. package/types/base-path.d.ts +4 -0
  72. package/types/orchestrator.d.ts +17 -0
  73. package/ui/dist/index.mjs +7358 -0
  74. package/ui/dist/index.mjs.map +7 -0
  75. package/ui/dist/server.mjs +12731 -0
  76. package/ui/dist/server.mjs.map +7 -0
  77. package/ui/fonts/OFL.txt +98 -0
  78. package/ui/fonts/google-sans-flex-latin-ext.woff2 +0 -0
  79. package/ui/fonts/google-sans-flex-latin.woff2 +0 -0
  80. package/ui/fonts/source-serif-4-italic-latin-ext.woff2 +0 -0
  81. package/ui/fonts/source-serif-4-italic-latin.woff2 +0 -0
  82. package/ui/fonts/source-serif-4-latin-ext.woff2 +0 -0
  83. package/ui/fonts/source-serif-4-latin.woff2 +0 -0
  84. package/ui/styles/base/_common.scss +97 -0
  85. package/ui/styles/base/_heading.scss +61 -0
  86. package/ui/styles/base/_markdown.scss +188 -0
  87. package/ui/styles/base/_type.scss +51 -0
  88. package/ui/styles/base/_utilities.scss +7 -0
  89. package/ui/styles/base/index.scss +5 -0
  90. package/ui/styles/components/_article-card.scss +108 -0
  91. package/ui/styles/components/_band.scss +104 -0
  92. package/ui/styles/components/_bibliography.scss +88 -0
  93. package/ui/styles/components/_buttons.scss +143 -0
  94. package/ui/styles/components/_card.scss +109 -0
  95. package/ui/styles/components/_diagram.scss +76 -0
  96. package/ui/styles/components/_feature.scss +66 -0
  97. package/ui/styles/components/_footer.scss +136 -0
  98. package/ui/styles/components/_gallery.scss +476 -0
  99. package/ui/styles/components/_image-story.scss +56 -0
  100. package/ui/styles/components/_interstitial-hero.scss +625 -0
  101. package/ui/styles/components/_layout.scss +83 -0
  102. package/ui/styles/components/_map.scss +296 -0
  103. package/ui/styles/components/_metadata-index.scss +68 -0
  104. package/ui/styles/components/_nav-tree.scss +80 -0
  105. package/ui/styles/components/_referenced-items.scss +69 -0
  106. package/ui/styles/components/_sub-navigation.scss +208 -0
  107. package/ui/styles/components/_swoosh.scss +50 -0
  108. package/ui/styles/components/_timeline.scss +350 -0
  109. package/ui/styles/components/_work.scss +71 -0
  110. package/ui/styles/components/header/_header.scss +294 -0
  111. package/ui/styles/components/header/_logo.scss +99 -0
  112. package/ui/styles/components/header/_navbar.scss +143 -0
  113. package/ui/styles/components/header/index.scss +3 -0
  114. package/ui/styles/components/iiif/_image.scss +32 -0
  115. package/ui/styles/components/iiif/_slider.scss +5 -0
  116. package/ui/styles/components/iiif/index.scss +2 -0
  117. package/ui/styles/components/index.scss +25 -0
  118. package/ui/styles/components/modal/_modal.scss +130 -0
  119. package/ui/styles/components/modal/index.scss +1 -0
  120. package/ui/styles/components/search/_filters.scss +259 -0
  121. package/ui/styles/components/search/_form.scss +187 -0
  122. package/ui/styles/components/search/_panel.scss +7 -0
  123. package/ui/styles/components/search/_results.scss +241 -0
  124. package/ui/styles/components/search/_tabs.scss +60 -0
  125. package/ui/styles/components/search/index.scss +4 -0
  126. package/ui/styles/index.css +5007 -0
  127. package/ui/styles/index.scss +11 -0
  128. package/ui/styles/settings/_breakpoints.scss +7 -0
  129. package/ui/styles/settings/_focus.scss +15 -0
  130. package/ui/styles/settings/_tokens.scss +170 -0
  131. package/ui/styles/settings/_type.scss +126 -0
  132. package/ui/styles/version-parity.css +116 -0
  133. package/ui/tailwind-canopy-iiif-plugin.js +39 -0
  134. package/ui/tailwind-canopy-iiif-preset.js +32 -0
  135. package/ui/tailwind-config.d.ts +29 -0
  136. package/ui/tailwind-config.js +159 -0
  137. package/ui/tailwind-default.config.mjs +4 -0
  138. package/ui/theme-color.js +130 -0
  139. package/ui/theme-palette.js +337 -0
  140. package/ui/theme.js +153 -0
  141. package/README.md +0 -3
@@ -0,0 +1,159 @@
1
+ /**
2
+ * @deprecated Removed in the next major release. Tailwind v4 reads a JS
3
+ * config only through `@config`, and the Canopy preset and plugin it wires up
4
+ * no longer add styles. Customize Tailwind in `app/styles/index.css` instead.
5
+ */
6
+ const path = require("node:path");
7
+ const {fileURLToPath} = require("node:url");
8
+
9
+ const canopyPreset = require("./tailwind-canopy-iiif-preset.js");
10
+ const canopyPlugin = require("./tailwind-canopy-iiif-plugin.js");
11
+
12
+ const DEFAULT_SAFELIST = ["canopy-footer", "canopy-footer__inner"];
13
+ const canopyUiDist = path.join(__dirname, "dist");
14
+ const canopyLibRoot = path.join(__dirname, "..");
15
+
16
+ const toArray = (value) => {
17
+ if (!value && value !== 0) return [];
18
+ return Array.isArray(value) ? value : [value];
19
+ };
20
+
21
+ const normalizeGlob = (filepath) => filepath.replace(/\\+/g, "/");
22
+
23
+ const mergeTheme = (baseTheme = {}, overrideTheme = {}) => {
24
+ if (!overrideTheme || typeof overrideTheme !== "object") return baseTheme;
25
+ const result = {...baseTheme, ...overrideTheme};
26
+ if (baseTheme.extend || overrideTheme.extend) {
27
+ result.extend = {
28
+ ...(baseTheme.extend || {}),
29
+ ...(overrideTheme.extend || {}),
30
+ };
31
+ }
32
+ return result;
33
+ };
34
+
35
+ const resolveProjectRoot = (metaUrl, explicitRoot) => {
36
+ if (explicitRoot) return path.resolve(explicitRoot);
37
+ if (metaUrl) {
38
+ const href = typeof metaUrl === "string" ? metaUrl : metaUrl.href;
39
+ if (href) {
40
+ const fromUrl = fileURLToPath(href);
41
+ const dir = path.dirname(fromUrl);
42
+ if (dir.includes(`${path.sep}node_modules${path.sep}`)) {
43
+ return process.cwd();
44
+ }
45
+ return dir;
46
+ }
47
+ }
48
+ return process.cwd();
49
+ };
50
+
51
+ const buildBaseContent = (root, includeCanopySources = true) => {
52
+ const content = [
53
+ normalizeGlob(path.join(root, "content/**/*.{mdx,html}")),
54
+ normalizeGlob(path.join(root, "app/**/*.{js,ts,jsx,tsx}")),
55
+ ];
56
+
57
+ if (includeCanopySources) {
58
+ content.push(
59
+ normalizeGlob(path.join(canopyUiDist, "**/*.{js,mjs,jsx,tsx}")),
60
+ normalizeGlob(path.join(canopyLibRoot, "iiif/components/**/*.{js,jsx}"))
61
+ );
62
+ }
63
+
64
+ return content;
65
+ };
66
+
67
+ const isUrlLike = (value) => {
68
+ if (!value) return false;
69
+ if (typeof value === "string") return true;
70
+ return typeof value.href === "string" && typeof value.protocol === "string";
71
+ };
72
+
73
+ let warnedDeprecated = false;
74
+
75
+ function warnDeprecated() {
76
+ if (warnedDeprecated) return;
77
+ warnedDeprecated = true;
78
+ process.emitWarning(
79
+ "@nulib/canopy/ui/tailwind-config is deprecated and does nothing. " +
80
+ "Tailwind v4 reads a JS config only through @config, and Canopy's preset and plugin no longer add styles. " +
81
+ "Customize Tailwind in app/styles/index.css: https://nulib-ds.github.io/canopy/docs/theme/#tailwind",
82
+ {type: "DeprecationWarning", code: "CANOPY_TAILWIND_CONFIG"},
83
+ );
84
+ }
85
+
86
+ function defineCanopyTailwindConfig(metaUrlOrOptions, maybeOptions) {
87
+ warnDeprecated();
88
+ const hasMetaArgument = isUrlLike(metaUrlOrOptions);
89
+ const metaUrl = hasMetaArgument ? metaUrlOrOptions : undefined;
90
+ const options = (hasMetaArgument ? maybeOptions : metaUrlOrOptions) || {};
91
+
92
+ const {
93
+ root,
94
+ content,
95
+ presets,
96
+ plugins,
97
+ safelist,
98
+ theme,
99
+ includeCanopyPreset = true,
100
+ includeCanopyPlugin = true,
101
+ includeCanopySafelist = true,
102
+ includeCanopySources = true,
103
+ ...rest
104
+ } = options;
105
+
106
+ const projectRoot = resolveProjectRoot(metaUrl, root);
107
+ const baseContent = buildBaseContent(projectRoot, includeCanopySources);
108
+ const basePresets = includeCanopyPreset ? [canopyPreset] : [];
109
+ const basePlugins = includeCanopyPlugin ? [canopyPlugin] : [];
110
+ const baseSafelist = includeCanopySafelist ? DEFAULT_SAFELIST.slice() : [];
111
+
112
+ const config = {
113
+ content: baseContent.slice(),
114
+ safelist: baseSafelist.slice(),
115
+ theme: {extend: {}},
116
+ ...rest,
117
+ };
118
+
119
+ if (content !== undefined) {
120
+ config.content = baseContent.concat(toArray(content));
121
+ }
122
+
123
+ if (presets !== undefined) {
124
+ if (presets === false) {
125
+ config.presets = [];
126
+ } else {
127
+ config.presets = toArray(presets);
128
+ }
129
+ } else if (includeCanopyPreset) {
130
+ config.presets = basePresets.slice();
131
+ }
132
+
133
+ if (plugins !== undefined) {
134
+ if (plugins === false) {
135
+ config.plugins = [];
136
+ } else {
137
+ config.plugins = toArray(plugins);
138
+ }
139
+ } else if (includeCanopyPlugin) {
140
+ config.plugins = basePlugins.slice();
141
+ }
142
+
143
+ if (safelist !== undefined) {
144
+ config.safelist =
145
+ safelist === false ? [] : baseSafelist.concat(toArray(safelist));
146
+ }
147
+
148
+ if (theme) {
149
+ config.theme = mergeTheme(config.theme, theme);
150
+ }
151
+
152
+ config.theme = mergeTheme(config.theme, {colors: {}});
153
+
154
+ return config;
155
+ }
156
+
157
+ module.exports = defineCanopyTailwindConfig;
158
+ module.exports.defineCanopyTailwindConfig = defineCanopyTailwindConfig;
159
+ module.exports.default = defineCanopyTailwindConfig;
@@ -0,0 +1,4 @@
1
+ // @deprecated Removed in the next major release; see tailwind-config.js.
2
+ import defineCanopyTailwindConfig from './tailwind-config.js';
3
+
4
+ export default defineCanopyTailwindConfig(import.meta.url);
@@ -0,0 +1,130 @@
1
+ // Color math for the theme: WCAG 2.2 contrast, and mixing in OKLab, so a step moves
2
+ // toward black or white only as far as a ratio needs, keeping its hue. Pure, with no
3
+ // fs or config access: theme-palette.js bundles it into the SSR UI entry.
4
+
5
+ const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i;
6
+
7
+ function parseHex(value) {
8
+ const match = HEX.exec(String(value == null ? "" : value).trim());
9
+ if (!match) return null;
10
+ let hex = match[1].toLowerCase();
11
+ if (hex.length === 3) {
12
+ hex = hex
13
+ .split("")
14
+ .map((digit) => digit + digit)
15
+ .join("");
16
+ }
17
+ const value24 = parseInt(hex, 16);
18
+ return [(value24 >> 16) & 255, (value24 >> 8) & 255, value24 & 255];
19
+ }
20
+
21
+ function toHex(rgb) {
22
+ return `#${rgb
23
+ .map((channel) =>
24
+ Math.max(0, Math.min(255, Math.round(channel)))
25
+ .toString(16)
26
+ .padStart(2, "0"),
27
+ )
28
+ .join("")}`;
29
+ }
30
+
31
+ // `#rgb` or `#rrggbb`, with or without its `#`, as `#rrggbb`; "" for anything else.
32
+ function normalizeHex(value) {
33
+ const rgb = parseHex(value);
34
+ return rgb ? toHex(rgb) : "";
35
+ }
36
+
37
+ function toLinear(channel) {
38
+ const value = channel / 255;
39
+ return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
40
+ }
41
+
42
+ function fromLinear(value) {
43
+ const clipped = Math.max(0, Math.min(1, value));
44
+ return (
45
+ 255 *
46
+ (clipped <= 0.0031308 ? 12.92 * clipped : 1.055 * clipped ** (1 / 2.4) - 0.055)
47
+ );
48
+ }
49
+
50
+ // WCAG 2.2 relative luminance and contrast ratio, unrounded.
51
+ function luminance(hex) {
52
+ const [r, g, b] = parseHex(hex).map(toLinear);
53
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
54
+ }
55
+
56
+ function contrast(a, b) {
57
+ const [lighter, darker] = [luminance(a), luminance(b)].sort((x, y) => y - x);
58
+ return (lighter + 0.05) / (darker + 0.05);
59
+ }
60
+
61
+ // sRGB ↔ OKLab (Björn Ottosson's matrices). Out-of-gamut results clip per channel.
62
+ function toOklab(hex) {
63
+ const [r, g, b] = parseHex(hex).map(toLinear);
64
+ const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
65
+ const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
66
+ const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
67
+ return [
68
+ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
69
+ 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
70
+ 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
71
+ ];
72
+ }
73
+
74
+ function fromOklab([lightness, a, b]) {
75
+ const l = (lightness + 0.3963377774 * a + 0.2158037573 * b) ** 3;
76
+ const m = (lightness - 0.1055613458 * a - 0.0638541728 * b) ** 3;
77
+ const s = (lightness - 0.0894841775 * a - 1.291485548 * b) ** 3;
78
+ return toHex(
79
+ [
80
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
81
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
82
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
83
+ ].map(fromLinear),
84
+ );
85
+ }
86
+
87
+ // `share` (0 to 1) of `other` mixed into `color`, as CSS's
88
+ // `color-mix(in oklab, color, other share)`.
89
+ function mix(color, other, share) {
90
+ const from = toOklab(color);
91
+ const to = toOklab(other);
92
+ return fromOklab(from.map((value, index) => value + (to[index] - value) * share));
93
+ }
94
+
95
+ // `color` moved toward `toward` (black or white) only as far as it needs to reach `min`
96
+ // against every one of `grounds`: unchanged where it already does. Moving away from a
97
+ // ground's lightness only raises the ratio, so the worst ground decides.
98
+ function ensureContrast(color, grounds, min, toward) {
99
+ const base = normalizeHex(color);
100
+ const list = (Array.isArray(grounds) ? grounds : [grounds]).filter(Boolean);
101
+ const worst = (candidate) =>
102
+ Math.min(...list.map((ground) => contrast(candidate, ground)));
103
+ if (!list.length || worst(base) >= min) return base;
104
+ if (worst(mix(base, toward, 1)) < min) return normalizeHex(toward);
105
+ let low = 0;
106
+ let high = 1;
107
+ for (let step = 0; step < 24; step += 1) {
108
+ const middle = (low + high) / 2;
109
+ if (worst(mix(base, toward, middle)) >= min) high = middle;
110
+ else low = middle;
111
+ }
112
+ // Rounding to a hex can land a hair under; step on until the hex itself passes.
113
+ let share = high;
114
+ let out = mix(base, toward, share);
115
+ while (worst(out) < min && share < 1) {
116
+ share = Math.min(1, share + 0.002);
117
+ out = mix(base, toward, share);
118
+ }
119
+ return out;
120
+ }
121
+
122
+ module.exports = {
123
+ contrast,
124
+ ensureContrast,
125
+ luminance,
126
+ mix,
127
+ normalizeHex,
128
+ parseHex,
129
+ toHex,
130
+ };
@@ -0,0 +1,337 @@
1
+ // Pure palette logic shared by the build (theme.js) and the docs theme
2
+ // showcase, so previews use exactly the tokens a build generates. No fs or
3
+ // config access here: it is bundled into the SSR UI entry.
4
+ const radixColors = require("@radix-ui/colors");
5
+ const {contrast, ensureContrast, mix, normalizeHex} = require("./theme-color");
6
+
7
+ const DEFAULT_APPEARANCE = "light";
8
+
9
+ const LEVELS = [
10
+ "50",
11
+ "100",
12
+ "200",
13
+ "300",
14
+ "400",
15
+ "500",
16
+ "600",
17
+ "700",
18
+ "800",
19
+ "900",
20
+ ];
21
+ const STEP_MAP = {
22
+ 50: 1,
23
+ 100: 2,
24
+ 200: 3,
25
+ 300: 4,
26
+ 400: 7,
27
+ 500: 8,
28
+ 600: 9,
29
+ 700: 10,
30
+ 800: 11,
31
+ 900: 12,
32
+ };
33
+
34
+ const AVAILABLE = new Set(
35
+ Object.keys(radixColors).filter(
36
+ (key) =>
37
+ /^[a-z]+$/.test(key) && radixColors[key] && radixColors[key][`${key}1`],
38
+ ),
39
+ );
40
+
41
+ // `auto` follows the visitor's system setting: the build emits light and dark tokens
42
+ // both (`appearanceVariablesToCss`). A scale itself is only ever light or dark.
43
+ const APPEARANCES = new Set(["light", "dark", "auto"]);
44
+
45
+ // Text and the controls drawn with it hold WCAG 2.2's 4.5:1 (1.4.3) in every palette
46
+ // and appearance (`tests/unit/a11y/contrast.test.js`). A step that falls short is moved
47
+ // toward black on light and white on dark only as far as the ratio needs.
48
+ const TEXT_CONTRAST = 4.5;
49
+ const FAR = {light: "#000000", dark: "#ffffff"};
50
+ // How far a filled accent control's fill moves on, away from its label, under the
51
+ // pointer or focus (`--color-accent-hover`).
52
+ const HOVER_SHARE = 0.2;
53
+
54
+ // Clover 4 (`@nulib/clover`) reads `--clover-color-*` tokens, as 3.16 did. Each one
55
+ // points at a Canopy token rather than a literal value so author overrides of
56
+ // `--color-accent-*` / `--color-gray-*` also reach Clover components.
57
+ // Radix dark scales invert, so the same steps hold for `appearance: dark`.
58
+ const CLOVER_ACCENT_TOKENS = {
59
+ "--clover-color-accent": "--color-accent-default", // accent 800
60
+ "--clover-color-accent-text": "--color-accent-default", // links, Read more
61
+ "--clover-color-accent-alt": "--color-accent-900", // hover states
62
+ };
63
+ const CLOVER_GRAY_TOKENS = {
64
+ "--clover-color-primary": "--color-gray-default", // gray 900, text
65
+ "--clover-color-primary-alt": "--color-gray-900", // no darker step exists
66
+ "--clover-color-primary-muted": "--color-gray-muted", // gray 800, placeholders
67
+ "--clover-color-secondary": "--color-gray-50", // surfaces
68
+ "--clover-color-secondary-alt": "--color-gray-400", // handles, popover arrows
69
+ "--clover-color-secondary-muted": "--color-gray-300", // subtle borders
70
+ };
71
+
72
+ function normalizePaletteName(raw) {
73
+ if (!raw) return "";
74
+ const cleaned = String(raw).trim().toLowerCase();
75
+ const compact = cleaned.replace(/[^a-z]/g, "");
76
+ return AVAILABLE.has(compact) ? compact : "";
77
+ }
78
+
79
+ // `accentColor` names a palette or gives a color as hex (`#0f62fe`, `#06f`).
80
+ function normalizeAccentName(raw) {
81
+ return normalizePaletteName(raw) || normalizeHex(raw);
82
+ }
83
+
84
+ function normalizeAppearance(raw) {
85
+ if (!raw) return DEFAULT_APPEARANCE;
86
+ const cleaned = String(raw).trim().toLowerCase();
87
+ return APPEARANCES.has(cleaned) ? cleaned : DEFAULT_APPEARANCE;
88
+ }
89
+
90
+ // The appearance a scale is drawn in: `dark`, or `light` for anything else.
91
+ function scaleAppearance(raw) {
92
+ return normalizeAppearance(raw) === "dark" ? "dark" : "light";
93
+ }
94
+
95
+ function resolveRadixPalette(name, appearance) {
96
+ if (!name || !AVAILABLE.has(name)) return null;
97
+ const paletteKey = appearance === "dark" ? `${name}Dark` : name;
98
+ const palette = radixColors[paletteKey];
99
+ if (palette && palette[`${name}1`]) return palette;
100
+ const fallback = radixColors[name];
101
+ return fallback && fallback[`${name}1`] ? fallback : null;
102
+ }
103
+
104
+ // A hex accent's steps, as Radix numbers them. Steps 1 to 8 mix the accent into the
105
+ // page's ground, step 9 is the accent, and 10 to 12 mix it toward black on light and
106
+ // white on dark, for hover and text. `[share of the accent, what it is mixed with]`.
107
+ const HEX_GROUND = {light: "#ffffff", dark: "#111113"};
108
+ const HEX_STEPS = {
109
+ light: {
110
+ 1: [0.03, "ground"],
111
+ 2: [0.06, "ground"],
112
+ 3: [0.12, "ground"],
113
+ 4: [0.18, "ground"],
114
+ 7: [0.38, "ground"],
115
+ 8: [0.6, "ground"],
116
+ 9: [1, "ground"],
117
+ 10: [0.88, "far"],
118
+ 11: [0.82, "far"],
119
+ 12: [0.45, "far"],
120
+ },
121
+ dark: {
122
+ 1: [0.06, "ground"],
123
+ 2: [0.1, "ground"],
124
+ 3: [0.2, "ground"],
125
+ 4: [0.28, "ground"],
126
+ 7: [0.45, "ground"],
127
+ 8: [0.62, "ground"],
128
+ 9: [1, "ground"],
129
+ 10: [0.9, "far"],
130
+ 11: [0.62, "far"],
131
+ 12: [0.22, "far"],
132
+ },
133
+ };
134
+
135
+ function hexScale(hex, appearance) {
136
+ const mode = appearance === "dark" ? "dark" : "light";
137
+ const scale = {};
138
+ for (const lvl of LEVELS) {
139
+ const [share, withWhat] = HEX_STEPS[mode][STEP_MAP[lvl]];
140
+ const other = withWhat === "far" ? FAR[mode] : HEX_GROUND[mode];
141
+ scale[lvl] = share === 1 ? hex : mix(other, hex, share);
142
+ }
143
+ return scale;
144
+ }
145
+
146
+ // Where text is drawn, as Radix steps of a gray (Tailwind levels: step 1 is 50, step 3 is
147
+ // 200, step 4 is 300): the page; `--canopy-surface`, a panel's solid fill (step 3); and
148
+ // `--canopy-surface-raised`, a card's (the page in light mode, step 4 in dark; a raised
149
+ // thing is lighter than the page in the dark, where a shadow does not read). Kept in step
150
+ // with `ui/styles/settings/_tokens.scss`.
151
+ const GROUND_STEPS = {
152
+ light: {page: 1, surface: 3, raised: 1},
153
+ dark: {page: 1, surface: 3, raised: 4},
154
+ };
155
+ const LEVEL_OF_STEP = {1: "50", 3: "200", 4: "300"};
156
+
157
+ // Every gray palette's steps, as hex: the grounds an accent's text may be drawn on,
158
+ // since `grayColor` may name any of them.
159
+ const groundCache = {};
160
+ function paletteGrounds(appearance, steps) {
161
+ const mode = appearance === "dark" ? "dark" : "light";
162
+ const unique = Array.from(new Set(steps));
163
+ const key = `${mode}:${unique.join(",")}`;
164
+ if (!groundCache[key]) {
165
+ groundCache[key] = Array.from(AVAILABLE).flatMap((name) => {
166
+ const palette = resolveRadixPalette(name, mode);
167
+ if (!palette) return [];
168
+ return unique.map((step) => normalizeHex(palette[`${name}${step}`])).filter(Boolean);
169
+ });
170
+ }
171
+ return groundCache[key];
172
+ }
173
+
174
+ // The text step (level 800) a role draws with, held at 4.5:1. An accent's is drawn on
175
+ // any gray's page and raised cards, and is the fill under its own level 50 (a primary
176
+ // button's label); a gray's is muted text on its own page, panels and raised cards.
177
+ function withTextStep(scale, role, appearance) {
178
+ const mode = appearance === "dark" ? "dark" : "light";
179
+ const steps = GROUND_STEPS[mode];
180
+ const grounds =
181
+ role === "accent"
182
+ ? [...paletteGrounds(mode, [steps.page, steps.raised]), scale["50"]]
183
+ : Array.from(new Set([steps.page, steps.surface, steps.raised])).map(
184
+ (step) => scale[LEVEL_OF_STEP[step]],
185
+ );
186
+ return {
187
+ ...scale,
188
+ 800: ensureContrast(scale["800"], grounds, TEXT_CONTRAST, FAR[appearance]),
189
+ };
190
+ }
191
+
192
+ // A palette's (or, as the accent, a hex color's) Tailwind levels 50 to 900. With
193
+ // `role` (`accent` or `gray`), level 800 is that role's text step, held at 4.5:1;
194
+ // without it, the levels are Radix's own.
195
+ function toTailwindScale(name, options = {}) {
196
+ const appearance = scaleAppearance(options.appearance);
197
+ let scale = null;
198
+ const hex = !AVAILABLE.has(name) && options.role === "accent" ? normalizeHex(name) : "";
199
+ if (hex) {
200
+ scale = hexScale(hex, appearance);
201
+ } else {
202
+ if (!name || !AVAILABLE.has(name)) return null;
203
+ const palette = resolveRadixPalette(name, appearance);
204
+ if (!palette) return null;
205
+ scale = {};
206
+ for (const lvl of LEVELS) {
207
+ const value = palette[`${name}${STEP_MAP[lvl]}`];
208
+ if (!value) return null;
209
+ scale[lvl] = value;
210
+ }
211
+ }
212
+ if (options.role === "accent" || options.role === "gray") {
213
+ return withTextStep(scale, options.role, appearance);
214
+ }
215
+ return scale;
216
+ }
217
+
218
+ // A filled accent control under the pointer or focus: its fill (level 800) moved on,
219
+ // away from its label (level 50), so the label's ratio only rises. A fill already at
220
+ // black (or white) has nowhere further to go, so it moves toward its label instead, as
221
+ // far as the label keeps 4.5:1.
222
+ const HOVER_VISIBLE = 1.15;
223
+ function accentHover(scale, appearance) {
224
+ if (!scale || !scale["800"]) return "";
225
+ const rest = scale["800"];
226
+ const away = mix(rest, FAR[scaleAppearance(appearance)], HOVER_SHARE);
227
+ if (contrast(away, rest) >= HOVER_VISIBLE || !scale["50"]) return away;
228
+ const toward = mix(rest, scale["50"], HOVER_SHARE * 1.5);
229
+ return contrast(toward, scale["50"]) >= TEXT_CONTRAST ? toward : away;
230
+ }
231
+
232
+ // `--color-accent-hover` as CSS: mixed from `--color-accent-default` itself, the same
233
+ // math as `accentHover`, so a site that sets its own accent (the Northwestern template)
234
+ // gets a hover of its own color. The one exception, a fill already at black or white,
235
+ // is the hex `accentHover` chose.
236
+ function accentHoverCss(scale, appearance) {
237
+ const hover = accentHover(scale, appearance);
238
+ if (!hover) return "";
239
+ const far = FAR[scaleAppearance(appearance)];
240
+ if (hover !== mix(scale["800"], far, HOVER_SHARE)) return hover;
241
+ return `color-mix(in oklab, var(--color-accent-default) ${Math.round(
242
+ (1 - HOVER_SHARE) * 100,
243
+ )}%, ${far})`;
244
+ }
245
+
246
+ function buildVariablesMap(brandScale, grayScale, options = {}) {
247
+ const appearance = scaleAppearance(options.appearance);
248
+ const vars = {};
249
+ if (brandScale) {
250
+ for (const lvl of LEVELS) {
251
+ const value = brandScale[lvl];
252
+ if (value) vars[`--color-accent-${lvl}`] = value;
253
+ }
254
+ if (brandScale["800"]) {
255
+ vars["--color-accent-default"] = brandScale["800"];
256
+ vars["--color-accent-hover"] = accentHoverCss(brandScale, appearance);
257
+ }
258
+ }
259
+ if (grayScale) {
260
+ for (const lvl of LEVELS) {
261
+ const value = grayScale[lvl];
262
+ if (value) vars[`--color-gray-${lvl}`] = value;
263
+ }
264
+ if (grayScale["900"]) vars["--color-gray-default"] = grayScale["900"];
265
+ if (grayScale["800"]) vars["--color-gray-muted"] = grayScale["800"];
266
+ }
267
+ if (brandScale) {
268
+ for (const [prop, token] of Object.entries(CLOVER_ACCENT_TOKENS)) {
269
+ vars[prop] = `var(${token})`;
270
+ }
271
+ }
272
+ if (grayScale) {
273
+ for (const [prop, token] of Object.entries(CLOVER_GRAY_TOKENS)) {
274
+ vars[prop] = `var(${token})`;
275
+ }
276
+ }
277
+ vars["color-scheme"] = appearance;
278
+ return vars;
279
+ }
280
+
281
+ function declarations(vars, indent) {
282
+ return Object.entries(vars || {})
283
+ .map(([prop, value]) => `${indent}${prop}: ${value};`)
284
+ .join("\n");
285
+ }
286
+
287
+ function variablesToCss(vars) {
288
+ const entries = Object.entries(vars || {});
289
+ if (!entries.length) return "";
290
+ const body = declarations(vars, " ");
291
+ return `@layer properties {\n :root {\n${body}\n }\n :host {\n${body}\n }\n}`;
292
+ }
293
+
294
+ // `appearance: auto`: the light tokens, the dark ones where the visitor's system asks
295
+ // for dark (unless the page is set `light`), and the dark ones where it is set `dark`.
296
+ // The appearance switch (`lib/build/appearance.js`) sets `light` or `dark` on `<html>`.
297
+ function appearanceVariablesToCss(lightVars, darkVars) {
298
+ const light = declarations(lightVars, " ");
299
+ const dark = declarations(darkVars, " ");
300
+ return [
301
+ "@layer properties {",
302
+ ` :root {\n${light}\n }`,
303
+ ` :host {\n${light}\n }`,
304
+ " @media (prefers-color-scheme: dark) {",
305
+ ` :root:not(.light) {\n${dark}\n }`,
306
+ ` :host {\n${dark}\n }`,
307
+ " }",
308
+ ` :root.dark {\n${dark}\n }`,
309
+ "}",
310
+ ].join("\n");
311
+ }
312
+
313
+ // Representative swatch color for a palette: its light-mode step 9.
314
+ function swatchColor(name) {
315
+ if (!name) return null;
316
+ if (!AVAILABLE.has(name)) return normalizeHex(name) || null;
317
+ return radixColors[name][`${name}9`] || null;
318
+ }
319
+
320
+ module.exports = {
321
+ LEVELS,
322
+ STEP_MAP,
323
+ TEXT_CONTRAST,
324
+ AVAILABLE_PALETTES: Array.from(AVAILABLE).sort(),
325
+ accentHover,
326
+ accentHoverCss,
327
+ appearanceVariablesToCss,
328
+ contrast,
329
+ normalizePaletteName,
330
+ normalizeAccentName,
331
+ normalizeAppearance,
332
+ scaleAppearance,
333
+ toTailwindScale,
334
+ buildVariablesMap,
335
+ variablesToCss,
336
+ swatchColor,
337
+ };