@bchconnect/ui 0.0.0-stage → 0.1.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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +280 -2
  3. package/custom-elements.json +1771 -0
  4. package/dist/assets.generated.js +28 -0
  5. package/dist/assets.generated.js.map +1 -0
  6. package/dist/bchconnect-modal.d.ts +89 -0
  7. package/dist/bchconnect-modal.js +121 -0
  8. package/dist/bchconnect-modal.js.map +1 -0
  9. package/dist/code.js +130 -0
  10. package/dist/code.js.map +1 -0
  11. package/dist/defaults.d.ts +50 -0
  12. package/dist/defaults.js +139 -0
  13. package/dist/defaults.js.map +1 -0
  14. package/dist/define.d.ts +17 -0
  15. package/dist/define.js +23 -0
  16. package/dist/define.js.map +1 -0
  17. package/dist/directory.js +37 -0
  18. package/dist/directory.js.map +1 -0
  19. package/dist/faces.js +80 -0
  20. package/dist/faces.js.map +1 -0
  21. package/dist/gesture.js +78 -0
  22. package/dist/gesture.js.map +1 -0
  23. package/dist/headless.d.ts +99 -0
  24. package/dist/headless.js +190 -0
  25. package/dist/headless.js.map +1 -0
  26. package/dist/html.js +17 -0
  27. package/dist/html.js.map +1 -0
  28. package/dist/icons.js +21 -0
  29. package/dist/icons.js.map +1 -0
  30. package/dist/index.d.ts +32 -0
  31. package/dist/index.js +8 -0
  32. package/dist/locales/es.d.ts +12 -0
  33. package/dist/locales/es.js +50 -0
  34. package/dist/locales/es.js.map +1 -0
  35. package/dist/messages.d.ts +95 -0
  36. package/dist/messages.js +63 -0
  37. package/dist/messages.js.map +1 -0
  38. package/dist/modal.d.ts +112 -0
  39. package/dist/modal.js +700 -0
  40. package/dist/modal.js.map +1 -0
  41. package/dist/motion.js +160 -0
  42. package/dist/motion.js.map +1 -0
  43. package/dist/page-scroll.js +28 -0
  44. package/dist/page-scroll.js.map +1 -0
  45. package/dist/register.js +14 -0
  46. package/dist/register.js.map +1 -0
  47. package/dist/sheet-query.js +8 -0
  48. package/dist/sheet-query.js.map +1 -0
  49. package/dist/sheet.js +329 -0
  50. package/dist/sheet.js.map +1 -0
  51. package/dist/state.d.ts +175 -0
  52. package/dist/state.js +11 -0
  53. package/dist/state.js.map +1 -0
  54. package/dist/styles/modal.css.sheet.js +13 -0
  55. package/dist/styles/modal.css.sheet.js.map +1 -0
  56. package/dist/styles/theme.generated.css.sheet.js +13 -0
  57. package/dist/styles/theme.generated.css.sheet.js.map +1 -0
  58. package/dist/styles/toast.css.sheet.js +13 -0
  59. package/dist/styles/toast.css.sheet.js.map +1 -0
  60. package/dist/styles/tokens.css.sheet.js +13 -0
  61. package/dist/styles/tokens.css.sheet.js.map +1 -0
  62. package/dist/theme.generated.d.ts +105 -0
  63. package/dist/theme.generated.js +96 -0
  64. package/dist/theme.generated.js.map +1 -0
  65. package/dist/toast.d.ts +96 -0
  66. package/dist/toast.js +180 -0
  67. package/dist/toast.js.map +1 -0
  68. package/package.json +87 -3
package/dist/modal.js ADDED
@@ -0,0 +1,700 @@
1
+
2
+ import { renderCode } from "./code.js";
3
+ import { safeHref } from "./html.js";
4
+ import { icon } from "./icons.js";
5
+ import { directoryScreen } from "./directory.js";
6
+ import { caption, captionKind, copyFace, statusFace } from "./faces.js";
7
+ import { mergeMessages } from "./messages.js";
8
+ import { animationsFinished, crossfade, enter, morphHeight, namedAnimationsFinished, retext, slideSheet, tempoOf } from "./motion.js";
9
+ import { lockPageScroll, unlockPageScroll } from "./page-scroll.js";
10
+ import { draggableSheet } from "./sheet.js";
11
+ import { SHEET_QUERY } from "./sheet-query.js";
12
+ import { NETWORK_LABEL } from "./state.js";
13
+ import modal_css_sheet_default from "./styles/modal.css.sheet.js";
14
+ import theme_generated_css_sheet_default from "./styles/theme.generated.css.sheet.js";
15
+ import tokens_css_sheet_default from "./styles/tokens.css.sheet.js";
16
+ import { LitElement, html, nothing } from "lit";
17
+ import { classMap } from "lit/directives/class-map.js";
18
+ import { keyed } from "lit/directives/keyed.js";
19
+ import { live } from "lit/directives/live.js";
20
+ import { createRef, ref } from "lit/directives/ref.js";
21
+ import { repeat } from "lit/directives/repeat.js";
22
+ //#region src/modal.ts
23
+ /** How long to wait after a deep link before assuming nothing opened (a failed handler gives no signal). */
24
+ const DEEP_LINK_GRACE = 1500;
25
+ const PLACEHOLDER_LINK = "bchconnect://placeholder/kqzvxwtnmrhpbdgjsfcylakqzvxwtnmrhpbdgjsfcylakqzvxwtnmrhpbdgjsfcylakqzvxwtnmrhpbdgjsfcyla";
26
+ const COPIED_FOR = 1800;
27
+ /** `@keyframes` names. See `#close`. */
28
+ const EXIT_ANIMATIONS = /* @__PURE__ */ new Set([
29
+ "lift",
30
+ "sink",
31
+ "sheet-out",
32
+ "scrim-out"
33
+ ]);
34
+ const NO_BROKEN_LOGOS = /* @__PURE__ */ new Set();
35
+ const OPEN_ARROW = html`<svg class="open-arrow" width="13" height="13" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8.5 8.5 3.5"/><path d="M4.75 3.5H8.5v3.75"/></svg>`;
36
+ const TITLE_KEYS = {
37
+ connect: "connectTitle",
38
+ wallets: "getWalletTitle"
39
+ };
40
+ /** The view's wallet, or the default: the first listed that speaks the protocol. */
41
+ function selectedWallet(view) {
42
+ return view.wallets.find((entry) => entry.id === view.wallet) ?? view.wallets.find((entry) => entry.protocols.includes(view.protocol)) ?? view.wallets[0];
43
+ }
44
+ function offeredProtocols(view, sheet) {
45
+ const wallet = sheet ? void 0 : selectedWallet(view);
46
+ if (wallet === void 0) return view.protocols;
47
+ return view.protocols.filter((entry) => entry.id === view.protocol || wallet.protocols.includes(entry.id));
48
+ }
49
+ function linkOf(view) {
50
+ return view.phase.kind === "awaiting-approval" ? view.phase.link : null;
51
+ }
52
+ function desktopLink(wallet, view) {
53
+ if (wallet.href === null) return null;
54
+ if (!wallet.href.includes("{link}")) return safeHref(wallet.href);
55
+ if (view.phase.kind !== "awaiting-approval") return "";
56
+ const link = encodeURIComponent(view.phase.link);
57
+ return safeHref(wallet.href.replaceAll("{link}", () => link));
58
+ }
59
+ let codeRenderer = null;
60
+ /** Sets the renderer, once, before any modal is created. @internal */
61
+ function useCodeRenderer(renderer) {
62
+ codeRenderer = renderer;
63
+ }
64
+ /**
65
+ * The `<bchc-modal>` web component
66
+ *
67
+ * @tag bchc-modal
68
+ *
69
+ * @attr {BchcAccent} data-bchc-accent - Curated accent color.
70
+ * @attr {BchcNeutral} data-bchc-neutral - Neutral family override; each accent has a default pairing.
71
+ * @attr {BchcRadius} data-bchc-radius - Corner radius preset applied to every rounded part.
72
+ * @attr {BchcFont} data-bchc-font - Font stack; `brand` falls back to `system` until a face is injected.
73
+ * @attr {BchcBlur} data-bchc-blur - Backdrop blur behind the modal.
74
+ * @attr {BchcMode} data-bchc-mode - Color scheme; `auto` follows `prefers-color-scheme`.
75
+ *
76
+ * @fires {CustomEvent<{ wallet: string }>} bchc-wallet - Fired when the visitor selects a wallet, and before `bchc-protocol` when a tab change pins the default wallet; `bchc-protocol` follows when the selected wallet lacks the current session type.
77
+ * @fires {CustomEvent<{ protocol: ProtocolId }>} bchc-protocol - Fired when the visitor picks a different session type.
78
+ * @fires {CustomEvent<{ screen: ModalScreen }>} bchc-screen - Fired when the visitor moves between the connect and wallets screens.
79
+ * @fires {CustomEvent<void>} bchc-close - Fired once the exit has played. Remove the element on it, or set `view` to `null` before showing it again.
80
+ * @fires {CustomEvent<void>} bchc-retry - Fired when "Try again" is pressed after a failure.
81
+ *
82
+ * @cssprop --bchc-font-brand-family - Brand font family read when `data-bchc-font="brand"`; falls back to the system stack until set.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * defineElements();
87
+ * const modal = document.createElement("bchc-modal") as BchcModal;
88
+ * modal.view = view;
89
+ * modal.addEventListener("bchc-wallet", (event) => { ... });
90
+ * modal.addEventListener("bchc-protocol", (event) => { ... });
91
+ * modal.addEventListener("bchc-close", () => modal.remove());
92
+ * document.body.append(modal);
93
+ * ```
94
+ *
95
+ * @beta
96
+ */
97
+ var BchcModal = class extends LitElement {
98
+ /** Lit's reactive property declarations. @internal */
99
+ static properties = {
100
+ view: { attribute: false },
101
+ messages: { attribute: false },
102
+ _appearance: { state: true },
103
+ _bodyView: { state: true },
104
+ _bodyKey: { state: true },
105
+ _sheet: { state: true },
106
+ _rtl: { state: true },
107
+ _title: { state: true },
108
+ _caption: { state: true },
109
+ _captionWallet: { state: true },
110
+ _status: { state: true },
111
+ _copied: { state: true },
112
+ _linkFailed: { state: true },
113
+ _brokenLogos: { state: true },
114
+ _accent: { attribute: "data-bchc-accent" },
115
+ _neutral: { attribute: "data-bchc-neutral" },
116
+ _radius: { attribute: "data-bchc-radius" },
117
+ _font: { attribute: "data-bchc-font" },
118
+ _blur: { attribute: "data-bchc-blur" },
119
+ _mode: { attribute: "data-bchc-mode" }
120
+ };
121
+ /** Lit's adopted style sheets. @internal */
122
+ static styles = [
123
+ theme_generated_css_sheet_default,
124
+ tokens_css_sheet_default,
125
+ modal_css_sheet_default
126
+ ];
127
+ #copyResetTimer = null;
128
+ #deepLinkTimer = null;
129
+ #closing = false;
130
+ #closed = null;
131
+ #entered = false;
132
+ #appearances = 0;
133
+ #crossfade = null;
134
+ #walletSwaps = 0;
135
+ #scrollLocked = false;
136
+ #overlay = createRef();
137
+ #card = createRef();
138
+ #scrim = createRef();
139
+ #body = createRef();
140
+ #drag = null;
141
+ #media = matchMedia(SHEET_QUERY);
142
+ #codeHost = document.createElement("button");
143
+ get #messages() {
144
+ return mergeMessages(this.messages);
145
+ }
146
+ constructor() {
147
+ super();
148
+ this.view = null;
149
+ this.messages = null;
150
+ this._appearance = null;
151
+ this._bodyView = null;
152
+ this._bodyKey = null;
153
+ this._sheet = false;
154
+ this._rtl = false;
155
+ this._title = "";
156
+ this._caption = "none";
157
+ this._captionWallet = null;
158
+ this._status = null;
159
+ this._copied = false;
160
+ this._linkFailed = false;
161
+ this._brokenLogos = NO_BROKEN_LOGOS;
162
+ this.#codeHost.className = "code";
163
+ this.#codeHost.type = "button";
164
+ this.#codeHost.setAttribute("data-act", "copy");
165
+ }
166
+ /** Called by the browser when the element is attached. @internal */
167
+ connectedCallback() {
168
+ super.connectedCallback();
169
+ this.#media.addEventListener("change", this.#onMediaChange);
170
+ document.addEventListener("visibilitychange", this.#onLeft);
171
+ document.addEventListener("keydown", this.#onKey);
172
+ addEventListener("blur", this.#onLeft);
173
+ this.requestUpdate();
174
+ }
175
+ /** Called by the browser when the element is detached. @internal */
176
+ disconnectedCallback() {
177
+ super.disconnectedCallback();
178
+ this.#media.removeEventListener("change", this.#onMediaChange);
179
+ document.removeEventListener("visibilitychange", this.#onLeft);
180
+ document.removeEventListener("keydown", this.#onKey);
181
+ removeEventListener("blur", this.#onLeft);
182
+ this.#hide();
183
+ this.requestUpdate();
184
+ }
185
+ /**
186
+ * Plays the exit, then emits `bchc-close`. A second call while the first
187
+ * is still playing does nothing.
188
+ */
189
+ async close() {
190
+ if (this.isConnected) await this.updateComplete;
191
+ await this.#close(false);
192
+ }
193
+ #close(dragged, settled) {
194
+ if (this.#closing) return this.#closed ?? Promise.resolve();
195
+ const overlay = this.#overlay.value;
196
+ if (overlay === void 0) return Promise.resolve();
197
+ this.#closing = true;
198
+ this.#closed = this.#playClose(overlay, dragged, settled);
199
+ return this.#closed;
200
+ }
201
+ async #playClose(overlay, dragged, settled) {
202
+ if (!dragged) this.#drag?.commit();
203
+ overlay.classList.add("is-closing");
204
+ if (dragged) {
205
+ overlay.classList.add("is-dragged");
206
+ await settled;
207
+ } else await namedAnimationsFinished(overlay, EXIT_ANIMATIONS);
208
+ if (!overlay.open) return;
209
+ const closed = new Promise((resolve) => overlay.addEventListener("close", resolve, { once: true }));
210
+ overlay.close();
211
+ await closed;
212
+ }
213
+ #onMediaChange = () => {
214
+ this.requestUpdate();
215
+ };
216
+ #onKey = (event) => {
217
+ if (event.key !== "Escape" || event.defaultPrevented) return;
218
+ event.preventDefault();
219
+ this.close();
220
+ };
221
+ #onCancel = (event) => {
222
+ if (event.defaultPrevented || !event.cancelable) return;
223
+ event.preventDefault();
224
+ this.close();
225
+ };
226
+ #onDialogClose = (event) => {
227
+ if (event.currentTarget !== this.#overlay.value) return;
228
+ this.#closing = true;
229
+ this.#unlockScroll();
230
+ this.#emit("bchc-close");
231
+ };
232
+ #onLeft = () => {
233
+ if (this.#deepLinkTimer !== null) {
234
+ clearTimeout(this.#deepLinkTimer);
235
+ this.#deepLinkTimer = null;
236
+ }
237
+ };
238
+ #emit(type, detail) {
239
+ this.dispatchEvent(new CustomEvent(type, {
240
+ detail,
241
+ bubbles: true,
242
+ composed: true
243
+ }));
244
+ }
245
+ #onOverlayClick(event) {
246
+ const target = event.target;
247
+ if (target === event.currentTarget) {
248
+ this.close();
249
+ return;
250
+ }
251
+ if (!(target instanceof Element)) return;
252
+ if (target.closest(".close") !== null) {
253
+ this.close();
254
+ return;
255
+ }
256
+ const tab = target.closest(".tab");
257
+ if (tab !== null) {
258
+ this.#selectProtocol(tab);
259
+ return;
260
+ }
261
+ const row = target.closest("[data-wallet]");
262
+ if (row !== null) {
263
+ this.#selectWallet(row);
264
+ return;
265
+ }
266
+ const actionTarget = target.closest("[data-act]");
267
+ const action = actionTarget?.getAttribute("data-act");
268
+ if (action === "retry") this.#emit("bchc-retry");
269
+ if (action === "copy") this.#copyLink();
270
+ if (action === "back") this.#emit("bchc-screen", { screen: "connect" });
271
+ if (action === "wallets") this.#emit("bchc-screen", { screen: "wallets" });
272
+ if (action === "open" && this.view?.phase.kind === "awaiting-approval" && actionTarget instanceof HTMLAnchorElement && actionTarget.hasAttribute("href")) this.#armDeepLinkHint();
273
+ }
274
+ #onOverlayKeydown(event) {
275
+ const target = event.target;
276
+ if (!(target instanceof Element)) return;
277
+ const radio = target.closest("[data-wallet]");
278
+ const tab = target.closest(".tab");
279
+ const item = radio ?? tab;
280
+ const group = item?.parentElement?.closest(".wallets, .tabs");
281
+ if (item === null || group === null || group === void 0) return;
282
+ if (event.key === "Enter" || event.key === " ") {
283
+ event.preventDefault();
284
+ if (tab !== null) this.#selectProtocol(tab);
285
+ else this.#selectWallet(item);
286
+ return;
287
+ }
288
+ const items = [...group.querySelectorAll(radio !== null ? "[data-wallet]" : ".tab")];
289
+ const at = items.indexOf(item);
290
+ const forward = this._rtl ? "ArrowLeft" : "ArrowRight";
291
+ const back = this._rtl ? "ArrowRight" : "ArrowLeft";
292
+ let next;
293
+ if (event.key === forward || radio !== null && event.key === "ArrowDown") next = (at + 1) % items.length;
294
+ else if (event.key === back || radio !== null && event.key === "ArrowUp") next = (at - 1 + items.length) % items.length;
295
+ else if (event.key === "Home") next = 0;
296
+ else if (event.key === "End") next = items.length - 1;
297
+ else return;
298
+ event.preventDefault();
299
+ const nextItem = items[next];
300
+ if (nextItem === void 0) return;
301
+ for (const entry of items) entry.tabIndex = entry === nextItem ? 0 : -1;
302
+ nextItem.focus({ preventScroll: true });
303
+ }
304
+ #hide() {
305
+ this.#overlay.value?.close();
306
+ this.#unlockScroll();
307
+ this.#drag?.release();
308
+ this.#drag = null;
309
+ if (this.#copyResetTimer !== null) {
310
+ clearTimeout(this.#copyResetTimer);
311
+ this.#copyResetTimer = null;
312
+ }
313
+ if (this.#deepLinkTimer !== null) {
314
+ clearTimeout(this.#deepLinkTimer);
315
+ this.#deepLinkTimer = null;
316
+ }
317
+ this._copied = false;
318
+ this.#closing = false;
319
+ this.#closed = null;
320
+ this.#entered = false;
321
+ this.#crossfade = null;
322
+ this._appearance = null;
323
+ this._bodyView = null;
324
+ this._bodyKey = null;
325
+ this._brokenLogos = NO_BROKEN_LOGOS;
326
+ }
327
+ #lockScroll() {
328
+ if (this.#scrollLocked) return;
329
+ this.#scrollLocked = true;
330
+ lockPageScroll();
331
+ }
332
+ #unlockScroll() {
333
+ if (!this.#scrollLocked) return;
334
+ this.#scrollLocked = false;
335
+ unlockPageScroll();
336
+ }
337
+ /** Lit hook run before each render; derives the shell and body state from `view`. @internal */
338
+ willUpdate(changed) {
339
+ this.#codeHost.setAttribute("aria-label", this.#messages.copyLinkLabel);
340
+ const view = this.view;
341
+ if (view === null) {
342
+ this.#hide();
343
+ return;
344
+ }
345
+ if (!this.isConnected) return;
346
+ if (this._appearance === null) {
347
+ if (view.phase.kind === "connected") return;
348
+ this.#appearances += 1;
349
+ this._appearance = this.#appearances;
350
+ this._title = this.#messages[TITLE_KEYS[view.screen]];
351
+ } else if (changed.has("messages")) {
352
+ const shown = changed.get("view") ?? view;
353
+ this._title = this.#messages[TITLE_KEYS[shown.screen]];
354
+ }
355
+ const previousSheet = this._sheet;
356
+ this._sheet = this.#media.matches;
357
+ this._rtl = getComputedStyle(this).direction === "rtl";
358
+ if (view.phase.kind !== "connected") this.#prepareBody(view, previousSheet);
359
+ }
360
+ #prepareBody(view, previousSheet) {
361
+ const previous = this._bodyView;
362
+ const previousKey = this._bodyKey;
363
+ const sheet = this._sheet;
364
+ const key = `${view.screen}:${sheet}`;
365
+ this._bodyView = view;
366
+ if (linkOf(view) !== (previous === null ? null : linkOf(previous))) {
367
+ this._linkFailed = false;
368
+ if (this.#deepLinkTimer !== null) {
369
+ clearTimeout(this.#deepLinkTimer);
370
+ this.#deepLinkTimer = null;
371
+ }
372
+ }
373
+ if (key === previousKey) return;
374
+ this._bodyKey = key;
375
+ this._linkFailed = false;
376
+ this._caption = captionKind(view, false);
377
+ this._captionWallet = this.#captionWalletOf(view);
378
+ this._status = view.phase.kind === "failed" ? view.phase.reason : null;
379
+ this.#crossfade = null;
380
+ if (previous === null || previousKey === null || previousSheet !== sheet) return;
381
+ this.#crossfade = { sheet };
382
+ }
383
+ /** Lit hook committing the template; a body rebuild is committed inside its motion. @internal */
384
+ update(changed) {
385
+ const motion = this.#crossfade;
386
+ this.#crossfade = null;
387
+ const card = this.#card.value;
388
+ const body = this.#body.value;
389
+ if (motion === null || card === void 0 || body === void 0) {
390
+ super.update(changed);
391
+ return;
392
+ }
393
+ (motion.sheet ? slideSheet : morphHeight)(card, () => crossfade(body, () => super.update(changed), 0));
394
+ }
395
+ /** Lit hook producing the template. @internal */
396
+ render() {
397
+ const view = this.view;
398
+ if (this._appearance === null || view === null) return nothing;
399
+ const messages = this.#messages;
400
+ return keyed(this._appearance, html`<dialog
401
+ class="overlay ${classMap({
402
+ "is-sheet": this._sheet,
403
+ "is-rtl": this._rtl
404
+ })}"
405
+ lang=${messages.lang}
406
+ aria-labelledby="modal-title"
407
+ ${ref(this.#overlay)}
408
+ @click=${this.#onOverlayClick}
409
+ @keydown=${this.#onOverlayKeydown}
410
+ @cancel=${this.#onCancel}
411
+ @close=${this.#onDialogClose}
412
+ >
413
+ <div class="scrim" aria-hidden="true" ${ref(this.#scrim)}></div>
414
+ <div class="card" tabindex="-1" autofocus ${ref(this.#card)}>
415
+ <div class="grabber" aria-hidden="true"></div>
416
+ <div class="head">
417
+ <button class="back" type="button" data-act="back" aria-label=${messages.back} .hidden=${view.screen !== "wallets"}>${icon("chevronLeft")}</button>
418
+ <h2 class="title" id="modal-title">${this._title}</h2>
419
+ <span class="badge" .hidden=${view.network === "mainnet" || view.screen === "wallets"}>${NETWORK_LABEL[view.network]}</span>
420
+ <button class="close" type="button" aria-label=${messages.close}>${icon("x")}</button>
421
+ </div>
422
+ <div class="body" ${ref(this.#body)}>${this.#bodyTemplate()}</div>
423
+ </div>
424
+ </dialog>`);
425
+ }
426
+ /** Lit hook run after each render. @internal */
427
+ updated() {
428
+ const view = this.view;
429
+ if (view === null || !this.isConnected) return;
430
+ const overlay = this.#overlay.value;
431
+ const card = this.#card.value;
432
+ const scrim = this.#scrim.value;
433
+ const body = this.#body.value;
434
+ if (overlay === void 0 || card === void 0 || scrim === void 0 || body === void 0) {
435
+ if (view.phase.kind === "connected") this.#emit("bchc-close");
436
+ return;
437
+ }
438
+ if (this.#drag === null) {
439
+ overlay.showModal();
440
+ this.#lockScroll();
441
+ this.#drag = draggableSheet(card, scrim, {
442
+ isActive: () => this.#media.matches,
443
+ onDismiss: (settled) => void this.#close(true, settled)
444
+ });
445
+ }
446
+ const sheet = this._sheet;
447
+ const title = overlay.querySelector(".title");
448
+ const titleText = this.#messages[TITLE_KEYS[view.screen]];
449
+ if (title instanceof HTMLElement && titleText !== this._title) retext(title, titleText, () => {
450
+ this._title = titleText;
451
+ return this.updateComplete;
452
+ });
453
+ if (view.phase.kind === "connected") {
454
+ this.#close(false);
455
+ return;
456
+ }
457
+ if (view.screen === "connect") {
458
+ this.#retextBody(body, view);
459
+ this.#codeHost.disabled = view.phase.kind !== "awaiting-approval";
460
+ this.#paintCode(view);
461
+ if (sheet) this.#measureSheetRest(card, body);
462
+ }
463
+ if (!this.#entered) {
464
+ this.#entered = true;
465
+ if (!sheet) {
466
+ enter(overlay.querySelectorAll(".head, .split > :not(.cell-tile), .single > :not(.tile)"), tempoOf(card));
467
+ overlay.classList.add("is-entering");
468
+ animationsFinished(overlay).then(() => overlay.classList.remove("is-entering"));
469
+ }
470
+ }
471
+ }
472
+ #retextBody(body, view) {
473
+ const nextCaption = captionKind(view, this._linkFailed);
474
+ const nextWallet = this.#captionWalletOf(view);
475
+ const captionElement = body.querySelector(".caption");
476
+ const walletElement = body.querySelector(".caption-wallet");
477
+ if (walletElement instanceof HTMLElement && nextCaption === "scan" && this._caption === "scan" && nextWallet !== null && nextWallet !== this._captionWallet) {
478
+ retext(walletElement, nextWallet, () => {
479
+ if (!walletElement.isConnected) return;
480
+ this._captionWallet = nextWallet;
481
+ return this.updateComplete;
482
+ });
483
+ this.#walletSwaps += 1;
484
+ } else if (captionElement instanceof HTMLElement && (nextCaption !== this._caption || nextCaption === "scan" && nextWallet !== this._captionWallet)) retext(captionElement, `${nextCaption}|${nextWallet}|${this.#walletSwaps}`, () => {
485
+ if (!captionElement.isConnected) return;
486
+ this._caption = nextCaption;
487
+ this._captionWallet = nextWallet;
488
+ return this.updateComplete;
489
+ });
490
+ const reason = view.phase.kind === "failed" ? view.phase.reason : null;
491
+ const status = body.querySelector(".status");
492
+ if (status instanceof HTMLElement && reason !== null && reason !== this._status) retext(status, reason, () => {
493
+ if (!status.isConnected) return;
494
+ this._status = reason;
495
+ return this.updateComplete;
496
+ });
497
+ }
498
+ #bodyTemplate() {
499
+ const view = this._bodyView;
500
+ if (view === null) return nothing;
501
+ return keyed(this._bodyKey, view.screen === "wallets" ? directoryScreen(view.directory, this.#messages, (logo, size, extra) => this.#logo(logo, size, extra)) : this.#connectScreen(view, this._sheet));
502
+ }
503
+ #sessionType(view) {
504
+ const protocols = offeredProtocols(view, this._sheet);
505
+ const sessionType = this.#messages.sessionType;
506
+ const mark = (entry) => entry.mark === null ? nothing : html`<img class="tab-mark" src=${entry.mark.src} alt="" width="16" height="16" />`;
507
+ const only = protocols.length === 1 ? protocols[0] : void 0;
508
+ if (only !== void 0) return html`
509
+ <div class="session is-info">
510
+ <span class="label" id="session-type">${sessionType}</span>
511
+ <span class="session-value" aria-labelledby="session-type">${mark(only)}<span>${only.name}</span></span>
512
+ </div>
513
+ `;
514
+ return html`
515
+ <div class="session is-tabs">
516
+ <span class="visually-hidden" id="session-type">${sessionType}</span>
517
+ <div class="tabs ${classMap({ "is-dense": protocols.length > 2 })}" role="tablist" aria-labelledby="session-type" style="--count:${protocols.length}">
518
+ ${repeat(protocols, (entry) => entry.id, (entry) => {
519
+ const active = entry.id === view.protocol;
520
+ return html`
521
+ <button class=${classMap({
522
+ tab: true,
523
+ "no-mark": entry.mark === null
524
+ })} type="button" role="tab" id="tab-${entry.id}" data-protocol=${entry.id} aria-selected=${String(active)} aria-controls="code-tile" .tabIndex=${live(active ? 0 : -1)} title=${entry.name}>
525
+ ${mark(entry)}
526
+ <span class="tab-name"><span class="tab-name-inner">${entry.name}</span></span>
527
+ </button>`;
528
+ })}
529
+ </div>
530
+ </div>
531
+ `;
532
+ }
533
+ #selectProtocol(tab) {
534
+ const view = this.view;
535
+ const protocol = tab.getAttribute("data-protocol");
536
+ if (view === null || protocol === null || protocol === view.protocol) return;
537
+ const pinned = view.wallets.some((entry) => entry.id === view.wallet);
538
+ const wallet = selectedWallet(view);
539
+ if (!pinned && wallet !== void 0 && !this._sheet) this.#emit("bchc-wallet", { wallet: wallet.id });
540
+ this.#emit("bchc-protocol", { protocol });
541
+ }
542
+ #selectWallet(row) {
543
+ const view = this.view;
544
+ const id = row.getAttribute("data-wallet");
545
+ if (view === null || id === null || id === selectedWallet(view)?.id) return;
546
+ const wallet = view.wallets.find((entry) => entry.id === id);
547
+ if (wallet === void 0) return;
548
+ this.#emit("bchc-wallet", { wallet: id });
549
+ if (wallet.protocols.includes(view.protocol)) return;
550
+ const fallback = view.protocols.find((entry) => wallet.protocols.includes(entry.id));
551
+ if (fallback !== void 0) this.#emit("bchc-protocol", { protocol: fallback.id });
552
+ }
553
+ #captionWalletOf(view) {
554
+ return this._sheet ? null : selectedWallet(view)?.id ?? null;
555
+ }
556
+ #walletPrompt() {
557
+ return html`<p class="get-one"><button type="button" data-act="wallets">${this.#messages.noWallet}</button></p>`;
558
+ }
559
+ #logo(logo, size, extraClass) {
560
+ const trimmed = logo?.trim();
561
+ if (trimmed === void 0 || trimmed === "" || this._brokenLogos.has(trimmed)) {
562
+ const className = extraClass === "" ? "logo-fallback" : `logo-fallback ${extraClass}`;
563
+ return html`<span class=${className} aria-hidden="true">${icon("wallet")}</span>`;
564
+ }
565
+ return html`<img class=${extraClass === "" ? nothing : extraClass} src=${trimmed} alt="" width=${size} height=${size} @error=${this.#onLogoError} />`;
566
+ }
567
+ #onLogoError(event) {
568
+ const img = event.currentTarget;
569
+ const src = img instanceof HTMLImageElement ? img.getAttribute("src") : null;
570
+ if (src === null || this._brokenLogos.has(src)) return;
571
+ this._brokenLogos = /* @__PURE__ */ new Set([...this._brokenLogos, src]);
572
+ }
573
+ #tile(view) {
574
+ const tabbed = offeredProtocols(view, this._sheet).length > 1;
575
+ return html`
576
+ <div class="tile ${classMap({
577
+ "is-initiating": view.phase.kind === "initiating",
578
+ "is-failed": view.phase.kind === "failed"
579
+ })}" id="code-tile" role=${tabbed ? "tabpanel" : nothing} aria-labelledby=${tabbed ? `tab-${view.protocol}` : nothing}>
580
+ <span class="probe" aria-hidden="true"></span>
581
+ <span class="skeleton" aria-hidden="true"></span>
582
+ ${codeRenderer === null ? html`<div class="code"></div>` : this.#codeHost}
583
+ <div class="status" role="status">${statusFace(this.#messages, this._status)}</div>
584
+ </div>
585
+ `;
586
+ }
587
+ #footer(view) {
588
+ const messages = this.#messages;
589
+ const named = view.wallets.find((entry) => entry.id === this._captionWallet);
590
+ const wallet = named === void 0 ? null : {
591
+ name: named.name,
592
+ template: html`<span class="caption-wallet">${this.#logo(named.logo, 16, "caption-mark")}<span class="caption-name">${named.name}</span></span>`
593
+ };
594
+ return html`
595
+ <div class=${classMap({
596
+ footer: true,
597
+ "is-void": view.phase.kind === "failed"
598
+ })}>
599
+ <p class="caption">${caption(messages, this._caption, wallet)}</p>
600
+ <button class="button pill ${classMap({ "is-copied": this._copied })}" type="button" data-act="copy" .disabled=${view.phase.kind !== "awaiting-approval"}>${copyFace(messages, this._copied)}</button>
601
+ </div>
602
+ `;
603
+ }
604
+ #connectScreen(view, sheet) {
605
+ const failed = view.phase.kind === "failed";
606
+ const messages = this.#messages;
607
+ const link = view.phase.kind === "awaiting-approval" ? safeHref(view.phase.link) : null;
608
+ if (sheet) return html`
609
+ <div class="single">
610
+ ${this.#sessionType(view)}
611
+ ${this.#tile(view)}
612
+ ${this.#footer(view)}
613
+ <div class="primary-slot ${classMap({ "is-failed": failed })}">
614
+ <a class="button primary block" data-act="open" href=${link ?? nothing} aria-disabled=${link === null ? "true" : nothing}>${messages.openInWallet}</a>
615
+ <button class="button primary block retry" type="button" data-act="retry">${icon("rotateCcw", 15)}${messages.tryAgain}</button>
616
+ </div>
617
+ ${this.#walletPrompt()}
618
+ </div>
619
+ `;
620
+ const selectedId = selectedWallet(view)?.id;
621
+ return html`
622
+ <div class="split">
623
+ <p class="cell-label section-label" id="wallets-heading">${messages.walletsHeading}</p>
624
+ <div class="cell-control">${this.#sessionType(view)}</div>
625
+ <div class="cell-list">
626
+ <div class="wallets" role="radiogroup" aria-labelledby="wallets-heading">
627
+ ${repeat(view.wallets, (wallet) => wallet.id, (wallet) => {
628
+ const selected = wallet.id === selectedId;
629
+ const desktop = desktopLink(wallet, view);
630
+ const href = desktop === null ? null : selected ? desktop : "";
631
+ return html`
632
+ <div class="wallet-row ${classMap({ "has-open": href !== null && selected })}">
633
+ <button class="wallet" type="button" role="radio" aria-checked=${String(selected)} data-wallet=${wallet.id} .tabIndex=${live(selected ? 0 : -1)}>
634
+ ${this.#logo(wallet.logo, 32, "")}
635
+ <span class="wallet-name" title=${wallet.name}>${wallet.name}</span>
636
+ </button>
637
+ ${href === null ? nothing : html`<a class="open ${classMap({ "is-shown": selected })}" tabindex=${selected ? nothing : -1} aria-hidden=${selected ? nothing : "true"} href=${href === "" ? nothing : href} role=${href === "" ? "link" : nothing} aria-disabled=${href === "" ? "true" : nothing} target="_blank" rel="noreferrer" aria-label=${messages.openWalletLabel(wallet.name)}><span class="open-text">${messages.open}</span>${OPEN_ARROW}</a>`}
638
+ </div>`;
639
+ })}
640
+ </div>
641
+ </div>
642
+ <div class="cell-tile">${this.#tile(view)}</div>
643
+ <div class="cell-prompt">${this.#walletPrompt()}</div>
644
+ ${this.#footer(view)}
645
+ </div>
646
+ `;
647
+ }
648
+ async #copyLink() {
649
+ const view = this.view;
650
+ if (view === null || view.phase.kind !== "awaiting-approval") return;
651
+ try {
652
+ await navigator.clipboard.writeText(view.phase.link);
653
+ } catch {
654
+ return;
655
+ }
656
+ this.#setCopied(true);
657
+ if (this.#copyResetTimer !== null) clearTimeout(this.#copyResetTimer);
658
+ this.#copyResetTimer = setTimeout(() => this.#setCopied(false), COPIED_FOR);
659
+ }
660
+ #setCopied(copied) {
661
+ this._copied = copied;
662
+ }
663
+ #armDeepLinkHint() {
664
+ if (this._linkFailed) return;
665
+ if (this.#deepLinkTimer !== null) clearTimeout(this.#deepLinkTimer);
666
+ this.#deepLinkTimer = setTimeout(() => {
667
+ this.#deepLinkTimer = null;
668
+ if (document.visibilityState !== "visible") return;
669
+ this._linkFailed = true;
670
+ }, DEEP_LINK_GRACE);
671
+ }
672
+ #measureSheetRest(card, body) {
673
+ const tile = body.querySelector(".tile");
674
+ if (!(tile instanceof HTMLElement)) return;
675
+ const rest = Math.ceil(card.offsetHeight - body.clientHeight + (body.scrollHeight - tile.offsetHeight));
676
+ card.style.setProperty("--bchc-sheet-rest", `${rest}px`);
677
+ }
678
+ #paintCode(view) {
679
+ const renderer = codeRenderer;
680
+ if (renderer === null) return;
681
+ const initiating = view.phase.kind === "initiating";
682
+ if (view.phase.kind !== "awaiting-approval" && !initiating) return;
683
+ const probe = this.#codeHost.parentElement?.querySelector(".probe");
684
+ if (!(probe instanceof HTMLElement)) return;
685
+ const tileStyles = getComputedStyle(probe);
686
+ const protocol = view.protocols.find((entry) => entry.id === view.protocol);
687
+ renderCode(renderer, this.#codeHost, {
688
+ link: view.phase.kind === "awaiting-approval" ? view.phase.link : PLACEHOLDER_LINK,
689
+ mark: protocol?.mark ?? null,
690
+ foreground: tileStyles.color,
691
+ background: tileStyles.backgroundColor,
692
+ accent: tileStyles.accentColor,
693
+ placeholder: initiating
694
+ });
695
+ }
696
+ };
697
+ //#endregion
698
+ export { BchcModal, useCodeRenderer };
699
+
700
+ //# sourceMappingURL=modal.js.map