diff --git a/CHANGELOG.md b/CHANGELOG.md index f9844a6c481..5fcc11d0bd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes for each version of this project will be documented in this file. +## 22.2.8 + +### 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 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.2.7 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 9ad1cda847d..7cabdf72f7c 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 @@ -1,4 +1,4 @@ -import { Component, ViewChild, ChangeDetectionStrategy } from '@angular/core'; +import { Component, ViewChild, ChangeDetectionStrategy, input } from '@angular/core'; import { TestBed, waitForAsync } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { IgxButtonDirective } from './button.directive'; @@ -123,6 +123,92 @@ 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 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; + + 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.hasAttribute('aria-label')).toBe(false); + }); + }); }); @Component({ @@ -153,3 +239,57 @@ class ButtonWithAttribsComponent { }) 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'); +} + +@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 f7fe49b0a3f..57ac4664fee 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,16 @@ 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); + /** The aria-label last written by igxLabel. */ + private _label: string | null = null; /** Emitted when the button is selected. */ @Output() @@ -82,16 +83,28 @@ 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.2.8. 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; + + if (label) { + this._renderer.setAttribute(element, 'aria-label', label); + } 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; } }