@nakshi/genui-blocks 0.0.0-stage → 1.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.
- package/LICENSE +202 -0
- package/README.md +59 -2
- package/dist/index.d.ts +1152 -0
- package/dist/index.js +250 -0
- package/dist/react/chart.d.ts +13 -0
- package/dist/react/index.d.ts +24 -0
- package/dist/react/index.js +916 -0
- package/dist/react/style.d.ts +16 -0
- package/dist/react/views.d.ts +86 -0
- package/package.json +54 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// packages/genui-blocks/src/index.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { createCatalog, defineBlock, firstFigure } from "@nakshi/genui";
|
|
4
|
+
function clip(text, max, keepLines = false) {
|
|
5
|
+
const clean = keepLines ? text.replace(/[ \t]+\n/g, `
|
|
6
|
+
`).trim() : text.replace(/\s+/g, " ").trim();
|
|
7
|
+
return clean.length <= max ? clean : `${clean.slice(0, max - 1).trimEnd()}…`;
|
|
8
|
+
}
|
|
9
|
+
var blank = (v) => v === null || typeof v === "string" && !v.trim();
|
|
10
|
+
var lenient = {
|
|
11
|
+
text: (max) => z.preprocess((v) => typeof v === "number" ? String(v) : typeof v === "string" ? clip(v, max) : v, z.string().min(1).max(max)),
|
|
12
|
+
optionalText: (max) => z.preprocess((v) => blank(v) ? undefined : typeof v === "number" ? String(v) : typeof v === "string" ? clip(v, max) : v, z.string().min(1).max(max).optional()),
|
|
13
|
+
markdown: (max) => z.preprocess((v) => typeof v === "string" ? clip(v, max, true) : v, z.string().min(1).max(max)),
|
|
14
|
+
list: (item, min, max) => z.preprocess((v) => Array.isArray(v) ? v.slice(0, max) : v, z.array(item).min(min).max(max)),
|
|
15
|
+
amount: z.preprocess((v) => typeof v === "string" && v.trim() !== "" && Number.isFinite(Number(v.replace(/,/g, ""))) ? Number(v.replace(/,/g, "")) : v, z.number().finite())
|
|
16
|
+
};
|
|
17
|
+
var { text, optionalText, markdown, list, amount } = lenient;
|
|
18
|
+
var step = z.object({ title: text(120), detail: optionalText(400), time: optionalText(40) });
|
|
19
|
+
var stepsWith = (min) => z.object({ title: optionalText(120), steps: list(step, min, 16) });
|
|
20
|
+
var steps = defineBlock({
|
|
21
|
+
type: "steps",
|
|
22
|
+
schema: stepsWith(2),
|
|
23
|
+
stream: { items: stepsWith(1) },
|
|
24
|
+
guide: {
|
|
25
|
+
use: '2–12 things done or happening in order: a how-to, a procedure, the stages of a process (not a numbered list). `detail` is one or two sentences; `time` only when it matters ("10 min", "Day 1").'
|
|
26
|
+
},
|
|
27
|
+
toText: (b) => [
|
|
28
|
+
...b.title ? [b.title] : [],
|
|
29
|
+
...b.steps.map((s, i) => `${i + 1}. ${s.title}${s.time ? ` (${s.time})` : ""}${s.detail ? ` — ${s.detail}` : ""}`)
|
|
30
|
+
].join(`
|
|
31
|
+
`)
|
|
32
|
+
});
|
|
33
|
+
var checklistGroup = z.object({ label: optionalText(80), items: list(text(160), 1, 40) });
|
|
34
|
+
var checklist = defineBlock({
|
|
35
|
+
type: "checklist",
|
|
36
|
+
schema: z.object({ title: optionalText(120), groups: list(checklistGroup, 1, 10), note: optionalText(300) }),
|
|
37
|
+
stream: "items",
|
|
38
|
+
guide: {
|
|
39
|
+
use: "things to tick off (packing, documents, shopping, to-dos), grouped by category; never a `- [ ]` list."
|
|
40
|
+
},
|
|
41
|
+
toText: (b) => [
|
|
42
|
+
...b.title ? [b.title] : [],
|
|
43
|
+
...b.groups.flatMap((g) => [...g.label ? [`${g.label}:`] : [], ...g.items.map((item) => `☐ ${item}`)]),
|
|
44
|
+
...b.note ? [b.note] : []
|
|
45
|
+
].join(`
|
|
46
|
+
`)
|
|
47
|
+
});
|
|
48
|
+
var ingredient = z.object({ item: text(120), qty: amount.optional(), unit: optionalText(30), note: optionalText(160) });
|
|
49
|
+
var recipe = defineBlock({
|
|
50
|
+
type: "recipe",
|
|
51
|
+
schema: z.object({
|
|
52
|
+
title: text(120),
|
|
53
|
+
servings: amount.pipe(z.number().int().min(1).max(100)),
|
|
54
|
+
time: optionalText(40),
|
|
55
|
+
ingredients: list(ingredient, 1, 60),
|
|
56
|
+
note: optionalText(400)
|
|
57
|
+
}),
|
|
58
|
+
stream: "items",
|
|
59
|
+
guide: {
|
|
60
|
+
use: 'ingredients for `servings`, with a numeric `qty` (leave it out for "to taste"); the method follows as steps.'
|
|
61
|
+
},
|
|
62
|
+
toText: (b) => [
|
|
63
|
+
`${b.title} (serves ${b.servings}${b.time ? `, ${b.time}` : ""})`,
|
|
64
|
+
...b.ingredients.map((i) => `- ${[i.qty, i.unit, i.item].filter((x) => x !== undefined).join(" ")}${i.note ? ` (${i.note})` : ""}`),
|
|
65
|
+
...b.note ? [b.note] : []
|
|
66
|
+
].join(`
|
|
67
|
+
`)
|
|
68
|
+
});
|
|
69
|
+
var stat = z.object({ label: text(60), value: text(40), sub: optionalText(80) });
|
|
70
|
+
var stats = defineBlock({
|
|
71
|
+
type: "stats",
|
|
72
|
+
schema: z.object({ title: optionalText(120), items: list(stat, 1, 8), note: optionalText(300) }),
|
|
73
|
+
stream: "items",
|
|
74
|
+
guide: { use: "2–6 headline figures the answer turns on, exactly as your sources state them; the source in `note`." },
|
|
75
|
+
toText: (b) => [
|
|
76
|
+
...b.title ? [b.title] : [],
|
|
77
|
+
...b.items.map((s) => `${s.label}: ${s.value}${s.sub ? ` (${s.sub})` : ""}`),
|
|
78
|
+
...b.note ? [b.note] : []
|
|
79
|
+
].join(`
|
|
80
|
+
`),
|
|
81
|
+
figures: (b) => b.items.flatMap((s) => {
|
|
82
|
+
const value = firstFigure(s.value);
|
|
83
|
+
return value === null ? [] : [{ label: s.label, value }];
|
|
84
|
+
})
|
|
85
|
+
});
|
|
86
|
+
var CHART_KINDS = ["bar", "line"];
|
|
87
|
+
var series = z.object({ name: text(60), values: z.array(amount.nullable()).min(1).max(24) });
|
|
88
|
+
var chart = defineBlock({
|
|
89
|
+
type: "chart",
|
|
90
|
+
schema: z.object({
|
|
91
|
+
title: optionalText(120),
|
|
92
|
+
kind: z.enum(CHART_KINDS),
|
|
93
|
+
unit: optionalText(40),
|
|
94
|
+
labels: z.array(text(40)).min(2).max(24),
|
|
95
|
+
series: list(series, 1, 4),
|
|
96
|
+
note: optionalText(300)
|
|
97
|
+
}).refine((b) => b.series.every((s) => s.values.length === b.labels.length), {
|
|
98
|
+
message: "each series has one value per label",
|
|
99
|
+
path: ["series"]
|
|
100
|
+
}),
|
|
101
|
+
stream: "whole",
|
|
102
|
+
guide: {
|
|
103
|
+
use: "a series of numbers: 2–24 categories, 1–4 series in one unit, one value per label (null where there is none). Only points you have, labelled as your sources label them."
|
|
104
|
+
},
|
|
105
|
+
toText: (b) => [
|
|
106
|
+
`${b.title ?? "Chart"}${b.unit ? ` (${b.unit})` : ""}`,
|
|
107
|
+
...b.series.map((s) => `${s.name}: ${b.labels.map((label, i) => `${label} ${s.values[i] ?? "—"}`).join(", ")}`),
|
|
108
|
+
...b.note ? [b.note] : []
|
|
109
|
+
].join(`
|
|
110
|
+
`),
|
|
111
|
+
figures: (b) => b.series.flatMap((s) => s.values.flatMap((value, i) => value == null ? [] : [{ label: `${s.name} · ${b.labels[i]}`, value }]))
|
|
112
|
+
});
|
|
113
|
+
var column = z.object({ name: text(60), subtitle: optionalText(80) });
|
|
114
|
+
var row = z.object({ label: text(80), values: z.array(text(160)).min(2).max(4), best: z.array(z.number().int().min(0)).max(4).optional() });
|
|
115
|
+
var compare = defineBlock({
|
|
116
|
+
type: "compare",
|
|
117
|
+
schema: z.object({ title: optionalText(120), columns: z.array(column).min(2).max(4), rows: list(row, 1, 12), note: optionalText(300) }).refine((b) => b.rows.every((r) => r.values.length === b.columns.length), { message: "each row has one value per column", path: ["rows"] }),
|
|
118
|
+
stream: "items",
|
|
119
|
+
guide: { use: "2–4 options on the same 3–8 rows; `best` is the 0-based column that wins a row. A small grid may stay a Markdown table." },
|
|
120
|
+
toText: (b) => [
|
|
121
|
+
...b.title ? [b.title] : [],
|
|
122
|
+
`| | ${b.columns.map((c) => c.name).join(" | ")} |`,
|
|
123
|
+
`|---|${b.columns.map(() => "---").join("|")}|`,
|
|
124
|
+
...b.rows.map((r) => `| ${r.label} | ${r.values.join(" | ")} |`),
|
|
125
|
+
...b.note ? [b.note] : []
|
|
126
|
+
].join(`
|
|
127
|
+
`)
|
|
128
|
+
});
|
|
129
|
+
var card = z.object({ title: text(80), text: optionalText(400), points: list(text(160), 0, 6).optional() });
|
|
130
|
+
var cardsWith = (min) => z.object({ title: optionalText(120), items: list(card, min, 10), note: optionalText(300) });
|
|
131
|
+
var cards = defineBlock({
|
|
132
|
+
type: "cards",
|
|
133
|
+
schema: cardsWith(2),
|
|
134
|
+
stream: { items: cardsWith(1) },
|
|
135
|
+
guide: { use: "2–8 options that each need their own words (approaches to choose between, a menu); match the count asked. Options on the same attributes are a compare." },
|
|
136
|
+
toText: (b) => [
|
|
137
|
+
...b.title ? [b.title] : [],
|
|
138
|
+
...b.items.flatMap((c) => [`- ${c.title}${c.text ? `: ${c.text}` : ""}`, ...(c.points ?? []).map((p) => ` - ${p}`)]),
|
|
139
|
+
...b.note ? [b.note] : []
|
|
140
|
+
].join(`
|
|
141
|
+
`)
|
|
142
|
+
});
|
|
143
|
+
var CALLOUT_TONES = ["info", "tip", "warning"];
|
|
144
|
+
var callout = defineBlock({
|
|
145
|
+
type: "callout",
|
|
146
|
+
schema: z.object({ tone: z.enum(CALLOUT_TONES).optional(), title: optionalText(80), text: text(600) }),
|
|
147
|
+
guide: {
|
|
148
|
+
use: 'at most one: a real hazard (`warning`, titled with the danger), or the one tip they must not miss. Never a definition, a "key idea" or a summary.'
|
|
149
|
+
},
|
|
150
|
+
toText: (b) => `${b.tone === "warning" ? "⚠ " : ""}${b.title ? `${b.title}: ` : ""}${b.text}`
|
|
151
|
+
});
|
|
152
|
+
var tab = z.object({ label: text(40), body: markdown(4000) });
|
|
153
|
+
var tabsWith = (min) => z.object({ title: optionalText(120), tabs: list(tab, min, 8) });
|
|
154
|
+
var tabs = defineBlock({
|
|
155
|
+
type: "tabs",
|
|
156
|
+
schema: tabsWith(2),
|
|
157
|
+
stream: { items: tabsWith(1) },
|
|
158
|
+
guide: { use: "only when they ask for separate views of one thing: 2–6 tabs of short Markdown, no nested fences." },
|
|
159
|
+
toText: (b) => [...b.title ? [b.title] : [], ...b.tabs.map((t) => `${t.label}:
|
|
160
|
+
${t.body}`)].join(`
|
|
161
|
+
|
|
162
|
+
`)
|
|
163
|
+
});
|
|
164
|
+
var section = z.object({ title: text(160), body: markdown(4000), open: z.boolean().optional() });
|
|
165
|
+
var accordionWith = (min) => z.object({ title: optionalText(120), items: list(section, min, 12) });
|
|
166
|
+
var accordion = defineBlock({
|
|
167
|
+
type: "accordion",
|
|
168
|
+
schema: accordionWith(2),
|
|
169
|
+
stream: { items: accordionWith(1) },
|
|
170
|
+
guide: { use: "only when they ask for sections they can open and close (an FAQ): 2–8 items of short Markdown." },
|
|
171
|
+
toText: (b) => [...b.title ? [b.title] : [], ...b.items.map((s) => `${s.title}
|
|
172
|
+
${s.body}`)].join(`
|
|
173
|
+
|
|
174
|
+
`)
|
|
175
|
+
});
|
|
176
|
+
var code = defineBlock({
|
|
177
|
+
type: "code",
|
|
178
|
+
schema: z.object({ title: optionalText(120), language: optionalText(30), code: z.string().min(1).max(50000), note: optionalText(300) }),
|
|
179
|
+
guide: { use: "a titled card when a file is the deliverable; any other code stays a normal ```lang fence." },
|
|
180
|
+
toText: (b) => [...b.title ? [b.title] : [], `\`\`\`${b.language ?? ""}
|
|
181
|
+
${b.code}
|
|
182
|
+
\`\`\``, ...b.note ? [b.note] : []].join(`
|
|
183
|
+
`)
|
|
184
|
+
});
|
|
185
|
+
var defaultBlocks = [steps, checklist, recipe, stats, chart, compare, cards, callout, tabs, accordion, code];
|
|
186
|
+
var defaultGuideGroups = [
|
|
187
|
+
{ name: "In order, and lists", items: ["steps", "checklist", "recipe"] },
|
|
188
|
+
{ name: "Numbers", items: ["stats", "chart"] },
|
|
189
|
+
{ name: "Options", items: ["compare", "cards"] },
|
|
190
|
+
{ name: "A warning", items: ["callout"] },
|
|
191
|
+
{ name: "Only when they ask for that layout", items: ["tabs", "accordion", "code"] }
|
|
192
|
+
];
|
|
193
|
+
function guideGroupsFor(catalog) {
|
|
194
|
+
const listed = new Set(defaultGuideGroups.flatMap((g) => g.items.filter((i) => typeof i === "string")));
|
|
195
|
+
const groups = defaultGuideGroups.map((g) => ({ name: g.name, items: g.items.filter((i) => typeof i !== "string" || catalog.has(i)) })).filter((g) => g.items.length > 0);
|
|
196
|
+
const rest = catalog.types.filter((t) => !listed.has(t));
|
|
197
|
+
return rest.length > 0 ? [...groups, { name: "More", items: rest }] : groups;
|
|
198
|
+
}
|
|
199
|
+
function defaultExamples(prefix) {
|
|
200
|
+
return [
|
|
201
|
+
"Examples:",
|
|
202
|
+
'"How do I descale a kettle?" — a procedure, so steps:',
|
|
203
|
+
"Vinegar and water dissolve the scale in about half an hour.",
|
|
204
|
+
`\`\`\`${prefix}:steps`,
|
|
205
|
+
'steps(title: "Descale a kettle", steps: [{title: "Fill halfway", detail: "Equal parts white vinegar and water."}, {title: "Boil, then wait", detail: "Switch it off and leave it to soak.", time: "20 min"}, {title: "Rinse twice", detail: "Boil plain water once to clear the smell."}])',
|
|
206
|
+
"```",
|
|
207
|
+
"Every month or two keeps it clear in hard-water areas.",
|
|
208
|
+
"",
|
|
209
|
+
'"What should I bring to a picnic?" — a list to tick off, so a checklist:',
|
|
210
|
+
"A few basics cover most picnics.",
|
|
211
|
+
`\`\`\`${prefix}:checklist`,
|
|
212
|
+
'checklist(title: "Picnic", groups: [{label: "Food", items: ["Sandwiches", "Fruit", "Water"]}, {label: "Kit", items: ["Blanket", "Napkins", "A bag for rubbish"]}])',
|
|
213
|
+
"```",
|
|
214
|
+
"",
|
|
215
|
+
'"How do I center a div?" — code, so no block:',
|
|
216
|
+
"Make the parent a flex container:",
|
|
217
|
+
"```css",
|
|
218
|
+
".parent { display: flex; justify-content: center; align-items: center; }",
|
|
219
|
+
"```"
|
|
220
|
+
].join(`
|
|
221
|
+
`);
|
|
222
|
+
}
|
|
223
|
+
function defaultGuide(catalog) {
|
|
224
|
+
return catalog.guide({ signatures: { groups: guideGroupsFor(catalog) }, examples: defaultExamples(catalog.prefix) });
|
|
225
|
+
}
|
|
226
|
+
function createDefaultCatalog(prefix = "ui") {
|
|
227
|
+
return createCatalog({ prefix, blocks: defaultBlocks });
|
|
228
|
+
}
|
|
229
|
+
export {
|
|
230
|
+
CALLOUT_TONES,
|
|
231
|
+
CHART_KINDS,
|
|
232
|
+
accordion,
|
|
233
|
+
callout,
|
|
234
|
+
cards,
|
|
235
|
+
chart,
|
|
236
|
+
checklist,
|
|
237
|
+
code,
|
|
238
|
+
compare,
|
|
239
|
+
createDefaultCatalog,
|
|
240
|
+
defaultBlocks,
|
|
241
|
+
defaultExamples,
|
|
242
|
+
defaultGuide,
|
|
243
|
+
defaultGuideGroups,
|
|
244
|
+
guideGroupsFor,
|
|
245
|
+
lenient,
|
|
246
|
+
recipe,
|
|
247
|
+
stats,
|
|
248
|
+
steps,
|
|
249
|
+
tabs
|
|
250
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type BlockViewProps } from '@nakshi/genui-react';
|
|
2
|
+
export interface ChartData {
|
|
3
|
+
title?: string;
|
|
4
|
+
kind: 'bar' | 'line';
|
|
5
|
+
unit?: string;
|
|
6
|
+
labels: string[];
|
|
7
|
+
series: Array<{
|
|
8
|
+
name: string;
|
|
9
|
+
values: Array<number | null>;
|
|
10
|
+
}>;
|
|
11
|
+
note?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function ChartView({ data }: BlockViewProps<ChartData>): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React views for @nakshi/genui-blocks: plain, accessible, themeable through
|
|
3
|
+
* CSS custom properties (--genui-border, --genui-accent, --genui-radius,
|
|
4
|
+
* --genui-series-1…4, --genui-chart-surface). Use them as they are, or as a
|
|
5
|
+
* starting point for your own.
|
|
6
|
+
*/
|
|
7
|
+
import { ChartView } from './chart.js';
|
|
8
|
+
import { AccordionView, CalloutView, CardsView, ChecklistView, CodeView, CompareView, RecipeView, StatsView, StepsView, TabsView } from './views.js';
|
|
9
|
+
export { AccordionView, CalloutView, CardsView, ChartView, ChecklistView, CodeView, CompareView, RecipeView, StatsView, StepsView, TabsView };
|
|
10
|
+
export type { ChartData } from './chart.js';
|
|
11
|
+
/** A view for every default block, keyed by type: `<Answer views={defaultViews} … />`. */
|
|
12
|
+
export declare const defaultViews: {
|
|
13
|
+
readonly steps: typeof StepsView;
|
|
14
|
+
readonly checklist: typeof ChecklistView;
|
|
15
|
+
readonly recipe: typeof RecipeView;
|
|
16
|
+
readonly stats: typeof StatsView;
|
|
17
|
+
readonly chart: typeof ChartView;
|
|
18
|
+
readonly compare: typeof CompareView;
|
|
19
|
+
readonly cards: typeof CardsView;
|
|
20
|
+
readonly callout: typeof CalloutView;
|
|
21
|
+
readonly tabs: typeof TabsView;
|
|
22
|
+
readonly accordion: typeof AccordionView;
|
|
23
|
+
readonly code: typeof CodeView;
|
|
24
|
+
};
|