1
1
import { AfterViewInit , Component , ElementRef , Input , Renderer2 , ViewChild } from '@angular/core' ;
2
2
import { DomSanitizer , SafeHtml } from '@angular/platform-browser' ;
3
3
import { IconSetService } from '../icon-set/icon-set.service' ;
4
+ import { IconSize , IIcon } from './icon.interface' ;
4
5
5
6
@Component ( {
6
7
selector : 'c-icon' ,
7
8
templateUrl : './icon.component.svg' ,
8
9
styleUrls : [ './icon.component.scss' ]
9
10
} )
10
- export class IconComponent implements AfterViewInit {
11
+ export class IconComponent implements IIcon , AfterViewInit {
11
12
12
13
@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 = '' ;
16
16
@Input ( ) title ?: string ;
17
17
@Input ( ) use = '' ;
18
- @Input ( ) customClasses : string | string [ ] | Set < string > | { [ klass : string ] : any } = '' ;
18
+ @Input ( ) customClasses ? : string | string [ ] | Set < string > | { [ klass : string ] : any } = '' ;
19
19
@Input ( ) width ?: string ;
20
20
@Input ( ) height ?: string ;
21
21
22
22
@Input ( )
23
23
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 ;
26
25
}
27
-
28
26
get name ( ) : string {
29
27
return this . _name ;
30
28
}
31
-
32
29
private _name ! : string ;
33
30
34
31
@Input ( )
35
32
set viewBox ( viewBox : string ) {
36
33
this . _viewBox = viewBox ;
37
34
}
38
35
get viewBox ( ) : string {
39
- return this . _viewBox ?? `0 0 ${ this . scale } ` ;
36
+ return this . _viewBox ?? this . scale ;
40
37
}
41
38
private _viewBox ! : string ;
42
39
43
40
@ViewChild ( 'svgElement' , { read : ElementRef } ) svgElementRef ! : ElementRef ;
44
41
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
+
45
49
constructor (
46
50
private renderer : Renderer2 ,
47
51
private elementRef : ElementRef ,
@@ -65,29 +69,26 @@ export class IconComponent implements AfterViewInit {
65
69
return this . title ? `<title>${ this . title } </title>` : '' ;
66
70
}
67
71
68
- get code ( ) : string [ ] | undefined | string {
72
+ get code ( ) : string | string [ ] | undefined {
69
73
if ( this . content ) {
70
74
return this . content ;
71
- } else if ( this . iconSet ) {
75
+ }
76
+ if ( this . iconSet && this . name ) {
72
77
return this . iconSet . getIcon ( this . name ) ;
73
78
}
79
+ if ( this . name && ! this . iconSet ?. icons [ this . name ] )
74
80
console . warn ( `c-icon component: icon name '${ this . name } ' does not exist for IconSet service. ` +
75
81
`To use icon by 'name' prop you need to add it to IconSet service. \n` ,
76
82
this . name
77
83
) ;
78
84
return undefined ;
79
85
}
80
86
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
-
86
87
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' ;
88
89
}
89
90
90
- get computedSize ( ) : 'custom-size' | 'sm' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | '6xl' | '7xl' | '8xl' | '9xl' | '' {
91
+ get computedSize ( ) : Exclude < IconSize , 'custom' > | undefined {
91
92
const addCustom = ! this . size && ( this . width || this . height ) ;
92
93
return this . size === 'custom' || addCustom ? 'custom-size' : this . size ;
93
94
}
0 commit comments