@aparte/core 1.2.0 → 1.4.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.
Files changed (43) hide show
  1. package/README.md +3 -2
  2. package/dist/components/chat/aparte-chat.d.ts +4 -0
  3. package/dist/components/chat/aparte-chat.d.ts.map +1 -1
  4. package/dist/components/composer/aparte-composer.d.ts +7 -0
  5. package/dist/components/composer/aparte-composer.d.ts.map +1 -1
  6. package/dist/components/composer/index.d.ts +1 -0
  7. package/dist/components/composer/index.d.ts.map +1 -1
  8. package/dist/components/composer/type-to-focus.d.ts +29 -0
  9. package/dist/components/composer/type-to-focus.d.ts.map +1 -0
  10. package/dist/components/index.d.ts +1 -1
  11. package/dist/components/index.d.ts.map +1 -1
  12. package/dist/components/launcher/aparte-launcher.d.ts +151 -13
  13. package/dist/components/launcher/aparte-launcher.d.ts.map +1 -1
  14. package/dist/config/icon-provider.d.ts +4 -0
  15. package/dist/config/icon-provider.d.ts.map +1 -1
  16. package/dist/config/locale.d.ts +7 -1
  17. package/dist/config/locale.d.ts.map +1 -1
  18. package/dist/config/sanitize.d.ts +1 -1
  19. package/dist/custom-elements.json +1059 -486
  20. package/dist/{defer-hydration-Oh04ScDm.js → defer-hydration-lK-7XQXf.js} +16 -2
  21. package/dist/defer-hydration-lK-7XQXf.js.map +1 -0
  22. package/dist/generated/element-attributes.d.ts +14 -0
  23. package/dist/generated/element-attributes.d.ts.map +1 -1
  24. package/dist/{glyphs-DFp-825O.js → glyphs-LolG04CC.js} +20 -14
  25. package/dist/glyphs-LolG04CC.js.map +1 -0
  26. package/dist/icons/glyphs.d.ts +6 -0
  27. package/dist/icons/glyphs.d.ts.map +1 -1
  28. package/dist/icons.js +17 -15
  29. package/dist/index.css +1 -1
  30. package/dist/index.d.ts +1 -1
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +373 -38
  33. package/dist/index.js.map +1 -1
  34. package/dist/index.node.d.ts +1 -1
  35. package/dist/index.node.d.ts.map +1 -1
  36. package/dist/index.node.js +1 -1
  37. package/dist/index.node.js.map +1 -1
  38. package/dist/interop/default-on-attributes.d.ts +17 -0
  39. package/dist/interop/default-on-attributes.d.ts.map +1 -0
  40. package/dist/interop/element-props.d.ts.map +1 -1
  41. package/package.json +2 -2
  42. package/dist/defer-hydration-Oh04ScDm.js.map +0 -1
  43. package/dist/glyphs-DFp-825O.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { K as deferHydrationOf, ae as resolveConfig, ap as eventOrigin, aq as activeElementIn, ak as subscribeConfigChange, ag as runWithConfig, o as AparteElicitationAbortError, w as buildApprovalPanel, x as buildElicitationPanel, ar as gateGlide, O as escapeAttr, H as cssEscape, ai as segmentTiming, as as chatBoundaryOf, at as relabelCodeLanguages, au as relabelCitations, av as toolCallRenderer, aw as toolRendererThatDrew, t as aparteGlobalConfig, ax as isMountedRenderer, ay as renderingVersionOf, az as onRenderersChange, aA as renderSourceList, aB as mergeSegmentUpdate, a0 as isSegmentSettled, aC as formatDuration, P as escapeHtml, aD as writeStreamedMarkdown, aE as replaceHighlighted, $ as isSafeUrl, C as copyText, V as getSegmentRenderer, X as installDefaultRenderersOnce, r as AparteMessageRepository, aF as CheckpointStateWatcher, aG as chatIdOf, aH as stampSegmentActivity, aI as stampSegmentOnInsert, aJ as stampSegmentOnUpdate, aK as renumberSegments, aL as openSegmentIds, aM as isTerminalStatus, aN as adoptMessageSegments, Z as isAwaitingReply, a6 as populateBubbleFromMessage, ao as uuid, af as revokeAttachmentUrls, a4 as parseMarkdownToSegments, aO as APARTE_DERIVED_SEGMENTS, aP as rememberConversationStatus, aQ as tellConversationStatus, aR as conversationStatusOf, e as APARTE_DEFAULT_LOCALE, a5 as placeFloating, aS as visibleReply, aT as usageTokens, q as AparteErrorCode, a1 as measureContext, aU as rootOf, aV as inRenderedContent, g as APARTE_HOST_ATTR, Y as installDialogTriggersOnce } from "./defer-hydration-Oh04ScDm.js";
2
- import { A, a, b, c, d, f, h, i, j, k, l, m, n, p, s, S, u, v, y, z, B, D, E, F, G, I, J, L, M, N, Q, R, T, U, W, _, a2, a3, a7, a8, a9, aa, ab, ac, ad, ah, aj, al, am, an } from "./defer-hydration-Oh04ScDm.js";
1
+ import { K as deferHydrationOf, ae as resolveConfig, ap as eventOrigin, aq as activeElementIn, ak as subscribeConfigChange, ag as runWithConfig, o as AparteElicitationAbortError, w as buildApprovalPanel, x as buildElicitationPanel, ar as gateGlide, O as escapeAttr, H as cssEscape, ai as segmentTiming, as as chatBoundaryOf, at as relabelCodeLanguages, au as relabelCitations, av as toolCallRenderer, aw as toolRendererThatDrew, t as aparteGlobalConfig, ax as isMountedRenderer, ay as renderingVersionOf, az as onRenderersChange, aA as renderSourceList, aB as mergeSegmentUpdate, a0 as isSegmentSettled, aC as formatDuration, P as escapeHtml, aD as writeStreamedMarkdown, aE as replaceHighlighted, $ as isSafeUrl, C as copyText, V as getSegmentRenderer, X as installDefaultRenderersOnce, r as AparteMessageRepository, aF as CheckpointStateWatcher, aG as chatIdOf, aH as stampSegmentActivity, aI as stampSegmentOnInsert, aJ as stampSegmentOnUpdate, aK as renumberSegments, aL as openSegmentIds, aM as isTerminalStatus, aN as adoptMessageSegments, Z as isAwaitingReply, a6 as populateBubbleFromMessage, ao as uuid, af as revokeAttachmentUrls, a4 as parseMarkdownToSegments, aO as APARTE_DERIVED_SEGMENTS, aP as rememberConversationStatus, aQ as tellConversationStatus, aR as conversationStatusOf, e as APARTE_DEFAULT_LOCALE, a5 as placeFloating, aS as visibleReply, aT as usageTokens, q as AparteErrorCode, a1 as measureContext, aU as rootOf, aV as inRenderedContent, g as APARTE_HOST_ATTR, Y as installDialogTriggersOnce } from "./defer-hydration-lK-7XQXf.js";
2
+ import { A, a, b, c, d, f, h, i, j, k, l, m, n, p, s, S, u, v, y, z, B, D, E, F, G, I, J, L, M, N, Q, R, T, U, W, _, a2, a3, a7, a8, a9, aa, ab, ac, ad, ah, aj, al, am, an } from "./defer-hydration-lK-7XQXf.js";
3
3
  function presenceOn(value) {
4
4
  return value === "" ? true : Boolean(value);
5
5
  }
@@ -915,14 +915,14 @@ class AparteElicitation extends HTMLElement {
915
915
  const m2 = request.message;
916
916
  return typeof m2 === "function" ? String(m2()) : String(m2 ?? "");
917
917
  };
918
- const CHAT = "aparte-chat, [data-aparte-chat], [data-aparte-host]";
918
+ const CHAT2 = "aparte-chat, [data-aparte-chat], [data-aparte-host]";
919
919
  const EDITABLE = 'input, textarea, select, [contenteditable]:not([contenteditable="false"])';
920
- const chatRoot = composer.closest(CHAT) ?? composer.parentElement ?? composer;
920
+ const chatRoot = composer.closest(CHAT2) ?? composer.parentElement ?? composer;
921
921
  const fromNobody = !(focusedBefore instanceof HTMLElement) || focusedBefore === document.body;
922
922
  const before = fromNobody ? null : focusedBefore;
923
923
  const inChat = before !== null && isWithin(before, chatRoot);
924
924
  const typing = before !== null && before.matches(EDITABLE);
925
- const busyElsewhere = before !== null && !inChat && (typing || before.tagName === "IFRAME" || before.closest(CHAT) !== null);
925
+ const busyElsewhere = before !== null && !inChat && (typing || before.tagName === "IFRAME" || before.closest(CHAT2) !== null);
926
926
  const status = (text) => {
927
927
  let region = composer.querySelector(":scope > .aparte-elic-status");
928
928
  if (!region) {
@@ -1113,7 +1113,7 @@ function isTranscriptEmpty(viewport) {
1113
1113
  }
1114
1114
  class AparteChat extends HTMLElement {
1115
1115
  static get observedAttributes() {
1116
- return ["placeholder", "disabled", "submit-on-enter", "attachments"];
1116
+ return ["placeholder", "disabled", "submit-on-enter", "attachments", "type-to-focus"];
1117
1117
  }
1118
1118
  _observer = null;
1119
1119
  _loadingObserver = null;
@@ -1135,6 +1135,7 @@ class AparteChat extends HTMLElement {
1135
1135
  this._forwardAttr("placeholder");
1136
1136
  this._forwardAttr("disabled");
1137
1137
  this._forwardAttr("submit-on-enter");
1138
+ this._forwardAttr("type-to-focus");
1138
1139
  this._syncEmptyWatch();
1139
1140
  this._syncLoadingWatch();
1140
1141
  this._watchForViewport();
@@ -1157,6 +1158,12 @@ class AparteChat extends HTMLElement {
1157
1158
  this._syncAttachments();
1158
1159
  return;
1159
1160
  }
1161
+ if (name === "type-to-focus") {
1162
+ const composer2 = this.querySelector("aparte-composer");
1163
+ if (newValue !== null) composer2?.setAttribute(name, newValue);
1164
+ else composer2?.removeAttribute(name);
1165
+ return;
1166
+ }
1160
1167
  if (name === "disabled" && newValue === null && this._gated) {
1161
1168
  this._gateOwns = true;
1162
1169
  return;
@@ -5374,6 +5381,70 @@ deferHydrationOf(AparteChatViewport);
5374
5381
  if (!customElements.get("aparte-chat-viewport")) {
5375
5382
  customElements.define("aparte-chat-viewport", AparteChatViewport);
5376
5383
  }
5384
+ const FIELDS = [
5385
+ "input",
5386
+ "textarea",
5387
+ "select",
5388
+ '[contenteditable]:not([contenteditable="false"])',
5389
+ '[role="textbox"]',
5390
+ '[role="searchbox"]',
5391
+ '[role="combobox"]',
5392
+ '[role="listbox"]',
5393
+ '[role="menu"]',
5394
+ '[role="menubar"]',
5395
+ '[role="tree"]',
5396
+ '[role="grid"]'
5397
+ ].join(", ");
5398
+ const CHAT = "aparte-chat, [data-aparte-chat]";
5399
+ function modeOf(composer) {
5400
+ const value = composer.getAttribute("type-to-focus");
5401
+ return value === "page" || value === "off" ? value : "chat";
5402
+ }
5403
+ function writes(event) {
5404
+ if (event.defaultPrevented || event.isComposing) return false;
5405
+ const altGr = event.getModifierState?.("AltGraph") === true;
5406
+ if ((event.ctrlKey || event.metaKey || event.altKey) && !altGr) return false;
5407
+ if (event.key === "Dead") return true;
5408
+ return event.key.length === 1 && event.key !== " ";
5409
+ }
5410
+ function focused(doc) {
5411
+ let active = doc.activeElement;
5412
+ while (active?.shadowRoot?.activeElement) active = active.shadowRoot.activeElement;
5413
+ return active;
5414
+ }
5415
+ function installTypeToFocus(composer) {
5416
+ const doc = composer.ownerDocument;
5417
+ let pointerInChat = false;
5418
+ const chat = () => {
5419
+ const target = composer.getAttribute("target");
5420
+ const root = composer.closest(CHAT) ?? (target ? doc.getElementById(target) : null);
5421
+ return root ? [root, composer] : [composer];
5422
+ };
5423
+ const onPointerDown = (event) => {
5424
+ const path = event.composedPath();
5425
+ pointerInChat = chat().some((el2) => path.includes(el2));
5426
+ };
5427
+ const onKeyDown2 = (event) => {
5428
+ const mode = modeOf(composer);
5429
+ if (mode === "off" || !writes(event) || composer.hasAttribute("disabled")) return;
5430
+ const input = composer.querySelector("aparte-composer-input");
5431
+ if (!input || input.hasAttribute("disabled")) return;
5432
+ const active = focused(doc);
5433
+ if (active?.closest(FIELDS)) return;
5434
+ if (mode === "chat") {
5435
+ const nowhere = !active || active === doc.body || active === doc.documentElement;
5436
+ const inChat = !nowhere && chat().some((el2) => el2.contains(active));
5437
+ if (!inChat && !(nowhere && pointerInChat)) return;
5438
+ }
5439
+ input.focusEnd?.();
5440
+ };
5441
+ doc.addEventListener("pointerdown", onPointerDown, true);
5442
+ doc.addEventListener("keydown", onKeyDown2);
5443
+ return () => {
5444
+ doc.removeEventListener("pointerdown", onPointerDown, true);
5445
+ doc.removeEventListener("keydown", onKeyDown2);
5446
+ };
5447
+ }
5377
5448
  const ONE_LINE_INPUTS = /* @__PURE__ */ new Set(["text", "search", "email", "url", "tel", "number", "password"]);
5378
5449
  function isOneLineField(target) {
5379
5450
  if (target instanceof HTMLTextAreaElement) return target.rows === 1;
@@ -5459,6 +5530,8 @@ class AparteComposer extends HTMLElement {
5459
5530
  static get observedAttributes() {
5460
5531
  return ["placeholder", "disabled", "target"];
5461
5532
  }
5533
+ /** Stops the document listeners of `type-to-focus`; null while disconnected. */
5534
+ _offTypeToFocus = null;
5462
5535
  connectedCallback() {
5463
5536
  window.addEventListener("aparte-message-start", this._onMessageStart);
5464
5537
  window.addEventListener("aparte-message-done", this._onMessageDone);
@@ -5468,6 +5541,8 @@ class AparteComposer extends HTMLElement {
5468
5541
  queueMicrotask(() => this._warnIfUnattached());
5469
5542
  this._evaluateModelGate();
5470
5543
  this._applyDirection();
5544
+ this._offTypeToFocus?.();
5545
+ this._offTypeToFocus = installTypeToFocus(this);
5471
5546
  }
5472
5547
  disconnectedCallback() {
5473
5548
  window.removeEventListener("aparte-message-start", this._onMessageStart);
@@ -5476,6 +5551,8 @@ class AparteComposer extends HTMLElement {
5476
5551
  window.removeEventListener("aparte-message-aborted", this._onMessageDone);
5477
5552
  window.removeEventListener("aparte-config-change", this._onConfigChangeEvent);
5478
5553
  this._listeners.clear();
5554
+ this._offTypeToFocus?.();
5555
+ this._offTypeToFocus = null;
5479
5556
  }
5480
5557
  attributeChangedCallback(name, _old, value) {
5481
5558
  if (name === "disabled") {
@@ -7159,8 +7236,8 @@ class AparteConversationList extends HTMLElement {
7159
7236
  }
7160
7237
  this._closeMenu();
7161
7238
  this._renaming = null;
7162
- const focused = activeElementIn(this);
7163
- const held = focused && this.contains(focused) ? focused.closest("[data-select-id], [data-more-id]") : null;
7239
+ const focused2 = activeElementIn(this);
7240
+ const held = focused2 && this.contains(focused2) ? focused2.closest("[data-select-id], [data-more-id]") : null;
7164
7241
  const restore = held?.dataset["selectId"] ? rowSelector("data-select-id", held.dataset["selectId"]) : held?.dataset["moreId"] ? rowSelector("data-more-id", held.dataset["moreId"]) : null;
7165
7242
  const heldRow = held?.closest("[data-conv-id]") ?? null;
7166
7243
  const visible = (el2) => !el2.closest("[hidden]");
@@ -9099,8 +9176,8 @@ class AparteScrollRail extends HTMLElement {
9099
9176
  _syncTabStop(ignoreFocus = false) {
9100
9177
  const ticks = Array.from(this.querySelectorAll(".aparte-scroll-rail__tick"));
9101
9178
  if (ticks.length === 0) return;
9102
- const focused = ignoreFocus ? null : activeElementIn(this);
9103
- const stop = (focused instanceof HTMLElement && ticks.includes(focused) ? focused : void 0) ?? ticks.find((t) => t.dataset["messageId"] === this._currentId) ?? ticks[ticks.length - 1];
9179
+ const focused2 = ignoreFocus ? null : activeElementIn(this);
9180
+ const stop = (focused2 instanceof HTMLElement && ticks.includes(focused2) ? focused2 : void 0) ?? ticks.find((t) => t.dataset["messageId"] === this._currentId) ?? ticks[ticks.length - 1];
9104
9181
  this._setTabStop(stop, ticks);
9105
9182
  }
9106
9183
  /**
@@ -10536,9 +10613,23 @@ const FOCUSABLE = [
10536
10613
  ].join(",");
10537
10614
  class AparteLauncher extends HTMLElement {
10538
10615
  static get observedAttributes() {
10539
- return ["open", "label", "breakpoint"];
10616
+ return ["open", "label", "breakpoint", "header", "heading", "subheading", "maximizable", "maximized"];
10540
10617
  }
10541
10618
  _button = null;
10619
+ /**
10620
+ * The element's header and the parts of it a render updates. Built once and updated in
10621
+ * place, never redrawn: the maximize button is what the person just pressed, and a new
10622
+ * node would take the focus out from under them.
10623
+ */
10624
+ _header = null;
10625
+ _avatar = null;
10626
+ /** The provider that drew the header's avatar, and what it returned to undo it. */
10627
+ _avatarDrawnBy = null;
10628
+ _avatarCleanup = null;
10629
+ _title = null;
10630
+ _subtitle = null;
10631
+ _maximize = null;
10632
+ _closeControl = null;
10542
10633
  /**
10543
10634
  * Watches the element's own children. The button is the one child that is not the host's,
10544
10635
  * so it is the one a framework reconciling the others knows nothing about: Vue's
@@ -10574,6 +10665,42 @@ class AparteLauncher extends HTMLElement {
10574
10665
  get sheet() {
10575
10666
  return this.hasAttribute("data-sheet");
10576
10667
  }
10668
+ /** The open chat is maximized: the whole window, or a panel along its edge with `maximizable="panel"`. */
10669
+ get maximized() {
10670
+ return this.hasAttribute("maximized");
10671
+ }
10672
+ set maximized(value) {
10673
+ this.toggleAttribute("maximized", presenceOn(value));
10674
+ }
10675
+ /**
10676
+ * The open chat covers the whole window: a sheet under the breakpoint, or maximized
10677
+ * without `maximizable="panel"`. What a page reads to take itself out of reach — the
10678
+ * launcher keeps Tab inside it meanwhile, and does not own the page.
10679
+ */
10680
+ get covers() {
10681
+ if (!this.open) return false;
10682
+ if (this.sheet) return true;
10683
+ if (!this.maximized) return false;
10684
+ if (!this._panel) return true;
10685
+ const width = this.getBoundingClientRect().width;
10686
+ return width > 0 && width >= document.documentElement.clientWidth - 1;
10687
+ }
10688
+ /** Maximized, the chat is a panel along the window's edge rather than the whole window. */
10689
+ get _panel() {
10690
+ return this.getAttribute("maximizable") === "panel";
10691
+ }
10692
+ /**
10693
+ * The element draws its header over a card that has none. A property as well as the
10694
+ * attribute because the attribute's OFF is a value, `"false"`, and a framework handed
10695
+ * `header={false}` removes a boolean attribute instead — which here means ON.
10696
+ */
10697
+ get header() {
10698
+ return this.getAttribute("header") !== "false";
10699
+ }
10700
+ set header(value) {
10701
+ if (value === false || value === "false") this.setAttribute("header", "false");
10702
+ else this.removeAttribute("header");
10703
+ }
10577
10704
  connectedCallback() {
10578
10705
  if (!this.classList.contains("aparte-launcher")) this.classList.add("aparte-launcher");
10579
10706
  if (!this._button) {
@@ -10582,14 +10709,16 @@ class AparteLauncher extends HTMLElement {
10582
10709
  this._button.type = "button";
10583
10710
  this._button.addEventListener("click", this._onButtonClick);
10584
10711
  }
10585
- for (const stale of this.querySelectorAll(":scope > .aparte-launcher__button")) {
10586
- if (stale !== this._button) stale.remove();
10712
+ for (const stale of this.querySelectorAll(":scope > .aparte-launcher__button, :scope > .aparte-launcher__header")) {
10713
+ if (stale !== this._button && stale !== this._header) stale.remove();
10587
10714
  }
10588
10715
  if (this._button.parentNode !== this) this.appendChild(this._button);
10589
10716
  if (typeof MutationObserver === "function") {
10590
- this._childWatch ??= new MutationObserver(() => this._keepButton());
10591
- this._childWatch.observe(this, { childList: true });
10592
- this._hostWatch ??= new MutationObserver(() => this._renderButton());
10717
+ this._childWatch ??= new MutationObserver((records) => {
10718
+ if (records.some((r) => r.target === this || AparteLauncher._touchesParts(r))) this._onChildren();
10719
+ });
10720
+ this._childWatch.observe(this, { childList: true, subtree: true });
10721
+ this._hostWatch ??= new MutationObserver(() => this._render());
10593
10722
  this._hostWatch.observe(this, { attributes: true, attributeFilter: [APARTE_HOST_ATTR], subtree: true });
10594
10723
  }
10595
10724
  this.addEventListener("click", this._onClick);
@@ -10597,7 +10726,7 @@ class AparteLauncher extends HTMLElement {
10597
10726
  window.addEventListener("aparte-config-change", this._onConfigChange);
10598
10727
  this._watchBreakpoint();
10599
10728
  this._lastOpen = this.open;
10600
- this._renderButton();
10729
+ this._render();
10601
10730
  this._ready = true;
10602
10731
  }
10603
10732
  disconnectedCallback() {
@@ -10608,15 +10737,24 @@ class AparteLauncher extends HTMLElement {
10608
10737
  this._hostWatch?.disconnect();
10609
10738
  this._media?.removeEventListener("change", this._onMediaChange);
10610
10739
  this._media = null;
10740
+ this._clearAvatar();
10611
10741
  this._ready = false;
10612
10742
  }
10613
- attributeChangedCallback(name) {
10743
+ attributeChangedCallback(name, previous, value) {
10614
10744
  if (!this._ready) return;
10615
10745
  if (name === "breakpoint") {
10616
10746
  this._watchBreakpoint();
10617
10747
  return;
10618
10748
  }
10619
- this._renderButton();
10749
+ this._render();
10750
+ if (name === "maximized") {
10751
+ if (previous === null !== (value === null)) this._announce();
10752
+ return;
10753
+ }
10754
+ if (name === "maximizable") {
10755
+ if (this.open && this.maximized && previous === "panel" !== (value === "panel")) this._announce();
10756
+ return;
10757
+ }
10620
10758
  if (name !== "open") return;
10621
10759
  const open = this.open;
10622
10760
  if (open === this._lastOpen) return;
@@ -10626,7 +10764,7 @@ class AparteLauncher extends HTMLElement {
10626
10764
  }
10627
10765
  _announce() {
10628
10766
  this.dispatchEvent(new CustomEvent("aparte-launcher-toggle", {
10629
- detail: { open: this.open, sheet: this.sheet },
10767
+ detail: { open: this.open, sheet: this.sheet, maximized: this.maximized, covers: this.covers },
10630
10768
  bubbles: true,
10631
10769
  composed: true
10632
10770
  }));
@@ -10644,6 +10782,17 @@ class AparteLauncher extends HTMLElement {
10644
10782
  toggle() {
10645
10783
  this.open = !this.open;
10646
10784
  }
10785
+ /**
10786
+ * Maximize the chat: the whole window, or a panel along its edge with
10787
+ * `maximizable="panel"`. Kept while the chat is closed, so it opens maximized.
10788
+ */
10789
+ maximize() {
10790
+ this.maximized = true;
10791
+ }
10792
+ /** Bring the chat back to its card. */
10793
+ restore() {
10794
+ this.maximized = false;
10795
+ }
10647
10796
  // ─── Button ───────────────────────────────────────────────────────────
10648
10797
  /**
10649
10798
  * The one opening that is a person's: the composer takes the focus, since they opened the
@@ -10657,7 +10806,24 @@ class AparteLauncher extends HTMLElement {
10657
10806
  this.toggle();
10658
10807
  if (opening) this._focusChat();
10659
10808
  };
10660
- _onConfigChange = () => this._renderButton();
10809
+ _onConfigChange = () => this._render();
10810
+ /** Everything the element draws, for the state it is in and the config it answers to. */
10811
+ _render() {
10812
+ const config = this._config();
10813
+ this._renderButton(config);
10814
+ this._renderHeader(config);
10815
+ this._syncMaximizeControls();
10816
+ }
10817
+ /**
10818
+ * The children changed: a framework reconciled them, hydration took out what it did not
10819
+ * render, a card arrived late. The button and the header go back if they were taken out,
10820
+ * and a card that now brings a header of its own is no longer given a second one.
10821
+ */
10822
+ _onChildren() {
10823
+ this._keepButton();
10824
+ this._renderHeader(this._config());
10825
+ this._syncMaximizeControls();
10826
+ }
10661
10827
  /**
10662
10828
  * The config the button answers to: the chat's own when the chat inside carries an
10663
10829
  * instance config (a wrapper's `config` prop, `attachConfig(chat, config)`), else the
@@ -10687,11 +10853,10 @@ class AparteLauncher extends HTMLElement {
10687
10853
  * contract: there is no `aria-controls`, since the one element it could name without
10688
10854
  * writing an id on the host's card is the launcher itself, the button's own ancestor.
10689
10855
  */
10690
- _renderButton() {
10856
+ _renderButton(config) {
10691
10857
  const button2 = this._button;
10692
10858
  if (!button2) return;
10693
10859
  this._keepButton();
10694
- const config = this._config();
10695
10860
  const locale = config.getLocale();
10696
10861
  const open = this.open;
10697
10862
  const label = open ? "" : (this.getAttribute("label") ?? "").trim();
@@ -10700,11 +10865,7 @@ class AparteLauncher extends HTMLElement {
10700
10865
  button2.setAttribute("aria-expanded", String(open));
10701
10866
  if (label) button2.removeAttribute("aria-label");
10702
10867
  else button2.setAttribute("aria-label", open ? locale.launcherClose ?? "Close the chat" : locale.launcherOpen ?? "Open the chat");
10703
- const icon = document.createElement("span");
10704
- icon.className = "aparte-launcher__icon";
10705
- icon.setAttribute("aria-hidden", "true");
10706
- icon.innerHTML = open ? config.getIcon("launcherClose") ?? config.getIcon("close") : config.getIcon("launcherOpen") ?? config.getIcon("messageSquare");
10707
- const parts = [icon];
10868
+ const parts = [AparteLauncher._glyph(open ? config.getIcon("launcherClose") ?? config.getIcon("close") : config.getIcon("launcherOpen") ?? config.getIcon("messageSquare"))];
10708
10869
  if (label) {
10709
10870
  const text = document.createElement("span");
10710
10871
  text.className = "aparte-launcher__label";
@@ -10713,6 +10874,158 @@ class AparteLauncher extends HTMLElement {
10713
10874
  }
10714
10875
  button2.replaceChildren(...parts);
10715
10876
  }
10877
+ /** A glyph, out of the accessibility tree: what holds it is named by its label. */
10878
+ static _glyph(svg) {
10879
+ const icon = document.createElement("span");
10880
+ icon.className = "aparte-launcher__icon";
10881
+ icon.setAttribute("aria-hidden", "true");
10882
+ icon.innerHTML = svg;
10883
+ return icon;
10884
+ }
10885
+ // ─── Header ───────────────────────────────────────────────────────────
10886
+ /**
10887
+ * A card that brings its own header: a `<header>` at its top level — how every card in
10888
+ * this repository with a header of its own is written, so they keep theirs and are not
10889
+ * given a second. A close control alone is not one: an "End chat" in a card's footer is a
10890
+ * close control too, and took the header away when it counted.
10891
+ */
10892
+ _cardHasHeader() {
10893
+ return Array.from(this.children).some((child) => child !== this._button && child !== this._header && child.querySelector(":scope > header") !== null);
10894
+ }
10895
+ /**
10896
+ * A change deeper in the card that the element answers: a header, or a maximize
10897
+ * control, arriving or leaving — a framework rendering a card's header after the card,
10898
+ * a conditional control. A streamed reply adds text and spans, so it calls for nothing.
10899
+ */
10900
+ static _touchesParts(record) {
10901
+ const parts = "header, [data-aparte-launcher-maximize]";
10902
+ return [...record.addedNodes, ...record.removedNodes].some((node) => node instanceof Element && (node.matches(parts) || node.querySelector(parts) !== null));
10903
+ }
10904
+ /** An icon-only control of the header: the recipe's ghost icon button. */
10905
+ static _headerButton(part) {
10906
+ const button2 = document.createElement("button");
10907
+ button2.type = "button";
10908
+ button2.className = `aparte-btn aparte-btn--icon aparte-btn--ghost aparte-launcher__${part}`;
10909
+ return button2;
10910
+ }
10911
+ /** A control's name, spoken and shown on hover: an icon alone says nothing to a pointer. */
10912
+ static _name(button2, name, svg) {
10913
+ button2.setAttribute("aria-label", name);
10914
+ button2.setAttribute("title", name);
10915
+ button2.replaceChildren(AparteLauncher._glyph(svg));
10916
+ }
10917
+ _buildHeader() {
10918
+ const header = document.createElement("div");
10919
+ header.className = "aparte-app-header aparte-launcher__header";
10920
+ this._avatar = document.createElement("span");
10921
+ this._avatar.className = "aparte-avatar aparte-launcher__avatar";
10922
+ this._avatar.setAttribute("data-role", "assistant");
10923
+ this._avatar.setAttribute("aria-hidden", "true");
10924
+ const heading = document.createElement("div");
10925
+ heading.className = "aparte-launcher__heading";
10926
+ this._title = document.createElement("span");
10927
+ this._title.className = "aparte-app-header__title aparte-launcher__title";
10928
+ this._title.setAttribute("role", "heading");
10929
+ this._title.setAttribute("aria-level", "2");
10930
+ this._subtitle = document.createElement("span");
10931
+ this._subtitle.className = "aparte-launcher__subheading";
10932
+ heading.append(this._title, this._subtitle);
10933
+ const actions = document.createElement("div");
10934
+ actions.className = "aparte-app-header__actions";
10935
+ this._maximize = AparteLauncher._headerButton("maximize");
10936
+ this._maximize.setAttribute("data-aparte-launcher-maximize", "");
10937
+ this._closeControl = AparteLauncher._headerButton("close");
10938
+ this._closeControl.setAttribute("data-aparte-launcher-close", "");
10939
+ actions.append(this._maximize, this._closeControl);
10940
+ header.append(this._avatar, heading, actions);
10941
+ return header;
10942
+ }
10943
+ /**
10944
+ * The header, when the element draws one: the title, the line under it, the maximize
10945
+ * button for its state, the ✕. It goes first among the children, and the stylesheet
10946
+ * joins it to the card under it.
10947
+ */
10948
+ _renderHeader(config) {
10949
+ if (!this.header || this._cardHasHeader()) {
10950
+ const hadFocus = !!this._header?.contains(activeElementIn(this));
10951
+ this._clearAvatar();
10952
+ this._header?.remove();
10953
+ if (hadFocus) (this._focusTarget() ?? this._button)?.focus();
10954
+ return;
10955
+ }
10956
+ const header = this._header ??= this._buildHeader();
10957
+ const locale = config.getLocale();
10958
+ const label = (this.getAttribute("label") ?? "").trim();
10959
+ const title = (this.getAttribute("heading") ?? (label || (locale.launcherHeading ?? "Chat"))).trim();
10960
+ this._title.textContent = title;
10961
+ this._title.hidden = !title;
10962
+ this._drawAvatar(config);
10963
+ const subtitle = (this.getAttribute("subheading") ?? "").trim();
10964
+ this._subtitle.textContent = subtitle;
10965
+ this._subtitle.hidden = !subtitle;
10966
+ const maximize = this._maximize;
10967
+ maximize.hidden = !this.hasAttribute("maximizable");
10968
+ AparteLauncher._name(
10969
+ maximize,
10970
+ this.maximized ? locale.launcherRestore ?? "Shrink the chat" : locale.launcherMaximize ?? "Expand the chat",
10971
+ config.getIcon(this.maximized ? "minimize" : "maximize")
10972
+ );
10973
+ AparteLauncher._name(this._closeControl, locale.launcherClose ?? "Close the chat", config.getIcon("close"));
10974
+ if (this.firstElementChild !== header) this.prepend(header);
10975
+ this._markAvatarImage();
10976
+ }
10977
+ /**
10978
+ * The assistant's avatar, by the provider that draws it in the messages — the one place an
10979
+ * app says what its assistant looks like, a framework component included. Drawn again only
10980
+ * when the provider changes, since a render runs on every attribute and a provider may
10981
+ * mount a component. Empty without one, and the stylesheet folds an empty slot away.
10982
+ */
10983
+ _drawAvatar(config) {
10984
+ const provider = config.getAvatarProvider();
10985
+ if (provider === this._avatarDrawnBy) return;
10986
+ this._clearAvatar();
10987
+ if (!provider || !this._avatar) return;
10988
+ this._avatarDrawnBy = provider;
10989
+ try {
10990
+ const cleanup = provider.render("assistant", this._avatar);
10991
+ if (typeof cleanup === "function") this._avatarCleanup = cleanup;
10992
+ } catch (error) {
10993
+ this._avatar.replaceChildren();
10994
+ console.error("[aparte] The avatar provider failed for the launcher's header, which shows no avatar. It is called with a host outside any message there.", error);
10995
+ }
10996
+ }
10997
+ /**
10998
+ * A picture from the stylesheet alone (`--aparte-avatar-image-assistant`), as a message's
10999
+ * slot shows it: an empty slot that has one is marked, which lifts the rule folding an empty
11000
+ * slot away. Read after the header is in the document, where its style resolves.
11001
+ */
11002
+ _markAvatarImage() {
11003
+ const slot = this._avatar;
11004
+ if (!slot) return;
11005
+ const image = typeof getComputedStyle === "function" ? getComputedStyle(slot).backgroundImage : "";
11006
+ slot.toggleAttribute("data-image", !slot.firstChild && !!image && image !== "none");
11007
+ }
11008
+ _clearAvatar() {
11009
+ try {
11010
+ this._avatarCleanup?.();
11011
+ } catch {
11012
+ }
11013
+ this._avatarCleanup = null;
11014
+ this._avatarDrawnBy = null;
11015
+ this._avatar?.replaceChildren();
11016
+ }
11017
+ /**
11018
+ * A maximize control of the host's says what it does: `aria-pressed` follows the state.
11019
+ * The element's own button is the exception, because it changes its NAME with the state
11020
+ * instead, and a toggle whose name changes must not also say it is pressed.
11021
+ */
11022
+ _syncMaximizeControls() {
11023
+ const pressed = String(this.maximized);
11024
+ for (const control of this.querySelectorAll("[data-aparte-launcher-maximize]")) {
11025
+ if (control === this._maximize || inRenderedContent(control)) continue;
11026
+ control.setAttribute("aria-pressed", pressed);
11027
+ }
11028
+ }
10716
11029
  // ─── Sheet ────────────────────────────────────────────────────────────
10717
11030
  _onMediaChange = (e) => this._applySheet(e.matches);
10718
11031
  /** The sheet's media query, from `breakpoint`: a length, or `none` for a card at any width. */
@@ -10737,6 +11050,10 @@ class AparteLauncher extends HTMLElement {
10737
11050
  _applySheet(sheet) {
10738
11051
  if (sheet === this.sheet) return;
10739
11052
  this.toggleAttribute("data-sheet", sheet);
11053
+ const active = activeElementIn(this);
11054
+ if (sheet && active?.matches("[data-aparte-launcher-maximize]") && this.contains(active)) {
11055
+ (this._closeControl?.isConnected ? this._closeControl : this._focusTarget())?.focus();
11056
+ }
10740
11057
  if (this._ready && this.open) this._announce();
10741
11058
  }
10742
11059
  // ─── Focus and keys ───────────────────────────────────────────────────
@@ -10747,17 +11064,34 @@ class AparteLauncher extends HTMLElement {
10747
11064
  /**
10748
11065
  * The composer when there is one — the person opened the chat to write — else the
10749
11066
  * first thing in the card that holds focus. The button itself never: it is what the
10750
- * person just pressed.
11067
+ * person just pressed. Nor the element's header, which comes first in the DOM: its ✕
11068
+ * is not what anyone opened the chat for, and focus landing there would read the way
11069
+ * out before the way in.
10751
11070
  */
10752
11071
  _focusChat() {
11072
+ this._focusTarget()?.focus();
11073
+ }
11074
+ /**
11075
+ * Where the focus goes in the chat: the composer, else the card's first control, else —
11076
+ * when the chat covers the window and its button is hidden — the header's ✕, the one way
11077
+ * out left on screen.
11078
+ */
11079
+ _focusTarget() {
10753
11080
  const composer = Array.from(this.querySelectorAll('[role="textbox"]:not([aria-disabled="true"])')).find((el2) => !inRenderedContent(el2) && !el2.closest("[hidden]") && el2.checkVisibility?.() !== false);
10754
- const target = composer ?? this._focusables().find((el2) => el2 !== this._button);
10755
- target?.focus();
11081
+ return composer ?? this._focusables().find((el2) => el2 !== this._button && !this._header?.contains(el2)) ?? (this.covers && this._closeControl?.isConnected ? this._closeControl : void 0);
10756
11082
  }
10757
- /** A `[data-aparte-launcher-close]` inside closes the chat — no script in your card. */
11083
+ /**
11084
+ * A `[data-aparte-launcher-close]` inside closes the chat, and a
11085
+ * `[data-aparte-launcher-maximize]` enlarges it or brings it back — no script in your card.
11086
+ */
10758
11087
  _onClick = (e) => {
10759
- const control = eventOrigin(e)?.closest?.("[data-aparte-launcher-close]");
10760
- if (control && this.contains(control) && !inRenderedContent(control)) this.close();
11088
+ const origin = eventOrigin(e);
11089
+ const placed = (selector) => {
11090
+ const control = origin?.closest?.(selector);
11091
+ return !!control && this.contains(control) && !inRenderedContent(control);
11092
+ };
11093
+ if (placed("[data-aparte-launcher-close]")) this.close();
11094
+ else if (placed("[data-aparte-launcher-maximize]")) this.maximized = !this.maximized;
10761
11095
  };
10762
11096
  /**
10763
11097
  * A modal dialog open inside the card, with the key pressed inside it. The browser
@@ -10780,10 +11114,11 @@ class AparteLauncher extends HTMLElement {
10780
11114
  if (e.key === "Escape") {
10781
11115
  if (e.defaultPrevented || e.isComposing || e.keyCode === 229 || this._inModalDialog(e)) return;
10782
11116
  e.preventDefault();
10783
- this.close();
11117
+ if (this.maximized && !this.sheet) this.maximized = false;
11118
+ else this.close();
10784
11119
  return;
10785
11120
  }
10786
- if (e.key !== "Tab" || !this.sheet) return;
11121
+ if (e.key !== "Tab" || !this.covers) return;
10787
11122
  const focusable = this._focusables();
10788
11123
  if (focusable.length === 0) return;
10789
11124
  const first = focusable[0];
@@ -10811,9 +11146,9 @@ function showHeldImage(link) {
10811
11146
  img.setAttribute("referrerpolicy", "no-referrer");
10812
11147
  img.setAttribute("alt", link.getAttribute("data-aparte-image-alt") ?? "");
10813
11148
  img.setAttribute("src", address);
10814
- const focused = link === document.activeElement;
11149
+ const focused2 = link === document.activeElement;
10815
11150
  link.replaceWith(img);
10816
- if (focused) {
11151
+ if (focused2) {
10817
11152
  img.tabIndex = -1;
10818
11153
  img.focus({ preventScroll: true });
10819
11154
  }