11import { AfterViewInit , Component , ElementRef , Input , Renderer2 , ViewChild } from '@angular/core' ;
22import { DomSanitizer , SafeHtml } from '@angular/platform-browser' ;
33import { IconSetService } from '../icon-set/icon-set.service' ;
4+ import { IconSize , IIcon } from './icon.interface' ;
45
56@Component ( {
67 selector : 'c-icon' ,
78 templateUrl : './icon.component.svg' ,
89 styleUrls : [ './icon.component.scss' ]
910} )
10- export class IconComponent implements AfterViewInit {
11+ export class IconComponent implements IIcon , AfterViewInit {
1112
1213 @Input ( ) attributes : any = { role : 'img' } ;
13- @Input ( ) content ?: string | string [ ] ;
14- @Input ( ) size : 'custom' | 'custom-size' | 'sm' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | '6xl' | '7xl' | '8xl' | '9xl' | '' = '' ;
15- @Input ( ) src ?: string ;
14+ @Input ( ) content ?: string | string [ ] | any [ ] ;
15+ @Input ( ) size : IconSize = '' ;
1616 @Input ( ) title ?: string ;
1717 @Input ( ) use = '' ;
18- @Input ( ) customClasses : string | string [ ] | Set < string > | { [ klass : string ] : any } = '' ;
18+ @Input ( ) customClasses ? : string | string [ ] | Set < string > | { [ klass : string ] : any } = '' ;
1919 @Input ( ) width ?: string ;
2020 @Input ( ) height ?: string ;
2121
2222 @Input ( )
2323 set name ( name : string ) {
24- const nameIsKebabCase = name . includes ( '-' ) ;
25- this . _name = nameIsKebabCase ? this . toCamelCase ( name ) : name ;
24+ this . _name = name . includes ( '-' ) ? this . toCamelCase ( name ) : name ;
2625 }
27-
2826 get name ( ) : string {
2927 return this . _name ;
3028 }
31-
3229 private _name ! : string ;
3330
3431 @Input ( )
3532 set viewBox ( viewBox : string ) {
3633 this . _viewBox = viewBox ;
3734 }
3835 get viewBox ( ) : string {
39- return this . _viewBox ?? `0 0 ${ this . scale } ` ;
36+ return this . _viewBox ?? this . scale ;
4037 }
4138 private _viewBox ! : string ;
4239
4340 @ViewChild ( 'svgElement' , { read : ElementRef } ) svgElementRef ! : ElementRef ;
4441
42+ get innerHtml ( ) : SafeHtml {
43+ const code = Array . isArray ( this . code ) ? this . code [ 1 ] || this . code [ 0 ] : this . code ?? '' ;
44+ // todo proper sanitize
45+ // const sanitized = this.sanitizer.sanitize(SecurityContext.HTML, code);
46+ return this . sanitizer . bypassSecurityTrustHtml ( ( this . titleCode + code ) ?? '' ) ;
47+ }
48+
4549 constructor (
4650 private renderer : Renderer2 ,
4751 private elementRef : ElementRef ,
@@ -65,29 +69,26 @@ export class IconComponent implements AfterViewInit {
6569 return this . title ? `<title>${ this . title } </title>` : '' ;
6670 }
6771
68- get code ( ) : string [ ] | undefined | string {
72+ get code ( ) : string | string [ ] | undefined {
6973 if ( this . content ) {
7074 return this . content ;
71- } else if ( this . iconSet ) {
75+ }
76+ if ( this . iconSet && this . name ) {
7277 return this . iconSet . getIcon ( this . name ) ;
7378 }
79+ if ( this . name && ! this . iconSet ?. icons [ this . name ] )
7480 console . warn ( `c-icon component: icon name '${ this . name } ' does not exist for IconSet service. ` +
7581 `To use icon by 'name' prop you need to add it to IconSet service. \n` ,
7682 this . name
7783 ) ;
7884 return undefined ;
7985 }
8086
81- get iconCode ( ) : SafeHtml {
82- const code = Array . isArray ( this . code ) ? this . code [ 1 ] || this . code [ 0 ] : this . code ;
83- return this . sanitizer . bypassSecurityTrustHtml ( this . titleCode + code ) ;
84- }
85-
8687 get scale ( ) : string {
87- return Array . isArray ( this . code ) && this . code . length > 1 ? this . code [ 0 ] : '64 64' ;
88+ return Array . isArray ( this . code ) && this . code . length > 1 ? `0 0 ${ this . code [ 0 ] } ` : '0 0 64 64' ;
8889 }
8990
90- get computedSize ( ) : 'custom-size' | 'sm' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | '6xl' | '7xl' | '8xl' | '9xl' | '' {
91+ get computedSize ( ) : Exclude < IconSize , 'custom' > | undefined {
9192 const addCustom = ! this . size && ( this . width || this . height ) ;
9293 return this . size === 'custom' || addCustom ? 'custom-size' : this . size ;
9394 }
0 commit comments