@sincpro/web-composer 1.4.0 → 1.6.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/README.md CHANGED
@@ -109,14 +109,23 @@ api.pick(ref, onPick); // open a chooser (a many2one search); its view calls api
109
109
  api.pick(ref, onPick, { to: "modal" }); // the chooser as a modal over the form
110
110
  api.pick(ref, onPick, { size: { w: 720, h: 520 } }); // a chooser window at that size (480×360 without it)
111
111
  api.pick<boolean>(confirmRef, (ok) => ok && post(), { to: "modal" }); // any value, not only a ref
112
+ api.pick(ref, onPick, { question: { chosen } }); // what the chooser is asked, in memory
113
+ api.choosing; // in the chooser: { question } while someone waits for its answer; null when opened to look
112
114
  ```
113
115
 
116
+ A **chooser** knows it is one: `api.choosing` says it was opened by `pick` and what it was asked,
117
+ so the same list draws a «Choose» per row or rows that open, with no flag in its reference. The
118
+ question lives exactly as long as the wait for the answer — in memory, like the answer — and so
119
+ does `choosing`: a link or a reload opens the view to look at it. What must survive those (a
120
+ filter, a company, a default) is not a question: it goes in `params`.
121
+
114
122
  A **modal** belongs to the surface it covers — the page, a window, a side panel or the peek —
115
123
  and blocks only that one: the rest of the workspace keeps answering, and a window keeps moving
116
124
  and closing by its own title bar. One per surface: opening another one over the same surface,
117
125
  or from inside it, replaces it. When its surface shows another record or closes, the modal goes
118
126
  with it (asking first if something was typed). What it shares with whoever opened it travels in
119
- its reference (`params`) and comes back with `api.answer`; `api.over` says what it covers. No
127
+ its reference (`params`) — or, for a chooser, in its `question` — and comes back with
128
+ `api.answer`; `api.over` says what it covers. No
120
129
  blur unless asked.
121
130
 
122
131
  **On a phone** — when the workspace is narrower than 640 px (`<Composer phoneBelow>`, `0` never)
@@ -243,6 +252,26 @@ useViewTool(api, {
243
252
  The agent is one more person at the desk: it opens through the same `open`, a phone still turns
244
253
  a window into the page, and throwing away what was typed still asks first.
245
254
 
255
+ ### When a view breaks
256
+
257
+ Each view draws inside its own boundary: a view that throws while drawing shows a notice in its
258
+ panel and the rest of the workspace goes on. `onViewError` tells the application, once per
259
+ failure, with the reference the view was opened with — hand it to the error reporter the use
260
+ cases report to:
261
+
262
+ ```tsx
263
+ <Composer
264
+ workspace={workspace}
265
+ registry={registry}
266
+ // @sincpro/client-framework's reportError, or the application's own reporter
267
+ onViewError={(error, { resource }) => reportError(error, `view ${formatRef(resource)}`)}
268
+ />
269
+ ```
270
+
271
+ A host drawn outside `Composer` (`PanelHost`, `ViewHost`) reads the same handler from
272
+ `ViewErrorProvider`. A handler that throws is ignored: telling about a failure never becomes a
273
+ second one.
274
+
246
275
  ### Testing a view
247
276
 
248
277
  A view talks to the workspace only through its `api`, so a test checks what it ASKED for — no
@@ -251,7 +280,7 @@ store, no shell, no browser:
251
280
  ```ts
252
281
  import { createTestApi } from "@sincpro/web-composer";
253
282
 
254
- const t = createTestApi(invoiceRef("F-1")); // { surface, over, crumbs, selection } optional
283
+ const t = createTestApi(invoiceRef("F-1")); // { surface, over, crumbs, selection, choosing } optional
255
284
  render(<Invoice resource={t.api.self} api={t.api} />);
256
285
  // … click «Register payment»
257
286
  expect(t.callsTo("open")[0].args).toEqual([paymentRef("F-1"), { to: "modal" }]);
@@ -4,7 +4,7 @@ export { Axis, Destination, DOCKS, DragKind, HandleKind, Surface, Zone, } from "
4
4
  export { type Box, type Handle, handles, KEYBOARD_STEP, layout, type Length, phoneLayout, resolve, sizeAt, type Slot, stableOrder, STRIP, ZONE_LIMITS, } from "./layout.js";
5
5
  export { localEntries, matches, MIN_QUERY, type PaletteEntry, PaletteSection, searchEntries, } from "./palette.js";
6
6
  export { chooserOnPhone, destinationOnPhone, PHONE_WIDTH, } from "./phone.js";
7
- export { type Action, createRegistry, type Crumb, loadView, type OpenOptions, type PickOptions, type Registry, type SearchHit, type ViewProps, type ViewSpec, type WorkspaceApi, } from "./registry.js";
7
+ export { type Action, type Choosing, createRegistry, type Crumb, loadView, type OpenOptions, type PickOptions, type Registry, type SearchHit, type ViewProps, type ViewSpec, type WorkspaceApi, } from "./registry.js";
8
8
  export { formatRef, isRef, parseRef, type ResourceRef, rootRef, sameRef, withParams, } from "./resource.js";
9
9
  export { fromHash, restore, type SessionMemory, toHash, } from "./url.js";
10
10
  export { back, closeModal, closePanel, closePeek, collapsePanel, cycleFocus, DEFAULT_ZONES, drillPeek, EMPTY, expandPanel, findPanel, floatPanel, focusedRef, focusPanel, goTo, hostOf, MAX_ZONE, maximize, MIN_SHARE, MIN_WINDOW, MIN_ZONE, MODAL, type Modal, modalId, modalOver, movePanel, nameOf, nameRef, navigatePanel, openModal, type OpenModalOptions, openPanel, type OpenPanelOptions, PAGE, type Panel, panelsIn, PEEK, peek, peekBack, peekGoTo, peekToPage, peekToSide, type Placement, push, raisePanel, refOn, replace, replacePeek, resizePanel, resizeZone, select, type Selected, selectionOf, settleModals, type Size, start, type WindowBox, type Workspace, type Zones, } from "./workspace.js";
@@ -24,6 +24,16 @@ export type PickOptions = {
24
24
  blur?: boolean;
25
25
  /** A window or a modal: the size the chooser opens at, in pixels. */
26
26
  size?: Size;
27
+ /**
28
+ * What the chooser is asked, beyond what it shows: the ids already chosen, the amount to
29
+ * suggest. Any value, in memory — it lives exactly as long as the wait for the answer, like the
30
+ * answer itself. What must survive a reload or a link (a filter, a company) goes in `params`.
31
+ */
32
+ question?: unknown;
33
+ };
34
+ /** Why a view was opened when it was opened to answer: what `pick` asked it. */
35
+ export type Choosing = {
36
+ question: unknown;
27
37
  };
28
38
  /** One step of a breadcrumb, ready for `List.Crumbs` or `Record.Crumbs`. */
29
39
  export type Crumb = {
@@ -101,13 +111,23 @@ export type WorkspaceApi = {
101
111
  * Open `ref` as a chooser: when its view calls `answer`, `onPick` receives the value and the
102
112
  * chooser closes. The value is a reference by default — a many2one — but it can be anything
103
113
  * the chooser's view answers: `true` for a confirmation, a date range, an amount. Closed
104
- * without an answer, `onPick` is never called.
114
+ * without an answer, `onPick` is never called. `question` reaches the chooser as
115
+ * `api.choosing.question`.
105
116
  *
106
117
  * api.pick<boolean>(confirmRef, (ok) => ok && post(), { to: Destination.Modal });
118
+ * api.pick(chooserRef, fill, { to: Destination.Modal, question: { chosen: ids } });
107
119
  */
108
120
  pick: <T = ResourceRef>(ref: ResourceRef, onPick: (value: T) => void, options?: PickOptions) => void;
121
+ /**
122
+ * Opened by `pick`, waiting for an answer: what it was asked. `null` when it was opened to be
123
+ * looked at — `open`, a link, a reload: the wait does not survive one, so neither does this.
124
+ * The same view can draw itself to choose (a «Choose» per row) or to read (rows that open).
125
+ * It stays while the chooser filters or drills (`setParams`, `navigate`) and goes when it
126
+ * answers or closes.
127
+ */
128
+ choosing: Choosing | null;
109
129
  /** From a chooser: deliver the value to whoever asked, and close this surface. Nobody asked
110
- * (it was opened with `open`, not `pick`): nothing happens. */
130
+ * (`choosing` is `null`): nothing happens. */
111
131
  answer: (value: unknown) => void;
112
132
  };
113
133
  /** Named `resource` rather than `ref` because `ref` is a reserved React prop. */
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  * Only the model TYPES that appear in these signatures are re-exported, so declaring a domain
7
7
  * does not force a two-place import.
8
8
  */
9
- export type { Action, Crumb, OpenOptions, PaletteEntry, PickOptions, Registry, ResourceRef, SearchHit, ViewProps, ViewSpec, Workspace, WorkspaceApi, } from "./domain/index.js";
9
+ export type { Action, Choosing, Crumb, OpenOptions, PaletteEntry, PickOptions, Registry, ResourceRef, SearchHit, ViewProps, ViewSpec, Workspace, WorkspaceApi, } from "./domain/index.js";
10
10
  export { Destination, focusedRef, PAGE, PaletteSection, PEEK, selectionOf, Surface, Zone, } from "./domain/index.js";
11
11
  export { Composer, type ComposerProps } from "./shell/Composer.js";
12
12
  export { type ComposerCopy, composerCopy, ENGLISH_COMPOSER_COPY, installComposerCopy, SPANISH_COMPOSER_COPY, } from "./shell/copy.js";
@@ -18,7 +18,7 @@ export { PanelHost, type PanelHostProps } from "./shell/PanelHost.js";
18
18
  export { RecordLink, type RecordLinkProps } from "./shell/RecordLink.js";
19
19
  export { type ComposerSkin, type ConfirmProps, DEFAULT_ICONS, type ModalProps, type NoticeProps, type PeekProps, type SkinIcons, } from "./shell/skin.js";
20
20
  export { ComposerStyles } from "./shell/styles.js";
21
- export { ViewHost, type ViewHostProps } from "./shell/ViewHost.js";
21
+ export { type OnViewError, type ViewErrorAbout, ViewErrorProvider, ViewHost, type ViewHostProps, } from "./shell/ViewHost.js";
22
22
  export { agentAvailable, type AgentTool, type AgentToolAnnotations, registerAgentTool, registerAgentTools, toolName, useViewTool, workspaceTools, type WorkspaceToolsOptions, } from "./state/agent.js";
23
23
  export { type ApiFactory, createApiFactory, type Target, targetOf, } from "./state/api.js";
24
24
  export { type ConfirmDiscard, createWorkspaceStore, type StoreOptions, type WorkspaceStore, } from "./state/store.js";
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ export { PanelHost } from "./shell/PanelHost.js";
17
17
  export { RecordLink } from "./shell/RecordLink.js";
18
18
  export { DEFAULT_ICONS, } from "./shell/skin.js";
19
19
  export { ComposerStyles } from "./shell/styles.js";
20
- export { ViewHost } from "./shell/ViewHost.js";
20
+ export { ViewErrorProvider, ViewHost, } from "./shell/ViewHost.js";
21
21
  export { agentAvailable, registerAgentTool, registerAgentTools, toolName, useViewTool, workspaceTools, } from "./state/agent.js";
22
22
  export { createApiFactory, targetOf, } from "./state/api.js";
23
23
  export { createWorkspaceStore, } from "./state/store.js";
@@ -1,5 +1,6 @@
1
1
  import { Destination, type Registry } from "../domain/index.js";
2
2
  import { type ComposerSkin } from "./skin.js";
3
+ import { type OnViewError } from "./ViewHost.js";
3
4
  import type { WorkspaceStore } from "../state/store.js";
4
5
  import { type ReactNode } from "react";
5
6
  export type ComposerProps = {
@@ -25,5 +26,8 @@ export type ComposerProps = {
25
26
  /** Offer the workspace to the browser's agent (WebMCP): where am I, open, search, act. Off by
26
27
  * default; without WebMCP in the browser it does nothing. One workspace per page should. */
27
28
  agentTools?: boolean;
29
+ /** Told when a view breaks while drawing — hand it to the app's error reporter. Its panel
30
+ * shows a notice and the rest of the workspace goes on either way. */
31
+ onViewError?: OnViewError;
28
32
  };
29
- export declare function Composer({ workspace, registry, empty, keyboard, defaultOpen, palette: paletteProp, onPaletteChange, skin: skinProp, phoneBelow, agentTools, }: ComposerProps): import("react").JSX.Element;
33
+ export declare function Composer({ workspace, registry, empty, keyboard, defaultOpen, palette: paletteProp, onPaletteChange, skin: skinProp, phoneBelow, agentTools, onViewError, }: ComposerProps): import("react").JSX.Element;
@@ -7,7 +7,7 @@ import { PanelHost } from "./PanelHost.js";
7
7
  import { ChromeButton, ConfirmDialog, PeekDrawer } from "./Peek.js";
8
8
  import { resolveSkin, SkinProvider, } from "./skin.js";
9
9
  import { ComposerStyles } from "./styles.js";
10
- import { ViewHost } from "./ViewHost.js";
10
+ import { ViewErrorProvider, ViewHost, } from "./ViewHost.js";
11
11
  import { registerAgentTools, workspaceTools } from "../state/agent.js";
12
12
  import { createApiFactory, targetOf } from "../state/api.js";
13
13
  import { useDropMonitor } from "../state/drag.js";
@@ -61,7 +61,7 @@ function PhoneDock({ panels, titleOf, onBring, }) {
61
61
  const copy = composerCopy().phone;
62
62
  return (_jsxs("nav", { "aria-label": copy.alsoOpen, className: "sc-dock", children: [_jsx("span", { className: "sc-dock-label", children: copy.alsoOpen }), panels.map((p) => (_jsx(ChromeButton, { onClick: () => onBring(p.id), children: titleOf(p.view) }, p.id)))] }));
63
63
  }
64
- export function Composer({ workspace, registry, empty, keyboard = true, defaultOpen = Destination.Page, palette: paletteProp, onPaletteChange, skin: skinProp, phoneBelow = PHONE_WIDTH, agentTools = false, }) {
64
+ export function Composer({ workspace, registry, empty, keyboard = true, defaultOpen = Destination.Page, palette: paletteProp, onPaletteChange, skin: skinProp, phoneBelow = PHONE_WIDTH, agentTools = false, onViewError, }) {
65
65
  const skin = useMemo(() => resolveSkin(skinProp), [skinProp]);
66
66
  const Drawer = skin.Peek ?? PeekDrawer;
67
67
  const Confirm = skin.Confirm ?? ConfirmDialog;
@@ -140,5 +140,6 @@ export function Composer({ workspace, registry, empty, keyboard = true, defaultO
140
140
  const peekModal = modalOver(state, PEEK);
141
141
  const peekSpec = peeked === null ? null : registry.resolve(peeked);
142
142
  const cameFrom = state.peekTrail.at(-1);
143
- return (_jsx(SkinProvider, { value: skin, children: _jsxs("div", { ref: root, className: "sc-root", ...(phone ? { "data-phone": "" } : {}), children: [skin.styles ? _jsx(ComposerStyles, {}) : null, state.page === null ? (_jsx("div", { className: "sc-empty", children: empty })) : (_jsx(PanelHost, { workspace: state, registry: registry, store: workspace, api: api, phone: phone, overlay: peeked === null ? null : (_jsxs(_Fragment, { children: [_jsx("div", { style: { display: "contents" }, inert: peekModal !== null, onPointerDownCapture: () => workspace.focusPanel(PEEK), children: _jsx(Drawer, { title: api.titleOf(peeked), onClose: workspace.closePeek, actions: _jsxs(_Fragment, { children: [phone ? null : (_jsx(ChromeButton, { onClick: () => workspace.peekToSide(peekSpec?.preferredZone ?? Zone.Right), children: copy.peek.openAsPanel })), cameFrom === undefined ? null : (_jsx(ChromeButton, { role: "ghost", icon: skin.icons.back, onClick: workspace.peekBack, children: copy.peek.back(api.titleOf(cameFrom)) })), _jsx(ChromeButton, { role: "ghost", onClick: workspace.peekToPage, children: copy.peek.openAsPage })] }), children: _jsx(ViewHost, { resource: peeked, registry: registry, api: api.for(peeked, { surface: Surface.Peek }) }) }) }), peekModal === null ? null : (_jsx(ModalLayer, { modal: peekModal, registry: registry, store: workspace, api: api, phone: phone }))] })) })), phone && state.page !== null && state.panels.length > 0 ? (_jsx(PhoneDock, { panels: state.panels, titleOf: api.titleOf, onBring: workspace.expandPanel })) : null, _jsx(Confirm, { open: asking !== null, title: copy.discard.title, detail: asking === null ? "" : copy.discard.detail(asking.names), keep: copy.discard.keep, discard: copy.discard.discard, onAnswer: (ok) => asking?.answer(ok) }), palette ? (_jsx(WorkspacePalette, { registry: registry, focused: focusedRef(state), onOpen: open, onAction: run, onClose: () => setPalette(false) })) : null] }) }));
143
+ const desk = (_jsxs("div", { ref: root, className: "sc-root", ...(phone ? { "data-phone": "" } : {}), children: [skin.styles ? _jsx(ComposerStyles, {}) : null, state.page === null ? (_jsx("div", { className: "sc-empty", children: empty })) : (_jsx(PanelHost, { workspace: state, registry: registry, store: workspace, api: api, phone: phone, overlay: peeked === null ? null : (_jsxs(_Fragment, { children: [_jsx("div", { style: { display: "contents" }, inert: peekModal !== null, onPointerDownCapture: () => workspace.focusPanel(PEEK), children: _jsx(Drawer, { title: api.titleOf(peeked), onClose: workspace.closePeek, actions: _jsxs(_Fragment, { children: [phone ? null : (_jsx(ChromeButton, { onClick: () => workspace.peekToSide(peekSpec?.preferredZone ?? Zone.Right), children: copy.peek.openAsPanel })), cameFrom === undefined ? null : (_jsx(ChromeButton, { role: "ghost", icon: skin.icons.back, onClick: workspace.peekBack, children: copy.peek.back(api.titleOf(cameFrom)) })), _jsx(ChromeButton, { role: "ghost", onClick: workspace.peekToPage, children: copy.peek.openAsPage })] }), children: _jsx(ViewHost, { resource: peeked, registry: registry, api: api.for(peeked, { surface: Surface.Peek }) }) }) }), peekModal === null ? null : (_jsx(ModalLayer, { modal: peekModal, registry: registry, store: workspace, api: api, phone: phone }))] })) })), phone && state.page !== null && state.panels.length > 0 ? (_jsx(PhoneDock, { panels: state.panels, titleOf: api.titleOf, onBring: workspace.expandPanel })) : null, _jsx(Confirm, { open: asking !== null, title: copy.discard.title, detail: asking === null ? "" : copy.discard.detail(asking.names), keep: copy.discard.keep, discard: copy.discard.discard, onAnswer: (ok) => asking?.answer(ok) }), palette ? (_jsx(WorkspacePalette, { registry: registry, focused: focusedRef(state), onOpen: open, onAction: run, onClose: () => setPalette(false) })) : null] }));
144
+ return (_jsx(SkinProvider, { value: skin, children: onViewError === undefined ? (desk) : (_jsx(ViewErrorProvider, { value: onViewError, children: desk })) }));
144
145
  }
@@ -1,5 +1,21 @@
1
1
  import { type Registry, type ResourceRef, type WorkspaceApi } from "../domain/index.js";
2
2
  import { type ReactNode } from "react";
3
+ /** Where a view failed: the reference it was opened with, and React's stack of components. */
4
+ export type ViewErrorAbout = {
5
+ resource: ResourceRef;
6
+ componentStack: string | undefined;
7
+ };
8
+ /**
9
+ * Told when a view breaks while drawing — once per failure, after the panel shows its notice.
10
+ *
11
+ * Context: the boundary keeps the rest of the workspace alive, but a console line is nobody's
12
+ * report: the application hands this to its error reporter (GlitchTip, Sentry), the same place
13
+ * its use cases report to. Whatever it does, it never breaks the workspace — a handler that
14
+ * throws is ignored.
15
+ */
16
+ export type OnViewError = (error: Error, about: ViewErrorAbout) => void;
17
+ /** `Composer onViewError` sets it for every view it hosts; a host of its own sets it here. */
18
+ export declare const ViewErrorProvider: import("react").Provider<OnViewError | undefined>;
3
19
  export type ViewHostProps = {
4
20
  resource: ResourceRef;
5
21
  registry: Registry;
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { formatRef, loadView, rootRef, } from "../domain/index.js";
3
3
  import { composerCopy } from "./copy.js";
4
4
  import { useSkin } from "./skin.js";
5
- import { Component, lazy, memo, Suspense, useMemo, } from "react";
5
+ import { Component, createContext, lazy, memo, Suspense, useContext, useMemo, } from "react";
6
6
  /**
7
7
  * The only thing that turns a reference into pixels. Resolves against the
8
8
  * registry, isolates failure, and lends the navigation API — the view never
@@ -20,6 +20,9 @@ import { Component, lazy, memo, Suspense, useMemo, } from "react";
20
20
  function DefaultNotice({ heading, detail, bad = false }) {
21
21
  return (_jsxs("div", { role: "status", className: "sc-notice", ...(bad ? { "data-tone": "bad" } : {}), children: [_jsx("strong", { children: heading }), _jsx("code", { children: detail })] }));
22
22
  }
23
+ const ViewErrorContext = createContext(undefined);
24
+ /** `Composer onViewError` sets it for every view it hosts; a host of its own sets it here. */
25
+ export const ViewErrorProvider = ViewErrorContext.Provider;
23
26
  class ErrorBoundary extends Component {
24
27
  state = { error: null };
25
28
  static getDerivedStateFromError(error) {
@@ -27,6 +30,15 @@ class ErrorBoundary extends Component {
27
30
  }
28
31
  componentDidCatch(error, info) {
29
32
  console.error(`[view ${this.props.name}] failed to render`, error, info.componentStack);
33
+ try {
34
+ this.props.onError?.(error, {
35
+ resource: this.props.resource,
36
+ componentStack: info.componentStack ?? undefined,
37
+ });
38
+ }
39
+ catch {
40
+ // Telling someone about a failure never becomes a second one.
41
+ }
30
42
  }
31
43
  componentDidUpdate(prev) {
32
44
  if (prev.name !== this.props.name && this.state.error !== null) {
@@ -44,10 +56,11 @@ function Host({ resource, registry, api, fallback }) {
44
56
  const key = formatRef(resource);
45
57
  const spec = registry.resolve(resource);
46
58
  const Notice = useSkin().Notice ?? DefaultNotice;
59
+ const onError = useContext(ViewErrorContext);
47
60
  const View = useMemo(() => (spec === null ? null : lazy(async () => ({ default: await loadView(spec) }))), [spec]);
48
61
  if (spec === null || View === null) {
49
62
  return (_jsx(Notice, { heading: composerCopy().view.unresolved, detail: `${key} — is the «${resource.domain}» domain missing from the registry?` }));
50
63
  }
51
- return (_jsx(ErrorBoundary, { name: key, Notice: Notice, children: _jsx(Suspense, { fallback: fallback ?? _jsx("p", { className: "sc-loading", children: composerCopy().view.loading(spec.domain) }), children: _jsx(View, { resource: resource, api: api }, formatRef(rootRef(resource))) }) }));
64
+ return (_jsx(ErrorBoundary, { name: key, resource: resource, Notice: Notice, onError: onError, children: _jsx(Suspense, { fallback: fallback ?? _jsx("p", { className: "sc-loading", children: composerCopy().view.loading(spec.domain) }), children: _jsx(View, { resource: resource, api: api }, formatRef(rootRef(resource))) }) }));
52
65
  }
53
66
  export const ViewHost = memo(Host);
@@ -51,6 +51,9 @@ const CSS = `
51
51
  }
52
52
  .sc-root *, .sc-root *::before, .sc-root *::after { box-sizing: border-box; }
53
53
  .sc-root :focus-visible { outline: 2px solid var(--_accent); outline-offset: 1px; }
54
+ /* A modal takes the focus when it opens so that it is announced, not to be pointed at: the
55
+ ring on the whole dialog read as a thick coloured border. The controls inside keep theirs. */
56
+ .sc-root .sc-modal:focus-visible { outline: 0; }
54
57
  .sc-root :is(.sc-control, .sc-button, .sc-menu-item, .sc-palette-search, .sc-option-icon) svg {
55
58
  flex-shrink: 0; width: var(--_icon); height: var(--_icon);
56
59
  }
package/dist/state/api.js CHANGED
@@ -37,6 +37,16 @@ export function createApiFactory(store, { registry, defaultOpen = Destination.Pa
37
37
  if (model.refOn(w, key) === null)
38
38
  cache.delete(key);
39
39
  });
40
+ /** `id` now waits for an answer, or no longer does. Its API is built again: `choosing` is a
41
+ * value read in render, and the surface may keep showing the same resource. */
42
+ function ask(id, pending) {
43
+ choosers.set(id, pending);
44
+ cache.delete(id);
45
+ }
46
+ function forget(id) {
47
+ if (choosers.delete(id))
48
+ cache.delete(id);
49
+ }
40
50
  const context = {
41
51
  focused: () => model.focusedRef(store.get()),
42
52
  selection: () => model.selectionOf(store.get()),
@@ -56,8 +66,14 @@ export function createApiFactory(store, { registry, defaultOpen = Destination.Pa
56
66
  const m = model.modalOver(store.get(), model.hostOf(me));
57
67
  return { blur: m?.blur ?? false, ...(m?.size === undefined ? {} : { size: m.size }) };
58
68
  };
69
+ /** A fresh modal or peek opened from OUTSIDE it is not the chooser that was there: whoever
70
+ * waited for that one is not waiting for this. From inside, it is the chooser drilling. */
59
71
  function open(ref, asked, { blur, size } = {}) {
60
72
  const to = isPhone() ? model.destinationOnPhone(asked) : asked;
73
+ if (to === Destination.Modal && !inModal)
74
+ forget(model.modalId(model.hostOf(me)));
75
+ if (to === Destination.Peek && !inPeek)
76
+ forget(PEEK);
61
77
  if (to === Destination.Modal)
62
78
  store.openModal(me, ref, {
63
79
  ...(blur === undefined ? {} : { blur }),
@@ -129,6 +145,10 @@ export function createApiFactory(store, { registry, defaultOpen = Destination.Pa
129
145
  surface: target.surface,
130
146
  over: inModal ? (model.modalOver(store.get(), target.host)?.over ?? null) : null,
131
147
  context,
148
+ choosing: (() => {
149
+ const pending = choosers.get(me);
150
+ return pending === undefined ? null : { question: pending.question };
151
+ })(),
132
152
  open: (ref, options) => open(ref, options?.to ??
133
153
  (inPeek && defaultOpen === Destination.Page ? Destination.Peek : defaultOpen), options),
134
154
  navigate: (to) => {
@@ -179,30 +199,34 @@ export function createApiFactory(store, { registry, defaultOpen = Destination.Pa
179
199
  setDirty: (dirty) => store.setDirty(me, resource, dirty),
180
200
  select: (refs) => store.apply((w) => model.select(w, me, refs)),
181
201
  pick: (ref, onPick, options) => {
202
+ const pending = {
203
+ onPick: onPick,
204
+ question: options?.question,
205
+ };
182
206
  const asked = options?.to ?? Destination.Window;
183
207
  const to = isPhone() ? model.chooserOnPhone(asked) : asked;
184
208
  if (to === Destination.Modal) {
185
209
  open(ref, to, options);
186
- choosers.set(model.modalId(model.hostOf(me)), onPick);
210
+ ask(model.modalId(model.hostOf(me)), pending);
187
211
  return;
188
212
  }
189
213
  if (to === Destination.Peek) {
190
214
  store.peek(ref);
191
- choosers.set(PEEK, onPick);
215
+ ask(PEEK, pending);
192
216
  return;
193
217
  }
194
218
  open(ref, to, options);
195
219
  const id = store.get().focus;
196
220
  if (id !== PAGE && id !== PEEK)
197
- choosers.set(id, onPick);
221
+ ask(id, pending);
198
222
  },
199
223
  answer: (value) => {
200
- const onPick = choosers.get(me);
201
- if (onPick === undefined)
224
+ const pending = choosers.get(me);
225
+ if (pending === undefined)
202
226
  return;
203
227
  /* Forget it BEFORE closing, so closing is not mistaken for anything else. */
204
228
  choosers.delete(me);
205
- onPick(value);
229
+ pending.onPick(value);
206
230
  if (inModal)
207
231
  store.closeModal(me);
208
232
  else if (inPeek)
@@ -1,4 +1,4 @@
1
- import { type Crumb, type ResourceRef, Surface, type WorkspaceApi } from "../domain/index.js";
1
+ import { type Choosing, type Crumb, type ResourceRef, Surface, type WorkspaceApi } from "../domain/index.js";
2
2
  /**
3
3
  * An `api` for testing a view WITHOUT a workspace: it does nothing, and remembers every call. A
4
4
  * view only talks to the workspace through its `api`, so what it asked for is what a test checks —
@@ -9,7 +9,8 @@ import { type Crumb, type ResourceRef, Surface, type WorkspaceApi } from "../dom
9
9
  * // … click «Register payment»
10
10
  * expect(t.calls).toContainEqual({ method: "open", args: [paymentRef("F-1"), { to: "modal" }] });
11
11
  *
12
- * A chooser it was asked to open answers with `respond(value)`, the way its view would.
12
+ * A chooser it was asked to open answers with `respond(value)`, the way its view would. A view
13
+ * tested AS a chooser gets `{ choosing: { question } }`, and its `answer` is recorded.
13
14
  */
14
15
  /** One call the view made: the method and its arguments, as given. */
15
16
  export type ApiCall = {
@@ -26,6 +27,8 @@ export type TestApiOptions = {
26
27
  crumbs?: readonly Crumb[];
27
28
  /** What `api.context.selection()` gives. */
28
29
  selection?: readonly ResourceRef[];
30
+ /** Opened by `pick` with this question (`api.choosing`). `null` without it: opened to look. */
31
+ choosing?: Choosing | null;
29
32
  };
30
33
  export type TestApi = {
31
34
  api: WorkspaceApi;
@@ -13,6 +13,7 @@ export function createTestApi(self, options = {}) {
13
13
  self,
14
14
  surface: options.surface ?? Surface.Page,
15
15
  over: options.over ?? null,
16
+ choosing: options.choosing ?? null,
16
17
  context: {
17
18
  focused: () => self,
18
19
  selection: () => selection,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-composer",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "The Sincpro workspace for React: one page with its breadcrumb, a peek, side panels and windows, and ⌘K — all over references, never data.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",