@ordergroove/offers 2.45.6 → 2.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordergroove/offers",
3
- "version": "2.45.6",
3
+ "version": "2.46.0",
4
4
  "description": "offer state component",
5
5
  "author": "Eugenio Lattanzio <eugenio63@gmail.com>",
6
6
  "homepage": "https://github.com/ordergroove/plush-toys#readme",
@@ -46,8 +46,8 @@
46
46
  "throttle-debounce": "^2.1.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@ordergroove/offers-templates": "^0.9.8",
49
+ "@ordergroove/offers-templates": "^0.10.0",
50
50
  "@types/lodash.memoize": "^4.1.9"
51
51
  },
52
- "gitHead": "60f759b8f2452b4569e6e5d0f6c8e06c96581d8e"
52
+ "gitHead": "29fe0f44f5d9dc13b9c8a150b894b5a99c31cd8d"
53
53
  }
@@ -1,9 +1,32 @@
1
1
  import { LitElement, html, css } from 'lit-element';
2
+ import { ifDefined } from 'lit-html/directives/if-defined.js';
3
+
4
+ const ACTIVATION_TYPES = {
5
+ AUTOMATIC: 'automatic',
6
+ MANUAL: 'manual'
7
+ };
2
8
 
3
9
  export class Tooltip extends LitElement {
10
+ constructor() {
11
+ super();
12
+ this.triggerLabel = 'Show tooltip';
13
+ this.open = false;
14
+ /** Default is "automatic" for backwards compatibility with existing templates */
15
+ this.activationType = ACTIVATION_TYPES.AUTOMATIC;
16
+ }
17
+
4
18
  static get properties() {
5
19
  return {
6
- placement: { type: String, default: 'bottom' }
20
+ placement: { type: String, default: 'bottom' },
21
+ /** Set the aria-label attribute of the trigger. */
22
+ triggerLabel: { type: String, attribute: 'trigger-label' },
23
+ /**
24
+ * "automatic" - show tooltip on hover and focus
25
+ * "manual" - show tooltip on hover and click
26
+ */
27
+ activationType: { type: String, attribute: 'activation-type' },
28
+ /** Whether the tooltip is showing. Internal property; only here so that we re-render when it changes */
29
+ open: { type: Boolean, attribute: false }
7
30
  };
8
31
  }
9
32
 
@@ -19,11 +42,27 @@ export class Tooltip extends LitElement {
19
42
  z-index: 9;
20
43
  }
21
44
 
45
+ /* reset default button styles */
46
+ button.trigger {
47
+ all: unset;
48
+ }
49
+
50
+ /* do not reset the button's default focus outline */
51
+ button.trigger:focus {
52
+ outline: revert;
53
+ }
54
+
22
55
  .trigger {
23
56
  display: block;
24
57
  cursor: pointer;
25
58
  }
26
59
 
60
+ /* for manual activation, hide the content completely from screen readers when the tooltip is closed */
61
+ /* otherwise, interactive elements may receive focus even when they are not visible */
62
+ [data-manual] .content {
63
+ visibility: hidden;
64
+ }
65
+
27
66
  .content {
28
67
  box-sizing: border-box;
29
68
  font-family: var(--og-tooltip-family, inherit);
@@ -152,9 +191,8 @@ export class Tooltip extends LitElement {
152
191
  border-left: solid var(--og-tooltip-background, #ececec) 10px;
153
192
  }
154
193
 
155
- .tooltip:hover .content,
156
- .trigger:focus + .content,
157
- .content:focus-within {
194
+ .tooltip[data-open] .content {
195
+ visibility: visible;
158
196
  opacity: 1;
159
197
  width: 200px;
160
198
  pointer-events: auto;
@@ -165,12 +203,22 @@ export class Tooltip extends LitElement {
165
203
 
166
204
  connectedCallback() {
167
205
  super.connectedCallback();
168
- this.recalculatePosition = this.recalculatePosition.bind(this);
169
- this.addEventListener('mouseenter', this.recalculatePosition);
170
- this.addEventListener('focusin', this.recalculatePosition);
206
+ this.abortController = new AbortController();
207
+ const signal = this.abortController.signal;
208
+
209
+ this.addEventListener('mouseenter', this.handleMouseEnter.bind(this), { signal });
210
+ this.addEventListener('mouseleave', this.handleMouseLeave.bind(this), { signal });
211
+ this.addEventListener('focusin', this.handleFocusIn.bind(this), { signal });
212
+ this.addEventListener('focusout', this.handleFocusOut.bind(this), { signal });
213
+ this.addEventListener('keydown', this.handleKeyDown.bind(this), { signal });
214
+
215
+ document.addEventListener('click', this.handleDocumentClick.bind(this), { signal });
171
216
  }
172
217
 
173
- recalculatePosition() {
218
+ async recalculatePosition() {
219
+ // wait for state changes to apply
220
+ await this.updateComplete;
221
+ if (!this.open) return;
174
222
  const trigger = this.shadowRoot.querySelector('.trigger');
175
223
  const triggerRect = trigger.getBoundingClientRect();
176
224
  const content = this.shadowRoot.querySelector('.content');
@@ -181,19 +229,83 @@ export class Tooltip extends LitElement {
181
229
  content.style.top = `${(-1 * contentRect.height + triggerRect.height) / 2}px`;
182
230
  }
183
231
 
232
+ handleMouseEnter() {
233
+ this.open = true;
234
+ this.recalculatePosition();
235
+ }
236
+
237
+ handleMouseLeave() {
238
+ this.open = false;
239
+ }
240
+
241
+ handleFocusIn() {
242
+ if (this.activationType !== ACTIVATION_TYPES.AUTOMATIC) return;
243
+ this.open = true;
244
+ this.recalculatePosition();
245
+ }
246
+
247
+ handleFocusOut(event) {
248
+ if (this.activationType !== ACTIVATION_TYPES.AUTOMATIC) return;
249
+ // keep the tooltip open if we're moving focus to another element inside the tooltip
250
+ if (!this.contains(event.relatedTarget)) {
251
+ this.open = false;
252
+ }
253
+ }
254
+
255
+ handleKeyDown(event) {
256
+ if (this.activationType !== ACTIVATION_TYPES.MANUAL) return;
257
+ // close the tooltip on Escape press
258
+ if (event.key === 'Escape' && this.open) {
259
+ this.open = false;
260
+ event.stopPropagation();
261
+ }
262
+ }
263
+
264
+ handleClick() {
265
+ if (this.activationType !== ACTIVATION_TYPES.MANUAL) return;
266
+ this.open = !this.open;
267
+ this.recalculatePosition();
268
+ }
269
+
270
+ handleDocumentClick(event) {
271
+ if (this.activationType !== ACTIVATION_TYPES.MANUAL || !this.open) return;
272
+ // close the tooltip if the user clicks outside of it
273
+ if (!this.contains(event.target)) {
274
+ this.open = false;
275
+ }
276
+ }
277
+
184
278
  disconnectedCallback() {
185
279
  super.disconnectedCallback();
186
- this.removeEventListener('mouseenter', this.recalculatePosition);
187
- this.removeEventListener('focusin', this.recalculatePosition);
280
+ // remove event listeners
281
+ this.abortController.abort();
188
282
  }
189
283
 
190
284
  render() {
285
+ // allow removing aria-label by setting trigger-label to any falsy value
286
+ // e.g. if the content inside the tooltip is sufficient
287
+ const triggerLabel = this.triggerLabel ? this.triggerLabel : undefined;
288
+
191
289
  return html`
192
- <span class="tooltip">
193
- <span class="trigger" tabindex="0">
194
- <slot name="trigger">${this.trigger}</slot>
195
- </span>
196
- <div class="content ${this.placement || 'bottom'}">
290
+ <span class="tooltip" ?data-open="${this.open}" ?data-manual="${this.activationType === ACTIVATION_TYPES.MANUAL}">
291
+ ${this.activationType === ACTIVATION_TYPES.MANUAL
292
+ ? html`
293
+ <button
294
+ class="trigger"
295
+ aria-label="${ifDefined(triggerLabel)}"
296
+ aria-expanded="${this.open}"
297
+ aria-controls="tooltip-content"
298
+ @click="${this.handleClick}"
299
+ >
300
+ <slot name="trigger">${this.trigger}</slot>
301
+ </button>
302
+ `
303
+ : html`
304
+ <span class="trigger" tabindex="0" aria-label="${ifDefined(triggerLabel)}">
305
+ <slot name="trigger">${this.trigger}</slot>
306
+ </span>
307
+ `}
308
+ <div class="content ${this.placement || 'bottom'}" role="tooltip" id="tooltip-content">
197
309
  <slot name="content">${this.content}</slot>
198
310
  </div>
199
311
  </span>
@@ -16,17 +16,20 @@ describe('Tooltip', () => {
16
16
  );
17
17
  });
18
18
 
19
- const getTooltip = async placement => {
19
+ const getTooltip = async ({ placement, activationType } = {}) => {
20
20
  const element = new Tooltip();
21
21
  if (placement) {
22
22
  element.setAttribute('placement', placement);
23
23
  }
24
+ if (activationType) {
25
+ element.setAttribute('activation-type', activationType);
26
+ }
24
27
  await appendToBody(element);
25
28
  return element;
26
29
  };
27
30
 
28
31
  const checkContentStyles = async (expectedStyles, placement, contentStyles) => {
29
- const element = await getTooltip(placement);
32
+ const element = await getTooltip({ placement });
30
33
  const contentDiv = element.shadowRoot.querySelector('.content');
31
34
 
32
35
  // Modify shadow content
@@ -36,7 +39,8 @@ describe('Tooltip', () => {
36
39
  });
37
40
  }
38
41
 
39
- element.recalculatePosition();
42
+ element.open = true;
43
+ await element.recalculatePosition();
40
44
  Object.keys(expectedStyles).forEach(key => expect(contentDiv.style[key]).toEqual(expectedStyles[key]));
41
45
  };
42
46
 
@@ -75,4 +79,211 @@ describe('Tooltip', () => {
75
79
  it('should not set style properties if placement is top-left', async () => {
76
80
  await checkContentStyles({ top: '', left: '' }, 'top-left');
77
81
  });
82
+
83
+ const ACTIVATION_TYPES = ['automatic', 'manual'];
84
+
85
+ ACTIVATION_TYPES.forEach(activationType => {
86
+ it(`${activationType} should show tooltip on mouseenter`, async () => {
87
+ const element = await getTooltip({ activationType });
88
+ expect(element.open).toBe(false);
89
+
90
+ element.dispatchEvent(new Event('mouseenter'));
91
+ await element.updateComplete;
92
+
93
+ expect(element.open).toBe(true);
94
+ });
95
+
96
+ it(`${activationType} should hide tooltip on mouseleave`, async () => {
97
+ const element = await getTooltip();
98
+ element.open = true;
99
+ await element.updateComplete;
100
+
101
+ element.dispatchEvent(new Event('mouseleave'));
102
+ await element.updateComplete;
103
+
104
+ expect(element.open).toBe(false);
105
+ });
106
+
107
+ it(`${activationType} should remove aria-label when trigger-label is empty`, async () => {
108
+ const element = await getTooltip({ activationType });
109
+ element.setAttribute('trigger-label', '');
110
+ await element.updateComplete;
111
+
112
+ const trigger = element.shadowRoot.querySelector('.trigger');
113
+ expect(trigger.hasAttribute('aria-label')).toBe(false);
114
+ });
115
+
116
+ it(`${activationType} should set aria-label when trigger-label has value`, async () => {
117
+ const element = await getTooltip({ activationType });
118
+ element.setAttribute('trigger-label', 'Custom tooltip label');
119
+ await element.updateComplete;
120
+
121
+ const trigger = element.shadowRoot.querySelector('.trigger');
122
+ expect(trigger.getAttribute('aria-label')).toBe('Custom tooltip label');
123
+ });
124
+ });
125
+
126
+ describe('automatic activation behavior', () => {
127
+ it('should show tooltip on focusin', async () => {
128
+ const element = await getTooltip();
129
+ expect(element.open).toBe(false);
130
+
131
+ element.dispatchEvent(new Event('focusin'));
132
+ await element.updateComplete;
133
+
134
+ expect(element.open).toBe(true);
135
+ });
136
+
137
+ it('should hide tooltip on focusout when focus moves outside', async () => {
138
+ const element = await getTooltip();
139
+ const externalElement = document.createElement('div');
140
+ document.body.appendChild(externalElement);
141
+
142
+ element.open = true;
143
+ await element.updateComplete;
144
+
145
+ const focusOutEvent = new FocusEvent('focusout', { relatedTarget: externalElement });
146
+ element.dispatchEvent(focusOutEvent);
147
+ await element.updateComplete;
148
+
149
+ expect(element.open).toBe(false);
150
+ document.body.removeChild(externalElement);
151
+ });
152
+
153
+ it('should keep tooltip open on focusout when focus stays within tooltip', async () => {
154
+ const element = await getTooltip();
155
+ const trigger = element.shadowRoot.querySelector('.trigger');
156
+
157
+ element.open = true;
158
+ await element.updateComplete;
159
+
160
+ // focus moving out of one element and back to the trigger
161
+ const focusOutEvent = new FocusEvent('focusout', { relatedTarget: trigger });
162
+ element.dispatchEvent(focusOutEvent);
163
+ await element.updateComplete;
164
+
165
+ expect(element.open).toBe(true);
166
+ });
167
+
168
+ it('should not open tooltip on click', async () => {
169
+ const element = await getTooltip();
170
+ const trigger = element.shadowRoot.querySelector('.trigger');
171
+
172
+ expect(element.open).toBe(false);
173
+
174
+ trigger.click();
175
+ await element.updateComplete;
176
+
177
+ expect(element.open).toBe(false);
178
+ });
179
+
180
+ it('should not do anything when clicking outside tooltip', async () => {
181
+ const element = await getTooltip();
182
+ element.open = true;
183
+ await element.updateComplete;
184
+
185
+ const outsideClick = new Event('click', { bubbles: true });
186
+ document.body.dispatchEvent(outsideClick);
187
+ await element.updateComplete;
188
+
189
+ expect(element.open).toBe(true);
190
+ });
191
+
192
+ it('updates attributes', async () => {
193
+ const element = await getTooltip();
194
+ element.open = true;
195
+ await element.updateComplete;
196
+
197
+ let style = getComputedStyle(element.shadowRoot.querySelector('.content'));
198
+ expect(style.opacity).toBe('1');
199
+
200
+ element.open = false;
201
+ await element.updateComplete;
202
+ style = getComputedStyle(element.shadowRoot.querySelector('.content'));
203
+ expect(style.opacity).toBe('0');
204
+ });
205
+ });
206
+
207
+ describe('manual activation behavior', () => {
208
+ it('should toggle tooltip on click', async () => {
209
+ const element = await getTooltip({ activationType: 'manual' });
210
+ const trigger = element.shadowRoot.querySelector('.trigger');
211
+
212
+ expect(element.open).toBe(false);
213
+
214
+ trigger.click();
215
+ await element.updateComplete;
216
+ expect(element.open).toBe(true);
217
+
218
+ trigger.click();
219
+ await element.updateComplete;
220
+ expect(element.open).toBe(false);
221
+ });
222
+
223
+ it('should not open tooltip on focus', async () => {
224
+ const element = await getTooltip({ activationType: 'manual' });
225
+
226
+ expect(element.open).toBe(false);
227
+
228
+ element.dispatchEvent(new Event('focusin'));
229
+ await element.updateComplete;
230
+
231
+ expect(element.open).toBe(false);
232
+ });
233
+
234
+ it('should close tooltip when clicking outside', async () => {
235
+ const element = await getTooltip({ activationType: 'manual' });
236
+ element.open = true;
237
+ await element.updateComplete;
238
+
239
+ const outsideClick = new Event('click', { bubbles: true });
240
+ document.body.dispatchEvent(outsideClick);
241
+ await element.updateComplete;
242
+
243
+ expect(element.open).toBe(false);
244
+ });
245
+
246
+ it('updates attributes', async () => {
247
+ const element = await getTooltip({ activationType: 'manual' });
248
+ element.open = true;
249
+ await element.updateComplete;
250
+ expect(element.shadowRoot.querySelector('.trigger').ariaExpanded).toBe('true');
251
+ let style = getComputedStyle(element.shadowRoot.querySelector('.content'));
252
+ expect(style.visibility).toBe('visible');
253
+ expect(style.opacity).toBe('1');
254
+
255
+ element.open = false;
256
+ await element.updateComplete;
257
+ expect(element.shadowRoot.querySelector('.trigger').ariaExpanded).toBe('false');
258
+ style = getComputedStyle(element.shadowRoot.querySelector('.content'));
259
+ expect(style.visibility).toBe('hidden');
260
+ expect(style.opacity).toBe('0');
261
+ });
262
+
263
+ it('should close tooltip on Escape key', async () => {
264
+ const element = await getTooltip({ activationType: 'manual' });
265
+ element.open = true;
266
+ await element.updateComplete;
267
+
268
+ expect(element.open).toBe(true);
269
+
270
+ const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape' });
271
+ element.dispatchEvent(escapeEvent);
272
+ await element.updateComplete;
273
+
274
+ expect(element.open).toBe(false);
275
+ });
276
+
277
+ it('should not close tooltip on other keys', async () => {
278
+ const element = await getTooltip({ activationType: 'manual' });
279
+ element.open = true;
280
+ await element.updateComplete;
281
+
282
+ const enterEvent = new KeyboardEvent('keydown', { key: 'Alt' });
283
+ element.dispatchEvent(enterEvent);
284
+ await element.updateComplete;
285
+
286
+ expect(element.open).toBe(true);
287
+ });
288
+ });
78
289
  });