Skip to content

Commit 872f9c2

Browse files
feat(pie-tag): DSW-2550 update component logic
1 parent 9c0cc3e commit 872f9c2

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

packages/components/pie-tag/src/index.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
LitElement, html, unsafeCSS, nothing, type PropertyValues,
33
} from 'lit';
4-
import { property } from 'lit/decorators.js';
4+
import { property, queryAssignedElements } from 'lit/decorators.js';
55
import { classMap, type ClassInfo } from 'lit/directives/class-map.js';
66
import { validPropertyValues, defineCustomElement } from '@justeattakeaway/pie-webc-core';
77
import styles from './tag.scss?inline';
@@ -45,30 +45,33 @@ export class PieTag extends LitElement implements TagProps {
4545
@validPropertyValues(componentSelector, iconPlacements, defaultProps.iconPlacement)
4646
public iconPlacement = defaultProps.iconPlacement;
4747

48+
@queryAssignedElements({ slot: 'icon', flatten: true }) _iconSlotNodes!: Array<HTMLElement>;
49+
4850
private isIconOnly = false;
4951

5052
updated (changedProperties: PropertyValues<this>) {
5153
if (changedProperties.has('size')) this.checkIfIsIconOnly();
5254
}
5355

5456
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';
5761

5862
// The default slot must be empty
59-
const defaultSlotText = this.textContent?.trim();
63+
const defaultSlotText = textContent?.trim();
6064
const isDefaultSlotEmpty = defaultSlotText === '';
6165

6266
// 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;
6768

69+
if (isLargeSize && isDefaultSlotEmpty && iconsSlotNotEmpty) {
6870
// 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';
7275

7376
this.isIconOnly = isIcon;
7477
this.requestUpdate();

0 commit comments

Comments
 (0)