2021-04-11 15:48:50 +04:30
|
|
|
import { Component, ElementRef, Input, Inject, ChangeDetectorRef, OnChanges, SimpleChanges } from '@angular/core';
|
|
|
|
|
import { Icons } from './icons.provider';
|
2021-04-11 19:04:24 +02:00
|
|
|
import { IconData } from '../icons/types';
|
|
|
|
|
import { createElement } from '../create-element';
|
2021-04-11 15:48:50 +04:30
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'lucide-angular, lucide-icon, i-lucide, span-lucide',
|
|
|
|
|
template: `<ng-content></ng-content>`,
|
|
|
|
|
styles: [`
|
|
|
|
|
:host {
|
|
|
|
|
display: inline-block;
|
|
|
|
|
fill: none;
|
|
|
|
|
stroke: inherit;
|
|
|
|
|
stroke-width: inherit;
|
|
|
|
|
stroke-linecap: round;
|
|
|
|
|
stroke-linejoin: round;
|
|
|
|
|
}
|
|
|
|
|
`]
|
|
|
|
|
})
|
2021-04-11 19:04:24 +02:00
|
|
|
|
2021-04-11 15:48:50 +04:30
|
|
|
export class LucideAngularComponent implements OnChanges {
|
|
|
|
|
@Input() name!: string;
|
|
|
|
|
@Input() img!: IconData;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(ElementRef) private elem: ElementRef,
|
|
|
|
|
@Inject(ChangeDetectorRef) private changeDetector: ChangeDetectorRef,
|
|
|
|
|
@Inject(Icons) private icons: Icons
|
|
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
if (changes.name) {
|
|
|
|
|
// icons are provided as an array of objects because of "multi: true"
|
|
|
|
|
const icons = Object.assign({}, ...(this.icons as any as object[]));
|
|
|
|
|
const icoOfName = icons[this.toPascalCase(changes.name.currentValue)] || '';
|
|
|
|
|
|
|
|
|
|
if (!icoOfName) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Icon not found: ${changes.name.currentValue}\n` +
|
|
|
|
|
`Please check icon name or \'lucide icon list\'`
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
const icoElement = createElement(icoOfName);
|
|
|
|
|
icoElement.setAttribute('stroke-width', 'inherit');
|
|
|
|
|
icoElement.setAttribute('fill', 'inherit');
|
|
|
|
|
icoElement.removeAttribute('width');
|
|
|
|
|
icoElement.removeAttribute('height');
|
|
|
|
|
|
|
|
|
|
this.elem.nativeElement.innerHTML = '';
|
|
|
|
|
this.elem.nativeElement.append(icoElement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (changes.img) {
|
|
|
|
|
const icoElement = createElement(changes.img.currentValue);
|
|
|
|
|
icoElement.setAttribute('stroke-width', 'inherit');
|
|
|
|
|
icoElement.setAttribute('fill', 'inherit');
|
|
|
|
|
icoElement.removeAttribute('width');
|
|
|
|
|
icoElement.removeAttribute('height');
|
|
|
|
|
|
|
|
|
|
this.elem.nativeElement.innerHTML = '';
|
|
|
|
|
this.elem.nativeElement.append(icoElement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.changeDetector.markForCheck();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toPascalCase(str: string): string {
|
|
|
|
|
return str.replace(/(\w)([a-z0-9]*)(_|-|\s*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
}
|