@peers-app/peers-ui 0.34.6 → 0.34.7

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.
Files changed (30) hide show
  1. package/dist/components/markdown-editor/editor.d.ts +5 -4
  2. package/dist/components/markdown-editor/editor.js +10 -145
  3. package/dist/components/markdown-editor/lexical-editor.d.ts +3 -0
  4. package/dist/components/markdown-editor/lexical-editor.js +153 -0
  5. package/dist/components/markdown-render.d.ts +8 -0
  6. package/dist/components/markdown-render.js +143 -0
  7. package/dist/components/markdown-with-mentions.d.ts +1 -1
  8. package/dist/components/markdown-with-mentions.js +7 -129
  9. package/dist/components/trust-level-dropdown.d.ts +6 -1
  10. package/dist/components/trust-level-dropdown.js +43 -14
  11. package/dist/isolated-screens/focus-relay.d.ts +24 -0
  12. package/dist/isolated-screens/focus-relay.js +104 -0
  13. package/dist/isolated-screens/frame-document.d.ts +7 -0
  14. package/dist/isolated-screens/frame-document.js +11 -1
  15. package/dist/isolated-screens/frame-protocol.d.ts +26 -0
  16. package/dist/isolated-screens/frame-protocol.js +21 -0
  17. package/dist/isolated-screens/frame-runtime.js +47 -14
  18. package/dist/isolated-screens/isolated-screen.d.ts +6 -0
  19. package/dist/isolated-screens/isolated-screen.js +40 -3
  20. package/dist/isolated-screens/piece-frames.js +57 -5
  21. package/dist/isolated-screens/piece-reach.js +17 -6
  22. package/dist/lazy-module.d.ts +17 -0
  23. package/dist/lazy-module.js +26 -0
  24. package/dist/operator-console/console-session.js +1 -9
  25. package/dist/screens/groups/group-members.js +7 -17
  26. package/dist/screens/invites/accept-invite.js +15 -4
  27. package/dist/ui-contracts/piece-height.d.ts +8 -2
  28. package/dist/ui-contracts/piece-height.js +6 -1
  29. package/dist/ui-contracts/piece.js +1 -1
  30. package/package.json +4 -3
@@ -5,6 +5,11 @@ interface TrustLevelDropdownProps {
5
5
  onChange?: (newLevel: TrustLevel) => void;
6
6
  type?: "user" | "device";
7
7
  }
8
- /** Select a context-scoped user or device trust level, including the full-access Self grant. */
8
+ /**
9
+ * Select a context-scoped user or device trust level, including the full-access Self grant.
10
+ * Without a level set it shows the default (`getUserTrust`), marked as such, and once one is set
11
+ * it offers Use the default, which clears it. The signed-in user, in their Personal, is always
12
+ * Self, so for them it only shows Self.
13
+ */
9
14
  export declare const TrustLevelDropdown: ({ userId, dataContext, onChange, type, }: TrustLevelDropdownProps) => import("react/jsx-runtime").JSX.Element;
10
15
  export {};
@@ -5,10 +5,27 @@ const jsx_runtime_1 = require("react/jsx-runtime");
5
5
  const peers_sdk_1 = require("@peers-app/peers-sdk");
6
6
  const react_1 = require("react");
7
7
  const loading_indicator_1 = require("./loading-indicator");
8
- /** Select a context-scoped user or device trust level, including the full-access Self grant. */
8
+ /** The context the dropdown reads and writes: the one given, else the user's Personal. */
9
+ async function contextFor(dataContext) {
10
+ return dataContext || (await (0, peers_sdk_1.getUserContext)()).userDataContext;
11
+ }
12
+ /** The user's trust there, the default included. */
13
+ async function readTrust(userId, dataContext) {
14
+ return (0, peers_sdk_1.getUserTrust)(await contextFor(dataContext), userId);
15
+ }
16
+ /**
17
+ * Select a context-scoped user or device trust level, including the full-access Self grant.
18
+ * Without a level set it shows the default (`getUserTrust`), marked as such, and once one is set
19
+ * it offers Use the default, which clears it. The signed-in user, in their Personal, is always
20
+ * Self, so for them it only shows Self.
21
+ */
9
22
  const TrustLevelDropdown = ({ userId, dataContext, onChange, type = "user", }) => {
10
- const [trustLevel, setTrustLevelState] = (0, react_1.useState)(peers_sdk_1.TrustLevel.Unknown);
23
+ const [trust, setTrust] = (0, react_1.useState)({
24
+ level: peers_sdk_1.TrustLevel.Unknown,
25
+ explicit: false,
26
+ });
11
27
  const [loading, setLoading] = (0, react_1.useState)(true);
28
+ const trustLevel = trust.level;
12
29
  const allTrustLevelConfigs = [
13
30
  {
14
31
  value: peers_sdk_1.TrustLevel.Self,
@@ -44,24 +61,32 @@ const TrustLevelDropdown = ({ userId, dataContext, onChange, type = "user", }) =
44
61
  // Filter configs based on type
45
62
  const trustLevelConfigs = allTrustLevelConfigs.filter((config) => !config.showFor || config.showFor === type);
46
63
  (0, react_1.useEffect)(() => {
47
- async function loadTrustLevel() {
48
- const userContext = await (0, peers_sdk_1.getUserContext)();
49
- const contextToUse = dataContext || userContext.userDataContext;
50
- const level = await (0, peers_sdk_1.getUserTrustLevel)(contextToUse, userId);
51
- setTrustLevelState(level);
64
+ let current = true;
65
+ void readTrust(userId, dataContext).then((read) => {
66
+ if (!current)
67
+ return;
68
+ setTrust(read);
52
69
  setLoading(false);
53
- }
54
- loadTrustLevel();
70
+ });
71
+ return () => {
72
+ current = false;
73
+ };
55
74
  }, [userId, dataContext]);
56
75
  async function handleTrustLevelChange(newLevel) {
57
- const userContext = await (0, peers_sdk_1.getUserContext)();
58
- const contextToUse = dataContext || userContext.userDataContext;
59
- await (0, peers_sdk_1.setUserTrustLevel)(userId, newLevel, contextToUse);
60
- setTrustLevelState(newLevel);
76
+ await (0, peers_sdk_1.setUserTrustLevel)(userId, newLevel, await contextFor(dataContext));
77
+ setTrust({ level: newLevel, explicit: true });
61
78
  if (onChange) {
62
79
  onChange(newLevel);
63
80
  }
64
81
  }
82
+ /** Clear the level set here, so the default applies again. */
83
+ async function restoreDefault() {
84
+ const context = await contextFor(dataContext);
85
+ await (0, peers_sdk_1.UserTrustLevels)(context).delete(userId);
86
+ const restored = await (0, peers_sdk_1.getUserTrust)(context, userId);
87
+ setTrust(restored);
88
+ onChange?.(restored.level);
89
+ }
65
90
  // Find current config
66
91
  const sortedConfigs = trustLevelConfigs.sort((a, b) => a.value - b.value);
67
92
  let currentConfig = { color: "secondary", icon: "bi-shield", text: "Unknown" };
@@ -76,6 +101,10 @@ const TrustLevelDropdown = ({ userId, dataContext, onChange, type = "user", }) =
76
101
  if (loading) {
77
102
  return (0, jsx_runtime_1.jsx)(loading_indicator_1.LoadingIndicator, {});
78
103
  }
79
- return ((0, jsx_runtime_1.jsxs)("div", { className: "dropdown", children: [(0, jsx_runtime_1.jsxs)("button", { className: `btn btn-sm btn-${currentConfig.color} dropdown-toggle d-flex align-items-center gap-1`, type: "button", "data-bs-toggle": "dropdown", "data-bs-auto-close": "true", "data-bs-popper-config": '{"strategy":"fixed"}', "aria-expanded": "false", children: [(0, jsx_runtime_1.jsx)("i", { className: currentConfig.icon }), currentConfig.text] }), (0, jsx_runtime_1.jsx)("ul", { className: "dropdown-menu", children: trustLevelConfigs.map((config) => ((0, jsx_runtime_1.jsx)("li", { children: (0, jsx_runtime_1.jsx)("button", { className: "dropdown-item p-2", onClick: () => handleTrustLevelChange(config.value), children: (0, jsx_runtime_1.jsxs)("span", { className: `badge bg-${config.color} d-flex align-items-center gap-2 w-100 py-2 px-3 fs-6 ${trustLevel === config.value ? "border border-dark border-2" : ""}`, children: [(0, jsx_runtime_1.jsx)("i", { className: config.icon }), config.text] }) }) }, config.value))) })] }));
104
+ // The signed-in user in their Personal: Self whatever is chosen, so there's nothing to choose.
105
+ if (trustLevel === peers_sdk_1.TrustLevel.Self && !trust.explicit) {
106
+ return ((0, jsx_runtime_1.jsxs)("span", { className: `btn btn-sm btn-${currentConfig.color} disabled d-inline-flex align-items-center gap-1`, "aria-disabled": "true", children: [(0, jsx_runtime_1.jsx)("i", { className: currentConfig.icon }), currentConfig.text] }));
107
+ }
108
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "dropdown", children: [(0, jsx_runtime_1.jsxs)("button", { className: `btn btn-sm btn-${currentConfig.color} dropdown-toggle d-flex align-items-center gap-1`, type: "button", "data-bs-toggle": "dropdown", "data-bs-auto-close": "true", "data-bs-popper-config": '{"strategy":"fixed"}', "aria-expanded": "false", children: [(0, jsx_runtime_1.jsx)("i", { className: currentConfig.icon }), currentConfig.text, !trust.explicit && " (default)"] }), trust.knownFrom && ((0, jsx_runtime_1.jsxs)("small", { className: "text-muted d-block mt-1", children: ["Known from ", trust.knownFrom.toLocaleDateString()] })), (0, jsx_runtime_1.jsxs)("ul", { className: "dropdown-menu", children: [trustLevelConfigs.map((config) => ((0, jsx_runtime_1.jsx)("li", { children: (0, jsx_runtime_1.jsx)("button", { className: "dropdown-item p-2", onClick: () => handleTrustLevelChange(config.value), children: (0, jsx_runtime_1.jsxs)("span", { className: `badge bg-${config.color} d-flex align-items-center gap-2 w-100 py-2 px-3 fs-6 ${trustLevel === config.value ? "border border-dark border-2" : ""}`, children: [(0, jsx_runtime_1.jsx)("i", { className: config.icon }), config.text] }) }) }, config.value))), trust.explicit && ((0, jsx_runtime_1.jsx)("li", { children: (0, jsx_runtime_1.jsx)("button", { className: "dropdown-item p-2", onClick: () => void restoreDefault(), children: "Use the default" }) }))] })] }));
80
109
  };
81
110
  exports.TrustLevelDropdown = TrustLevelDropdown;
@@ -0,0 +1,24 @@
1
+ /** Which end of a piece focus enters at: its first control (Tab) or its last (Shift+Tab). */
2
+ export type FocusEdge = "first" | "last";
3
+ /** Which way focus leaves a piece: to the control after its slot, or the one before it. */
4
+ export type FocusDirection = "next" | "previous";
5
+ /** The elements Tab stops at in a document, in document order (positive tabindex aside). */
6
+ export declare function tabbables(doc: Document): HTMLElement[];
7
+ /**
8
+ * Focus a document's first or last control.
9
+ *
10
+ * @returns Whether it had one.
11
+ */
12
+ export declare function focusEdge(doc: Document, edge: FocusEdge): boolean;
13
+ /**
14
+ * Whether Tab (or Shift+Tab) on the focused element leaves the document's controls: from its last
15
+ * control, its first, or from nothing focusable.
16
+ */
17
+ export declare function tabLeaves(doc: Document, backwards: boolean): boolean;
18
+ /** The edge focus enters a slot at, from the element focus came from. */
19
+ export declare function edgeEnteredFrom(slot: Element, from: Element | null): FocusEdge;
20
+ /**
21
+ * Focus the control after (or before) a slot in its document, leaving the slot itself out. With
22
+ * none there, focus leaves the slot.
23
+ */
24
+ export declare function focusBeside(slot: HTMLElement, direction: FocusDirection): void;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ // Tab across a piece drawn over a framed screen's slot. The host draws the piece's frame beside
3
+ // the screen's frame, not inside it, so the browser's Tab order skips from the control before the
4
+ // slot to the control after it. The screen's slot takes focus in its place and the host moves it
5
+ // into the piece's frame; the piece's frame hands it back when Tab leaves its first or last
6
+ // control (frame-protocol.ts: `pieceFocus`, `focusEdge`, `focusLeave`, `pieceFocusLeave`).
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.tabbables = tabbables;
9
+ exports.focusEdge = focusEdge;
10
+ exports.tabLeaves = tabLeaves;
11
+ exports.edgeEnteredFrom = edgeEnteredFrom;
12
+ exports.focusBeside = focusBeside;
13
+ // A named radio group with a selection has one stop. Unselected groups are :indeterminate.
14
+ const TABBABLE = `:is(
15
+ a[href], area[href], button, input, select, textarea, iframe, details > summary:first-of-type,
16
+ [contenteditable='true'], [contenteditable='plaintext-only'], [contenteditable=''], [tabindex]
17
+ ):not(:disabled, input[type='hidden'], [inert], [inert] *)` +
18
+ ":not(input[type='radio'][name]:not([name=''], :checked, :indeterminate))";
19
+ /** The elements Tab stops at in a document, in document order (positive tabindex aside). */
20
+ function tabbables(doc) {
21
+ return [...doc.querySelectorAll(TABBABLE)].filter((element) => {
22
+ // An editing host is a native Tab stop even when its implicit tabIndex reads as -1.
23
+ if (element.tabIndex < 0 &&
24
+ (!element.hasAttribute("contenteditable") ||
25
+ element.hasAttribute("tabindex") ||
26
+ element.parentElement?.isContentEditable)) {
27
+ return false;
28
+ }
29
+ return (element.checkVisibility?.({ checkVisibilityCSS: true }) ??
30
+ (element.getClientRects().length > 0 && getComputedStyle(element).visibility === "visible"));
31
+ });
32
+ }
33
+ /**
34
+ * Focus a document's first or last control.
35
+ *
36
+ * @returns Whether it had one.
37
+ */
38
+ function focusEdge(doc, edge) {
39
+ const all = tabbables(doc);
40
+ const target = edge === "first" ? all[0] : all[all.length - 1];
41
+ target?.focus();
42
+ return target !== undefined;
43
+ }
44
+ /**
45
+ * Whether Tab (or Shift+Tab) on the focused element leaves the document's controls: from its last
46
+ * control, its first, or from nothing focusable.
47
+ */
48
+ function tabLeaves(doc, backwards) {
49
+ const all = tabbables(doc);
50
+ if (all.length === 0)
51
+ return true;
52
+ const active = doc.activeElement;
53
+ const direction = backwards ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;
54
+ if (!active || active === doc.body || active === doc.documentElement) {
55
+ // A click or Shift+click on text puts the selection's focus at the clicked end, and a text
56
+ // field that is blurred or removed leaves it at the field's place in its parent. It
57
+ // approximates the browser's Tab starting point, which can differ after a drag.
58
+ // With no selection in the body, let the browser choose the next control.
59
+ const selection = doc.getSelection();
60
+ const from = selection?.focusNode;
61
+ if (!selection || !from || !doc.body.contains(from))
62
+ return false;
63
+ // A point, not a node: in an element, the offset says which children come before it. A stop
64
+ // around the point is on neither side.
65
+ const point = doc.createRange();
66
+ point.setStart(from, selection.focusOffset);
67
+ return !all.some((stop) => backwards
68
+ ? point.comparePoint(stop, stop.childNodes.length) < 0
69
+ : point.comparePoint(stop, 0) > 0);
70
+ }
71
+ // When no radio is selected yet, Tab enters the group at either end, then skips its peers.
72
+ if (active instanceof HTMLInputElement &&
73
+ active.type === "radio" &&
74
+ active.name &&
75
+ !active.checked) {
76
+ return !all.some((stop) => (active.compareDocumentPosition(stop) & direction) !== 0 &&
77
+ !(stop instanceof HTMLInputElement &&
78
+ stop.type === "radio" &&
79
+ stop.name === active.name &&
80
+ stop.form === active.form));
81
+ }
82
+ // A click can focus a control with tabindex=-1. Tab resumes at its position in the document.
83
+ return !all.some((stop) => (active.compareDocumentPosition(stop) & direction) !== 0);
84
+ }
85
+ /** The edge focus enters a slot at, from the element focus came from. */
86
+ function edgeEnteredFrom(slot, from) {
87
+ // DOCUMENT_POSITION_FOLLOWING: `from` comes after the slot, so Shift+Tab brought focus back.
88
+ return from && slot.compareDocumentPosition(from) & Node.DOCUMENT_POSITION_FOLLOWING
89
+ ? "last"
90
+ : "first";
91
+ }
92
+ /**
93
+ * Focus the control after (or before) a slot in its document, leaving the slot itself out. With
94
+ * none there, focus leaves the slot.
95
+ */
96
+ function focusBeside(slot, direction) {
97
+ const all = tabbables(slot.ownerDocument).filter((element) => element === slot || !slot.contains(element));
98
+ const at = all.indexOf(slot);
99
+ const target = at === -1 ? undefined : all[direction === "next" ? at + 1 : at - 1];
100
+ if (target)
101
+ target.focus();
102
+ else
103
+ slot.blur();
104
+ }
@@ -9,6 +9,13 @@
9
9
  * The script says hello to the parent once. The host, which made the frame and knows its window,
10
10
  * answers with the frame runtime's source and a `MessagePort`; the script keeps the port where the
11
11
  * runtime's first module finds it and runs the source. A second boot is ignored.
12
+ *
13
+ * The source runs as a script element, not through `eval`. Every frame gets the same source, and
14
+ * Chromium compiles a script once for all the frames in its process, where it compiles each
15
+ * frame's `eval` again: a frame after the first starts in about a fifth of the time (35 ms
16
+ * instead of 175 here). WebKit reuses the compiled code either way.
17
+ * The script has run once it's in the document, so it's taken out again: its text would
18
+ * otherwise keep a copy of the runtime's source in every frame (about 7 MB each).
12
19
  */
13
20
  /** The message a frame's document sends its parent when it's ready to boot. */
14
21
  export declare const FRAME_HELLO = "peers-frame-hello";
@@ -10,6 +10,13 @@
10
10
  * The script says hello to the parent once. The host, which made the frame and knows its window,
11
11
  * answers with the frame runtime's source and a `MessagePort`; the script keeps the port where the
12
12
  * runtime's first module finds it and runs the source. A second boot is ignored.
13
+ *
14
+ * The source runs as a script element, not through `eval`. Every frame gets the same source, and
15
+ * Chromium compiles a script once for all the frames in its process, where it compiles each
16
+ * frame's `eval` again: a frame after the first starts in about a fifth of the time (35 ms
17
+ * instead of 175 here). WebKit reuses the compiled code either way.
18
+ * The script has run once it's in the document, so it's taken out again: its text would
19
+ * otherwise keep a copy of the runtime's source in every frame (about 7 MB each).
13
20
  */
14
21
  Object.defineProperty(exports, "__esModule", { value: true });
15
22
  exports.FRAME_CSP = exports.FRAME_PORT_GLOBAL = exports.FRAME_BOOT = exports.FRAME_HELLO = void 0;
@@ -67,7 +74,10 @@ ${surface}
67
74
  if (booted || event.source !== parent || !data || data.type !== "${exports.FRAME_BOOT}") return;
68
75
  booted = true;
69
76
  window.${exports.FRAME_PORT_GLOBAL} = event.ports[0];
70
- (0, eval)(data.runtime);
77
+ var script = document.createElement("script");
78
+ script.textContent = data.runtime;
79
+ document.head.appendChild(script);
80
+ script.remove();
71
81
  });
72
82
  parent.postMessage({ type: "${exports.FRAME_HELLO}" }, "*");
73
83
  })();
@@ -43,6 +43,11 @@ export interface IFrameInit {
43
43
  width?: number;
44
44
  /** The emits the embedder's version declares: the piece's `emit` has these, and `open`. */
45
45
  emits?: string[];
46
+ /**
47
+ * Drawn over a framed screen's slot: Tab leaving its first or last control goes back to the
48
+ * screen through the host (`focusLeave`), and the host moves focus in (`focusEdge`).
49
+ */
50
+ overSlot?: boolean;
46
51
  };
47
52
  /** The tab's path. */
48
53
  path: string;
@@ -124,6 +129,18 @@ export declare const FrameNotice: {
124
129
  readonly pieceEmit: "piece.emit";
125
130
  /** From a piece's frame: it asked to close. */
126
131
  readonly pieceClose: "piece.close";
132
+ /** From a piece's frame: it has drawn its piece, so the embedder's loading view can go. */
133
+ readonly pieceDrawn: "piece.drawn";
134
+ /**
135
+ * From a screen: Tab reached a piece's slot, `{ slotId, edge }` (`first` with Tab, `last` with
136
+ * Shift+Tab). The host moves focus into the piece's frame drawn over it.
137
+ */
138
+ readonly pieceFocus: "piece.focus";
139
+ /**
140
+ * From a piece's frame drawn over a slot: Tab left its last control, or Shift+Tab its first,
141
+ * `{ direction }` (`next` or `previous`). The host hands focus back to the screen.
142
+ */
143
+ readonly focusLeave: "focus.leave";
127
144
  /**
128
145
  * From a piece's frame that takes its own height: its content's height, `{ height }`, and its
129
146
  * width, `{ width }`, when it takes its content's width too.
@@ -168,6 +185,15 @@ export declare const HostNotice: {
168
185
  readonly pieceEmit: "piece.emit";
169
186
  /** A piece the screen embeds asked to close: `{ slotId }`. */
170
187
  readonly pieceClose: "piece.close";
188
+ /** A piece the screen embeds has drawn: `{ slotId }`. Its loading view can go. */
189
+ readonly pieceDrawn: "piece.drawn";
190
+ /** To a piece's frame: focus came in from its screen, `{ edge }`: its first or last control. */
191
+ readonly focusEdge: "focus.edge";
192
+ /**
193
+ * To a screen: Tab left the piece over `slotId`, `{ slotId, direction }`. The screen focuses
194
+ * the control after the slot (`next`) or before it (`previous`).
195
+ */
196
+ readonly pieceFocusLeave: "piece.focus-leave";
171
197
  /**
172
198
  * A piece the screen embeds, which takes its own height, is now `{ slotId, height }` tall, and
173
199
  * `width` wide when it takes its content's width.
@@ -78,6 +78,18 @@ exports.FrameNotice = {
78
78
  pieceEmit: "piece.emit",
79
79
  /** From a piece's frame: it asked to close. */
80
80
  pieceClose: "piece.close",
81
+ /** From a piece's frame: it has drawn its piece, so the embedder's loading view can go. */
82
+ pieceDrawn: "piece.drawn",
83
+ /**
84
+ * From a screen: Tab reached a piece's slot, `{ slotId, edge }` (`first` with Tab, `last` with
85
+ * Shift+Tab). The host moves focus into the piece's frame drawn over it.
86
+ */
87
+ pieceFocus: "piece.focus",
88
+ /**
89
+ * From a piece's frame drawn over a slot: Tab left its last control, or Shift+Tab its first,
90
+ * `{ direction }` (`next` or `previous`). The host hands focus back to the screen.
91
+ */
92
+ focusLeave: "focus.leave",
81
93
  /**
82
94
  * From a piece's frame that takes its own height: its content's height, `{ height }`, and its
83
95
  * width, `{ width }`, when it takes its content's width too.
@@ -122,6 +134,15 @@ exports.HostNotice = {
122
134
  pieceEmit: "piece.emit",
123
135
  /** A piece the screen embeds asked to close: `{ slotId }`. */
124
136
  pieceClose: "piece.close",
137
+ /** A piece the screen embeds has drawn: `{ slotId }`. Its loading view can go. */
138
+ pieceDrawn: "piece.drawn",
139
+ /** To a piece's frame: focus came in from its screen, `{ edge }`: its first or last control. */
140
+ focusEdge: "focus.edge",
141
+ /**
142
+ * To a screen: Tab left the piece over `slotId`, `{ slotId, direction }`. The screen focuses
143
+ * the control after the slot (`next`) or before it (`previous`).
144
+ */
145
+ pieceFocusLeave: "piece.focus-leave",
125
146
  /**
126
147
  * A piece the screen embeds, which takes its own height, is now `{ slotId, height }` tall, and
127
148
  * `width` wide when it takes its content's width.
@@ -13,6 +13,7 @@ const piece_height_1 = require("../ui-contracts/piece-height");
13
13
  const piece_resolution_1 = require("../ui-contracts/piece-resolution");
14
14
  const popover_surface_1 = require("../ui-contracts/popover-surface");
15
15
  const ui_inspection_1 = require("../ui-inspection");
16
+ const focus_relay_1 = require("./focus-relay");
16
17
  const frame_bridge_1 = require("./frame-bridge");
17
18
  const frame_protocol_1 = require("./frame-protocol");
18
19
  const wheel_relay_1 = require("./wheel-relay");
@@ -24,6 +25,8 @@ const pieces = new Map();
24
25
  let loadedVersionId;
25
26
  /** Whether this frame draws a piece, whose Escape and wheel go to its embedder. */
26
27
  let drawsPiece = false;
28
+ /** Whether this frame's piece is drawn over a framed screen's slot, so Tab out goes through the host. */
29
+ let relaysTab = false;
27
30
  /**
28
31
  * Apply the host's theme. The starting document uses the theme attribute for its color scheme
29
32
  * and keeps a piece's background transparent across page color updates.
@@ -116,6 +119,19 @@ function frameEmitters(host, names) {
116
119
  function PieceInFrame(props) {
117
120
  const { host, piece } = props;
118
121
  const emit = (0, react_1.useMemo)(() => frameEmitters(host, piece.emits), [host, piece.emits.join("\n")]);
122
+ // Once the piece's first render is on the page, the embedder's loading view can go.
123
+ (0, react_1.useEffect)(() => {
124
+ host.notify(frame_protocol_1.FrameNotice.pieceDrawn);
125
+ }, [host]);
126
+ // A piece in a popover paints the popover's background, from its first render: over a slot the
127
+ // host leaves the frame clear until then (isolated-screen.tsx), and the slot paints it instead.
128
+ (0, react_1.useLayoutEffect)(() => {
129
+ if (piece.width === undefined)
130
+ return;
131
+ const html = document.documentElement;
132
+ html.classList.add("bg-body");
133
+ return () => html.classList.remove("bg-body");
134
+ }, [piece.width]);
119
135
  const Component = piece.component;
120
136
  return ((0, jsx_runtime_1.jsx)(PieceRoot, { host: host, height: piece.height, width: piece.width, children: (0, jsx_runtime_1.jsx)(ScreenErrorBoundary, { packageName: props.packageName, onError: (message) => host.notify(frame_protocol_1.FrameNotice.failed, { message }), children: (0, jsx_runtime_1.jsx)(Component, { props: props.props, emit: emit, close: () => host.notify(frame_protocol_1.FrameNotice.pieceClose), groupId: piece.groupId }) }) }));
121
137
  }
@@ -202,6 +218,7 @@ let latestInit = 0;
202
218
  async function handleInit(host, init) {
203
219
  const thisInit = ++latestInit;
204
220
  drawsPiece = init.piece !== undefined;
221
+ relaysTab = init.piece?.overSlot === true;
205
222
  const firstInit = !root;
206
223
  if (firstInit) {
207
224
  const bridge = (0, frame_bridge_1.frameBridge)();
@@ -325,27 +342,43 @@ function isHostShortcut(event) {
325
342
  * embedder. A popover inside the frame stops its own Escape before it gets here; a listener the
326
343
  * piece added after this one can still take it, so it's sent once the event has reached them all.
327
344
  * With ⌘ or Ctrl held, Escape is a shortcut like the others (Ctrl+Escape cancels the host's tab
328
- * switcher).
345
+ * switcher). In a piece drawn over a slot, an unhandled Tab at an edge goes back to the screen.
329
346
  */
330
347
  function installKeyForwarding(host) {
348
+ host.onNotify(frame_protocol_1.HostNotice.focusEdge, (payload) => {
349
+ // The host focused this frame as it sent this. Focus the user moved since (a click on a host
350
+ // field) stays where it is: a frame without focus can still take it with `.focus()`.
351
+ if (!relaysTab || !document.hasFocus())
352
+ return;
353
+ const edge = payload?.edge === "last" ? "last" : "first";
354
+ // Nothing to focus: Tab goes straight on past the piece.
355
+ if (!(0, focus_relay_1.focusEdge)(document, edge)) {
356
+ host.notify(frame_protocol_1.FrameNotice.focusLeave, { direction: edge === "first" ? "next" : "previous" });
357
+ }
358
+ });
331
359
  const forward = (event) => {
332
360
  if (event.defaultPrevented)
333
361
  return;
334
- if (event.key === "Escape" &&
335
- drawsPiece &&
336
- event.type === "keydown" &&
337
- !event.metaKey &&
338
- !event.ctrlKey) {
339
- setTimeout(() => {
340
- if (event.defaultPrevented)
341
- return;
342
- host.notify(frame_protocol_1.FrameNotice.key, { type: event.type, key: event.key, code: event.code });
343
- }, 0);
344
- return;
345
- }
346
362
  const modifierKey = event.key === "Control" || event.key === "Meta";
347
- if (!(event.metaKey || event.ctrlKey || modifierKey))
363
+ if (!(event.metaKey || event.ctrlKey || modifierKey)) {
364
+ if (!drawsPiece || event.type !== "keydown")
365
+ return;
366
+ if (event.key === "Escape") {
367
+ setTimeout(() => {
368
+ if (event.defaultPrevented)
369
+ return;
370
+ host.notify(frame_protocol_1.FrameNotice.key, { type: event.type, key: event.key, code: event.code });
371
+ }, 0);
372
+ }
373
+ else if (event.key === "Tab" &&
374
+ relaysTab &&
375
+ !event.altKey &&
376
+ (0, focus_relay_1.tabLeaves)(document, event.shiftKey)) {
377
+ event.preventDefault();
378
+ host.notify(frame_protocol_1.FrameNotice.focusLeave, { direction: event.shiftKey ? "previous" : "next" });
379
+ }
348
380
  return;
381
+ }
349
382
  if (isHostShortcut(event))
350
383
  event.preventDefault();
351
384
  host.notify(frame_protocol_1.FrameNotice.key, {
@@ -53,6 +53,8 @@ export interface IFramePieceEvents {
53
53
  onSize(height: number, width?: number): void;
54
54
  /** It failed to load or threw. */
55
55
  onFailed(message: string): void;
56
+ /** It has drawn its piece: the embedder's loading view can go. */
57
+ onDrawn?(): void;
56
58
  /** Escape pressed in it that it left alone: for a popover around it. */
57
59
  onKey?(key: {
58
60
  key: string;
@@ -148,6 +150,10 @@ declare class FrameSession {
148
150
  private updateSlot;
149
151
  /** Tell a piece slot's frame what happened in its piece. */
150
152
  slotNotify(type: string, payload: Record<string, unknown>): void;
153
+ /** Focus came to this piece's frame from its screen's slot: at its first or last control. */
154
+ focusFrom(edge: "first" | "last"): void;
155
+ /** Tab left the piece over one of this screen's slots: focus beside the slot. */
156
+ focusBack(slotId: string, direction: "next" | "previous"): void;
151
157
  /** Show a screen or a piece: sends the init now if the frame is ready, or once it says so. */
152
158
  show(getContent: () => FrameContent): Promise<void>;
153
159
  private frameReady;
@@ -299,6 +299,28 @@ class FrameSession {
299
299
  opts.pieceEvents()?.onEmit(name, payload);
300
300
  });
301
301
  channel.onNotify(frame_protocol_1.FrameNotice.pieceClose, () => opts.pieceEvents()?.onClose());
302
+ channel.onNotify(frame_protocol_1.FrameNotice.pieceDrawn, () => opts.pieceEvents()?.onDrawn?.());
303
+ // Tab across a piece over a slot: in from the screen, and back out to it.
304
+ channel.onNotify(frame_protocol_1.FrameNotice.pieceFocus, (payload) => {
305
+ // The user can move to another frame or a window control before the notice arrives.
306
+ if (document.activeElement !== opts.element)
307
+ return;
308
+ const edge = payload?.edge === "last" ? "last" : "first";
309
+ for (const session of liveSessions) {
310
+ const link = session.opts.slotOf?.();
311
+ if (link?.session === this && link.slotId === payload?.slotId)
312
+ session.focusFrom(edge);
313
+ }
314
+ });
315
+ channel.onNotify(frame_protocol_1.FrameNotice.focusLeave, (payload) => {
316
+ if (document.activeElement !== opts.element)
317
+ return;
318
+ const link = this.opts.slotOf?.();
319
+ if (!link?.session)
320
+ return;
321
+ const direction = payload?.direction === "previous" ? "previous" : "next";
322
+ link.session.focusBack(link.slotId, direction);
323
+ });
302
324
  channel.onNotify(frame_protocol_1.FrameNotice.size, ({ height, width }) => {
303
325
  if (typeof height !== "number" || !Number.isFinite(height) || height < 0)
304
326
  return;
@@ -446,6 +468,16 @@ class FrameSession {
446
468
  slotNotify(type, payload) {
447
469
  this.channel.notify(type, payload);
448
470
  }
471
+ /** Focus came to this piece's frame from its screen's slot: at its first or last control. */
472
+ focusFrom(edge) {
473
+ this.opts.element.focus();
474
+ this.channel.notify(frame_protocol_1.HostNotice.focusEdge, { edge });
475
+ }
476
+ /** Tab left the piece over one of this screen's slots: focus beside the slot. */
477
+ focusBack(slotId, direction) {
478
+ this.opts.element.focus();
479
+ this.channel.notify(frame_protocol_1.HostNotice.pieceFocusLeave, { slotId, direction });
480
+ }
449
481
  /** Show a screen or a piece: sends the init now if the frame is ready, or once it says so. */
450
482
  async show(getContent) {
451
483
  const userContext = await (0, peers_sdk_1.getUserContext)();
@@ -470,6 +502,7 @@ class FrameSession {
470
502
  height: content.height,
471
503
  ...(content.width !== undefined ? { width: content.width } : {}),
472
504
  ...(content.emits ? { emits: content.emits } : {}),
505
+ ...(this.opts.slotOf?.() ? { overSlot: true } : {}),
473
506
  },
474
507
  }),
475
508
  props: content.props,
@@ -644,8 +677,11 @@ function PackageFrameElement(props) {
644
677
  };
645
678
  let frameClass = content.kind === "screen" ? "peers-screen-frame" : "peers-piece-frame";
646
679
  if (content.kind === "piece" && content.width !== undefined) {
647
- // PopoverSurface positions the picker over other content and supplies no background.
648
- frameClass += " bg-body";
680
+ // PopoverSurface positions the picker over other content and supplies no background. Over a
681
+ // slot the frame stays clear until the piece paints its own on its first render, so the
682
+ // embedder's loading view in the slot shows through until then.
683
+ if (!props.slotOf)
684
+ frameClass += " bg-body";
649
685
  // Keep the viewport at the cap so vw units and media queries can't feed back into the
650
686
  // reported width. The piece draws at (0, 0), regardless of its embedder's writing direction.
651
687
  frameStyle.width = content.width;
@@ -653,7 +689,7 @@ function PackageFrameElement(props) {
653
689
  frameStyle.top = 0;
654
690
  frameStyle.left = 0;
655
691
  }
656
- return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-package-frame", style: { position: "relative", width: "100%", height: "100%", overflow: FRAME_OVERFLOW }, children: [(0, jsx_runtime_1.jsx)("iframe", { ref: frameRef, title: props.label, srcDoc: srcDoc, sandbox: "allow-scripts allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-downloads", allow: "clipboard-read; clipboard-write; fullscreen; autoplay", "data-peers-frame": frameId, "data-peers-package": pkg.packageId, className: frameClass, style: frameStyle }), shownSlots.map((slot) => ((0, jsx_runtime_1.jsx)(PieceSlotFrame, { slot: slot, session: sessionRef }, slot.slotId)))] }));
692
+ return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-package-frame", style: { position: "relative", width: "100%", height: "100%", overflow: FRAME_OVERFLOW }, children: [(0, jsx_runtime_1.jsx)("iframe", { ref: frameRef, title: props.label, tabIndex: props.slotOf ? -1 : undefined, srcDoc: srcDoc, sandbox: "allow-scripts allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-downloads", allow: "clipboard-read; clipboard-write; fullscreen; autoplay", "data-peers-frame": frameId, "data-peers-package": pkg.packageId, className: frameClass, style: frameStyle }), shownSlots.map((slot) => ((0, jsx_runtime_1.jsx)(PieceSlotFrame, { slot: slot, session: sessionRef }, slot.slotId)))] }));
657
693
  }
658
694
  /**
659
695
  * A piece a framed screen embeds, drawn over the slot the screen reports: a frame of its own,
@@ -671,6 +707,7 @@ function PieceSlotFrame(props) {
671
707
  session.current?.slotNotify(frame_protocol_1.HostNotice.pieceEmit, { slotId, name, payload });
672
708
  },
673
709
  onClose: () => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceClose, { slotId }),
710
+ onDrawn: () => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceDrawn, { slotId }),
674
711
  onSize: (height, width) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceSize, width === undefined ? { slotId, height } : { slotId, height, width }),
675
712
  onFailed: (message) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceFailed, { slotId, message }),
676
713
  onKey: (key) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceKey, { slotId, ...key }),