@behavioral-agentic-design-system/core 0.15.0 → 0.16.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.
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ // Shipped as the `bads-generate-stories` bin. The component SOURCE is
7
+ // package-owned, so resolve it relative to this script's own location inside
8
+ // the core package (works in the monorepo and from node_modules in a fork).
9
+ // The stories OUTPUT and the import-path scope are consumer-facing: stories
10
+ // land in the fork's components/ dir, and imports always reference the core
11
+ // package by its real published name (read from core's own package.json, so a
12
+ // fork's rename never corrupts the scope).
13
+ const CORE = require(path.join(__dirname, '..', 'package.json')).name;
14
+ const componentDir = path.join(__dirname, '..', 'src/components/ui');
15
+ const storiesDir = path.join(process.cwd(), 'components');
16
+
17
+ if (!fs.existsSync(storiesDir)) fs.mkdirSync(storiesDir, { recursive: true });
18
+
19
+ const componentFiles = fs.readdirSync(componentDir)
20
+ .filter(f => f.endsWith('.tsx'))
21
+ .sort();
22
+
23
+ // Extract the first PascalCase export from a component file. Handles:
24
+ // export function Foo(...)
25
+ // export const Foo = ...
26
+ // const Foo = ...; export { Foo }
27
+ function firstExport(source) {
28
+ const patterns = [
29
+ /export\s+function\s+([A-Z][A-Za-z0-9_]*)/,
30
+ /export\s+const\s+([A-Z][A-Za-z0-9_]*)/,
31
+ /export\s*\{\s*([A-Z][A-Za-z0-9_]*)/,
32
+ ];
33
+ for (const re of patterns) {
34
+ const m = source.match(re);
35
+ if (m) return m[1];
36
+ }
37
+ return null;
38
+ }
39
+
40
+ function storyFileName(exportName) {
41
+ return `${exportName}.stories.tsx`;
42
+ }
43
+
44
+ function storyContent(exportName, importPath) {
45
+ return `import { Meta, StoryObj } from "@storybook/react";
46
+ import { ${exportName} } from "${importPath}";
47
+
48
+ const meta: Meta<typeof ${exportName}> = {
49
+ title: "Components/${exportName}",
50
+ component: ${exportName},
51
+ parameters: {
52
+ layout: "centered",
53
+ },
54
+ tags: ["autodocs"],
55
+ };
56
+
57
+ export default meta;
58
+ type Story = StoryObj<typeof meta>;
59
+
60
+ export const Default: Story = {
61
+ args: {},
62
+ };
63
+ `;
64
+ }
65
+
66
+ // Drop any existing stories so we always emit a clean, parseable set.
67
+ const force = process.argv.includes('--force');
68
+ if (force) {
69
+ for (const f of fs.readdirSync(storiesDir)) {
70
+ if (f.endsWith('.stories.tsx')) fs.unlinkSync(path.join(storiesDir, f));
71
+ }
72
+ }
73
+
74
+ const existing = new Set(
75
+ fs.readdirSync(storiesDir).filter(f => f.endsWith('.stories.tsx'))
76
+ );
77
+
78
+ let written = 0;
79
+ let skipped = 0;
80
+
81
+ for (const file of componentFiles) {
82
+ const slug = file.replace('.tsx', '');
83
+ const source = fs.readFileSync(path.join(componentDir, file), 'utf8');
84
+ const exportName = firstExport(source);
85
+ if (!exportName) {
86
+ console.warn(`! skipped ${file} — no PascalCase export found`);
87
+ skipped += 1;
88
+ continue;
89
+ }
90
+ const out = storyFileName(exportName);
91
+ if (existing.has(out)) {
92
+ skipped += 1;
93
+ continue;
94
+ }
95
+ const importPath = `${CORE}/components/ui/${slug}`;
96
+ fs.writeFileSync(path.join(storiesDir, out), storyContent(exportName, importPath));
97
+ console.log(`✓ ${out}`);
98
+ written += 1;
99
+ }
100
+
101
+ console.log(`\nWrote ${written}, skipped ${skipped}.`);
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * oklch-to-hex — convert CSS color values to hex.
4
+ *
5
+ * Used during client onboarding when extracting brand colors from a client's
6
+ * existing CSS (which often uses oklch, hsl, or rgb). brand.config.ts and the
7
+ * Customize tab color picker want hex, so we convert.
8
+ *
9
+ * Usage:
10
+ * node scripts/oklch-to-hex.js "oklch(0.65 0.22 264)"
11
+ * node scripts/oklch-to-hex.js "hsl(220 90% 56%)"
12
+ * node scripts/oklch-to-hex.js "rgb(37 99 235)"
13
+ * node scripts/oklch-to-hex.js path/to/globals.css
14
+ *
15
+ * Single value → prints the hex.
16
+ * File path → prints every color found, one per line, with the original.
17
+ *
18
+ * Note: oklch conversion follows the CSS Color 4 spec (D65 white point).
19
+ */
20
+
21
+ 'use strict';
22
+
23
+ const fs = require('fs');
24
+ const path = require('path');
25
+
26
+ // ---- color math ---------------------------------------------------------
27
+
28
+ function clamp(n, min, max) { return Math.min(max, Math.max(min, n)); }
29
+
30
+ function srgbToHex([r, g, b]) {
31
+ const to = (c) => clamp(Math.round(c * 255), 0, 255).toString(16).padStart(2, '0');
32
+ return `#${to(r)}${to(g)}${to(b)}`;
33
+ }
34
+
35
+ function linearToSrgb(c) {
36
+ const a = Math.abs(c);
37
+ const sign = Math.sign(c);
38
+ return sign * (a > 0.0031308 ? 1.055 * Math.pow(a, 1 / 2.4) - 0.055 : 12.92 * a);
39
+ }
40
+
41
+ function oklabToLinearSrgb(L, a, b) {
42
+ const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
43
+ const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
44
+ const s_ = L - 0.0894841775 * a - 1.2914855480 * b;
45
+
46
+ const l = l_ ** 3;
47
+ const m = m_ ** 3;
48
+ const s = s_ ** 3;
49
+
50
+ return [
51
+ +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
52
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
53
+ -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
54
+ ];
55
+ }
56
+
57
+ function oklchToHex(L, C, h) {
58
+ // L can be 0-1 or 0%-100%; C is unitless (~0-0.4); h is degrees.
59
+ const lightness = L > 1 ? L / 100 : L;
60
+ const hRad = (h * Math.PI) / 180;
61
+ const a = C * Math.cos(hRad);
62
+ const b = C * Math.sin(hRad);
63
+ const linear = oklabToLinearSrgb(lightness, a, b);
64
+ return srgbToHex(linear.map(linearToSrgb));
65
+ }
66
+
67
+ function hslToHex(h, s, l) {
68
+ s /= 100;
69
+ l /= 100;
70
+ const k = (n) => (n + h / 30) % 12;
71
+ const a = s * Math.min(l, 1 - l);
72
+ const f = (n) => l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
73
+ return srgbToHex([f(0), f(8), f(4)]);
74
+ }
75
+
76
+ function rgbToHex(r, g, b) {
77
+ return srgbToHex([r / 255, g / 255, b / 255]);
78
+ }
79
+
80
+ // ---- parser -------------------------------------------------------------
81
+
82
+ function parseNums(s) {
83
+ return s
84
+ .replace(/[\/,]/g, ' ')
85
+ .split(/\s+/)
86
+ .filter(Boolean)
87
+ .map((v) => {
88
+ if (v.endsWith('%')) return parseFloat(v);
89
+ return parseFloat(v);
90
+ });
91
+ }
92
+
93
+ function colorToHex(input) {
94
+ const v = input.trim();
95
+
96
+ if (/^#[0-9a-f]{3,8}$/i.test(v)) return v.toLowerCase();
97
+
98
+ const oklch = v.match(/^oklch\(([^)]+)\)$/i);
99
+ if (oklch) {
100
+ const [L, C, h] = parseNums(oklch[1]);
101
+ return oklchToHex(L, C, h);
102
+ }
103
+
104
+ const hsl = v.match(/^hsla?\(([^)]+)\)$/i);
105
+ if (hsl) {
106
+ const [h, s, l] = parseNums(hsl[1]);
107
+ return hslToHex(h, s, l);
108
+ }
109
+
110
+ const rgb = v.match(/^rgba?\(([^)]+)\)$/i);
111
+ if (rgb) {
112
+ const [r, g, b] = parseNums(rgb[1]);
113
+ return rgbToHex(r, g, b);
114
+ }
115
+
116
+ return null;
117
+ }
118
+
119
+ // ---- entry --------------------------------------------------------------
120
+
121
+ function processFile(filePath) {
122
+ const text = fs.readFileSync(filePath, 'utf8');
123
+ const matches = text.match(/(oklch|hsla?|rgba?)\([^)]+\)|#[0-9a-f]{3,8}/gi) || [];
124
+ const seen = new Set();
125
+ for (const m of matches) {
126
+ const hex = colorToHex(m);
127
+ if (!hex) continue;
128
+ const key = `${m.toLowerCase()}→${hex}`;
129
+ if (seen.has(key)) continue;
130
+ seen.add(key);
131
+ console.log(`${m.padEnd(40)} → ${hex}`);
132
+ }
133
+ }
134
+
135
+ function main() {
136
+ const arg = process.argv.slice(2).join(' ').trim();
137
+ if (!arg) {
138
+ console.error('usage: node scripts/oklch-to-hex.js "<color>" | <path-to-css>');
139
+ process.exit(1);
140
+ }
141
+
142
+ if (fs.existsSync(arg) && fs.statSync(arg).isFile()) {
143
+ processFile(path.resolve(arg));
144
+ return;
145
+ }
146
+
147
+ const hex = colorToHex(arg);
148
+ if (!hex) {
149
+ console.error(`couldn't parse: ${arg}`);
150
+ console.error('expected one of: oklch(...), hsl(...), rgb(...), #hex, or a file path');
151
+ process.exit(1);
152
+ }
153
+ console.log(hex);
154
+ }
155
+
156
+ main();