@meetelise/chat 1.59.3 → 1.60.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.
@@ -1500,6 +1500,22 @@ export class TourScheduler extends LitElement {
1500
1500
  `;
1501
1501
  }
1502
1502
 
1503
+ /**
1504
+ * Whether to ask "How did you hear about us?". Not on a broker referral: the broker's client gets the building's
1505
+ * broker source. Not rendered, the select isn't read on submit either (selectedLeadSource queries it), so a source
1506
+ * picked before the broker switch was turned on isn't sent.
1507
+ */
1508
+ showLeadSourceSelect(): boolean {
1509
+ return (
1510
+ !(this.brokerIntakeEnabled && this.isBrokerReferral) &&
1511
+ this.leadSources.length > 0 &&
1512
+ (this.featureFlagShowDropdown === FeatureFlagsShowDropdown.always ||
1513
+ (this.featureFlagShowDropdown ===
1514
+ FeatureFlagsShowDropdown.onAttributionFailure &&
1515
+ this.currentLeadSource.length === 0))
1516
+ );
1517
+ }
1518
+
1503
1519
  userInfoAndLayoutMenu(): TemplateResult {
1504
1520
  const isReferral = this.brokerIntakeEnabled && this.isBrokerReferral;
1505
1521
  return html`<h2 class="journey-header">Your Information</h2>
@@ -1639,11 +1655,7 @@ export class TourScheduler extends LitElement {
1639
1655
  >
1640
1656
  </me-select>`
1641
1657
  : ""}
1642
- ${this.leadSources.length > 0 &&
1643
- (this.featureFlagShowDropdown === FeatureFlagsShowDropdown.always ||
1644
- (this.featureFlagShowDropdown ===
1645
- FeatureFlagsShowDropdown.onAttributionFailure &&
1646
- this.currentLeadSource.length === 0))
1658
+ ${this.showLeadSourceSelect()
1647
1659
  ? html` <me-select
1648
1660
  id="leadSource"
1649
1661
  value="${this.currentLeadSource}"
@@ -566,6 +566,22 @@ export class EmailUsWindow extends LitElement {
566
566
  ].filter((v) => v).length;
567
567
  };
568
568
 
569
+ /**
570
+ * Whether to ask "How did you hear about us?". Not on a broker referral: the broker's client gets the building's
571
+ * broker source. Not rendered, the select isn't read on submit either (selectedLeadSource queries it), so a source
572
+ * picked before the broker switch was turned on isn't sent.
573
+ */
574
+ showLeadSourceSelect(): boolean {
575
+ return (
576
+ !(this.brokerIntakeEnabled && this.isBrokerReferral) &&
577
+ this.leadSources.length > 0 &&
578
+ (this.featureFlagShowDropdown === FeatureFlagsShowDropdown.always ||
579
+ (this.featureFlagShowDropdown ===
580
+ FeatureFlagsShowDropdown.onAttributionFailure &&
581
+ this.currentLeadSource.length === 0))
582
+ );
583
+ }
584
+
569
585
  renderBrokerToggle = (): TemplateResult => html`
570
586
  <label class="email-us__broker-toggle">
571
587
  <span>Are you a broker or locator?</span>
@@ -794,11 +810,7 @@ export class EmailUsWindow extends LitElement {
794
810
  ? html`${this.renderBrokerToggle()}
795
811
  <div class="email-us__vertical-spacer"></div>`
796
812
  : ""}
797
- ${this.leadSources.length > 0 &&
798
- (this.featureFlagShowDropdown === FeatureFlagsShowDropdown.always ||
799
- (this.featureFlagShowDropdown ===
800
- FeatureFlagsShowDropdown.onAttributionFailure &&
801
- this.currentLeadSource.length === 0))
813
+ ${this.showLeadSourceSelect()
802
814
  ? html`
803
815
  <me-select
804
816
  id="leadSource"
@@ -82,14 +82,26 @@ export const healthChatStyles = css`
82
82
  z-index: 100001;
83
83
  }
84
84
  #healthcare-inner-footer {
85
- display: flex;
85
+ display: grid;
86
+ grid-template-columns: minmax(0, 1fr) auto;
87
+ grid-template-areas:
88
+ "label ."
89
+ "field send";
86
90
  align-items: center;
87
- justify-content: space-between;
88
91
  box-sizing: border-box;
89
92
  padding: 16px;
90
- gap: 0px;
93
+ column-gap: 0px;
94
+ row-gap: 4px;
95
+ }
96
+ #healthcare-inner-footer .message-input-label {
97
+ grid-area: label;
98
+ color: black;
99
+ }
100
+ #healthcare-inner-footer > #healthcare-send-message-bttn {
101
+ grid-area: send;
91
102
  }
92
103
  #healthcare-inner-footer > #message-input {
104
+ grid-area: field;
93
105
  width: 100%;
94
106
  max-height: 100px;
95
107
  overflowy: auto;
@@ -83,14 +83,28 @@ export const leasingChatStyles = css`
83
83
  width: 100%;
84
84
  background: black;
85
85
 
86
- display: flex;
86
+ display: grid;
87
+ grid-template-columns: minmax(0, 1fr) auto;
88
+ grid-template-areas:
89
+ "label ."
90
+ "field send";
87
91
  align-items: center;
88
- justify-content: space-between;
92
+ column-gap: 16px;
93
+ row-gap: 4px;
89
94
  box-sizing: border-box;
90
- gap: 16px;
91
95
  padding: 24px;
92
96
  z-index: 100001;
93
97
  }
98
+ #footer .message-input-label {
99
+ grid-area: label;
100
+ color: white;
101
+ }
102
+ #footer #message-input {
103
+ grid-area: field;
104
+ }
105
+ #footer #send-message-bttn {
106
+ grid-area: send;
107
+ }
94
108
  #message-input {
95
109
  width: 100%;
96
110
  height: auto;
@@ -119,4 +133,8 @@ export const leasingChatStyles = css`
119
133
  border: none;
120
134
  cursor: pointer;
121
135
  }
136
+ #send-message-bttn[aria-disabled="true"] {
137
+ cursor: default;
138
+ opacity: 0.45;
139
+ }
122
140
  `;
@@ -202,6 +202,14 @@ export const pubnubChatStyles = css`
202
202
  opacity: 0.5;
203
203
  }
204
204
 
205
+ .message-input-label {
206
+ font-size: 12px;
207
+ font-weight: 400;
208
+ line-height: 1.4;
209
+ letter-spacing: 0em;
210
+ text-align: left;
211
+ }
212
+
205
213
  .sr-only {
206
214
  position: absolute;
207
215
  width: 1px;
@@ -40,6 +40,11 @@ import {
40
40
  getConsentPreChatDisclaimer,
41
41
  } from "../disclaimers";
42
42
  import { BuildingWebchatView } from "../fetchBuildingWebchatView";
43
+ import {
44
+ hideHostSiblings,
45
+ restoreHostSiblings,
46
+ widgetLightHost,
47
+ } from "../hostPageModal";
43
48
 
44
49
  @customElement("pubnub-chat")
45
50
  export class PubnubChat extends LitElement {
@@ -136,6 +141,18 @@ export class PubnubChat extends LitElement {
136
141
 
137
142
  private hasReplySnapshot = false;
138
143
 
144
+ /** True from Send until the reply paint settles, so focus can be repaired. */
145
+ private holdMessageFieldFocus = false;
146
+
147
+ /** True until initializePubnub and publish settle. Streaming is still false then. */
148
+ @state()
149
+ private sendInFlight = false;
150
+
151
+ private releaseFocusHoldQueued = false;
152
+
153
+ /** Widget element whose siblings were hidden for the phone dialog. */
154
+ private hostPageLock: Element | null = null;
155
+
139
156
  @state()
140
157
  privacyPolicyUrl = "http://bit.ly/me_privacy_policy";
141
158
 
@@ -163,6 +180,7 @@ export class PubnubChat extends LitElement {
163
180
 
164
181
  disconnectedCallback(): void {
165
182
  this._cleanupMobileListener?.();
183
+ this.releaseHostPage();
166
184
  super.disconnectedCallback();
167
185
  }
168
186
 
@@ -170,24 +188,44 @@ export class PubnubChat extends LitElement {
170
188
  if (message.length === 0) {
171
189
  return;
172
190
  }
173
- this.shouldAutoScroll = true;
174
- this.messageInput.value = "";
175
- this.autoResizeMessageInput();
176
-
177
- // The first message triggers a channel creation which can take a few seconds,
178
- // so we immediately add the message to the UI to avoid a delay.
179
- if (this.messages.length === 0) {
180
- this.messages = [
181
- {
182
- timestamp: 0,
183
- message,
184
- isLeadMessage: true,
185
- chunks: [{ text: message, order: 0, isDone: true }],
186
- type: SimpleMessageTypes.text,
187
- },
188
- ];
191
+ // isCurrentlyStreamingMessage stays false during channel creation and
192
+ // publish. Hold this gate for the whole call so Enter and Send cannot
193
+ // start a second sendMessage while initializePubnub is still running.
194
+ if (this.sendInFlight || this.myPubnub?.isCurrentlyStreamingMessage) {
195
+ if (this.holdMessageFieldFocus) this.focusMessageField();
196
+ return;
197
+ }
198
+ this.sendInFlight = true;
199
+ try {
200
+ this.shouldAutoScroll = true;
201
+ // Focus the field before the reply render. If Send is focused when it
202
+ // becomes unavailable, the browser moves focus to the document and
203
+ // iOS VoiceOver reads the host page behind the chat.
204
+ this.holdMessageFieldFocus = true;
205
+ this.focusMessageField();
206
+ this.messageInput.value = "";
207
+ this.autoResizeMessageInput();
208
+
209
+ // The first message triggers a channel creation which can take a few seconds,
210
+ // so we immediately add the message to the UI to avoid a delay.
211
+ if (this.messages.length === 0) {
212
+ this.messages = [
213
+ {
214
+ timestamp: 0,
215
+ message,
216
+ isLeadMessage: true,
217
+ chunks: [{ text: message, order: 0, isDone: true }],
218
+ type: SimpleMessageTypes.text,
219
+ },
220
+ ];
221
+ }
222
+ await this.myPubnub?.sendMessage(message);
223
+ } finally {
224
+ this.sendInFlight = false;
225
+ this.focusMessageField();
226
+ await this.updateComplete;
227
+ this.restoreMessageFieldFocus();
189
228
  }
190
- await this.myPubnub?.sendMessage(message);
191
229
  };
192
230
 
193
231
  firstUpdated(): void {
@@ -282,14 +320,73 @@ export class PubnubChat extends LitElement {
282
320
 
283
321
  async updated(): Promise<void> {
284
322
  this.scrollToChatBottom();
285
- try {
286
- const modal = document.getElementById("#healthchat-container");
287
- if (!modal) return;
288
- modal.focus();
289
- } catch (e) {
290
- // eslint-disable-next-line no-console
291
- console.error(e);
323
+ // Do not focus a container here. An earlier call looked up
324
+ // "#healthchat-container" on document (the hash is not part of the id,
325
+ // and the node lives in this shadow root), so it never matched. Focusing
326
+ // the dialog on every reply render would pull the caret out of the field.
327
+ this.syncHostPage();
328
+ this.restoreMessageFieldFocus();
329
+ this.scheduleFocusHoldRelease();
330
+ }
331
+
332
+ /** Phone layout only. Desktop must leave the host page alone. */
333
+ private syncHostPage(): void {
334
+ if (!this._isMobile) {
335
+ this.releaseHostPage();
336
+ return;
337
+ }
338
+ const host = widgetLightHost(this);
339
+ if (!host?.parentElement) return;
340
+ this.hostPageLock = host;
341
+ hideHostSiblings(host);
342
+ }
343
+
344
+ private releaseHostPage(): void {
345
+ if (!this.hostPageLock) return;
346
+ restoreHostSiblings(this.hostPageLock);
347
+ this.hostPageLock = null;
348
+ }
349
+
350
+ /**
351
+ * If Send or a dropped focus (document / body) is active during a send,
352
+ * put the caret back in the field. A control the user moved to inside
353
+ * the dialog, or a desktop page control they clicked, is left alone.
354
+ */
355
+ private restoreMessageFieldFocus(): void {
356
+ if (!this.holdMessageFieldFocus) return;
357
+ const active = deepestActive();
358
+ if (active instanceof HTMLElement && active.id === "message-input") return;
359
+ const lost =
360
+ !active ||
361
+ active === document.body ||
362
+ active === document.documentElement;
363
+ const onSend =
364
+ active instanceof HTMLElement && active.id === "send-message-bttn";
365
+ if (lost || onSend) this.focusMessageField();
366
+ }
367
+
368
+ private scheduleFocusHoldRelease(): void {
369
+ if (!this.holdMessageFieldFocus || this.releaseFocusHoldQueued) return;
370
+ if (
371
+ this.sendInFlight ||
372
+ this.isLoadingMessages ||
373
+ this.myPubnub?.isCurrentlyStreamingMessage
374
+ ) {
375
+ return;
292
376
  }
377
+ this.releaseFocusHoldQueued = true;
378
+ requestAnimationFrame(() => {
379
+ this.releaseFocusHoldQueued = false;
380
+ if (
381
+ this.sendInFlight ||
382
+ this.isLoadingMessages ||
383
+ this.myPubnub?.isCurrentlyStreamingMessage
384
+ ) {
385
+ return;
386
+ }
387
+ this.restoreMessageFieldFocus();
388
+ this.holdMessageFieldFocus = false;
389
+ });
293
390
  }
294
391
 
295
392
  autoResizeMessageInput = (): void => {
@@ -301,6 +398,17 @@ export class PubnubChat extends LitElement {
301
398
  return this.requiresConsent && !this.hasReceivedConsent;
302
399
  };
303
400
 
401
+ /** Visible caption for the message field. Stays on screen while typing. */
402
+ private renderMessageFieldLabel(color?: string): TemplateResult {
403
+ const label = "Message";
404
+ return html`<label
405
+ class="message-input-label"
406
+ for="message-input"
407
+ style=${styleMap({ color })}
408
+ >${label}</label
409
+ >`;
410
+ }
411
+
304
412
  /** Caret in the message field when the composer is on screen. */
305
413
  focusMessageField(): boolean {
306
414
  const root = this.shadowRoot;
@@ -308,7 +416,7 @@ export class PubnubChat extends LitElement {
308
416
  if (!(input instanceof HTMLTextAreaElement) || !root) return false;
309
417
  // Consent hides the footer with display:none. Focusing it would no-op.
310
418
  if (input.getClientRects().length === 0) return false;
311
- if (root.activeElement !== input) input.focus();
419
+ if (root.activeElement !== input) input.focus({ preventScroll: true });
312
420
  return root.activeElement === input;
313
421
  }
314
422
 
@@ -505,9 +613,9 @@ export class PubnubChat extends LitElement {
505
613
  })}
506
614
  >
507
615
  <div id="utilities-inner-footer">
616
+ ${this.renderMessageFieldLabel()}
508
617
  <textarea
509
618
  id="message-input"
510
- aria-label="Message"
511
619
  class=${classMap({
512
620
  ["webchat-font__desktop"]: !this._isMobile,
513
621
  ["webchat-font__mobile"]: this._isMobile,
@@ -682,9 +790,9 @@ export class PubnubChat extends LitElement {
682
790
  })}
683
791
  >
684
792
  <div id="healthcare-inner-footer">
793
+ ${this.renderMessageFieldLabel()}
685
794
  <textarea
686
795
  id="message-input"
687
- aria-label="Message"
688
796
  class=${classMap({
689
797
  ["webchat-font__desktop"]: !this._isMobile,
690
798
  ["webchat-font__mobile"]: this._isMobile,
@@ -900,9 +1008,16 @@ export class PubnubChat extends LitElement {
900
1008
  : undefined,
901
1009
  })}
902
1010
  >
1011
+ ${this.renderMessageFieldLabel(
1012
+ shouldUsePrimaryColorForChatFooter(
1013
+ this.building.orgId,
1014
+ this.building.id
1015
+ ) && this.primaryColor !== defaultPrimaryColor
1016
+ ? "black"
1017
+ : undefined
1018
+ )}
903
1019
  <textarea
904
1020
  id="message-input"
905
- aria-label="Message"
906
1021
  class=${classMap({
907
1022
  ["webchat-font__desktop"]: !this._isMobile,
908
1023
  ["webchat-font__mobile"]: this._isMobile,
@@ -918,13 +1033,16 @@ export class PubnubChat extends LitElement {
918
1033
  })}
919
1034
  placeholder="Ask a question..."
920
1035
  @keydown=${(e: KeyboardEvent) => {
921
- if (
922
- e.key === "Enter" &&
923
- !this.myPubnub?.isCurrentlyStreamingMessage
924
- ) {
1036
+ if (e.key !== "Enter") return;
1037
+ // Swallow the key while a send is in flight so it cannot insert
1038
+ // a newline or start another publish. Streaming already ignores Enter.
1039
+ if (this.sendInFlight) {
925
1040
  e.preventDefault();
926
- this.sendMessage(this.messageInput.value);
1041
+ return;
927
1042
  }
1043
+ if (this.myPubnub?.isCurrentlyStreamingMessage) return;
1044
+ e.preventDefault();
1045
+ this.sendMessage(this.messageInput.value);
928
1046
  }}
929
1047
  @input=${() => this.autoResizeMessageInput()}
930
1048
  rows="1"
@@ -934,7 +1052,10 @@ export class PubnubChat extends LitElement {
934
1052
  tabindex="0"
935
1053
  id="send-message-bttn"
936
1054
  @click=${() => this.sendMessage(this.messageInput.value)}
937
- ?disabled=${this.myPubnub?.isCurrentlyStreamingMessage}
1055
+ aria-disabled=${this.sendInFlight ||
1056
+ this.myPubnub?.isCurrentlyStreamingMessage
1057
+ ? "true"
1058
+ : "false"}
938
1059
  aria-label="Send message"
939
1060
  >
940
1061
  ${shouldUsePrimaryColorForChatFooter(
@@ -986,6 +1107,14 @@ export function trapTabInside(
986
1107
  }
987
1108
  }
988
1109
 
1110
+ function deepestActive(): Element | null {
1111
+ let active: Element | null = document.activeElement;
1112
+ while (active?.shadowRoot?.activeElement) {
1113
+ active = active.shadowRoot.activeElement;
1114
+ }
1115
+ return active;
1116
+ }
1117
+
989
1118
  function activeElementInside(container: HTMLElement): HTMLElement | null {
990
1119
  let active: Element | null = document.activeElement;
991
1120
  while (active?.shadowRoot?.activeElement) {
@@ -72,14 +72,26 @@ export const utiltiesChatStyles = css`
72
72
  z-index: 100001;
73
73
  }
74
74
  #utilities-inner-footer {
75
- display: flex;
75
+ display: grid;
76
+ grid-template-columns: minmax(0, 1fr) auto;
77
+ grid-template-areas:
78
+ "label ."
79
+ "field send";
76
80
  align-items: center;
77
- justify-content: space-between;
78
81
  box-sizing: border-box;
79
82
  padding: 16px;
80
- gap: 0px;
83
+ column-gap: 0px;
84
+ row-gap: 4px;
85
+ }
86
+ #utilities-inner-footer .message-input-label {
87
+ grid-area: label;
88
+ color: black;
89
+ }
90
+ #utilities-inner-footer > #utilities-send-message-bttn {
91
+ grid-area: send;
81
92
  }
82
93
  #utilities-inner-footer > #message-input {
94
+ grid-area: field;
83
95
  width: 100%;
84
96
  max-height: 100px;
85
97
  overflowy: auto;
@@ -568,3 +568,69 @@ it("moves what a broker typed on the tour form into their own details when they
568
568
  ]);
569
569
  expect(tour.brokerEmail).to.equal("");
570
570
  });
571
+
572
+ const leadSourceFields = {
573
+ leadSources: ["Zillow", "Apartments.com"],
574
+ featureFlagShowDropdown: "always",
575
+ };
576
+
577
+ const leadSourceSelect = async (emailUs: EmailUsWindow) => {
578
+ await emailUs.updateComplete;
579
+ return emailUs.shadowRoot?.querySelector("me-select#leadSource") ?? null;
580
+ };
581
+
582
+ it("doesn't ask a broker how they heard about us; anyone else is still asked", async () => {
583
+ const emailUs = createForm({
584
+ brokerIntakeEnabled: true,
585
+ ...leadSourceFields,
586
+ });
587
+ document.body.appendChild(emailUs);
588
+
589
+ expect(await leadSourceSelect(emailUs)).not.to.equal(null);
590
+
591
+ emailUs.isBrokerReferral = true;
592
+ expect(await leadSourceSelect(emailUs)).to.equal(null);
593
+
594
+ // Broker intake off: the switch is ignored, so the question stays.
595
+ emailUs.brokerIntakeEnabled = false;
596
+ expect(await leadSourceSelect(emailUs)).not.to.equal(null);
597
+ emailUs.remove();
598
+ });
599
+
600
+ it("doesn't send a source picked before the broker switch was turned on", async () => {
601
+ const requests = captureRequests();
602
+ const emailUs = createForm({
603
+ brokerIntakeEnabled: true,
604
+ ...leadSourceFields,
605
+ });
606
+ document.body.appendChild(emailUs);
607
+ const select = (await leadSourceSelect(emailUs)) as HTMLElement & {
608
+ value?: string;
609
+ };
610
+ select.value = "Zillow";
611
+
612
+ Object.assign(emailUs, { isBrokerReferral: true, ...brokerFields });
613
+ await emailUs.updateComplete;
614
+ const submission = emailUs.onClick();
615
+ // Answer the request before asserting, so a failure can't leave the submission waiting.
616
+ requests[0]?.respond(200, { "Content-Type": "application/json" }, "{}");
617
+ await submission;
618
+
619
+ expect(requests).to.have.length(1);
620
+ const body = JSON.parse(requests[0].requestBody ?? "{}");
621
+ expect(JSON.stringify(body.lead_sources)).not.to.contain("Zillow");
622
+ emailUs.remove();
623
+ });
624
+
625
+ it("doesn't ask on the tour form either when a broker is referring a client", () => {
626
+ const asked = (fields: Record<string, unknown>) =>
627
+ createTour({ ...leadSourceFields, ...fields }).showLeadSourceSelect();
628
+
629
+ expect(asked({ brokerIntakeEnabled: true })).to.equal(true);
630
+ expect(asked({ brokerIntakeEnabled: true, isBrokerReferral: true })).to.equal(
631
+ false
632
+ );
633
+ expect(
634
+ asked({ brokerIntakeEnabled: false, isBrokerReferral: true })
635
+ ).to.equal(true);
636
+ });