Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

### General
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ''"
>
<span class="igx-button-group__item-content {{ itemContentCssClass }}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IButtonEventArgs> | 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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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({
Expand Down Expand Up @@ -156,3 +242,57 @@ function hasClass(element: HTMLElement, className: string): boolean {
})
class ButtonWithRoleComponent {
}

@Component({
template: `<span igxButton aria-label="Save">Test</span>`,
changeDetection: ChangeDetectionStrategy.Eager,
imports: [IgxButtonDirective]
})
class ButtonWithAriaLabelComponent {
}

@Component({
template: `<span igxButton [attr.aria-label]="ariaLabel()">Test</span>`,
changeDetection: ChangeDetectionStrategy.Eager,
imports: [IgxButtonDirective]
})
class ButtonWithBoundAriaLabelComponent {
public ariaLabel = input('Save');
}

@Component({
template: `<span igxButton [igxLabel]="label()">Test</span>`,
changeDetection: ChangeDetectionStrategy.Eager,
imports: [IgxButtonDirective]
})
class ButtonWithIgxLabelComponent {
public label = input<string | null>('Close');
}

@Component({
template: `<span igxButton aria-label="Save" [igxLabel]="label()">Test</span>`,
changeDetection: ChangeDetectionStrategy.Eager,
imports: [IgxButtonDirective]
})
class ButtonWithAriaLabelAndIgxLabelComponent {
public label = input<string | null>('Close');
}

@Component({
template: `<span igxButton [attr.aria-label]="ariaLabel()" [igxLabel]="label()">Test</span>`,
changeDetection: ChangeDetectionStrategy.Eager,
imports: [IgxButtonDirective]
})
class ButtonWithBoundAriaLabelAndIgxLabelComponent {
public ariaLabel = input('Save');
public label = input<string | null>('Close');
}

@Component({
template: `<span igxButton aria-label="Close" [igxLabel]="label()">Test</span>`,
changeDetection: ChangeDetectionStrategy.Eager,
imports: [IgxButtonDirective]
})
class ServerRenderedIgxLabelComponent {
public label = input<string | null>('Close');
}
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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>(IgxButtonType.Flat);
protected readonly _label = signal<string | null>(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()
Expand Down Expand Up @@ -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.3.2. Use the native `aria-label` attribute instead.
*
* @example
* ```html
* <button type="button" igxButton="flat" igxLabel="Label"></button>
* <button type="button" igxButton="flat" aria-label="Label"></button>
* ```
*/
@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;
}
}

Expand Down