ain-ui 0.2.3 → 1.0.4
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/NOTICE +37 -0
- package/README.md +179 -77
- package/dist/agent.d.ts +131 -0
- package/dist/agent.js +822 -0
- package/dist/ainui.d.ts +15 -1
- package/dist/ainui.js +53 -10
- package/dist/basic.d.ts +5 -47
- package/dist/basic.js +22 -29
- package/dist/catalog/standard.gen.d.ts +5 -0
- package/dist/catalog/standard.gen.js +6 -0
- package/dist/catalog-v1.json +2560 -0
- package/dist/catalog.json +19917 -2061
- package/dist/chat.js +6 -7
- package/dist/expression.d.ts +23 -0
- package/dist/expression.js +222 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/macros.d.ts +12 -0
- package/dist/macros.js +57 -0
- package/dist/payment.js +6 -7
- package/dist/pickers.d.ts +158 -0
- package/dist/pickers.js +110 -0
- package/dist/protocol.d.ts +100 -0
- package/dist/protocol.js +232 -0
- package/dist/react/ain.d.ts +1 -0
- package/dist/react/ain.js +225 -0
- package/dist/react/catalog.d.ts +9 -0
- package/dist/react/catalog.js +23 -0
- package/dist/react/core.d.ts +91 -0
- package/dist/react/core.js +173 -0
- package/dist/react/functions.d.ts +2 -0
- package/dist/react/functions.js +29 -0
- package/dist/react/standard/composites.d.ts +1 -0
- package/dist/react/standard/composites.js +110 -0
- package/dist/react/standard/data.d.ts +1 -0
- package/dist/react/standard/data.js +481 -0
- package/dist/react/standard/feedback.d.ts +11 -0
- package/dist/react/standard/feedback.js +111 -0
- package/dist/react/standard/inputs.d.ts +5 -0
- package/dist/react/standard/inputs.js +467 -0
- package/dist/react/standard/layout.d.ts +5 -0
- package/dist/react/standard/layout.js +104 -0
- package/dist/react/standard/media.d.ts +33 -0
- package/dist/react/standard/media.js +208 -0
- package/dist/react/standard/rich.d.ts +1 -0
- package/dist/react/standard/rich.js +146 -0
- package/dist/react/standard/text.d.ts +9 -0
- package/dist/react/standard/text.js +209 -0
- package/dist/react/surface.d.ts +46 -0
- package/dist/react/surface.js +134 -0
- package/dist/react.d.ts +3 -35
- package/dist/react.js +3 -272
- package/dist/renderer.d.ts +1 -1
- package/dist/renderer.js +299 -4
- package/dist/styles.css +805 -20
- package/package.json +28 -11
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
/** Standard catalog — inputs: buttons, forms and the controls that write a value back to the data model. */
|
|
4
|
+
import { createContext, useContext, useEffect, useId, useRef, useState } from "react";
|
|
5
|
+
import { Kids, boxStyle, cx, kid, openUrl, standard, surfaceColor, useHost } from "../core.js";
|
|
6
|
+
import { IconGlyph } from "./media.js";
|
|
7
|
+
/** True inside a Form, so a Button with `submit` becomes its submit button. */
|
|
8
|
+
export const FormContext = createContext(false);
|
|
9
|
+
/** Options arrive as strings or `{value, label, description}`; anything else is dropped. */
|
|
10
|
+
function options(list) {
|
|
11
|
+
if (!Array.isArray(list))
|
|
12
|
+
return [];
|
|
13
|
+
const out = [];
|
|
14
|
+
for (const o of list) {
|
|
15
|
+
if (typeof o === "string" || typeof o === "number")
|
|
16
|
+
out.push({ value: String(o), label: String(o) });
|
|
17
|
+
else if (o && typeof o === "object" && o.value != null) {
|
|
18
|
+
const value = String(o.value);
|
|
19
|
+
const label = o.label, description = o.description;
|
|
20
|
+
out.push({ value, label: label == null || label === "" ? value : String(label), description: typeof description === "string" && description ? description : undefined });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
const str = (v) => (v == null ? "" : String(v));
|
|
26
|
+
const num = (v, fallback) => (typeof v === "number" && Number.isFinite(v) ? v : fallback);
|
|
27
|
+
const grow = (weight) => (typeof weight === "number" ? { flex: `${weight} 1 0%`, minWidth: 0 } : {});
|
|
28
|
+
const call = (fn, ...args) => { if (typeof fn === "function")
|
|
29
|
+
void fn(...args); };
|
|
30
|
+
const verdict = (valid, message) => ({ valid, message });
|
|
31
|
+
const isVerdict = (v) => !!v && typeof v === "object" && "valid" in v;
|
|
32
|
+
const within = (n, a) => (typeof a.min !== "number" || n >= a.min) && (typeof a.max !== "number" || n <= a.max);
|
|
33
|
+
const range = (a, unit = "") => typeof a.min === "number" && typeof a.max === "number" ? `between ${a.min} and ${a.max}${unit}`
|
|
34
|
+
: typeof a.min === "number" ? `at least ${a.min}${unit}` : `at most ${a.max}${unit}`;
|
|
35
|
+
/**
|
|
36
|
+
* The catalog's validators, evaluated here when the binder hands back no verdict for a `@call` (the catalog
|
|
37
|
+
* declares them `rendererOnly`; a host that registers its own implementations takes precedence).
|
|
38
|
+
*/
|
|
39
|
+
const VALIDATORS = {
|
|
40
|
+
required: (a) => verdict(!(a.value == null || a.value === false || (typeof a.value === "string" && !a.value.trim()) || (Array.isArray(a.value) && !a.value.length)), "This field is required"),
|
|
41
|
+
regex: (a) => { try {
|
|
42
|
+
return verdict(new RegExp(str(a.pattern)).test(str(a.value)), "Invalid format");
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return verdict(true, "");
|
|
46
|
+
} },
|
|
47
|
+
length: (a) => verdict(within(str(a.value).length, a), `Must be ${range(a, " characters")}`),
|
|
48
|
+
numeric: (a) => {
|
|
49
|
+
const n = typeof a.value === "number" ? a.value : typeof a.value === "string" && a.value.trim() ? Number(a.value) : NaN;
|
|
50
|
+
return Number.isFinite(n) ? verdict(within(n, a), `Must be ${range(a)}`) : verdict(false, "Must be a number");
|
|
51
|
+
},
|
|
52
|
+
email: (a) => verdict(/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str(a.value)), "Enter a valid email address"),
|
|
53
|
+
and: (a) => (Array.isArray(a.values) ? a.values : []).every(truthy),
|
|
54
|
+
or: (a) => (Array.isArray(a.values) ? a.values : []).some(truthy),
|
|
55
|
+
not: (a) => !truthy(a.value),
|
|
56
|
+
};
|
|
57
|
+
const truthy = (v) => (isVerdict(v) ? !!v.valid : !!v);
|
|
58
|
+
const callName = (v) => (v && typeof v === "object" ? (typeof v["@call"] === "string" ? v["@call"] : typeof v.call === "string" ? v.call : undefined) : undefined);
|
|
59
|
+
const isBinding = (v) => v && typeof v === "object" && (typeof v["@path"] === "string" || (typeof v.path === "string" && Object.keys(v).length === 1));
|
|
60
|
+
function evaluate(node, dc) {
|
|
61
|
+
if (Array.isArray(node))
|
|
62
|
+
return node.map((n) => evaluate(n, dc));
|
|
63
|
+
const name = callName(node);
|
|
64
|
+
if (name && VALIDATORS[name]) {
|
|
65
|
+
const args = {};
|
|
66
|
+
for (const [k, v] of Object.entries(node.args ?? {}))
|
|
67
|
+
args[k] = evaluate(v, dc);
|
|
68
|
+
return VALIDATORS[name](args);
|
|
69
|
+
}
|
|
70
|
+
if (node && typeof node === "object" && !isVerdict(node)) {
|
|
71
|
+
try {
|
|
72
|
+
return dc.resolveDynamicValue(node);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return node;
|
|
79
|
+
}
|
|
80
|
+
function bindings(node, out = []) {
|
|
81
|
+
if (Array.isArray(node))
|
|
82
|
+
node.forEach((n) => bindings(n, out));
|
|
83
|
+
else if (isBinding(node))
|
|
84
|
+
out.push(node);
|
|
85
|
+
else if (node && typeof node === "object")
|
|
86
|
+
Object.values(node).forEach((n) => bindings(n, out));
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/** The state of the component's `checks`: the first failed message, and whether an error-level check fails. */
|
|
90
|
+
function useChecks(props, context) {
|
|
91
|
+
const raw = context?.componentModel?.properties?.checks;
|
|
92
|
+
const key = Array.isArray(raw) ? JSON.stringify(raw) : "";
|
|
93
|
+
const [, bump] = useState(0);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (!key)
|
|
96
|
+
return;
|
|
97
|
+
const dc = context.dataContext;
|
|
98
|
+
const subs = [];
|
|
99
|
+
for (const b of bindings(raw)) {
|
|
100
|
+
try {
|
|
101
|
+
subs.push(dc.subscribeDynamicValue(b, () => bump((n) => n + 1)));
|
|
102
|
+
}
|
|
103
|
+
catch { /* an unreadable binding never changes */ }
|
|
104
|
+
}
|
|
105
|
+
return () => subs.forEach((s) => { try {
|
|
106
|
+
s?.unsubscribe?.();
|
|
107
|
+
}
|
|
108
|
+
catch { /* already gone */ } });
|
|
109
|
+
}, [key, context]);
|
|
110
|
+
const failed = [];
|
|
111
|
+
if (Array.isArray(raw)) {
|
|
112
|
+
const resolved = Array.isArray(props.checks) ? props.checks : [];
|
|
113
|
+
raw.forEach((rule, i) => {
|
|
114
|
+
const isRule = rule && typeof rule === "object" && "condition" in rule;
|
|
115
|
+
const given = isRule ? resolved[i]?.condition : resolved[i];
|
|
116
|
+
let result = typeof given === "boolean" || isVerdict(given) ? given : evaluate(isRule ? rule.condition : rule, context.dataContext);
|
|
117
|
+
// A call that only the agent can answer has no verdict here; it must not block the user.
|
|
118
|
+
if (result == null || typeof result.then === "function")
|
|
119
|
+
result = true;
|
|
120
|
+
if (truthy(result))
|
|
121
|
+
return;
|
|
122
|
+
const own = isRule ? resolved[i]?.message : undefined;
|
|
123
|
+
const v = isVerdict(result) ? result : { valid: false };
|
|
124
|
+
failed.push({ valid: false, message: typeof own === "string" && own ? own : str(v.message) || "Invalid value", severity: v.severity ?? "error" });
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const fromBinder = Array.isArray(props.validationErrors) ? props.validationErrors.map((e) => (typeof e === "string" ? e : str(e?.message))).filter(Boolean) : [];
|
|
128
|
+
return { error: failed[0]?.message ?? fromBinder[0], invalid: props.isValid === false || failed.some((f) => f.severity !== "warning" && f.severity !== "info") };
|
|
129
|
+
}
|
|
130
|
+
/** Label above, control, then the first failed check. `group` labels a set of controls instead of one. */
|
|
131
|
+
function Field({ view, kind, id, error, group, className, children }) {
|
|
132
|
+
const p = view.props;
|
|
133
|
+
return _jsxs("div", { className: cx("ainui-field", `ainui-${kind}`, p.block && "ainui-field-block", p.disabled && "ainui-field-disabled", className), style: grow(p.weight), children: [p.label != null && p.label !== "" && _jsxs("label", { className: "ainui-field-label", id: `${id}-label`, htmlFor: group ? undefined : id, children: [str(p.label), p.required && _jsx("span", { className: "ainui-field-required", "aria-hidden": "true", children: " *" })] }), children, error && _jsx("div", { className: "ainui-field-error", id: `${id}-error`, "aria-live": "polite", ref: view.validationErrorsRef, children: error })] });
|
|
134
|
+
}
|
|
135
|
+
const described = (id, error) => (error ? { "aria-describedby": `${id}-error` } : {});
|
|
136
|
+
const labelled = (id, p) => (p.label != null && p.label !== "" ? { "aria-labelledby": `${id}-label` } : {});
|
|
137
|
+
/**
|
|
138
|
+
* A text draft kept beside the bound value, so typing (and IME composition) is never interrupted by the data
|
|
139
|
+
* model's round trip. `write` returns the text form of what it stored.
|
|
140
|
+
*/
|
|
141
|
+
function useDraft(value, write) {
|
|
142
|
+
const external = str(value);
|
|
143
|
+
const [draft, setDraft] = useState(external);
|
|
144
|
+
const last = useRef(external);
|
|
145
|
+
useEffect(() => { if (external !== last.current) {
|
|
146
|
+
last.current = external;
|
|
147
|
+
setDraft(external);
|
|
148
|
+
} }, [external]);
|
|
149
|
+
return [draft, (text) => { setDraft(text); last.current = write(text); }];
|
|
150
|
+
}
|
|
151
|
+
/** `#`/`9`/`0` take a digit, `A`/`a` a letter, `*` either; any other mask character is inserted as written. */
|
|
152
|
+
function applyMask(mask, input) {
|
|
153
|
+
const rest = [...input].filter((c) => /[\p{L}\p{N}]/u.test(c));
|
|
154
|
+
const take = (ok) => { while (rest.length && !ok.test(rest[0]))
|
|
155
|
+
rest.shift(); return rest.shift() ?? ""; };
|
|
156
|
+
let out = "";
|
|
157
|
+
for (const m of mask) {
|
|
158
|
+
if (!rest.length)
|
|
159
|
+
break;
|
|
160
|
+
out += m === "#" || m === "9" || m === "0" ? take(/\d/) : m === "A" || m === "a" ? take(/\p{L}/u) : m === "*" ? take(/./u) : m;
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
/* ── Button, Pressable, Form ────────────────────────────────────────────────────────────────────────────────── */
|
|
165
|
+
const Button = standard("Button", ({ props, buildChild, context, accessibilityRef }) => {
|
|
166
|
+
const host = useHost();
|
|
167
|
+
const inForm = useContext(FormContext);
|
|
168
|
+
const { error, invalid } = useChecks(props, context);
|
|
169
|
+
const [copied, setCopied] = useState(false);
|
|
170
|
+
const timer = useRef(undefined);
|
|
171
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
172
|
+
const submit = !!props.submit && inForm;
|
|
173
|
+
const disabled = !!props.disabled || !!props.loading || invalid;
|
|
174
|
+
const danger = props.tone === "danger" || props.color === "danger";
|
|
175
|
+
const variant = props.variant === "borderless" ? "ghost" : props.variant;
|
|
176
|
+
const click = (e) => {
|
|
177
|
+
if (disabled) {
|
|
178
|
+
e.preventDefault();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (props.href) {
|
|
182
|
+
e.preventDefault();
|
|
183
|
+
openUrl(host, props.href);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (typeof props.copy === "string") {
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
try {
|
|
189
|
+
void globalThis.navigator?.clipboard?.writeText?.(props.copy)?.catch?.(() => undefined);
|
|
190
|
+
}
|
|
191
|
+
catch { /* no clipboard here */ }
|
|
192
|
+
setCopied(true);
|
|
193
|
+
clearTimeout(timer.current);
|
|
194
|
+
timer.current = setTimeout(() => setCopied(false), 1500);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (typeof props.confirm === "string" && props.confirm && typeof globalThis.confirm === "function" && !globalThis.confirm(props.confirm)) {
|
|
198
|
+
e.preventDefault();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (submit)
|
|
202
|
+
return;
|
|
203
|
+
// `action` is the A2UI basic catalog / AINUI v1 name for onClick.
|
|
204
|
+
call(props.onClick ?? props.action);
|
|
205
|
+
};
|
|
206
|
+
const hasChildren = Array.isArray(props.children) && props.children.length > 0;
|
|
207
|
+
const icon = copied ? "check" : typeof props.icon === "string" && props.icon ? props.icon : undefined;
|
|
208
|
+
return _jsxs("button", { ref: accessibilityRef, type: submit ? "submit" : "button", disabled: disabled, "aria-busy": props.loading ? true : undefined, title: copied ? "Copied" : invalid ? error : undefined, "data-copied": copied ? "" : undefined, onClick: click, style: grow(props.weight), className: cx("ainui-button", variant && variant !== "default" && `ainui-button-${variant}`, danger && "ainui-button-danger", props.color === "secondary" && "ainui-button-secondary", typeof props.size === "string" && `ainui-button-${props.size}`, props.pill !== false && "ainui-button-pill", props.block && "ainui-button-block", props.uniform && "ainui-button-uniform", props.loading && "ainui-button-loading"), children: [props.loading && _jsx("span", { className: "ainui-button-spinner", "aria-hidden": "true" }), icon && _jsx("span", { className: "ainui-button-icon", "data-icon": icon, "aria-hidden": "true", children: _jsx(IconGlyph, { name: icon, size: 16 }) }), hasChildren ? _jsx(Kids, { of: props.children, buildChild: buildChild }) : props.child ? kid(buildChild, props.child) : str(props.label ?? props.text) || null, copied && _jsx("span", { className: "ainui-visually-hidden", role: "status", children: "Copied" })] });
|
|
209
|
+
});
|
|
210
|
+
function useOnVisible(fn) {
|
|
211
|
+
const ref = useRef(null);
|
|
212
|
+
const done = useRef(false);
|
|
213
|
+
useEffect(() => {
|
|
214
|
+
const el = ref.current;
|
|
215
|
+
if (typeof fn !== "function" || !el || done.current || typeof IntersectionObserver === "undefined")
|
|
216
|
+
return;
|
|
217
|
+
const io = new IntersectionObserver((entries) => {
|
|
218
|
+
if (entries.some((e) => e.isIntersecting) && !done.current) {
|
|
219
|
+
done.current = true;
|
|
220
|
+
io.disconnect();
|
|
221
|
+
void fn();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
io.observe(el);
|
|
225
|
+
return () => io.disconnect();
|
|
226
|
+
}, [fn]);
|
|
227
|
+
return ref;
|
|
228
|
+
}
|
|
229
|
+
const CONTROLS = "a,button,input,select,textarea,label,summary,[role=button],[role=link],[role=radio],[role=tab],[role=checkbox],[role=switch]";
|
|
230
|
+
const Pressable = standard("Pressable", ({ props, buildChild }) => {
|
|
231
|
+
const host = useHost();
|
|
232
|
+
const ref = useOnVisible(props.onVisible);
|
|
233
|
+
const disabled = !!props.disabled;
|
|
234
|
+
const press = () => { if (disabled)
|
|
235
|
+
return; if (props.href)
|
|
236
|
+
openUrl(host, props.href);
|
|
237
|
+
else
|
|
238
|
+
call(props.onClick); };
|
|
239
|
+
// A control inside sends only its own event.
|
|
240
|
+
const fromInner = (e) => {
|
|
241
|
+
const hit = e.target?.closest?.(CONTROLS);
|
|
242
|
+
return !!hit && hit !== e.currentTarget && e.currentTarget.contains(hit);
|
|
243
|
+
};
|
|
244
|
+
const hover = surfaceColor(props.hoverBackground);
|
|
245
|
+
return _jsx("div", { ref: ref, role: typeof props.role === "string" && props.role ? props.role : props.href ? "link" : "button", tabIndex: disabled ? -1 : 0, "aria-disabled": disabled || undefined, "aria-pressed": typeof props.ariaPressed === "boolean" ? props.ariaPressed : undefined, title: typeof props.tooltip === "string" && props.tooltip ? props.tooltip : undefined, className: cx("ainui-pressable", disabled && "ainui-pressable-disabled", props.flush && "ainui-flush", props.insetBorder && "ainui-pressable-inset-border"), style: { display: "flex", flexDirection: "column", gap: props.disableAutoSpacing ? 0 : undefined, ...boxStyle(props), ...(hover ? { "--ainui-pressable-hover": hover } : {}) }, onClick: (e) => { if (!fromInner(e))
|
|
246
|
+
press(); }, onKeyDown: (e) => {
|
|
247
|
+
if (e.target !== e.currentTarget || (e.key !== "Enter" && e.key !== " "))
|
|
248
|
+
return;
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
press();
|
|
251
|
+
}, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) });
|
|
252
|
+
});
|
|
253
|
+
const ENTER_SUBMITS = new Set(["text", "search", "email", "url", "tel", "password", "number", "date", "time", "datetime-local"]);
|
|
254
|
+
const Form = standard("Form", ({ props, buildChild }) => {
|
|
255
|
+
const sending = useRef(false);
|
|
256
|
+
const send = () => {
|
|
257
|
+
// One user gesture can reach here twice (Enter plus the browser's implicit submission).
|
|
258
|
+
if (sending.current)
|
|
259
|
+
return;
|
|
260
|
+
sending.current = true;
|
|
261
|
+
setTimeout(() => { sending.current = false; }, 0);
|
|
262
|
+
call(props.onSubmit);
|
|
263
|
+
};
|
|
264
|
+
return _jsx("form", { className: cx("ainui-form", props.flush && "ainui-flush"), role: typeof props.role === "string" && props.role ? props.role : undefined, "aria-label": typeof props.label === "string" && props.label ? props.label : undefined, style: { display: "flex", flexDirection: "column", gap: props.disableAutoSpacing ? 0 : undefined, ...boxStyle(props) }, onSubmit: (e) => { e.preventDefault(); send(); }, onKeyDown: (e) => {
|
|
265
|
+
const el = e.target;
|
|
266
|
+
if (e.key !== "Enter" || e.defaultPrevented || e.nativeEvent?.isComposing || el?.tagName !== "INPUT" || !ENTER_SUBMITS.has(el.type))
|
|
267
|
+
return;
|
|
268
|
+
e.preventDefault();
|
|
269
|
+
const form = e.currentTarget;
|
|
270
|
+
if (typeof form.requestSubmit === "function") {
|
|
271
|
+
try {
|
|
272
|
+
form.requestSubmit();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
catch { /* fall through */ }
|
|
276
|
+
}
|
|
277
|
+
send();
|
|
278
|
+
}, children: _jsx(FormContext.Provider, { value: true, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }) });
|
|
279
|
+
});
|
|
280
|
+
/* ── Text ───────────────────────────────────────────────────────────────────────────────────────────────────── */
|
|
281
|
+
const INPUT_TYPE = { number: "number", obscured: "password", email: "email", url: "url", tel: "tel" };
|
|
282
|
+
function TextAreaControl({ view, id, error, invalid }) {
|
|
283
|
+
const p = view.props;
|
|
284
|
+
const [draft, setDraft] = useDraft(p.value, (text) => { call(p.setValue, text); return text; });
|
|
285
|
+
const el = useRef(null);
|
|
286
|
+
useEffect(() => {
|
|
287
|
+
const t = el.current;
|
|
288
|
+
if (!t || !p.autoResize)
|
|
289
|
+
return;
|
|
290
|
+
t.style.height = "auto";
|
|
291
|
+
if (!t.scrollHeight)
|
|
292
|
+
return;
|
|
293
|
+
const line = typeof getComputedStyle === "function" ? parseFloat(getComputedStyle(t).lineHeight) : NaN;
|
|
294
|
+
const cap = typeof p.maxRows === "number" && line > 0 ? p.maxRows * line + (t.offsetHeight - t.clientHeight) + 16 : Infinity;
|
|
295
|
+
t.style.height = `${Math.min(t.scrollHeight + (t.offsetHeight - t.clientHeight), cap)}px`;
|
|
296
|
+
t.style.overflowY = t.scrollHeight > cap ? "auto" : "hidden";
|
|
297
|
+
}, [draft, p.autoResize, p.maxRows]);
|
|
298
|
+
return _jsx("textarea", { id: id, ref: (node) => { el.current = node; view.accessibilityRef?.(node); }, className: cx("ainui-input", "ainui-input-multiline", typeof p.size === "string" && `ainui-input-${p.size}`, p.autoResize && "ainui-input-auto-resize"), rows: Math.max(1, Math.round(num(p.rows, 3))), value: draft, name: p.name || undefined, placeholder: p.placeholder || undefined, required: !!p.required, disabled: !!p.disabled, "aria-invalid": invalid || !!p.invalid || undefined, ...described(id, error), onChange: (e) => setDraft(e.target.value) });
|
|
299
|
+
}
|
|
300
|
+
const TextArea = standard("TextArea", (view) => {
|
|
301
|
+
const id = useId();
|
|
302
|
+
const { error, invalid } = useChecks(view.props, view.context);
|
|
303
|
+
return _jsx(Field, { view: view, kind: "text-area", id: id, error: error, children: _jsx(TextAreaControl, { view: view, id: id, error: error, invalid: invalid }) });
|
|
304
|
+
});
|
|
305
|
+
function TextInputControl({ view, id, error, invalid }) {
|
|
306
|
+
const p = view.props;
|
|
307
|
+
const numeric = p.variant === "number";
|
|
308
|
+
const [draft, setDraft] = useDraft(p.value, (text) => {
|
|
309
|
+
if (!numeric) {
|
|
310
|
+
call(p.setValue, text);
|
|
311
|
+
return text;
|
|
312
|
+
}
|
|
313
|
+
const n = text.trim() === "" ? NaN : Number(text);
|
|
314
|
+
if (Number.isFinite(n)) {
|
|
315
|
+
call(p.setValue, n);
|
|
316
|
+
return String(n);
|
|
317
|
+
}
|
|
318
|
+
// An unfinished number ("-", "1e") is not written; an emptied field clears the value.
|
|
319
|
+
if (text.trim() === "")
|
|
320
|
+
call(p.setValue, "");
|
|
321
|
+
return "";
|
|
322
|
+
});
|
|
323
|
+
return _jsx("input", { id: id, ref: view.accessibilityRef, type: INPUT_TYPE[p.variant] ?? "text", className: cx("ainui-input", typeof p.size === "string" && `ainui-input-${p.size}`, p.pill !== false && "ainui-input-pill"), value: draft, name: p.name || undefined, placeholder: p.placeholder || undefined, required: !!p.required, disabled: !!p.disabled, "aria-invalid": invalid || !!p.invalid || undefined, ...described(id, error), onFocus: (e) => { if (p.autoSelect)
|
|
324
|
+
e.currentTarget.select?.(); }, onChange: (e) => setDraft(typeof p.mask === "string" && p.mask && !numeric ? applyMask(p.mask, e.target.value) : e.target.value) });
|
|
325
|
+
}
|
|
326
|
+
const TextField = standard("TextField", (view) => {
|
|
327
|
+
const id = useId();
|
|
328
|
+
const { error, invalid } = useChecks(view.props, view.context);
|
|
329
|
+
const Control = view.props.variant === "longText" ? TextAreaControl : TextInputControl;
|
|
330
|
+
return _jsx(Field, { view: view, kind: "text-field", id: id, error: error, children: _jsx(Control, { view: view, id: id, error: error, invalid: invalid }) });
|
|
331
|
+
});
|
|
332
|
+
/* ── Choice ─────────────────────────────────────────────────────────────────────────────────────────────────── */
|
|
333
|
+
const Select = standard("Select", (view) => {
|
|
334
|
+
const { props: p } = view;
|
|
335
|
+
const id = useId();
|
|
336
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
337
|
+
const list = options(p.options);
|
|
338
|
+
const value = str(p.value);
|
|
339
|
+
const known = value === "" || list.some((o) => o.value === value);
|
|
340
|
+
return _jsx(Field, { view: view, kind: "select", id: id, error: error, children: _jsxs("span", { className: cx("ainui-select-box", typeof p.icon === "string" && p.icon && "ainui-select-with-icon"), children: [typeof p.icon === "string" && p.icon && _jsx("span", { className: "ainui-select-icon", "data-icon": p.icon, "aria-hidden": "true", children: _jsx(IconGlyph, { name: p.icon, size: 16 }) }), _jsxs("select", { id: id, ref: view.accessibilityRef, className: cx("ainui-input", "ainui-select-control", typeof p.size === "string" && `ainui-input-${p.size}`, p.pill !== false && "ainui-input-pill", value === "" && "ainui-select-empty"), value: value, name: p.name || undefined, required: !!p.required, disabled: !!p.disabled, "aria-invalid": invalid || undefined, ...described(id, error), onChange: (e) => call(p.setValue, e.target.value), children: [typeof p.placeholder === "string" && p.placeholder ? _jsx("option", { value: "", disabled: true, hidden: !!p.clearable, children: p.placeholder })
|
|
341
|
+
: !p.clearable && value === "" && _jsx("option", { value: "", disabled: true, hidden: true }), p.clearable && _jsx("option", { value: "", children: typeof p.clearLabel === "string" && p.clearLabel ? p.clearLabel : "None" }), !known && _jsx("option", { value: value, children: value }), list.map((o) => _jsx("option", { value: o.value, title: o.description, children: o.label }, o.value))] })] }) });
|
|
342
|
+
});
|
|
343
|
+
const CheckBox = standard("CheckBox", (view) => {
|
|
344
|
+
const { props: p } = view;
|
|
345
|
+
const id = useId();
|
|
346
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
347
|
+
const checked = p.value === true;
|
|
348
|
+
return _jsxs("div", { className: cx("ainui-field", "ainui-check-box", `ainui-check-box-${p.shape === "square" ? "square" : "round"}`, p.disabled && "ainui-field-disabled"), style: grow(p.weight), children: [_jsxs("label", { className: cx("ainui-check-box-row", checked && p.lineThrough && "ainui-check-box-line-through"), htmlFor: id, children: [_jsx("input", { id: id, ref: view.accessibilityRef, type: "checkbox", checked: checked, name: p.name || undefined, disabled: !!p.disabled, required: !!p.required, "aria-invalid": invalid || undefined, ...described(id, error), onChange: (e) => call(p.setValue, e.target.checked) }), p.label != null && p.label !== "" && _jsxs("span", { className: "ainui-check-box-label", children: [str(p.label), p.required && _jsx("span", { className: "ainui-field-required", "aria-hidden": "true", children: " *" })] })] }), error && _jsx("div", { className: "ainui-field-error", id: `${id}-error`, "aria-live": "polite", ref: view.validationErrorsRef, children: error })] });
|
|
349
|
+
});
|
|
350
|
+
/** The catalog gives a lone Radio no binding for its checked state (RadioGroup keeps the value), so it is the browser's own. */
|
|
351
|
+
const Radio = standard("Radio", ({ props: p, accessibilityRef }) => {
|
|
352
|
+
const id = useId();
|
|
353
|
+
return _jsxs("label", { className: cx("ainui-radio", p.disabled && "ainui-field-disabled"), htmlFor: id, children: [_jsx("input", { id: id, ref: accessibilityRef, type: "radio", value: str(p.value), disabled: !!p.disabled, required: !!p.required }), p.label != null && p.label !== "" && _jsx("span", { className: "ainui-radio-label", children: str(p.label) })] });
|
|
354
|
+
});
|
|
355
|
+
const RadioGroup = standard("RadioGroup", (view) => {
|
|
356
|
+
const { props: p } = view;
|
|
357
|
+
const id = useId();
|
|
358
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
359
|
+
const value = str(p.value);
|
|
360
|
+
const variant = p.variant === "pills" || p.variant === "cards" ? p.variant : "list";
|
|
361
|
+
return _jsx(Field, { view: view, kind: "radio-group", id: id, error: error, group: true, children: _jsx("div", { ref: view.accessibilityRef, role: "radiogroup", ...labelled(id, p), ...described(id, error), "aria-required": p.required || undefined, "aria-invalid": invalid || undefined, className: cx("ainui-radio-group-options", `ainui-radio-group-${variant}`, `ainui-radio-group-${p.direction === "col" ? "col" : "row"}`), children: options(p.options).map((o, i) => _jsxs("label", { className: cx("ainui-radio-group-option", o.value === value && "ainui-selected"), htmlFor: `${id}-${i}`, children: [_jsx("input", { id: `${id}-${i}`, type: "radio", name: p.name || id, value: o.value, checked: o.value === value, disabled: !!p.disabled, required: !!p.required, onChange: () => call(p.setValue, o.value) }), _jsxs("span", { className: "ainui-radio-group-text", children: [_jsx("span", { className: "ainui-radio-group-label", children: o.label }), o.description && _jsx("span", { className: "ainui-radio-group-description", children: o.description })] })] }, o.value)) }) });
|
|
362
|
+
});
|
|
363
|
+
const SegmentedControl = standard("SegmentedControl", (view) => {
|
|
364
|
+
const { props: p } = view;
|
|
365
|
+
const id = useId();
|
|
366
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
367
|
+
const list = options(p.options);
|
|
368
|
+
const value = str(p.value);
|
|
369
|
+
const current = list.findIndex((o) => o.value === value);
|
|
370
|
+
const box = useRef(null);
|
|
371
|
+
const move = (e, i) => {
|
|
372
|
+
const next = e.key === "ArrowRight" || e.key === "ArrowDown" ? (i + 1) % list.length : e.key === "ArrowLeft" || e.key === "ArrowUp" ? (i - 1 + list.length) % list.length
|
|
373
|
+
: e.key === "Home" ? 0 : e.key === "End" ? list.length - 1 : -1;
|
|
374
|
+
if (next < 0 || p.disabled)
|
|
375
|
+
return;
|
|
376
|
+
e.preventDefault();
|
|
377
|
+
call(p.setValue, list[next].value);
|
|
378
|
+
box.current?.querySelectorAll("[role=radio]")[next]?.focus?.();
|
|
379
|
+
};
|
|
380
|
+
return _jsx(Field, { view: view, kind: "segmented-control", id: id, error: error, group: true, children: _jsx("div", { ref: (node) => { box.current = node; view.accessibilityRef?.(node); }, role: "radiogroup", ...labelled(id, p), ...described(id, error), "aria-invalid": invalid || undefined, className: cx("ainui-segmented-control-track", `ainui-segmented-control-${typeof p.variant === "string" && p.variant ? p.variant : "ghost"}`, typeof p.size === "string" && `ainui-segmented-control-${p.size}`, p.pill !== false && "ainui-segmented-control-pill", p.block && "ainui-segmented-control-block"), children: list.map((o, i) => _jsx("button", { type: "button", role: "radio", "aria-checked": i === current, tabIndex: i === current || (current < 0 && i === 0) ? 0 : -1, disabled: !!p.disabled, title: o.description, className: cx("ainui-segmented-control-option", i === current && "ainui-selected"), onClick: () => call(p.setValue, o.value), onKeyDown: (e) => move(e, i), children: o.label }, o.value)) }) });
|
|
381
|
+
});
|
|
382
|
+
const ChoicePicker = standard("ChoicePicker", (view) => {
|
|
383
|
+
const { props: p } = view;
|
|
384
|
+
const id = useId();
|
|
385
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
386
|
+
const [filter, setFilter] = useState("");
|
|
387
|
+
const multiple = p.variant === "multipleSelection";
|
|
388
|
+
const chosen = Array.isArray(p.value) ? p.value.map(String) : typeof p.value === "string" && p.value ? [p.value] : [];
|
|
389
|
+
const all = options(p.options);
|
|
390
|
+
const needle = filter.trim().toLowerCase();
|
|
391
|
+
const shown = p.filterable && needle ? all.filter((o) => o.label.toLowerCase().includes(needle) || o.value.toLowerCase().includes(needle)) : all;
|
|
392
|
+
const pick = (value, on) => {
|
|
393
|
+
if (!multiple) {
|
|
394
|
+
call(p.setValue, [value]);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
// Keep the catalog's option order, and any chosen value that is no longer offered.
|
|
398
|
+
const next = new Set(chosen);
|
|
399
|
+
if (on)
|
|
400
|
+
next.add(value);
|
|
401
|
+
else
|
|
402
|
+
next.delete(value);
|
|
403
|
+
call(p.setValue, [...all.map((o) => o.value).filter((v) => next.has(v)), ...chosen.filter((v) => next.has(v) && !all.some((o) => o.value === v))]);
|
|
404
|
+
};
|
|
405
|
+
return _jsxs(Field, { view: view, kind: "choice-picker", id: id, error: error, group: true, children: [p.filterable && _jsx("input", { type: "search", className: "ainui-input ainui-choice-picker-filter", "aria-label": "Filter options", placeholder: "Filter", value: filter, disabled: !!p.disabled, onChange: (e) => setFilter(e.target.value) }), _jsxs("div", { ref: view.accessibilityRef, role: multiple ? "group" : "radiogroup", ...labelled(id, p), ...described(id, error), "aria-invalid": invalid || undefined, "aria-required": p.required || undefined, className: cx("ainui-choice-picker-options", `ainui-choice-picker-${p.displayStyle === "chips" ? "chips" : "checkbox"}`), children: [shown.map((o) => {
|
|
406
|
+
const on = chosen.includes(o.value), key = `${id}-${all.indexOf(o)}`;
|
|
407
|
+
return _jsxs("label", { className: cx("ainui-choice-picker-option", on && "ainui-selected"), htmlFor: key, children: [_jsx("input", { id: key, type: multiple ? "checkbox" : "radio", name: multiple ? undefined : id, value: o.value, checked: on, disabled: !!p.disabled, onChange: (e) => pick(o.value, e.target.checked) }), _jsxs("span", { className: "ainui-choice-picker-text", children: [_jsx("span", { className: "ainui-choice-picker-label", children: o.label }), o.description && _jsx("span", { className: "ainui-choice-picker-description", children: o.description })] })] }, o.value);
|
|
408
|
+
}), p.filterable && shown.length === 0 && _jsx("span", { className: "ainui-choice-picker-empty", children: "No matching options" })] })] });
|
|
409
|
+
});
|
|
410
|
+
/* ── Number, date and time ──────────────────────────────────────────────────────────────────────────────────── */
|
|
411
|
+
const Slider = standard("Slider", (view) => {
|
|
412
|
+
const { props: p } = view;
|
|
413
|
+
const id = useId();
|
|
414
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
415
|
+
const min = num(p.min, 0);
|
|
416
|
+
const max = Math.max(min, num(p.max, 100));
|
|
417
|
+
const step = typeof p.steps === "number" && p.steps >= 1 && max > min ? (max - min) / Math.round(p.steps) : num(p.step, 1) > 0 ? num(p.step, 1) : 1;
|
|
418
|
+
const value = Math.min(max, Math.max(min, num(p.value, min)));
|
|
419
|
+
const marks = (Array.isArray(p.marks) ? p.marks : []).filter((m) => m && typeof m.value === "number" && m.value >= min && m.value <= max);
|
|
420
|
+
const at = (v) => (max > min ? ((v - min) / (max - min)) * 100 : 0);
|
|
421
|
+
const shown = `${Number(value.toFixed(6))}${typeof p.unit === "string" ? p.unit : ""}`;
|
|
422
|
+
return _jsxs(Field, { view: view, kind: "slider", id: id, error: error, className: p.sliderStyle === "discrete" && "ainui-slider-discrete", children: [_jsxs("div", { className: "ainui-slider-row", children: [_jsx("input", { id: id, ref: view.accessibilityRef, type: "range", className: "ainui-slider-input", min: min, max: max, step: step, value: value, name: p.name || undefined, disabled: !!p.disabled, list: marks.length ? `${id}-marks` : undefined, "aria-valuetext": shown, "aria-invalid": invalid || undefined, ...described(id, error), style: { "--ainui-slider-fill": `${at(value)}%` }, onChange: (e) => { const n = Number(e.target.value); if (Number.isFinite(n))
|
|
423
|
+
call(p.setValue, n); } }), _jsx("output", { className: "ainui-slider-value", htmlFor: id, children: shown })] }), marks.length > 0 && _jsxs(_Fragment, { children: [_jsx("datalist", { id: `${id}-marks`, children: marks.map((m) => _jsx("option", { value: m.value, label: typeof m.label === "string" ? m.label : undefined }, m.value)) }), _jsx("div", { className: "ainui-slider-marks", "aria-hidden": "true", children: marks.map((m) => _jsx("span", { className: "ainui-slider-mark", style: { left: `${at(m.value)}%` }, children: typeof m.label === "string" && m.label ? m.label : m.value }, m.value)) })] })] });
|
|
424
|
+
});
|
|
425
|
+
const DATE = /^\d{4}-\d{2}-\d{2}/, TIME = /^\d{2}:\d{2}/;
|
|
426
|
+
const datePart = (v) => (typeof v === "string" && DATE.test(v) ? v.slice(0, 10) : "");
|
|
427
|
+
const timePart = (v) => {
|
|
428
|
+
if (typeof v !== "string")
|
|
429
|
+
return "";
|
|
430
|
+
const t = v.includes("T") ? v.slice(v.indexOf("T") + 1) : v;
|
|
431
|
+
return TIME.test(t) ? t.slice(0, 5) : "";
|
|
432
|
+
};
|
|
433
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
434
|
+
const DateTimeInput = standard("DateTimeInput", (view) => {
|
|
435
|
+
const { props: p } = view;
|
|
436
|
+
const id = useId();
|
|
437
|
+
const { error, invalid } = useChecks(p, view.context);
|
|
438
|
+
const time = !!p.enableTime;
|
|
439
|
+
const date = p.enableDate === true || !time;
|
|
440
|
+
const mode = date && time ? "datetime-local" : time ? "time" : "date";
|
|
441
|
+
const fit = (v) => (mode === "date" ? datePart(v) : mode === "time" ? timePart(v) : datePart(v) && timePart(v) ? `${datePart(v)}T${timePart(v)}` : "");
|
|
442
|
+
const value = fit(p.value), min = fit(p.min) || undefined, max = fit(p.max) || undefined;
|
|
443
|
+
const step = typeof p.timeStep === "number" && p.timeStep >= 1 ? Math.round(p.timeStep) : 0;
|
|
444
|
+
const common = { disabled: !!p.disabled, required: !!p.required, "aria-invalid": invalid || undefined, ...described(id, error) };
|
|
445
|
+
const write = (v) => call(p.setValue, v);
|
|
446
|
+
let control;
|
|
447
|
+
if (time && step) {
|
|
448
|
+
// Fixed times are a list; with a date beside it the two halves are joined into one value.
|
|
449
|
+
const day = mode === "time" ? "" : datePart(p.value);
|
|
450
|
+
const now = timePart(p.value);
|
|
451
|
+
const times = [];
|
|
452
|
+
for (let m = 0; m < 1440; m += step)
|
|
453
|
+
times.push(`${pad(Math.floor(m / 60))}:${pad(m % 60)}`);
|
|
454
|
+
const allowed = mode === "time" ? times.filter((t) => (!min || t >= min) && (!max || t <= max)) : times;
|
|
455
|
+
if (now && !allowed.includes(now)) {
|
|
456
|
+
allowed.push(now);
|
|
457
|
+
allowed.sort();
|
|
458
|
+
}
|
|
459
|
+
const list = _jsxs("select", { id: mode === "time" ? id : `${id}-time`, ref: mode === "time" ? view.accessibilityRef : undefined, className: "ainui-input ainui-date-time-input-times", "aria-label": mode === "time" ? undefined : "Time", value: now, ...common, disabled: common.disabled || (mode !== "time" && !day), onChange: (e) => write(mode === "time" ? e.target.value : `${day}T${e.target.value}`), children: [_jsx("option", { value: "", disabled: true, hidden: true, children: typeof p.placeholder === "string" && p.placeholder ? p.placeholder : "Time" }), allowed.map((t) => _jsx("option", { value: t, children: t }, t))] });
|
|
460
|
+
control = mode === "time" ? list : _jsxs(_Fragment, { children: [_jsx("input", { id: id, ref: view.accessibilityRef, type: "date", className: "ainui-input", value: day, min: datePart(p.min) || undefined, max: datePart(p.max) || undefined, ...common, onChange: (e) => write(e.target.value ? `${e.target.value}T${now || allowed[0] || "00:00"}` : "") }), list] });
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
control = _jsx("input", { id: id, ref: view.accessibilityRef, type: mode, className: "ainui-input", value: value, min: min, max: max, placeholder: typeof p.placeholder === "string" && p.placeholder ? p.placeholder : undefined, ...common, onChange: (e) => write(mode === "datetime-local" ? e.target.value.slice(0, 16) : e.target.value) });
|
|
464
|
+
}
|
|
465
|
+
return _jsx(Field, { view: view, kind: "date-time-input", id: id, error: error, children: _jsxs("div", { className: "ainui-date-time-input-row", children: [control, p.clearable && str(p.value) !== "" && _jsx("button", { type: "button", className: "ainui-date-time-input-clear", "aria-label": "Clear", disabled: !!p.disabled, onClick: () => write(""), children: _jsx("span", { "aria-hidden": "true", children: "\u00D7" }) })] }) });
|
|
466
|
+
});
|
|
467
|
+
export const inputs = [Button, Pressable, Form, TextField, TextArea, Select, CheckBox, Radio, RadioGroup, Slider, SegmentedControl, ChoicePicker, DateTimeInput];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Standard catalog — layout: containers, grids, carousels, tabs. */
|
|
2
|
+
import React from "react";
|
|
3
|
+
/** Calls `fn` once, the first time the element scrolls into view. */
|
|
4
|
+
export declare function useOnVisible(fn: (() => unknown) | undefined): React.RefObject<HTMLDivElement | null>;
|
|
5
|
+
export declare const layout: import("@a2ui/react").ReactComponentImplementation<import("zod").ZodTypeAny>[];
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
/** Standard catalog — layout: containers, grids, carousels, tabs. */
|
|
4
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
|
+
import { Kids, boxStyle, color, cx, kid, mediaUrl, openUrl, px, space, standard, useHost } from "../core.js";
|
|
6
|
+
/** Calls `fn` once, the first time the element scrolls into view. */
|
|
7
|
+
export function useOnVisible(fn) {
|
|
8
|
+
const ref = useRef(null);
|
|
9
|
+
const done = useRef(false);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const el = ref.current;
|
|
12
|
+
if (!fn || !el || done.current || typeof IntersectionObserver === "undefined")
|
|
13
|
+
return;
|
|
14
|
+
const io = new IntersectionObserver((entries) => {
|
|
15
|
+
if (entries.some((e) => e.isIntersecting) && !done.current) {
|
|
16
|
+
done.current = true;
|
|
17
|
+
io.disconnect();
|
|
18
|
+
void fn();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
io.observe(el);
|
|
22
|
+
return () => io.disconnect();
|
|
23
|
+
}, [fn]);
|
|
24
|
+
return ref;
|
|
25
|
+
}
|
|
26
|
+
const flex = (direction, className) => ({ props, buildChild }) => {
|
|
27
|
+
const ref = useOnVisible(props.onVisible);
|
|
28
|
+
const dir = className === "ainui-box" && props.direction === "row" ? "row" : direction;
|
|
29
|
+
return _jsx("div", { ref: ref, role: props.role, className: cx(className, props.flush && "ainui-flush"), style: { display: "flex", flexDirection: dir, ...(dir === "row" ? { alignItems: "center" } : {}), gap: props.disableAutoSpacing ? 0 : undefined, ...boxStyle(props) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) });
|
|
30
|
+
};
|
|
31
|
+
const Box = standard("Box", flex("column", "ainui-box"));
|
|
32
|
+
const Column = standard("Column", flex("column", "ainui-column"));
|
|
33
|
+
const Row = standard("Row", flex("row", "ainui-row"));
|
|
34
|
+
const Grid = standard("Grid", ({ props, buildChild }) => {
|
|
35
|
+
const ref = useOnVisible(props.onVisible);
|
|
36
|
+
// `minItemWidth` is AINUI v1's name for `minChildWidth`.
|
|
37
|
+
const min = props.minChildWidth ?? props.minItemWidth;
|
|
38
|
+
const columns = typeof props.columns === "number" ? `repeat(${props.columns}, minmax(0, 1fr))`
|
|
39
|
+
: typeof props.columns === "string" && props.columns ? props.columns
|
|
40
|
+
: `repeat(auto-fill, minmax(min(100%, ${px(min) ?? "176px"}), 1fr))`;
|
|
41
|
+
const rows = typeof props.rows === "number" ? `repeat(${props.rows}, minmax(0, auto))` : props.rows || undefined;
|
|
42
|
+
return _jsx("div", { ref: ref, className: "ainui-grid", style: { display: "grid", gridTemplateColumns: columns, gridTemplateRows: rows, ...boxStyle(props) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) });
|
|
43
|
+
});
|
|
44
|
+
const spanStyle = (p) => ({
|
|
45
|
+
...(p.span ? { gridColumn: `span ${p.span} / span ${p.span}` } : {}), ...(p.rowSpan ? { gridRow: `span ${p.rowSpan} / span ${p.rowSpan}` } : {}),
|
|
46
|
+
});
|
|
47
|
+
const GridItem = standard("GridItem", ({ props, buildChild }) => _jsx("div", { className: "ainui-grid-item", style: { display: "flex", flexDirection: "column", minWidth: 0, ...spanStyle(props), ...boxStyle(props) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }));
|
|
48
|
+
const Card = standard("Card", ({ props, buildChild }) => {
|
|
49
|
+
const ref = useOnVisible(props.onVisible);
|
|
50
|
+
return _jsxs("div", { ref: ref, className: cx("ainui-card", `ainui-card-${props.variant ?? "default"}`, props.size && `ainui-card-${props.size}`, props.shadowStyle && props.shadowStyle !== "none" && `ainui-shadow-${props.shadowStyle}`), style: boxStyle(props), children: [kid(buildChild, props.child), _jsx(Kids, { of: props.children, buildChild: buildChild })] });
|
|
51
|
+
});
|
|
52
|
+
/** A dense, magazine-like grid: `pack` fills holes, `fixed` keeps source order. */
|
|
53
|
+
const Flow = standard("Flow", ({ props, buildChild }) => _jsx("div", { className: "ainui-flow", style: { display: "grid", gridTemplateColumns: `repeat(${props.columns ?? 3}, minmax(0, 1fr))`,
|
|
54
|
+
...(props.rows ? { gridTemplateRows: `repeat(${props.rows}, minmax(0, 1fr))` } : {}), gridAutoFlow: props.layout === "fixed" ? "row" : "row dense", ...boxStyle(props) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }));
|
|
55
|
+
const FlowItem = standard("FlowItem", ({ props, buildChild }) => _jsx("div", { className: "ainui-flow-item", style: { display: "flex", flexDirection: "column", minWidth: 0, ...spanStyle(props), ...boxStyle(props) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }));
|
|
56
|
+
const Carousel = standard("Carousel", ({ props, buildChild }) => {
|
|
57
|
+
const track = useRef(null);
|
|
58
|
+
const count = Array.isArray(props.children) ? props.children.length : 0;
|
|
59
|
+
const go = (index) => {
|
|
60
|
+
const el = track.current;
|
|
61
|
+
const target = el?.children[Math.max(0, Math.min(count - 1, index))];
|
|
62
|
+
if (el && target)
|
|
63
|
+
el.scrollTo?.({ left: target.offsetLeft - el.offsetLeft, behavior: "smooth" });
|
|
64
|
+
props.setActiveIndex?.(Math.max(0, Math.min(count - 1, index)));
|
|
65
|
+
};
|
|
66
|
+
// An index set by the agent (or a binding) scrolls the slide into view.
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
const el = track.current, target = el?.children[props.activeIndex ?? -1];
|
|
69
|
+
if (el && target)
|
|
70
|
+
el.scrollTo?.({ left: target.offsetLeft - el.offsetLeft, behavior: "smooth" });
|
|
71
|
+
}, [props.activeIndex]);
|
|
72
|
+
const active = typeof props.activeIndex === "number" ? props.activeIndex : 0;
|
|
73
|
+
const visible = typeof props.visibleItems === "number" && props.visibleItems > 0 ? props.visibleItems : undefined;
|
|
74
|
+
return _jsxs("div", { className: "ainui-carousel", role: "group", "aria-roledescription": "carousel", "aria-label": props.label, style: boxStyle({ ...props, gap: undefined }), children: [props.showArrows !== false && count > 1 && _jsx("button", { type: "button", className: "ainui-carousel-arrow ainui-carousel-prev", "aria-label": props.previousLabel ?? "Previous", disabled: active <= 0, onClick: () => go(active - 1), children: "\u2039" }), _jsx("div", { ref: track, className: "ainui-carousel-track", style: { gap: space(props.gap) ?? "12px", scrollSnapType: props.snap === "none" ? "none" : `x ${props.snap ?? "mandatory"}`,
|
|
75
|
+
["--ainui-carousel-align"]: props.snapAlign ?? "start", ...(visible ? { ["--ainui-carousel-item"]: `calc((100% - ${(visible - 1) * 12}px) / ${visible})` } : {}) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }), props.showArrows !== false && count > 1 && _jsx("button", { type: "button", className: "ainui-carousel-arrow ainui-carousel-next", "aria-label": props.nextLabel ?? "Next", disabled: active >= count - 1, onClick: () => go(active + 1), children: "\u203A" })] });
|
|
76
|
+
});
|
|
77
|
+
const CarouselItem = standard("CarouselItem", ({ props, buildChild }) => _jsx("div", { className: "ainui-carousel-item", style: { display: "flex", flexDirection: "column", ...boxStyle(props) }, children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }));
|
|
78
|
+
const MEDIA_WIDTH = { sm: 160, md: 240, lg: 320 };
|
|
79
|
+
const CarouselMediaItem = standard("CarouselMediaItem", ({ props, buildChild }) => {
|
|
80
|
+
const host = useHost();
|
|
81
|
+
const [zoom, setZoom] = useState(false);
|
|
82
|
+
const src = mediaUrl(props.url, host);
|
|
83
|
+
const click = () => (props.onClick ? void props.onClick() : props.href ? openUrl(host, props.href) : props.lightbox !== false ? setZoom(true) : undefined);
|
|
84
|
+
return _jsxs("figure", { className: "ainui-carousel-item ainui-carousel-media", style: { width: MEDIA_WIDTH[props.size ?? "md"], ...boxStyle(props) }, children: [_jsx("button", { type: "button", className: "ainui-media-button", onClick: click, "aria-label": props.description ?? "Open image", children: src && _jsx("img", { src: src, alt: props.description ?? "", loading: "lazy" }) }), _jsx("figcaption", { children: _jsx(Kids, { of: props.children, buildChild: buildChild }) }), zoom && _jsxs("div", { className: "ainui-lightbox", role: "dialog", "aria-modal": "true", "aria-label": props.description ?? "Image", onClick: () => setZoom(false), children: [_jsx("img", { src: src, alt: props.description ?? "" }), _jsx("button", { type: "button", className: "ainui-lightbox-close", "aria-label": "Close", children: "\u00D7" })] })] });
|
|
85
|
+
});
|
|
86
|
+
const Spacer = standard("Spacer", ({ props }) => _jsx("div", { className: "ainui-spacer", "aria-hidden": "true", style: { flex: "1 1 0%", minWidth: px(props.minSize), minHeight: px(props.minSize) } }));
|
|
87
|
+
const Divider = standard("Divider", ({ props }) => {
|
|
88
|
+
const vertical = props.axis === "vertical";
|
|
89
|
+
const line = `${props.size ?? 1}px solid ${props.color && props.color !== "default" ? color(props.color) : "var(--ainui-border)"}`;
|
|
90
|
+
return _jsx("div", { role: "separator", "aria-orientation": vertical ? "vertical" : "horizontal", className: cx("ainui-divider", props.flush && "ainui-flush"), style: vertical ? { alignSelf: "stretch", borderLeft: line, marginInline: space(props.spacing) } : { width: "100%", borderTop: line, marginBlock: space(props.spacing) } });
|
|
91
|
+
});
|
|
92
|
+
const Tabs = standard("Tabs", ({ props, buildChild }) => {
|
|
93
|
+
const tabs = Array.isArray(props.tabs) ? props.tabs : [];
|
|
94
|
+
const [local, setLocal] = useState(0);
|
|
95
|
+
const selected = Math.min(tabs.length - 1, Math.max(0, typeof props.selectedIndex === "number" ? props.selectedIndex : local));
|
|
96
|
+
const choose = (i) => { setLocal(i); props.setSelectedIndex?.(i); };
|
|
97
|
+
return _jsxs("div", { className: "ainui-tabs", style: typeof props.weight === "number" ? { flex: `${props.weight} 1 0%`, minWidth: 0 } : undefined, children: [_jsx("div", { role: "tablist", className: "ainui-tablist", onKeyDown: (e) => {
|
|
98
|
+
if (e.key === "ArrowRight")
|
|
99
|
+
choose((selected + 1) % tabs.length);
|
|
100
|
+
else if (e.key === "ArrowLeft")
|
|
101
|
+
choose((selected - 1 + tabs.length) % tabs.length);
|
|
102
|
+
}, children: tabs.map((t, i) => _jsx("button", { type: "button", role: "tab", "aria-selected": i === selected, tabIndex: i === selected ? 0 : -1, className: "ainui-tab", onClick: () => choose(i), children: t.title }, i)) }), _jsx("div", { role: "tabpanel", className: "ainui-tabpanel", children: tabs[selected] ? kid(buildChild, tabs[selected].child) : null })] });
|
|
103
|
+
});
|
|
104
|
+
export const layout = [Box, Column, Row, Grid, GridItem, Card, Flow, FlowItem, Carousel, CarouselItem, CarouselMediaItem, Spacer, Divider, Tabs];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Standard catalog — media: images, icons, vector drawings, video, audio, sandboxed HTML. */
|
|
2
|
+
import React from "react";
|
|
3
|
+
/** A Lucide icon (kebab-case or A2UI name) or `{svgPath}`; an empty svg of the same size until the icon data loads. */
|
|
4
|
+
export declare function IconGlyph({ name, size, label, strokeWidth, className, style }: {
|
|
5
|
+
name: unknown;
|
|
6
|
+
size?: number | string;
|
|
7
|
+
label?: string;
|
|
8
|
+
strokeWidth?: number;
|
|
9
|
+
className?: string;
|
|
10
|
+
style?: React.CSSProperties;
|
|
11
|
+
}): React.JSX.Element;
|
|
12
|
+
export type LightboxImage = {
|
|
13
|
+
src: string;
|
|
14
|
+
alt?: string;
|
|
15
|
+
caption?: string;
|
|
16
|
+
};
|
|
17
|
+
/** A full-screen view of one image of a set. A click or Escape closes it; arrows move within the set. */
|
|
18
|
+
export declare function Lightbox({ images, index, onIndex, onClose }: {
|
|
19
|
+
images: LightboxImage[];
|
|
20
|
+
index: number;
|
|
21
|
+
onIndex?: (i: number) => void;
|
|
22
|
+
onClose: () => void;
|
|
23
|
+
}): React.JSX.Element | null;
|
|
24
|
+
export declare function siteHost(url: unknown): string;
|
|
25
|
+
/** The icon of the site at `url`; nothing when the site has none or the URL is not a web address. */
|
|
26
|
+
export declare function SiteIcon({ url, size, alt, frame, inline }: {
|
|
27
|
+
url: unknown;
|
|
28
|
+
size?: number;
|
|
29
|
+
alt?: string;
|
|
30
|
+
frame?: boolean;
|
|
31
|
+
inline?: boolean;
|
|
32
|
+
}): React.JSX.Element | null;
|
|
33
|
+
export declare const media: import("@a2ui/react").ReactComponentImplementation<import("zod").ZodTypeAny>[];
|