openaire-library/utils/icons/icons.component.ts

83 lines
1.9 KiB
TypeScript
Raw Normal View History

import {Component, ElementRef, Input, OnInit} from "@angular/core";
import {IconsService} from "./icons.service";
/**
* First add your icon to Icon registry, by adding this to your component Module.
*
* e.g export class ExampleModule {
* constructor(private iconsService: IconsService) {
* this.iconsService.registerIcons([arrow_right])
* }
* }
*
* */
@Component({
selector: 'icon',
template: `
<span *ngIf="svg" class="uk-icon" [class.uk-flex]="flex" [ngClass]="customClass" [ngStyle]="style" [innerHTML]="svg | safeHtml"></span>
2022-01-17 18:46:22 +01:00
<span *ngIf="!svg && iconName" class="material-icons" [ngClass]="customClass + (type?(' ' + type):'')" [ngStyle]="style">{{iconName}}</span>
`
})
2022-01-17 18:46:22 +01:00
export class IconsComponent implements OnInit {
public svg;
2022-01-17 18:46:22 +01:00
public iconName: string;
public style;
/**
* True if this icon should have display flex (Optional, Default: false)
* */
@Input()
public flex = false;
/**
*
* Add custom class(es)(Optional)
* */
@Input()
2022-01-17 18:46:22 +01:00
public customClass = '';
/**
* Color of svg (Optional)
* */
@Input()
public fill;
/**
* Color of svg stroke (Optional)
* */
@Input()
public stroke;
/**
2022-01-17 18:46:22 +01:00
* Size of icon (Default: 1)
* */
@Input()
public ratio = 1;
2022-01-17 18:46:22 +01:00
/**
* In case of Material icon only. Type of icon (Optional)
* */
@Input()
public type: "outlined" | "round" | "sharp" | "two-tone" | null = null;
/**
* Name of icon in registry (Required)
* */
@Input()
set name(iconName: string) {
2022-01-17 18:46:22 +01:00
this.iconName = iconName;
this.svg = this.iconsService.getIcon(iconName);
}
2022-01-17 18:46:22 +01:00
constructor(private iconsService: IconsService) {}
ngOnInit() {
2022-01-17 18:46:22 +01:00
if(this.svg) {
this.style = {
transform: 'scale(' + this.ratio + ')',
fill: this.fill,
stroke: this.stroke
};
} else {
this.style = {
"font-size.px": this.ratio*20,
fill: this.fill,
stroke: this.stroke
};
}
}
}