@uibot/blocks-studio 0.0.0-stage → 0.2.1

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,374 @@
1
+ import type { ActionChain } from "@uibot/contracts";
2
+ import type { RendererComponent } from "@uibot/renderer-react";
3
+
4
+ function cn(...cls: (string | false | null | undefined)[]) {
5
+ return cls.filter(Boolean).join(" ");
6
+ }
7
+
8
+ /** Build a one-step SetState ActionChain targeting `path`. */
9
+ function setState(path: string, value: unknown): ActionChain {
10
+ return [{ action: "SetState", params: { path, value: value as never } }];
11
+ }
12
+
13
+ // ── SearchableTable ───────────────────────────────────────────────────────────
14
+
15
+ interface Column {
16
+ key: string;
17
+ label: string;
18
+ align?: "left" | "center" | "right" | undefined;
19
+ }
20
+
21
+ function parseColumns(raw: unknown): Column[] {
22
+ if (!Array.isArray(raw)) return [];
23
+ return raw
24
+ .filter((c): c is Record<string, unknown> => Boolean(c) && typeof c === "object")
25
+ .map(
26
+ (c): Column => ({
27
+ key: typeof c["key"] === "string" ? c["key"] : "",
28
+ label: typeof c["label"] === "string" ? c["label"] : "",
29
+ align:
30
+ c["align"] === "right" || c["align"] === "center"
31
+ ? (c["align"] as "right" | "center")
32
+ : "left",
33
+ }),
34
+ )
35
+ .filter((c) => c.key);
36
+ }
37
+
38
+ function parseRows(raw: unknown): Array<Record<string, unknown>> {
39
+ return Array.isArray(raw)
40
+ ? raw.filter((r): r is Record<string, unknown> => Boolean(r) && typeof r === "object")
41
+ : [];
42
+ }
43
+
44
+ /**
45
+ * Table with a built-in search box. Typing dispatches a SetState onto
46
+ * `searchKey` (default `search`) so the host's `_filter` expression can narrow
47
+ * `rows`. The block itself also filters its already-resolved `rows` client-side
48
+ * (case-insensitive substring across all column values) so it stays useful even
49
+ * when no host dispatch is wired.
50
+ */
51
+ export const SearchableTable: RendererComponent = (props, ctx) => {
52
+ const columns = parseColumns(props["columns"]);
53
+ const rows = parseRows(props["rows"]);
54
+ const searchKey = typeof props["searchKey"] === "string" ? props["searchKey"] : "search";
55
+ const placeholder = typeof props["placeholder"] === "string" ? props["placeholder"] : "Search…";
56
+ const emptyText =
57
+ typeof props.emptyText === "string" && props.emptyText.trim()
58
+ ? props.emptyText
59
+ : "No matching rows";
60
+ const query = String(ctx.resolve.getState(searchKey) ?? "").toLowerCase();
61
+
62
+ const visibleRows = query
63
+ ? rows.filter((row) =>
64
+ columns.some((col) =>
65
+ String(row[col.key] ?? "")
66
+ .toLowerCase()
67
+ .includes(query),
68
+ ),
69
+ )
70
+ : rows;
71
+
72
+ return (
73
+ <div
74
+ className="uibot-searchable-table space-y-3"
75
+ data-uibot-type="SearchableTable"
76
+ data-empty={visibleRows.length === 0}
77
+ >
78
+ <input
79
+ type="search"
80
+ defaultValue={query}
81
+ placeholder={placeholder}
82
+ aria-label={placeholder}
83
+ className="h-9 w-full max-w-xs rounded-md border bg-background px-3 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring"
84
+ onChange={(e) => ctx.dispatch?.(setState(searchKey, e.currentTarget.value), { event: e })}
85
+ />
86
+ <div
87
+ className="w-full overflow-auto rounded-lg border focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
88
+ role="region"
89
+ aria-label="Table results"
90
+ tabIndex={0}
91
+ >
92
+ <table className="w-full text-sm">
93
+ <thead>
94
+ <tr className="border-b bg-muted/50">
95
+ {columns.map((col) => (
96
+ <th
97
+ key={col.key}
98
+ scope="col"
99
+ className={cn(
100
+ "px-4 py-3 text-xs font-medium uppercase tracking-wide text-muted-foreground",
101
+ col.align === "right"
102
+ ? "text-right"
103
+ : col.align === "center"
104
+ ? "text-center"
105
+ : "text-left",
106
+ )}
107
+ >
108
+ {col.label}
109
+ </th>
110
+ ))}
111
+ </tr>
112
+ </thead>
113
+ <tbody>
114
+ {visibleRows.map((row, i) => (
115
+ <tr key={i} className="border-b transition-colors last:border-0 hover:bg-muted/30">
116
+ {columns.map((col) => (
117
+ <td
118
+ key={col.key}
119
+ className={cn(
120
+ "px-4 py-3 text-foreground",
121
+ col.align === "right"
122
+ ? "text-right"
123
+ : col.align === "center"
124
+ ? "text-center"
125
+ : "",
126
+ )}
127
+ >
128
+ {String(row[col.key] ?? "")}
129
+ </td>
130
+ ))}
131
+ </tr>
132
+ ))}
133
+ {visibleRows.length === 0 && (
134
+ <tr>
135
+ <td
136
+ colSpan={columns.length || 1}
137
+ className="px-4 py-10 text-center text-sm text-muted-foreground"
138
+ >
139
+ {emptyText}
140
+ </td>
141
+ </tr>
142
+ )}
143
+ </tbody>
144
+ </table>
145
+ </div>
146
+ </div>
147
+ );
148
+ };
149
+
150
+ // ── SplitMasterDetail ─────────────────────────────────────────────────────────
151
+
152
+ interface MasterItem {
153
+ id: string;
154
+ title: string;
155
+ subtitle?: string | undefined;
156
+ }
157
+
158
+ function parseItems(raw: unknown): MasterItem[] {
159
+ if (!Array.isArray(raw)) return [];
160
+ return raw
161
+ .map((it, i) => {
162
+ if (!it || typeof it !== "object") return null;
163
+ const rec = it as Record<string, unknown>;
164
+ return {
165
+ id: typeof rec["id"] === "string" ? rec["id"] : `item-${i}`,
166
+ title: typeof rec["title"] === "string" ? rec["title"] : "",
167
+ subtitle: typeof rec["subtitle"] === "string" ? rec["subtitle"] : undefined,
168
+ } as MasterItem;
169
+ })
170
+ .filter((it): it is MasterItem => it !== null);
171
+ }
172
+
173
+ /**
174
+ * Master/detail split: a selectable left list and a right detail pane. Clicking
175
+ * an item dispatches SetState onto `selectKey` (default `selectedId`); the host
176
+ * typically reads it back via `_find` to drive the detail slot. The detail pane
177
+ * renders the `detail` slot (host-composed) when present, else `children`.
178
+ */
179
+ export const SplitMasterDetail: RendererComponent = (props, ctx) => {
180
+ const items = parseItems(props["items"]);
181
+ const selectKey = typeof props["selectKey"] === "string" ? props["selectKey"] : "selectedId";
182
+ const selectedId = String(ctx.resolve.getState(selectKey) ?? items[0]?.id ?? "");
183
+ const detail = ctx.slots["detail"] as React.ReactNode[] | undefined;
184
+
185
+ return (
186
+ <div
187
+ className="uibot-split-master-detail grid gap-4 md:grid-cols-[minmax(220px,1fr)_2fr]"
188
+ data-uibot-type="SplitMasterDetail"
189
+ >
190
+ <ul className="space-y-1 rounded-lg border bg-muted/20 p-2" role="listbox" aria-label="Items">
191
+ {items.map((it) => {
192
+ const active = it.id === selectedId;
193
+ return (
194
+ <li key={it.id}>
195
+ <button
196
+ type="button"
197
+ role="option"
198
+ aria-selected={active}
199
+ onClick={(e) => ctx.dispatch?.(setState(selectKey, it.id), { event: e })}
200
+ className={cn(
201
+ "w-full rounded-md px-3 py-2 text-left text-sm transition-colors",
202
+ active ? "bg-accent text-accent-foreground" : "hover:bg-accent/50",
203
+ )}
204
+ >
205
+ <span className="block font-medium">{it.title}</span>
206
+ {it.subtitle && (
207
+ <span className="block text-xs text-muted-foreground">{it.subtitle}</span>
208
+ )}
209
+ </button>
210
+ </li>
211
+ );
212
+ })}
213
+ {items.length === 0 && (
214
+ <li className="px-3 py-4 text-center text-xs text-muted-foreground">No items</li>
215
+ )}
216
+ </ul>
217
+ <div className="rounded-lg border bg-card p-5">
218
+ {detail && detail.length > 0 ? detail : (ctx.children as React.ReactNode[])}
219
+ </div>
220
+ </div>
221
+ );
222
+ };
223
+
224
+ // ── DetailField ───────────────────────────────────────────────────────────────
225
+
226
+ /** Label + value display row, for use inside a detail pane. */
227
+ export const DetailField: RendererComponent = (props) => {
228
+ const label = typeof props["label"] === "string" ? props["label"] : "";
229
+ const value = props["value"];
230
+ const inline = props["inline"] === true;
231
+
232
+ return (
233
+ <div
234
+ className={cn(
235
+ "uibot-detail-field",
236
+ inline ? "flex items-baseline gap-2" : "flex flex-col gap-0.5 py-1.5",
237
+ )}
238
+ data-uibot-type="DetailField"
239
+ >
240
+ <dt className="text-xs font-medium uppercase tracking-wide text-muted-foreground">{label}</dt>
241
+ <dd className="text-sm text-foreground">
242
+ {value === undefined || value === null || value === "" ? "—" : String(value)}
243
+ </dd>
244
+ </div>
245
+ );
246
+ };
247
+
248
+ // ── BadgeGroup ────────────────────────────────────────────────────────────────
249
+
250
+ type BadgeTone = "default" | "success" | "warning" | "error" | "info";
251
+
252
+ const BADGE_TONE: Record<BadgeTone, string> = {
253
+ default: "bg-secondary text-secondary-foreground",
254
+ success: "bg-green-100 text-green-700",
255
+ warning: "bg-amber-100 text-amber-700",
256
+ error: "bg-destructive/10 text-destructive",
257
+ info: "bg-sky-100 text-sky-700",
258
+ };
259
+
260
+ interface BadgeItem {
261
+ label: string;
262
+ tone?: BadgeTone | undefined;
263
+ onClick?: ActionChain | undefined;
264
+ }
265
+
266
+ function parseBadges(raw: unknown): BadgeItem[] {
267
+ if (!Array.isArray(raw)) return [];
268
+ return raw
269
+ .map((b) => {
270
+ if (typeof b === "string") return { label: b } as BadgeItem;
271
+ if (b && typeof b === "object") {
272
+ const rec = b as Record<string, unknown>;
273
+ const tone = rec["tone"];
274
+ const onClick = rec["onClick"];
275
+ return {
276
+ label: typeof rec["label"] === "string" ? rec["label"] : "",
277
+ tone:
278
+ tone === "success" || tone === "warning" || tone === "error" || tone === "info"
279
+ ? (tone as BadgeTone)
280
+ : "default",
281
+ onClick: Array.isArray(onClick) ? (onClick as ActionChain) : undefined,
282
+ } as BadgeItem;
283
+ }
284
+ return null;
285
+ })
286
+ .filter((b): b is BadgeItem => b !== null && b.label.length > 0);
287
+ }
288
+
289
+ const BADGE_BASE = "inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium";
290
+
291
+ /**
292
+ * A horizontal group of status badges. Items carrying an `onClick` ActionChain
293
+ * render as focusable `<button>`s that dispatch the chain on click (so e.g. a
294
+ * tag chip can drive a SetState filter); items without `onClick` stay as inert
295
+ * `<span>`s.
296
+ */
297
+ export const BadgeGroup: RendererComponent = (props, ctx) => {
298
+ const items = parseBadges(props["items"]);
299
+ return (
300
+ <div
301
+ className="uibot-badge-group flex flex-wrap items-center gap-1.5"
302
+ data-uibot-type="BadgeGroup"
303
+ >
304
+ {items.map((b, i) =>
305
+ b.onClick ? (
306
+ <button
307
+ key={i}
308
+ type="button"
309
+ onClick={(e) => ctx.dispatch?.(b.onClick!, { event: e })}
310
+ className={cn(BADGE_BASE, "cursor-pointer", BADGE_TONE[b.tone ?? "default"])}
311
+ >
312
+ {b.label}
313
+ </button>
314
+ ) : (
315
+ <span key={i} className={cn(BADGE_BASE, BADGE_TONE[b.tone ?? "default"])}>
316
+ {b.label}
317
+ </span>
318
+ ),
319
+ )}
320
+ </div>
321
+ );
322
+ };
323
+
324
+ // ── KeyValueGrid ──────────────────────────────────────────────────────────────
325
+
326
+ interface KVPair {
327
+ key: string;
328
+ value: unknown;
329
+ }
330
+
331
+ function parsePairs(raw: unknown): KVPair[] {
332
+ if (Array.isArray(raw)) {
333
+ return raw
334
+ .filter((p): p is Record<string, unknown> => Boolean(p) && typeof p === "object")
335
+ .map((p) => ({ key: typeof p["key"] === "string" ? p["key"] : "", value: p["value"] }))
336
+ .filter((p) => p.key);
337
+ }
338
+ if (raw && typeof raw === "object") {
339
+ return Object.entries(raw as Record<string, unknown>).map(([key, value]) => ({ key, value }));
340
+ }
341
+ return [];
342
+ }
343
+
344
+ // Static literal columns map — see grid.tsx for the purge rationale.
345
+ const KV_COLS: Record<string, string> = {
346
+ "1": "sm:grid-cols-1",
347
+ "2": "sm:grid-cols-2",
348
+ "3": "sm:grid-cols-3",
349
+ };
350
+
351
+ /** Two-column key/value grid for compact metadata display. */
352
+ export const KeyValueGrid: RendererComponent = (props) => {
353
+ const pairs = parsePairs(props["items"] ?? props["data"]);
354
+ const cols = String(props["cols"] ?? "2");
355
+ const colsCls = KV_COLS[cols] ?? KV_COLS["2"];
356
+
357
+ return (
358
+ <dl
359
+ className={cn("uibot-key-value-grid grid grid-cols-1 gap-x-6 gap-y-2", colsCls)}
360
+ data-uibot-type="KeyValueGrid"
361
+ >
362
+ {pairs.map((p, i) => (
363
+ <div key={i} className="flex flex-col gap-0.5 border-b border-dashed py-1.5 last:border-0">
364
+ <dt className="text-xs font-medium uppercase tracking-wide text-muted-foreground">
365
+ {p.key}
366
+ </dt>
367
+ <dd className="text-sm text-foreground">
368
+ {p.value === undefined || p.value === null || p.value === "" ? "—" : String(p.value)}
369
+ </dd>
370
+ </div>
371
+ ))}
372
+ </dl>
373
+ );
374
+ };
@@ -0,0 +1,150 @@
1
+ import type { RendererComponent } from "@uibot/renderer-react";
2
+
3
+ interface Column {
4
+ id: string;
5
+ title: string;
6
+ }
7
+
8
+ interface Card {
9
+ id: string;
10
+ column: string;
11
+ title: string;
12
+ subtitle?: string;
13
+ badge?: string;
14
+ }
15
+
16
+ function parseColumns(raw: unknown): Column[] {
17
+ if (!Array.isArray(raw)) return [];
18
+ return raw
19
+ .map((c) => {
20
+ if (!c || typeof c !== "object") return null;
21
+ const rec = c as Record<string, unknown>;
22
+ const id = typeof rec["id"] === "string" ? rec["id"] : null;
23
+ if (!id) return null;
24
+ return { id, title: typeof rec["title"] === "string" ? rec["title"] : id } as Column;
25
+ })
26
+ .filter((c): c is Column => c !== null);
27
+ }
28
+
29
+ function parseCards(raw: unknown): Card[] {
30
+ if (!Array.isArray(raw)) return [];
31
+ return raw
32
+ .map((c, i) => {
33
+ if (!c || typeof c !== "object") return null;
34
+ const rec = c as Record<string, unknown>;
35
+ const column = typeof rec["column"] === "string" ? rec["column"] : null;
36
+ if (!column) return null;
37
+ return {
38
+ id: typeof rec["id"] === "string" ? rec["id"] : `card-${i}`,
39
+ column,
40
+ title: typeof rec["title"] === "string" ? rec["title"] : "",
41
+ subtitle: typeof rec["subtitle"] === "string" ? rec["subtitle"] : undefined,
42
+ badge: typeof rec["badge"] === "string" ? rec["badge"] : undefined,
43
+ } as Card;
44
+ })
45
+ .filter((c): c is Card => c !== null);
46
+ }
47
+
48
+ /** Column accent cycles the marketing palette — theme-token safe in any host. */
49
+ const COLUMN_ACCENTS = [
50
+ "var(--marketing-accent)",
51
+ "var(--mk-accent-2)",
52
+ "var(--mk-accent-3)",
53
+ "var(--mk-accent-4)",
54
+ ] as const;
55
+
56
+ const MONO = { fontFamily: "var(--font-marketing-mono)" } as const;
57
+
58
+ /** Fit columns to the container: equal-width grid up to 4 lanes, scroll beyond. */
59
+ const FIT_COLS: Record<number, string> = {
60
+ 1: "grid-cols-1",
61
+ 2: "grid-cols-2",
62
+ 3: "grid-cols-3",
63
+ 4: "grid-cols-4",
64
+ };
65
+
66
+ /**
67
+ * Horizontal kanban board: columns as swimlanes, cards grouped by `column`.
68
+ * Up to four lanes fit the container width without scrolling; five or more
69
+ * fall back to a horizontal scroll strip. Editorial chrome: per-column accent
70
+ * dot + mono count chip, cards with a hover lift and a hairline badge.
71
+ * Read-only display.
72
+ */
73
+ export const KanbanBoard: RendererComponent = (props) => {
74
+ const columns = parseColumns(props["columns"]);
75
+ const cards = parseCards(props["cards"]);
76
+ const fitClass = FIT_COLS[columns.length];
77
+
78
+ return (
79
+ <div
80
+ className={
81
+ fitClass
82
+ ? `uibot-kanban grid ${fitClass} gap-3 pb-1`
83
+ : "uibot-kanban grid auto-cols-[minmax(240px,1fr)] grid-flow-col gap-3 overflow-x-auto pb-2"
84
+ }
85
+ data-uibot-type="KanbanBoard"
86
+ role="region"
87
+ aria-label="Kanban board"
88
+ >
89
+ {columns.map((col, colIdx) => {
90
+ const accent = COLUMN_ACCENTS[colIdx % COLUMN_ACCENTS.length];
91
+ const colCards = cards.filter((c) => c.column === col.id);
92
+ return (
93
+ <div
94
+ key={col.id}
95
+ className="flex min-h-[7rem] flex-col rounded-lg border bg-muted/25 p-2.5"
96
+ >
97
+ <h4
98
+ className="mb-2.5 flex items-center gap-1.5 px-1 text-[0.65rem] font-medium uppercase tracking-[0.12em] text-muted-foreground"
99
+ style={MONO}
100
+ >
101
+ <span
102
+ aria-hidden="true"
103
+ className="size-1.5 shrink-0 rounded-full"
104
+ style={{
105
+ background: accent,
106
+ boxShadow: `0 0 0 3px color-mix(in oklch, ${accent} 20%, transparent)`,
107
+ }}
108
+ />
109
+ <span className="truncate">{col.title}</span>
110
+ <span className="ml-auto shrink-0 rounded-full border bg-background/70 px-1.5 py-px text-[0.6rem] text-muted-foreground/80">
111
+ {colCards.length}
112
+ </span>
113
+ </h4>
114
+ <div className="space-y-2">
115
+ {colCards.map((card) => (
116
+ <div
117
+ key={card.id}
118
+ className="rounded-md border bg-card p-3 shadow-sm transition-all duration-200 hover:-translate-y-0.5 hover:border-[color-mix(in_oklch,var(--marketing-accent)_38%,var(--border))] hover:shadow-md"
119
+ data-uibot-card={card.id}
120
+ >
121
+ <div className="flex items-start justify-between gap-2">
122
+ <p className="text-sm font-medium leading-snug text-foreground">{card.title}</p>
123
+ {card.badge && (
124
+ <span
125
+ className="shrink-0 rounded-full border bg-muted/50 px-1.5 py-px text-[0.6rem] font-medium tracking-wide text-muted-foreground"
126
+ style={MONO}
127
+ >
128
+ {card.badge}
129
+ </span>
130
+ )}
131
+ </div>
132
+ {card.subtitle && (
133
+ <p className="mt-1.5 truncate text-[0.6875rem] text-muted-foreground/90">
134
+ {card.subtitle}
135
+ </p>
136
+ )}
137
+ </div>
138
+ ))}
139
+ {colCards.length === 0 && (
140
+ <p className="rounded-md border border-dashed py-4 text-center text-xs text-muted-foreground">
141
+ Empty
142
+ </p>
143
+ )}
144
+ </div>
145
+ </div>
146
+ );
147
+ })}
148
+ </div>
149
+ );
150
+ };