Skip to content

Commit

Permalink
fix(button): fix disable state of the button (#12151)
Browse files Browse the repository at this point in the history
### Related Ticket(s)

[ADCMS-6961](https://jsw.ibm.com/browse/ADCMS-6961)

### Description

Fixes some odd icon logic left in the button component. The fix allows us to consistently re-render the correct CTA type icon whenever a change in the parent component may mean a different `<slot name="icon">` element is rendered. This allows for consistency across disable/active states.

### Testing instructions

* Toggling disable knob and/or changing the `href` should maintain the expected icon and reflect the relevant state.
* Change CTA type knob state to make sure it reflects the right value

### Changelog

**Changed**

- Fixed icon issues in the button component for disable state

<!-- React and Web Component deploy previews are enabled by default. -->
<!-- To enable additional available deploy previews, apply the following -->
<!-- labels for the corresponding package: -->
<!-- *** "test: e2e": Codesandbox examples and e2e integration tests -->
<!-- *** "package: services": Services -->
<!-- *** "package: utilities": Utilities -->
<!-- *** "RTL": React / Web Components (RTL) -->
<!-- *** "feature flag": React / Web Components (experimental) -->
  • Loading branch information
m4olivei authored Dec 6, 2024
1 parent ea9594e commit 32a8214
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packages/web-components/src/components/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class C4DButton extends CTAMixin(StableSelectorMixin(CDSButton)) {
@query('a')
_linkNode;

@property()
iconDiv;
@query(`slot[name='icon']`)
iconSlot?: HTMLElement;

@property()
span;
Expand Down Expand Up @@ -135,18 +135,26 @@ class C4DButton extends CTAMixin(StableSelectorMixin(CDSButton)) {

updated(changedProperties) {
super.updated(changedProperties);

if (changedProperties.has('ctaType')) {
if (!this.iconDiv) {
this.iconDiv = this.shadowRoot?.querySelector("slot[name='icon']");
}

const { iconDiv } = this;

iconDiv.querySelector('svg')?.remove();
iconDiv.innerHTML = this._renderButtonIcon();
iconDiv
?.querySelector('svg')
const updateIconForProperties = [
'ctaType',
'disabled',
'tooltipText',
'href',
];
const { iconSlot } = this;

// Note that the parent may render a different <slot name="icon">
// based on changes to either disabled, tooltipText, or href, so we make
// sure to re-render the icon if any of those change, in addition to the
// ctaType.
if (
iconSlot &&
updateIconForProperties.some((prop) => changedProperties.has(prop))
) {
iconSlot.querySelector('svg')?.remove();
iconSlot.innerHTML = this._renderButtonIcon();
iconSlot
.querySelector('svg')
?.classList.add(`${prefix}--card__cta`, `${c4dPrefix}-ce--cta__icon`);
}
}
Expand Down

0 comments on commit 32a8214

Please sign in to comment.