Skip to content

Commit 9ad6502

Browse files
committed
refactor(icon.component): cleanup, classList simplify
1 parent 75aa22a commit 9ad6502

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

Diff for: projects/coreui-icons-angular/src/lib/icon/icon.component.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { Component, DebugElement, ViewChild, inject } from '@angular/core';
2+
import { Component, DebugElement, inject, ViewChild } from '@angular/core';
33
import { By } from '@angular/platform-browser';
44

55
import { cilList } from '@coreui/icons';
@@ -8,9 +8,9 @@ import { IconSetService } from '../icon-set';
88
import { IconComponent } from './icon.component';
99

1010
@Component({
11-
template: '<c-icon #icon name="cil-list" size="lg" class="test" />',
12-
imports: [IconComponent],
13-
providers: [IconSetService]
11+
template: `<c-icon #icon name="cil-list" size="lg" class="test" />`,
12+
imports: [IconComponent],
13+
providers: [IconSetService]
1414
})
1515
class TestComponent {
1616
iconSet = inject(IconSetService);

Diff for: projects/coreui-icons-angular/src/lib/icon/icon.component.ts

+10-28
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import { NgClass } from '@angular/common';
2-
import {
3-
afterNextRender,
4-
Component,
5-
computed,
6-
effect,
7-
ElementRef,
8-
inject,
9-
input,
10-
Renderer2,
11-
signal,
12-
viewChild
13-
} from '@angular/core';
2+
import { Component, computed, effect, ElementRef, inject, input, Renderer2, viewChild } from '@angular/core';
143
import { DomSanitizer } from '@angular/platform-browser';
154

165
import { HtmlAttributesDirective } from '../shared/html-attr.directive';
@@ -19,25 +8,18 @@ import { IconSize, IIcon, NgCssClass } from './icon.interface';
198
import { transformName } from './icon.utils';
209

2110
@Component({
22-
exportAs: 'cIconComponent',
23-
imports: [NgClass, HtmlAttributesDirective],
24-
selector: 'c-icon',
25-
styleUrls: ['./icon.component.scss'],
26-
templateUrl: './icon.component.svg',
27-
host: { ngSkipHydration: 'true', style: 'display: none' }
11+
exportAs: 'cIconComponent',
12+
imports: [NgClass, HtmlAttributesDirective],
13+
selector: 'c-icon',
14+
styleUrls: ['./icon.component.scss'],
15+
templateUrl: './icon.component.svg',
16+
host: { ngSkipHydration: 'true', style: 'display: none' }
2817
})
2918
export class IconComponent implements IIcon {
3019
readonly #renderer = inject(Renderer2);
3120
readonly #elementRef = inject(ElementRef);
3221
readonly #sanitizer = inject(DomSanitizer);
3322
readonly #iconSet = inject(IconSetService);
34-
readonly #hostElement = signal<ElementRef<any> | undefined>(undefined);
35-
36-
constructor() {
37-
afterNextRender(() => {
38-
this.#hostElement.set(this.#elementRef);
39-
});
40-
}
4123

4224
readonly content = input<string | string[] | any[]>();
4325

@@ -53,12 +35,12 @@ export class IconComponent implements IIcon {
5335

5436
readonly svgElementRef = viewChild<ElementRef>('svgElement');
5537

56-
readonly svgElementEffect = effect(() => {
38+
readonly #svgElementEffect = effect(() => {
5739
const svgElementRef = this.svgElementRef();
58-
const hostElement = this.#hostElement()?.nativeElement;
40+
const hostElement: Element = this.#elementRef.nativeElement;
5941
if (svgElementRef && hostElement) {
6042
const svgElement = svgElementRef.nativeElement;
61-
hostElement.classList?.values()?.forEach((item: string) => {
43+
hostElement.classList?.forEach((item: string) => {
6244
this.#renderer.addClass(svgElement, item);
6345
});
6446
const parentElement = this.#renderer.parentNode(hostElement);

0 commit comments

Comments
 (0)