@oberik/sdk 0.84.0 → 0.85.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.
@@ -32,6 +32,7 @@
32
32
  */
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
34
  exports.WidgetSession = exports.WIDGET_FUNCTIONS = exports.WidgetExprError = exports.WIDGET_CATALOG_VERSION = exports.WIDGET_COMPONENTS = void 0;
35
+ exports.previewRender = previewRender;
35
36
  exports.widgetValueText = widgetValueText;
36
37
  exports.formatWidgetValue = formatWidgetValue;
37
38
  exports.evaluateFormula = evaluateFormula;
@@ -49,6 +50,10 @@ exports.webPlatform = webPlatform;
49
50
  exports.nativePlatform = nativePlatform;
50
51
  exports.mountWidget = mountWidget;
51
52
  exports.defineWidgetElement = defineWidgetElement;
53
+ /** A preview as something to draw. */
54
+ function previewRender(p) {
55
+ return { id: `preview:${p.call_id}`, call_id: p.call_id, widget: p.widget, fallback: p.fallback, interactive: false, preview: true };
56
+ }
52
57
  /**
53
58
  * Every component and its props. Checked against the server's catalog
54
59
  * (`app/widgets/catalog.json`) by `ui/src/widget-catalog-agrees.test.ts`, and every
@@ -1106,7 +1111,7 @@ function toneSoft(t, tone) {
1106
1111
  class WidgetSession {
1107
1112
  render;
1108
1113
  opts;
1109
- disabled;
1114
+ holdReplies;
1110
1115
  state;
1111
1116
  last = null;
1112
1117
  listeners = new Set();
@@ -1114,10 +1119,10 @@ class WidgetSession {
1114
1119
  view;
1115
1120
  /** Per-instance UI state that isn't the widget's: which tab, which disclosure is open. */
1116
1121
  ui = {};
1117
- constructor(render, opts = {}, disabled = false) {
1122
+ constructor(render, opts = {}, holdReplies = false) {
1118
1123
  this.render = render;
1119
1124
  this.opts = opts;
1120
- this.disabled = disabled;
1125
+ this.holdReplies = holdReplies;
1121
1126
  this.state = { ...(render.widget.state ?? {}) };
1122
1127
  if (render.answered?.values) {
1123
1128
  for (const [k, v] of Object.entries(render.answered.values)) {
@@ -1130,15 +1135,23 @@ class WidgetSession {
1130
1135
  get() {
1131
1136
  return this.view;
1132
1137
  }
1138
+ /** Carry another session's local state over — the same widget, grown. */
1139
+ adopt(prev) {
1140
+ for (const [k, v] of Object.entries(prev.state))
1141
+ if (k in this.state)
1142
+ this.state[k] = v;
1143
+ Object.assign(this.ui, prev.ui);
1144
+ this.view = this.compute();
1145
+ }
1133
1146
  subscribe(fn) {
1134
1147
  this.listeners.add(fn);
1135
1148
  return () => this.listeners.delete(fn);
1136
1149
  }
1137
- /** New handlers or a new `disabled`. Silent: the host calling this is already redrawing. */
1138
- setOptions(opts, disabled = false) {
1150
+ /** New handlers or a new `holdReplies`. Silent: the host calling this is already redrawing. */
1151
+ setOptions(opts, holdReplies = false) {
1139
1152
  this.opts = opts;
1140
- if (disabled !== this.disabled) {
1141
- this.disabled = disabled;
1153
+ if (holdReplies !== this.holdReplies) {
1154
+ this.holdReplies = holdReplies;
1142
1155
  this.view = this.compute();
1143
1156
  }
1144
1157
  }
@@ -1146,7 +1159,7 @@ class WidgetSession {
1146
1159
  return this.state[key] ?? null;
1147
1160
  }
1148
1161
  set(key, value) {
1149
- if (this.view.locked || !(key in this.state))
1162
+ if (!(key in this.state))
1150
1163
  return;
1151
1164
  if (this.state[key] === value)
1152
1165
  return;
@@ -1178,8 +1191,6 @@ class WidgetSession {
1178
1191
  }
1179
1192
  return;
1180
1193
  }
1181
- if (this.view.locked)
1182
- return;
1183
1194
  if (action.type === "set") {
1184
1195
  let v;
1185
1196
  try {
@@ -1193,7 +1204,7 @@ class WidgetSession {
1193
1204
  this.set(action.key, v);
1194
1205
  return;
1195
1206
  }
1196
- if (!this.view.canReply)
1207
+ if (this.view.replyLocked || !this.view.canReply)
1197
1208
  return;
1198
1209
  const include = action.include ?? boundKeys(this.render.widget);
1199
1210
  const values = {};
@@ -1243,15 +1254,15 @@ class WidgetSession {
1243
1254
  broken = true;
1244
1255
  }
1245
1256
  }
1246
- const locked = this.disabled || this.sent || !!this.render.answered;
1247
- let canReply = !locked;
1257
+ const replyLocked = this.holdReplies || this.sent || !!this.render.answered || !!this.render.preview;
1258
+ let canReply = !replyLocked;
1248
1259
  if (canReply) {
1249
1260
  for (const n of allNodes(this.render.widget)) {
1250
1261
  if (n.type === "input" && n.required && (this.state[n.bind] === "" || this.state[n.bind] === null))
1251
1262
  canReply = false;
1252
1263
  }
1253
1264
  }
1254
- return { widget, broken, locked, sent: this.sent || !!this.render.answered, canReply };
1265
+ return { widget, broken, replyLocked, sent: this.sent || !!this.render.answered, canReply };
1255
1266
  }
1256
1267
  }
1257
1268
  exports.WidgetSession = WidgetSession;
@@ -1310,7 +1321,10 @@ function layoutWidget(session, theme) {
1310
1321
  body.push(...nodes(ctx, w.children ?? [], "children"));
1311
1322
  const footer = (w.actions ?? []).filter((b) => b.visible !== false).map((b, i) => button(ctx, b, `actions[${i}]`));
1312
1323
  const replies = (w.actions ?? []).some((b) => b.visible !== false && b.action.type === "reply");
1313
- if (replies && !v.locked && !v.canReply) {
1324
+ if (session.render.preview) {
1325
+ footer.unshift(text(t, "Building…", { fontSize: FONT.caption, color: t.c.muted, marginRight: "auto" }, 1));
1326
+ }
1327
+ else if (replies && !v.replyLocked && !v.canReply) {
1314
1328
  // A disabled button that does not say why reads as broken.
1315
1329
  footer.unshift(text(t, "Fill in what's marked *", { fontSize: FONT.caption, color: t.c.muted, marginRight: "auto" }, 1));
1316
1330
  }
@@ -1456,7 +1470,7 @@ const LAYOUTS = {
1456
1470
  const { t, s, v } = ctx;
1457
1471
  return box({ borderRadius: Math.max(6, t.radius - 4), borderWidth: 1, borderColor: t.c.border, borderStyle: "solid", overflow: "hidden" }, n.items.map((it, i) => {
1458
1472
  const p = `${path}.items[${i}]`;
1459
- const pressable = !!it.action && (it.action.type === "link" || !v.locked);
1473
+ const pressable = !!it.action && (it.action.type !== "reply" || v.canReply);
1460
1474
  const right = [];
1461
1475
  if (it.badge)
1462
1476
  right.push(pill(t, it.badge.label, it.badge.tone));
@@ -1568,7 +1582,7 @@ const LAYOUTS = {
1568
1582
  max: n.max,
1569
1583
  step: n.step ?? 1,
1570
1584
  label: n.label,
1571
- disabled: v.locked,
1585
+ disabled: false,
1572
1586
  track: t.c.surface === t.c.background ? t.c.border : t.c.border,
1573
1587
  fill: t.c.accent,
1574
1588
  thumb: t.c.background,
@@ -1579,7 +1593,7 @@ const LAYOUTS = {
1579
1593
  toggle: (ctx, n) => {
1580
1594
  const { t, s, v } = ctx;
1581
1595
  const on = !!s.value(n.bind);
1582
- return box({ flexDirection: "row", alignItems: "center", gap: 12, cursor: v.locked ? undefined : "pointer", opacity: v.locked ? 0.7 : 1 }, [
1596
+ return box({ flexDirection: "row", alignItems: "center", gap: 12, cursor: "pointer" }, [
1583
1597
  box({ flexGrow: 1, flexShrink: 1, gap: 1 }, [
1584
1598
  text(t, n.label, { fontWeight: "500" }),
1585
1599
  ...(n.description ? [text(t, n.description, { fontSize: FONT.label, color: t.c.muted })] : []),
@@ -1587,7 +1601,7 @@ const LAYOUTS = {
1587
1601
  box({ width: 36, height: 20, borderRadius: 10, padding: 2, backgroundColor: on ? t.c.accent : t.c.border, alignItems: on ? "flex-end" : "flex-start", justifyContent: "center" }, [
1588
1602
  box({ width: 16, height: 16, borderRadius: 8, backgroundColor: "#ffffff" }, []),
1589
1603
  ]),
1590
- ], { role: "switch", label: n.label, checked: on, disabled: v.locked }, v.locked ? undefined : () => s.set(n.bind, !on));
1604
+ ], { role: "switch", label: n.label, checked: on }, () => s.set(n.bind, !on));
1591
1605
  },
1592
1606
  select: (ctx, n) => {
1593
1607
  const { t, s, v } = ctx;
@@ -1604,9 +1618,8 @@ const LAYOUTS = {
1604
1618
  borderStyle: "solid",
1605
1619
  borderColor: on ? t.c.accent : t.c.border,
1606
1620
  backgroundColor: on ? soft(t, t.c.accent, t.dark ? 0.22 : 0.1) : t.c.background,
1607
- cursor: v.locked ? undefined : "pointer",
1608
- opacity: v.locked && !on ? 0.6 : 1,
1609
- }, [text(t, o.label, { fontSize: FONT.label, fontWeight: on ? "600" : "500", color: on ? t.c.accent : t.c.text })], { role: "radio", checked: on, label: o.label, disabled: v.locked }, v.locked ? undefined : () => s.set(n.bind, o.value));
1621
+ cursor: "pointer",
1622
+ }, [text(t, o.label, { fontSize: FONT.label, fontWeight: on ? "600" : "500", color: on ? t.c.accent : t.c.text })], { role: "radio", checked: on, label: o.label }, () => s.set(n.bind, o.value));
1610
1623
  }), { role: "radiogroup", label: n.label ?? undefined }),
1611
1624
  ]);
1612
1625
  },
@@ -1634,7 +1647,7 @@ const LAYOUTS = {
1634
1647
  kind: n.kind ?? "text",
1635
1648
  placeholder: n.placeholder ?? (n.kind === "date" ? "YYYY-MM-DD" : ""),
1636
1649
  label: n.label,
1637
- disabled: v.locked,
1650
+ disabled: false,
1638
1651
  placeholderColor: t.c.faint,
1639
1652
  change: (x) => {
1640
1653
  if (n.kind === "number") {
@@ -1658,7 +1671,9 @@ function pill(t, label, tone) {
1658
1671
  function button(ctx, n, path) {
1659
1672
  const { t, s, v } = ctx;
1660
1673
  const isLink = n.action.type === "link";
1661
- const off = !!n.disabled || (!isLink && (v.locked || (n.action.type === "reply" && !v.canReply)));
1674
+ // Only a reply waits on anything. A set button is local and a link is a link — both
1675
+ // work in a widget that was answered long ago.
1676
+ const off = !!n.disabled || (n.action.type === "reply" && !v.canReply);
1662
1677
  const style = n.style ?? "secondary";
1663
1678
  const bg = style === "primary" ? t.c.accent : style === "danger" ? t.c.danger : style === "ghost" ? undefined : t.c.background;
1664
1679
  const fg = style === "primary" ? t.c.onAccent : style === "danger" ? "#ffffff" : style === "ghost" ? t.c.accent : t.c.text;
@@ -1796,10 +1811,16 @@ function createWidgetComponent(React, platform) {
1796
1811
  };
1797
1812
  const ref = React.useRef(null);
1798
1813
  if (ref.current === null || ref.current.render !== props.render) {
1799
- ref.current = new WidgetSession(props.render, opts, !!props.disabled);
1814
+ const prev = ref.current;
1815
+ ref.current = new WidgetSession(props.render, opts, !!props.holdReplies);
1816
+ // A preview growing, or becoming the finished widget, keeps what the user already
1817
+ // did to it — a slider dragged while the rest was still arriving stays put.
1818
+ if (prev && prev.render.call_id && prev.render.call_id === props.render.call_id) {
1819
+ ref.current.adopt(prev);
1820
+ }
1800
1821
  }
1801
1822
  const session = ref.current;
1802
- session.setOptions(opts, !!props.disabled);
1823
+ session.setOptions(opts, !!props.holdReplies);
1803
1824
  const [, bump] = React.useReducer((x) => x + 1, 0);
1804
1825
  React.useEffect(() => session.subscribe(bump), [session]);
1805
1826
  const theme = React.useMemo(() => resolveWidgetTheme(props.theme), [JSON.stringify(props.theme ?? {})]);
@@ -1811,7 +1832,22 @@ function createWidgetComponent(React, platform) {
1811
1832
  function fallbackEl(textValue, theme) {
1812
1833
  return h(P.View, { style: P.style({ padding: 12, borderRadius: theme.radius, borderWidth: 1, borderColor: theme.c.border, borderStyle: "solid", display: "flex", flexDirection: "column" }, "box") }, h(P.Text, { style: P.style({ color: theme.c.text, fontSize: 14, lineHeight: 20 }, "text") }, textValue || "(widget)"));
1813
1834
  }
1814
- function draw(el, key) {
1835
+ function draw(el, key, depth = 0) {
1836
+ // The card's direct children fall in as they arrive — a widget being written shows
1837
+ // its pieces landing, and the first paint does too.
1838
+ if (depth === 1 && P.name === "web")
1839
+ return h(Appear, { key }, drawInner(el, key, depth));
1840
+ return drawInner(el, key, depth);
1841
+ }
1842
+ function Appear({ children }) {
1843
+ const [shown, setShown] = React.useState(false);
1844
+ React.useEffect(() => {
1845
+ const raf = globalThis.requestAnimationFrame ?? ((f) => setTimeout(f, 16));
1846
+ raf(() => setShown(true));
1847
+ }, []);
1848
+ return h("div", { style: { display: "flex", flexDirection: "column", opacity: shown ? 1 : 0, transform: shown ? "none" : "translateY(6px)", transition: "opacity 240ms ease, transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1)" } }, children);
1849
+ }
1850
+ function drawInner(el, key, depth) {
1815
1851
  switch (el.k) {
1816
1852
  case "text": {
1817
1853
  const props = { key, style: P.style(el.style, "text") };
@@ -1822,7 +1858,7 @@ function createWidgetComponent(React, platform) {
1822
1858
  return h(P.Text, props, el.text);
1823
1859
  }
1824
1860
  case "box": {
1825
- const kids = el.children.map((c, i) => draw(c, `${key}.${i}`));
1861
+ const kids = el.children.map((c, i) => draw(c, `${key}.${i}`, depth + 1));
1826
1862
  if (el.press) {
1827
1863
  return h(P.Pressable, { key, onPress: el.press, style: P.style(el.style, "box"), hoverStyle: el.hover ? P.style(el.hover, "box") : undefined, a11y: el.a11y }, ...kids);
1828
1864
  }
@@ -1853,6 +1889,11 @@ function createWidgetComponent(React, platform) {
1853
1889
  return { failed: true };
1854
1890
  }
1855
1891
  componentDidCatch() { }
1892
+ componentDidUpdate(prev) {
1893
+ // A new render (the preview grew, or became final) gets a fresh chance to draw.
1894
+ if (this.state.failed && prev.render !== this.props.render)
1895
+ this.setState({ failed: false });
1896
+ }
1856
1897
  render() {
1857
1898
  const p = this.props;
1858
1899
  if (this.state.failed)
@@ -1861,7 +1902,9 @@ function createWidgetComponent(React, platform) {
1861
1902
  }
1862
1903
  }
1863
1904
  return function OberikWidget(props) {
1864
- return h(Boundary, { ...props, key: props.render.id });
1905
+ // Keyed by the call that made it, not the render id: a preview and the finished widget
1906
+ // are one card, and remounting between them would replay every entrance.
1907
+ return h(Boundary, { ...props, key: props.render.call_id ?? props.render.id });
1865
1908
  };
1866
1909
  }
1867
1910
  const UNITLESS = new Set(["flex", "flexGrow", "flexShrink", "opacity", "fontWeight", "zIndex", "aspectRatio"]);
@@ -2187,14 +2230,14 @@ function mountWidget(target, render, opts = {}) {
2187
2230
  const doc = target.ownerDocument ?? globalThis.document;
2188
2231
  if (!doc)
2189
2232
  throw new Error("mountWidget needs a DOM; use widgetToText on a server");
2190
- let session = new WidgetSession(render, opts, !!opts.disabled);
2233
+ let session = new WidgetSession(render, opts, !!opts.holdReplies);
2191
2234
  let theme = resolveWidgetTheme(opts.theme);
2192
2235
  let root = null;
2193
2236
  let unsubscribe = session.subscribe(paint);
2194
2237
  function paint() {
2195
2238
  const v = session.get();
2196
2239
  const tree = v.broken ? { k: "text", style: { color: theme.c.text, fontSize: 14 }, text: render.fallback } : layoutWidget(session, theme);
2197
- root = patch(doc, target, root, tree);
2240
+ root = patch(doc, target, root, tree, 0, root !== null);
2198
2241
  }
2199
2242
  paint();
2200
2243
  return {
@@ -2204,11 +2247,14 @@ function mountWidget(target, render, opts = {}) {
2204
2247
  if (next !== render) {
2205
2248
  render = next;
2206
2249
  unsubscribe();
2207
- session = new WidgetSession(render, opts, !!opts.disabled);
2250
+ const prev = session;
2251
+ session = new WidgetSession(render, opts, !!opts.holdReplies);
2252
+ if (prev.render.call_id && prev.render.call_id === render.call_id)
2253
+ session.adopt(prev);
2208
2254
  unsubscribe = session.subscribe(paint);
2209
2255
  }
2210
2256
  else
2211
- session.setOptions(opts, !!opts.disabled);
2257
+ session.setOptions(opts, !!opts.holdReplies);
2212
2258
  paint();
2213
2259
  },
2214
2260
  destroy() {
@@ -2221,12 +2267,15 @@ function mountWidget(target, render, opts = {}) {
2221
2267
  }
2222
2268
  const SVG_NS = "http://www.w3.org/2000/svg";
2223
2269
  /** Draw `el` into `parent`, reusing `existing` where its tag matches. */
2224
- function patch(doc, parent, existing, el) {
2270
+ function patch(doc, parent, existing, el, depth = 0, animate = false) {
2225
2271
  const tag = el.k === "field" ? (el.kind === "multiline" ? "textarea" : "input") : el.k === "slider" ? "input" : el.k === "image" ? "img" : "div";
2226
2272
  let node = existing;
2227
2273
  if (!node || node.tagName?.toLowerCase() !== tag || node.__k !== el.k) {
2228
2274
  const fresh = doc.createElement(tag);
2229
2275
  fresh.__k = el.k;
2276
+ // A piece of the card arriving after it was first drawn — the widget is being written
2277
+ // — falls in rather than popping. Same entrance as the React host's.
2278
+ fresh.__enter = animate && depth === 1;
2230
2279
  if (node)
2231
2280
  parent.replaceChild(fresh, node);
2232
2281
  else
@@ -2235,6 +2284,19 @@ function patch(doc, parent, existing, el) {
2235
2284
  }
2236
2285
  const setStyle = (s) => {
2237
2286
  node.removeAttribute("style");
2287
+ if (depth === 1) {
2288
+ s = { ...s, transition: "opacity 240ms ease, transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1)" };
2289
+ if (node.__enter) {
2290
+ const settled = { opacity: s.opacity === undefined ? "" : String(s.opacity), transform: s.transform === undefined ? "" : String(s.transform) };
2291
+ s = { ...s, opacity: 0, transform: "translateY(6px)" };
2292
+ node.__enter = false;
2293
+ const raf = globalThis.requestAnimationFrame ?? ((f) => setTimeout(f, 16));
2294
+ raf(() => {
2295
+ node.style.opacity = settled.opacity;
2296
+ node.style.transform = settled.transform;
2297
+ });
2298
+ }
2299
+ }
2238
2300
  for (const [k, v] of Object.entries(s)) {
2239
2301
  if (v === undefined || v === null)
2240
2302
  continue;
@@ -2276,7 +2338,7 @@ function patch(doc, parent, existing, el) {
2276
2338
  node.removeAttribute("tabindex");
2277
2339
  }
2278
2340
  const kids = Array.from(node.childNodes);
2279
- el.children.forEach((c, i) => patch(doc, node, kids[i], c));
2341
+ el.children.forEach((c, i) => patch(doc, node, kids[i], c, depth + 1, animate));
2280
2342
  for (let i = kids.length - 1; i >= el.children.length; i--)
2281
2343
  node.removeChild(kids[i]);
2282
2344
  return node;
@@ -2436,7 +2498,7 @@ function defineWidgetElement(tag = "oberik-widget") {
2436
2498
  mounted = null;
2437
2499
  _render = null;
2438
2500
  _theme;
2439
- _disabled = false;
2501
+ _holdReplies = false;
2440
2502
  set render(v) {
2441
2503
  this._render = v;
2442
2504
  this.sync();
@@ -2448,8 +2510,8 @@ function defineWidgetElement(tag = "oberik-widget") {
2448
2510
  this._theme = v;
2449
2511
  this.sync();
2450
2512
  }
2451
- set disabled(v) {
2452
- this._disabled = !!v;
2513
+ set holdReplies(v) {
2514
+ this._holdReplies = !!v;
2453
2515
  this.sync();
2454
2516
  }
2455
2517
  connectedCallback() {
@@ -2464,7 +2526,7 @@ function defineWidgetElement(tag = "oberik-widget") {
2464
2526
  return;
2465
2527
  const opts = {
2466
2528
  theme: this._theme,
2467
- disabled: this._disabled,
2529
+ holdReplies: this._holdReplies,
2468
2530
  onReply: (press, text) => this.dispatchEvent(new g.CustomEvent("widget-reply", { detail: { press, text }, bubbles: true })),
2469
2531
  onLink: (url) => {
2470
2532
  const ok = this.dispatchEvent(new g.CustomEvent("widget-link", { detail: { url }, bubbles: true, cancelable: true }));