From d40191394f13131cd2e1332c7ec6336acb791abd Mon Sep 17 00:00:00 2001 From: Simeon Simeonoff Date: Wed, 25 Nov 2026 14:37:49 +0200 Subject: [PATCH 1/2] fix(button): keep aria-label set outside igxLabel The aria-label host binding wrote null when igxLabel was unset, which removed any aria-label the template set (regression from #17155). The button now writes aria-label only through igxLabel and restores the element's own value when igxLabel is cleared. Label styles now skip .igx-button, so igxLabel on a button no longer gets the IgxLabelDirective styles. The button's igxLabel input is deprecated in favor of aria-label. Closes #18009 --- CHANGELOG.md | 13 +++ .../button-group-content.component.html | 2 +- .../components/label/_label-component.scss | 3 +- .../styles/components/label/_label-theme.scss | 3 +- .../src/directives/button/README.md | 1 + .../button/button.directive.spec.ts | 102 ++++++++++++++++++ .../src/directives/button/button.directive.ts | 33 ++++-- 7 files changed, 148 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c55b5d85f63..b3ab6c067fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ All notable changes for each version of this project will be documented in this file. +## 22.3.2 + +### General + +- `IgxButtonDirective` + - **Deprecation** - The `igxLabel` input has been deprecated and will be removed in a future version. Use the native `aria-label` attribute instead. + +### Bug Fixes + +- `IgxButtonDirective` + - An `aria-label` set on the button, statically or through a binding, is no longer removed. The button changes `aria-label` only when `igxLabel` is set, and restores the original value when `igxLabel` is cleared (#18009). + - Buttons that use `igxLabel` no longer receive the label styles of `IgxLabelDirective`, such as its text color (#18009). + ## 22.3.1 ### Bug Fixes diff --git a/projects/igniteui-angular/button-group/src/button-group/button-group-content.component.html b/projects/igniteui-angular/button-group/src/button-group/button-group-content.component.html index c81e0d2b664..efcd45c64fc 100644 --- a/projects/igniteui-angular/button-group/src/button-group/button-group-content.component.html +++ b/projects/igniteui-angular/button-group/src/button-group/button-group-content.component.html @@ -6,7 +6,7 @@ [selected]="button.selected" [attr.data-togglable]="button.togglable" [disabled]="disabled || button.disabled" - [igxLabel]="button.label" + [attr.aria-label]="button.label" [igxRipple]="button.ripple || ''" > diff --git a/projects/igniteui-angular/core/src/core/styles/components/label/_label-component.scss b/projects/igniteui-angular/core/src/core/styles/components/label/_label-component.scss index 8de49ebc668..2152babcac9 100644 --- a/projects/igniteui-angular/core/src/core/styles/components/label/_label-component.scss +++ b/projects/igniteui-angular/core/src/core/styles/components/label/_label-component.scss @@ -10,7 +10,8 @@ $deps: () ); - [igxLabel] { + // igxButton has an igxLabel input of its own, so keep label styles off buttons. + [igxLabel]:where(:not(.igx-button)) { @extend %label-base !optional; } } diff --git a/projects/igniteui-angular/core/src/core/styles/components/label/_label-theme.scss b/projects/igniteui-angular/core/src/core/styles/components/label/_label-theme.scss index 93dc8ce68e3..08b5636d523 100644 --- a/projects/igniteui-angular/core/src/core/styles/components/label/_label-theme.scss +++ b/projects/igniteui-angular/core/src/core/styles/components/label/_label-theme.scss @@ -7,7 +7,8 @@ /// @see {mixin} tokens /// @param {Map} $theme - The theme used to style the component. @mixin label($theme) { - @include tokens($theme, $mode: 'scoped', $scope: '[igxLabel]'); + // igxButton has an igxLabel input of its own, so keep label styles off buttons. + @include tokens($theme, $mode: 'scoped', $scope: '[igxLabel]:where(:not(.igx-button))'); @include layer(base) { $variant: map.get($theme, '_meta', 'theme'); diff --git a/projects/igniteui-angular/directives/src/directives/button/README.md b/projects/igniteui-angular/directives/src/directives/button/README.md index 33cd329940b..e66927a32e8 100644 --- a/projects/igniteui-angular/directives/src/directives/button/README.md +++ b/projects/igniteui-angular/directives/src/directives/button/README.md @@ -14,6 +14,7 @@ A walkthrough of how to get started can be found [here](https://www.infragistics | `igxButton` | string | Set the type of igxButton to be used. Default is set to flat. | | `buttonSelected` | EventEmitter | Emitted only when a button gets selected, or deselected, and not on initialization. | | `selected` | boolean | Gets or sets whether the button is selected. Mainly used in the IgxButtonGroup component and it will have no effect if set separately. | +| `igxLabel` | string | **Deprecated.** Sets the button's `aria-label`. Use the native `aria-label` attribute instead. | # Button types | Name | Description | diff --git a/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts b/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts index 353a6325285..4257f051d4d 100644 --- a/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts +++ b/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts @@ -120,6 +120,73 @@ describe('IgxButton', () => { const button = fixture.debugElement.query(By.css('span')).nativeElement as HTMLElement; expect(button.getAttribute('role')).toBe('menuitem'); }); + + describe('aria-label', () => { + it('should keep a static aria-label', () => { + const fixture = TestBed.createComponent(ButtonWithAriaLabelComponent); + fixture.detectChanges(); + fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('span').getAttribute('aria-label')).toBe('Save'); + }); + + it('should keep a bound aria-label', () => { + const fixture = TestBed.createComponent(ButtonWithBoundAriaLabelComponent); + fixture.detectChanges(); + const button = fixture.nativeElement.querySelector('span') as HTMLElement; + + expect(button.getAttribute('aria-label')).toBe('Save'); + + fixture.componentRef.setInput('ariaLabel', 'Close'); + fixture.detectChanges(); + + expect(button.getAttribute('aria-label')).toBe('Close'); + }); + + it('should not add an aria-label when neither aria-label nor igxLabel is set', () => { + const fixture = TestBed.createComponent(ButtonWithAttribsComponent); + fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('span').hasAttribute('aria-label')).toBe(false); + }); + + it('should set aria-label from igxLabel', () => { + const fixture = TestBed.createComponent(ButtonWithIgxLabelComponent); + fixture.detectChanges(); + const button = fixture.nativeElement.querySelector('span') as HTMLElement; + + expect(button.getAttribute('aria-label')).toBe('Close'); + + fixture.componentRef.setInput('label', 'Dismiss'); + fixture.detectChanges(); + + expect(button.getAttribute('aria-label')).toBe('Dismiss'); + }); + + it('should remove the aria-label it set when igxLabel is cleared', () => { + const fixture = TestBed.createComponent(ButtonWithIgxLabelComponent); + fixture.detectChanges(); + const button = fixture.nativeElement.querySelector('span') as HTMLElement; + + fixture.componentRef.setInput('label', null); + fixture.detectChanges(); + + expect(button.hasAttribute('aria-label')).toBe(false); + }); + + it('should restore the static aria-label when igxLabel is cleared', () => { + const fixture = TestBed.createComponent(ButtonWithAriaLabelAndIgxLabelComponent); + fixture.detectChanges(); + const button = fixture.nativeElement.querySelector('span') as HTMLElement; + + expect(button.getAttribute('aria-label')).toBe('Close'); + + fixture.componentRef.setInput('label', null); + fixture.detectChanges(); + + expect(button.getAttribute('aria-label')).toBe('Save'); + }); + }); }); @Component({ @@ -156,3 +223,38 @@ function hasClass(element: HTMLElement, className: string): boolean { }) class ButtonWithRoleComponent { } + +@Component({ + template: `Test`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxButtonDirective] +}) +class ButtonWithAriaLabelComponent { +} + +@Component({ + template: `Test`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxButtonDirective] +}) +class ButtonWithBoundAriaLabelComponent { + public ariaLabel = input('Save'); +} + +@Component({ + template: `Test`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxButtonDirective] +}) +class ButtonWithIgxLabelComponent { + public label = input('Close'); +} + +@Component({ + template: `Test`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxButtonDirective] +}) +class ButtonWithAriaLabelAndIgxLabelComponent { + public label = input('Close'); +} diff --git a/projects/igniteui-angular/directives/src/directives/button/button.directive.ts b/projects/igniteui-angular/directives/src/directives/button/button.directive.ts index f7fe49b0a3f..d00184da7e8 100644 --- a/projects/igniteui-angular/directives/src/directives/button/button.directive.ts +++ b/projects/igniteui-angular/directives/src/directives/button/button.directive.ts @@ -1,4 +1,4 @@ -import { Directive, EventEmitter, Input, Output, booleanAttribute, signal } from '@angular/core'; +import { Directive, EventEmitter, Input, Output, Renderer2, booleanAttribute, inject, signal } from '@angular/core'; import { IBaseEventArgs } from 'igniteui-angular/core'; import { IgxBaseButtonType, IgxButtonBaseDirective } from './button-base'; @@ -36,15 +36,17 @@ export type IgxButtonType = typeof IgxButtonType[keyof typeof IgxButtonType]; '[class.igx-button--outlined]': '_type() === "outlined"', '[class.igx-button--fab]': '_type() === "fab"', '[class.igx-button--ready]': '_hasRendered()', - '[attr.aria-label]': '_label()', '[attr.data-selected]': '_selected() ? "true" : "false"', '(click)': 'buttonSelected.emit({ button: this })', } }) export class IgxButtonDirective extends IgxButtonBaseDirective { + private readonly _renderer = inject(Renderer2); protected readonly _type = signal(IgxButtonType.Flat); - protected readonly _label = signal(null); protected readonly _selected = signal(false); + private _label: string | null = null; + /** The element's own aria-label, saved when igxLabel first replaces it. */ + private _ownAriaLabel: string | null = null; /** Emitted when the button is selected. */ @Output() @@ -84,14 +86,33 @@ export class IgxButtonDirective extends IgxButtonBaseDirective { /** * Sets the `aria-label` attribute. * + * @deprecated in version 22.3.2. Use the native `aria-label` attribute instead. + * * @example * ```html - * + * * ``` */ @Input({ alias: 'igxLabel' }) - public set label(value: string) { - this._label.set(value || null); + public set label(value: string | null | undefined) { + const label = value || null; + const element = this.nativeElement; + + // Touch aria-label only through igxLabel, so the template's own value survives. + if (label) { + if (!this._label) { + this._ownAriaLabel = element.getAttribute('aria-label'); + } + this._renderer.setAttribute(element, 'aria-label', label); + } else if (this._label) { + if (this._ownAriaLabel === null) { + this._renderer.removeAttribute(element, 'aria-label'); + } else { + this._renderer.setAttribute(element, 'aria-label', this._ownAriaLabel); + } + } + + this._label = label; } } From f897fc1f9934a8144554dff555a5484581e1404c Mon Sep 17 00:00:00 2001 From: Simeon Simeonoff Date: Wed, 25 Nov 2026 14:51:05 +0200 Subject: [PATCH 2/2] fix(button): remove only the aria-label igxLabel wrote Drop the snapshot and restore of the element's own aria-label. Clearing igxLabel now removes the attribute only if it still holds the value igxLabel wrote, so a newer native aria-label is kept and a hydrated server value is not mistaken for an authored one. --- CHANGELOG.md | 2 +- .../button/button.directive.spec.ts | 44 +++++++++++++++++-- .../src/directives/button/button.directive.ts | 18 +++----- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3ab6c067fa..defdd756174 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ All notable changes for each version of this project will be documented in this ### Bug Fixes - `IgxButtonDirective` - - An `aria-label` set on the button, statically or through a binding, is no longer removed. The button changes `aria-label` only when `igxLabel` is set, and restores the original value when `igxLabel` is cleared (#18009). + - An `aria-label` set on the button, statically or through a binding, is no longer removed. The button changes `aria-label` only through `igxLabel`, and clearing `igxLabel` removes only the value it set (#18009). - Buttons that use `igxLabel` no longer receive the label styles of `IgxLabelDirective`, such as its text color (#18009). ## 22.3.1 diff --git a/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts b/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts index 4257f051d4d..546d963c2fe 100644 --- a/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts +++ b/projects/igniteui-angular/directives/src/directives/button/button.directive.spec.ts @@ -174,17 +174,36 @@ describe('IgxButton', () => { expect(button.hasAttribute('aria-label')).toBe(false); }); - it('should restore the static aria-label when igxLabel is cleared', () => { + it('should replace a static aria-label while igxLabel is set', () => { const fixture = TestBed.createComponent(ButtonWithAriaLabelAndIgxLabelComponent); fixture.detectChanges(); + + expect(fixture.nativeElement.querySelector('span').getAttribute('aria-label')).toBe('Close'); + }); + + it('should keep an aria-label that changed after igxLabel set it', () => { + const fixture = TestBed.createComponent(ButtonWithBoundAriaLabelAndIgxLabelComponent); + fixture.detectChanges(); const button = fixture.nativeElement.querySelector('span') as HTMLElement; - expect(button.getAttribute('aria-label')).toBe('Close'); + fixture.componentRef.setInput('ariaLabel', 'Dismiss'); + fixture.detectChanges(); + fixture.componentRef.setInput('label', null); + fixture.detectChanges(); + + expect(button.getAttribute('aria-label')).toBe('Dismiss'); + }); + + it('should remove a server-rendered aria-label when igxLabel is cleared', () => { + // After hydration the element already has the aria-label igxLabel wrote on the server. + const fixture = TestBed.createComponent(ServerRenderedIgxLabelComponent); + fixture.detectChanges(); + const button = fixture.nativeElement.querySelector('span') as HTMLElement; fixture.componentRef.setInput('label', null); fixture.detectChanges(); - expect(button.getAttribute('aria-label')).toBe('Save'); + expect(button.hasAttribute('aria-label')).toBe(false); }); }); }); @@ -258,3 +277,22 @@ class ButtonWithIgxLabelComponent { class ButtonWithAriaLabelAndIgxLabelComponent { public label = input('Close'); } + +@Component({ + template: `Test`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxButtonDirective] +}) +class ButtonWithBoundAriaLabelAndIgxLabelComponent { + public ariaLabel = input('Save'); + public label = input('Close'); +} + +@Component({ + template: `Test`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxButtonDirective] +}) +class ServerRenderedIgxLabelComponent { + public label = input('Close'); +} diff --git a/projects/igniteui-angular/directives/src/directives/button/button.directive.ts b/projects/igniteui-angular/directives/src/directives/button/button.directive.ts index d00184da7e8..7eaf84b56dc 100644 --- a/projects/igniteui-angular/directives/src/directives/button/button.directive.ts +++ b/projects/igniteui-angular/directives/src/directives/button/button.directive.ts @@ -44,9 +44,8 @@ export class IgxButtonDirective extends IgxButtonBaseDirective { private readonly _renderer = inject(Renderer2); protected readonly _type = signal(IgxButtonType.Flat); protected readonly _selected = signal(false); + /** The aria-label last written by igxLabel. */ private _label: string | null = null; - /** The element's own aria-label, saved when igxLabel first replaces it. */ - private _ownAriaLabel: string | null = null; /** Emitted when the button is selected. */ @Output() @@ -84,7 +83,7 @@ export class IgxButtonDirective extends IgxButtonBaseDirective { } /** - * Sets the `aria-label` attribute. + * Sets the `aria-label` attribute. While it has a value, it replaces any other `aria-label` on the button. * * @deprecated in version 22.3.2. Use the native `aria-label` attribute instead. * @@ -98,18 +97,11 @@ export class IgxButtonDirective extends IgxButtonBaseDirective { const label = value || null; const element = this.nativeElement; - // Touch aria-label only through igxLabel, so the template's own value survives. if (label) { - if (!this._label) { - this._ownAriaLabel = element.getAttribute('aria-label'); - } this._renderer.setAttribute(element, 'aria-label', label); - } else if (this._label) { - if (this._ownAriaLabel === null) { - this._renderer.removeAttribute(element, 'aria-label'); - } else { - this._renderer.setAttribute(element, 'aria-label', this._ownAriaLabel); - } + } else if (this._label !== null && element.getAttribute('aria-label') === this._label) { + // Remove only the value igxLabel wrote; a newer aria-label belongs to someone else. + this._renderer.removeAttribute(element, 'aria-label'); } this._label = label;