Skip to content

Commit 10889d7

Browse files
refactor(module:icon): enhanced accessibility & simplify code (#9453)
1 parent dbebd02 commit 10889d7

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

components/icon/icon.directive.ts

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ import { NzIconPatchService, NzIconService } from './icon.service';
3434
selector: 'nz-icon,[nz-icon]',
3535
exportAs: 'nzIcon',
3636
host: {
37-
class: 'anticon'
37+
class: 'anticon',
38+
'[class]': `'anticon-' + type`,
39+
'[class.anticon-spin]': `nzSpin || type === 'loading'`,
40+
role: 'img',
41+
'[attr.aria-label]': 'type'
3842
}
3943
})
4044
export class NzIconDirective extends IconDirective implements OnChanges, AfterContentChecked {
@@ -45,14 +49,8 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
4549
private pendingTasks = inject(PendingTasks);
4650
private isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
4751

48-
cacheClassName: string | null = null;
49-
@Input({ transform: booleanAttribute })
50-
set nzSpin(value: boolean) {
51-
this.spin = value;
52-
}
53-
52+
@Input({ transform: booleanAttribute }) nzSpin: boolean = false;
5453
@Input({ transform: numberAttribute }) nzRotate: number = 0;
55-
5654
@Input()
5755
set nzType(value: string) {
5856
this.type = value;
@@ -77,7 +75,6 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
7775

7876
private readonly el: HTMLElement;
7977
private iconfont?: string;
80-
private spin: boolean = false;
8178

8279
constructor(public readonly iconService: NzIconService) {
8380
super(iconService);
@@ -86,9 +83,9 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
8683
}
8784

8885
override ngOnChanges(changes: SimpleChanges): void {
89-
const { nzType, nzTwotoneColor, nzSpin, nzTheme, nzRotate } = changes;
86+
const { nzType, nzTwotoneColor, nzTheme, nzRotate } = changes;
9087

91-
if (nzType || nzTwotoneColor || nzSpin || nzTheme) {
88+
if (nzType || nzTwotoneColor || nzTheme) {
9289
// This is used to reduce the number of change detections
9390
// while the icon is being loaded asynchronously.
9491
this.ngZone.runOutsideAngular(() => this.changeIcon2());
@@ -121,8 +118,6 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
121118
* Replacement of `changeIcon` for more modifications.
122119
*/
123120
private changeIcon2(): void {
124-
this.setClassName();
125-
126121
// It is used to hydrate the icon component properly when
127122
// zoneless change detection is used in conjunction with server-side rendering.
128123
const removeTask = this.pendingTasks.add();
@@ -151,7 +146,6 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
151146

152147
if (svgOrRemove) {
153148
this.setSVGData(svgOrRemove);
154-
this.handleSpin(svgOrRemove);
155149
this.handleRotate(svgOrRemove);
156150
}
157151
});
@@ -160,14 +154,6 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
160154
});
161155
}
162156

163-
private handleSpin(svg: SVGElement): void {
164-
if (this.spin || this.type === 'loading') {
165-
this.renderer.addClass(svg, 'anticon-spin');
166-
} else {
167-
this.renderer.removeClass(svg, 'anticon-spin');
168-
}
169-
}
170-
171157
private handleRotate(svg: SVGElement): void {
172158
if (this.nzRotate) {
173159
this.renderer.setAttribute(svg, 'style', `transform: rotate(${this.nzRotate}deg)`);
@@ -176,14 +162,6 @@ export class NzIconDirective extends IconDirective implements OnChanges, AfterCo
176162
}
177163
}
178164

179-
private setClassName(): void {
180-
if (this.cacheClassName) {
181-
this.renderer.removeClass(this.el, this.cacheClassName);
182-
}
183-
this.cacheClassName = `anticon-${this.type}`;
184-
this.renderer.addClass(this.el, this.cacheClassName);
185-
}
186-
187165
private setSVGData(svg: SVGElement): void {
188166
this.renderer.setAttribute(svg, 'data-icon', this.type as string);
189167
this.renderer.setAttribute(svg, 'aria-hidden', 'true');

0 commit comments

Comments
 (0)