diff --git a/packages/webawesome/docs/docs/resources/changelog.md b/packages/webawesome/docs/docs/resources/changelog.md index c5ae7264e..c13910d33 100644 --- a/packages/webawesome/docs/docs/resources/changelog.md +++ b/packages/webawesome/docs/docs/resources/changelog.md @@ -21,6 +21,7 @@ Components with the Experimental badge sh - Fixed a bug in `` where its value would revert to `""` when checked / unchecked [pr:1547] - Fixed a bug that caused icon button labels to not render in frameworks [issue:1542] - Fixed a bug in `` that caused the `name` property not to reflect [pr:1538] +- Fixed a bug in `` that caused icon buttons to render when non-text nodes were slotted in [issue:1475] ## 3.0.0-beta.6 diff --git a/packages/webawesome/src/components/button/button.ts b/packages/webawesome/src/components/button/button.ts index 4fa26436b..50e3cb3a5 100644 --- a/packages/webawesome/src/components/button/button.ts +++ b/packages/webawesome/src/components/button/button.ts @@ -176,22 +176,32 @@ export default class WaButton extends WebAwesomeFormAssociatedElement { const nodes = this.labelSlot.assignedNodes({ flatten: true }); let hasIconLabel = false; let hasIcon = false; - let text = ''; + let hasText = false; + let hasOtherElements = false; - // If there's only an icon and no text, it's an icon button + // Check all slotted nodes [...nodes].forEach(node => { - if (node.nodeType === Node.ELEMENT_NODE && (node as WaIcon).localName === 'wa-icon') { - hasIcon = true; - if (!hasIconLabel) hasIconLabel = (node as WaIcon).label !== undefined; - } + if (node.nodeType === Node.ELEMENT_NODE) { + const element = node as HTMLElement; - // Concatenate text nodes - if (node.nodeType === Node.TEXT_NODE) { - text += node.textContent; + if (element.localName === 'wa-icon') { + hasIcon = true; + if (!hasIconLabel) hasIconLabel = (element as WaIcon).label !== undefined; + } else { + // Any other element type means it's not an icon button + hasOtherElements = true; + } + } else if (node.nodeType === Node.TEXT_NODE) { + // Check if text node has actual content + const text = node.textContent?.trim() || ''; + if (text.length > 0) { + hasText = true; + } } }); - this.isIconButton = text.trim() === '' && hasIcon; + // It's only an icon button if there's an icon and nothing else + this.isIconButton = hasIcon && !hasText && !hasOtherElements; if (this.isIconButton && !hasIconLabel) { console.warn(