|
1 | 1 | import {
|
2 | 2 | LitElement, html, unsafeCSS, nothing, type PropertyValues,
|
3 | 3 | } from 'lit';
|
4 |
| -import { property } from 'lit/decorators.js'; |
| 4 | +import { property, queryAssignedElements } from 'lit/decorators.js'; |
5 | 5 | import { classMap, type ClassInfo } from 'lit/directives/class-map.js';
|
6 | 6 | import { validPropertyValues, defineCustomElement } from '@justeattakeaway/pie-webc-core';
|
7 | 7 | import styles from './tag.scss?inline';
|
@@ -45,30 +45,33 @@ export class PieTag extends LitElement implements TagProps {
|
45 | 45 | @validPropertyValues(componentSelector, iconPlacements, defaultProps.iconPlacement)
|
46 | 46 | public iconPlacement = defaultProps.iconPlacement;
|
47 | 47 |
|
| 48 | + @queryAssignedElements({ slot: 'icon', flatten: true }) _iconSlotNodes!: Array<HTMLElement>; |
| 49 | + |
48 | 50 | private isIconOnly = false;
|
49 | 51 |
|
50 | 52 | updated (changedProperties: PropertyValues<this>) {
|
51 | 53 | if (changedProperties.has('size')) this.checkIfIsIconOnly();
|
52 | 54 | }
|
53 | 55 |
|
54 | 56 | private checkIfIsIconOnly () {
|
55 |
| - // The size must be large |
56 |
| - const isLargeSize = this.size === 'large'; |
| 57 | + const { size, textContent, _iconSlotNodes } = this; |
| 58 | + |
| 59 | + // The instance size must be large |
| 60 | + const isLargeSize = size === 'large'; |
57 | 61 |
|
58 | 62 | // The default slot must be empty
|
59 |
| - const defaultSlotText = this.textContent?.trim(); |
| 63 | + const defaultSlotText = textContent?.trim(); |
60 | 64 | const isDefaultSlotEmpty = defaultSlotText === '';
|
61 | 65 |
|
62 | 66 | // The icon slot must have some content
|
63 |
| - const iconSlot = this.shadowRoot?.querySelector('slot[name="icon"]') as HTMLSlotElement | null; |
64 |
| - |
65 |
| - if (isLargeSize && isDefaultSlotEmpty && iconSlot) { |
66 |
| - const iconSlotNodes = iconSlot.assignedNodes({ flatten: true }); |
| 67 | + const iconsSlotNotEmpty = _iconSlotNodes.length > 0; |
67 | 68 |
|
| 69 | + if (isLargeSize && isDefaultSlotEmpty && iconsSlotNotEmpty) { |
68 | 70 | // The icon slot content must be an icon
|
69 |
| - if (iconSlotNodes && iconSlotNodes.length === 1) { |
70 |
| - const firstNode = (iconSlotNodes[0] as Element); |
71 |
| - const isIcon = firstNode.tagName.startsWith('ICON-'); |
| 71 | + if (_iconSlotNodes && _iconSlotNodes.length === 1) { |
| 72 | + const firstNode = (_iconSlotNodes[0] as Element); |
| 73 | + const tag = firstNode.tagName.toUpperCase(); |
| 74 | + const isIcon = tag.startsWith('ICON-') || tag === 'SVG'; |
72 | 75 |
|
73 | 76 | this.isIconOnly = isIcon;
|
74 | 77 | this.requestUpdate();
|
|
0 commit comments