@jack-henry/jh-elements 2.0.0-beta.11 → 2.0.0-beta.13

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.
@@ -13,6 +13,7 @@ const openAttr = 'open';
13
13
  * @cssprop --jh-tooltip-color-text - The tooltip text color. Defaults to `--jh-color-content-on-primary-enabled`.
14
14
  *
15
15
  * @slot default - Use to insert the element that triggers the tooltip.
16
+ * @slot jh-tooltip-content - Use to insert the content of the tooltip.
16
17
  *
17
18
  * @customElement jh-tooltip
18
19
  */
@@ -37,10 +38,10 @@ export class JhTooltip extends LitElement {
37
38
  );
38
39
  border-radius: var(--jh-border-radius-100);
39
40
  padding: var(--jh-dimension-200);
40
- font-family: var(--jh-font-helper-regular-font-family);
41
- font-weight: var(--jh-font-helper-regular-font-weight);
42
- font-size: var(--jh-font-helper-regular-font-size);
43
- line-height: var(--jh-font-helper-regular-line-height);
41
+ font-family: var(--jh-font-helper-bold-font-family);
42
+ font-weight: var(--jh-font-helper-bold-font-weight);
43
+ font-size: var(--jh-font-helper-bold-font-size);
44
+ line-height: var(--jh-font-helper-bold-line-height);
44
45
  max-width: 160px;
45
46
  width: max-content;
46
47
  position: absolute;
@@ -204,12 +205,6 @@ export class JhTooltip extends LitElement {
204
205
  type: Boolean,
205
206
  attribute: 'flip-disabled',
206
207
  },
207
- /**
208
- * Provides information about the item which triggered the tooltip.
209
- */
210
- label: {
211
- type: String,
212
- },
213
208
  /**
214
209
  * Determines whether the tooltip is open or closed. Can be set on the tooltip to force it open.
215
210
  */
@@ -232,8 +227,6 @@ export class JhTooltip extends LitElement {
232
227
  this.#internals = this.attachInternals();
233
228
  /**@type {?Boolean} */
234
229
  this.flipDisabled = false;
235
- /** @type {?string} */
236
- this.label = null;
237
230
  /**@type {?Boolean} */
238
231
  this.open = false;
239
232
  /** @type {?string} */
@@ -244,39 +237,6 @@ export class JhTooltip extends LitElement {
244
237
  super.connectedCallback();
245
238
  /** @ignore */
246
239
  this.id = `tooltip-describedby-${id++}`;
247
- let observer = new MutationObserver(this.#handleEmptyLabel.bind(this));
248
- let options = {
249
- childList: true,
250
- };
251
- observer.observe(this.shadowRoot, options);
252
- }
253
-
254
- #handleEmptyLabel(mutations) {
255
- for (let mutation of mutations) {
256
- const addedNodes = Array.from(mutation.addedNodes);
257
- //check if any of the added nodes is a span
258
- const spanAdded = addedNodes.some(
259
- (addedNode) => addedNode.tagName === 'SPAN'
260
- );
261
-
262
- if (spanAdded) {
263
- this.#internals.role = 'tooltip';
264
- this.addEventListener('focus', this.#handleOpenTooltip, true);
265
- this.addEventListener('mouseenter', this.#handleOpenTooltip);
266
- this.addEventListener('blur', this.#handleCloseTooltip, true);
267
- this.addEventListener('mouseleave', this.#handleCloseTooltip);
268
- this.addEventListener('keydown', this.#handleKeyDown);
269
- }
270
-
271
- if (mutation.removedNodes.length > 0) {
272
- this.#internals.role = '';
273
- this.removeEventListener('focus', this.#handleOpenTooltip);
274
- this.removeEventListener('mouseenter', this.#handleOpenTooltip);
275
- this.removeEventListener('blur', this.#handleCloseTooltip);
276
- this.removeEventListener('mouseleave', this.#handleCloseTooltip);
277
- this.removeEventListener('keydown', this.#handleKeyDown);
278
- }
279
- }
280
240
  }
281
241
 
282
242
  disconnectedCallback() {
@@ -288,13 +248,42 @@ export class JhTooltip extends LitElement {
288
248
  this.removeEventListener('keydown', this.#handleKeyDown);
289
249
  }
290
250
 
291
- #handleSlotChange() {
251
+ #handleContentSlotChange(e) {
252
+ const slot = e.target;
253
+ const hasContent = slot.assignedNodes().length > 0;
254
+
255
+ if (hasContent) {
256
+ this.#internals.role = 'tooltip';
257
+ this.addEventListener('focus', this.#handleOpenTooltip, true);
258
+ this.addEventListener('mouseenter', this.#handleOpenTooltip);
259
+ this.addEventListener('blur', this.#handleCloseTooltip, true);
260
+ this.addEventListener('mouseleave', this.#handleCloseTooltip);
261
+ this.addEventListener('keydown', this.#handleKeyDown);
262
+ } else {
263
+ this.#internals.role = '';
264
+ this.#handleCloseTooltip();
265
+ this.removeEventListener('focus', this.#handleOpenTooltip);
266
+ this.removeEventListener('mouseenter', this.#handleOpenTooltip);
267
+ this.removeEventListener('blur', this.#handleCloseTooltip);
268
+ this.removeEventListener('mouseleave', this.#handleCloseTooltip);
269
+ this.removeEventListener('keydown', this.#handleKeyDown);
270
+ }
271
+ }
272
+
273
+ #handleSlotChange(e) {
292
274
  //get id set previously in the constructor and set it to reference aria-describedby on the first element in the slot
293
275
  const tooltipId = this.getAttribute('id');
294
- this.firstElementChild.setAttribute('aria-describedby', tooltipId);
276
+ const slottedElements = e.target.assignedElements();
277
+ const tooltipContent = this.shadowRoot.querySelector('slot[name="jh-tooltip-content"]').assignedElements();
278
+
279
+ if (slottedElements.length === 0 || tooltipContent.length === 0) {
280
+ return;
281
+ }
282
+ const triggerElement = slottedElements[0];
283
+ triggerElement.setAttribute('aria-describedby', tooltipId);
295
284
 
296
285
  //get display property of element in slot and set it on the jh-tooltip.
297
- const tooltipDisplay = window.getComputedStyle(this.firstElementChild).display;
286
+ const tooltipDisplay = window.getComputedStyle(triggerElement).display;
298
287
  this.style.setProperty('display', tooltipDisplay );
299
288
  }
300
289
 
@@ -323,7 +312,7 @@ export class JhTooltip extends LitElement {
323
312
  //check if current position is a valid position otherwise make it fail.
324
313
  if (!['left', 'right', 'top-start', 'top-end', 'top-center', 'bottom-start', 'bottom-end', 'bottom-center'].includes(currentPosition)) return;
325
314
 
326
- if (this.flipDisabled === false && this.label) {
315
+ if (this.flipDisabled === false) {
327
316
 
328
317
  //break current position into tooltop position and arrow position
329
318
  const [currentTooltip, currentArrow] = currentPosition.split('-');
@@ -450,10 +439,10 @@ export class JhTooltip extends LitElement {
450
439
  #getDimensions() {
451
440
  return {
452
441
  tooltipWidth: this.shadowRoot
453
- .querySelector('span')
442
+ .querySelector('.content')
454
443
  .getBoundingClientRect().width,
455
444
  tooltipHeight: this.shadowRoot
456
- .querySelector('span')
445
+ .querySelector('.content')
457
446
  .getBoundingClientRect().height,
458
447
  elemHeight: this.getBoundingClientRect().height,
459
448
  elemWidth: this.getBoundingClientRect().width,
@@ -475,22 +464,14 @@ export class JhTooltip extends LitElement {
475
464
  }
476
465
 
477
466
  render() {
478
- let label;
479
-
480
- if (this.label) {
481
- label = html`
482
- <span
483
- class=${ifDefined(this.open ? 'show' : null)}
484
- aria-hidden=${this.open ? 'false' : 'true'}
485
- >
486
- ${this.label}
487
- </span>
488
- `;
489
- }
490
-
491
467
  return html`
492
468
  <slot @slotchange=${this.#handleSlotChange}></slot>
493
- ${label}
469
+ <span
470
+ class="content ${ifDefined(this.open ? 'show' : null)}"
471
+ aria-hidden=${this.open ? 'false' : 'true'}>
472
+ <slot name="jh-tooltip-content" @slotchange=${this.#handleContentSlotChange}></slot>
473
+ </span>
474
+
494
475
  `;
495
476
  }
496
477
  }