Skip to content

Commit 6a19217

Browse files
committed
refactor(icon): afterNextRender in case of SSR
1 parent a689ee1 commit 6a19217

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { toCamelCase } from './icon.utils';
1212
templateUrl: './icon.component.svg',
1313
styleUrls: ['./icon.component.scss'],
1414
standalone: true,
15-
imports: [NgClass, NgIf, HtmlAttributesDirective]
15+
imports: [NgClass, NgIf, HtmlAttributesDirective],
16+
// eslint-disable-next-line @angular-eslint/no-host-metadata-property
17+
host: { ngSkipHydration: 'true' }
1618
})
1719
export class IconComponent implements IIcon, AfterViewInit {
1820

projects/coreui-icons-angular/src/lib/icon/icon.directive.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ describe('IconDirective', () => {
4141
sanitizer = fixture.componentRef.injector.get(DomSanitizer);
4242
iconSetService = fixture.componentRef.injector.get(IconSetService);
4343
});
44-
4544
it('should create an instance', () => {
46-
const directive = new IconDirective(renderer, svgEl, sanitizer, iconSetService);
47-
expect(directive).toBeTruthy();
45+
TestBed.runInInjectionContext(() => {
46+
const directive = new IconDirective(renderer, svgEl, sanitizer, iconSetService);
47+
expect(directive).toBeTruthy();
48+
});
4849
});
4950
it('icon classes should be applied', () => {
5051
expect(svgEl.nativeElement).toBeTruthy();

projects/coreui-icons-angular/src/lib/icon/icon.directive.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, ElementRef, HostBinding, Input, Renderer2 } from '@angular/core';
1+
import { afterNextRender, Directive, ElementRef, HostBinding, Input, Renderer2 } from '@angular/core';
22
import { DomSanitizer } from '@angular/platform-browser';
33

44
import { IconSetService } from '../icon-set';
@@ -51,7 +51,7 @@ export class IconDirective implements IIcon {
5151
return this.customClasses ?? classes;
5252
}
5353

54-
@HostBinding('innerHtml')
54+
// @HostBinding('innerHtml')
5555
get innerHtml() {
5656
const code = Array.isArray(this.code) ? this.code[1] || this.code[0] : this.code ?? '';
5757
// todo proper sanitize
@@ -64,7 +64,11 @@ export class IconDirective implements IIcon {
6464
private elementRef: ElementRef,
6565
private sanitizer: DomSanitizer,
6666
private iconSet: IconSetService
67-
) { }
67+
) {
68+
afterNextRender(() => {
69+
this.elementRef.nativeElement.innerHTML = this.innerHtml;
70+
});
71+
}
6872

6973
get titleCode(): string {
7074
return this.title ? `<title>${this.title}</title>` : '';

0 commit comments

Comments
 (0)