Introducing absoluteStrokeWidth option on Lucide Components (#939)

* Add more music icons and another mic icon (#746)

* Revert "Add more music icons and another mic icon (#746)" (#750)

This reverts commit 57cba6ae0e.

* add scale Stroke width

* Added scaleStrokeWidth prop to all packages

* Add scaleStrokeWidth to types

* Rename scaleStrokeWidth to absoluteStrokeWidth

* Adds common API elements to the Angular package (#949)

* Almost complete rewrite of the Angular package

* Update tsconfig.spec.json

* fixes icon build export file name

* Updates Angular documentation with the new properties + provider injection

* Update lucide-angular.md

* refactored scaleStrokeWidth to be absoluteStrokeWidth to match other packages

* removed codelyzer from devDeps + added flexible angular core dependencies

* Deprecates createElement helper in favour of Renderer2 to support SSR

---------

Co-authored-by: Karsa <karsa@karsa.org>

* Add absoluteStrokeWidth in docs

* update snapshots

* Manual merge of main

* Fixed incorrectly merged pnpm-lock.yaml

* Fixes lucide-angular build

* [lucide-angular] Global configuration for properties + bugfix for legacy icon provider   (#1012)

* Almost complete rewrite of the Angular package

* Update tsconfig.spec.json

* fixes icon build export file name

* Updates Angular documentation with the new properties + provider injection

* Update lucide-angular.md

* refactored scaleStrokeWidth to be absoluteStrokeWidth to match other packages

* removed codelyzer from devDeps + added flexible angular core dependencies

* Deprecates createElement helper in favour of Renderer2 to support SSR

* Added global configuration and fixed undefined bug in legacy icon provider. Also updated README.md

* Replaces removed line in README.md

* Fixes merge error

* Updates export template to use the non-deprecated type

* downgrade building to ng-cli@13

* downgrade to es2020

---------

Co-authored-by: Karsa <karsa@karsa.org>

* rename scaleStrokeWidth to absoluteStrokeWidth in readme

---------

Co-authored-by: it-is-not <72697755+it-is-not@users.noreply.github.com>
Co-authored-by: Karsa <contact@karsa.org>
Co-authored-by: Eric Fennis <eric@dreamteam.nl>
Co-authored-by: Karsa <karsa@karsa.org>
Co-authored-by: Eric Fennis <eric.fennis@nac41112.nedap.local>
This commit is contained in:
Eric Fennis
2023-04-20 16:08:34 +02:00
committed by GitHub
parent f35f45c7b3
commit a87ae2a92b
63 changed files with 7480 additions and 6627 deletions

View File

@@ -0,0 +1,43 @@
module.exports = {
"root": true,
"overrides": [
{
"files": [
"*.ts"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates"
],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "lucide",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": ["lucide", "i", "span"],
"style": "kebab-case"
}
]
}
},
{
"files": [
"*.html"
],
"extends": [
"plugin:@angular-eslint/template/recommended"
],
"rules": {}
}
]
};

View File

@@ -1,13 +1,13 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# compiled output
# Compiled output
/dist
/tmp
/out-tsc
# Only exists if Bazel was run
/bazel-out
# dependencies
# Node
/node_modules
# profiling files
@@ -23,7 +23,8 @@ speed-measure-plugin*.json
.settings/
*.sublime-workspace
# IDE - VSCode
# Visual Studio Code
/.vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
@@ -32,7 +33,7 @@ speed-measure-plugin*.json
.history/*
.editorconfig
# misc
# Miscellaneous
/.sass-cache
/connect.lock
/coverage
@@ -42,7 +43,7 @@ yarn-error.log
testem.log
/typings
# System Files
# System files
.DS_Store
Thumbs.db
@@ -51,4 +52,4 @@ package-lock.json
src/createElement.js
# angular cache
.angular/cache
.angular/cache

View File

@@ -6,150 +6,106 @@ Implementation of the lucide icon library for angular applications.
## Installation
```sh
```bash
yarn add lucide-angular
```
or
```sh
```bash
npm install lucide-angular
```
## How to use
There are three ways for use this library.
### Step 1: Import `LucideAngularModule`
### Method 1: createElement
After install `lucide-angular` change content of file `app.component.html` and `app.component.ts`.
```html
<!-- app.component.html -->
<div id="lucide-icon"></div>
```
In any Angular module you wish to use Lucide icons in, you have to import `LucideAngularModule`, and pick any icons you wish to use:
```js
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { createElement } from 'lucide-angular';
import { Activity } from 'lucide-angular/icons';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
ngOnInit(): void {
const div = document.getElementById('lucide-icon');
const elm = createElement(Activity);
elm.setAttribute('color', 'red'); // or set `width`, `height`, `fill`, `stroke-width`, ...
if (div) {
div.appendChild(elm);
}
}
}
```
### Method 2: User **Tag** with **name** property
After install `lucide-angular` change content of file `app.component.html`, `app.component.ts`, `app.component.css` and `app.module.ts`.
```js
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LucideAngularModule, AlarmCheck, Edit } from 'lucide-angular';
import { LucideAngularModule, File, Home, Menu, UserCheck } from 'lucide-angular';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
LucideAngularModule.pick({ AlarmCheck, Edit }) // add all of icons that is imported.
],
providers: [],
bootstrap: [AppComponent]
LucideAngularModule.pick({File, Home, Menu, UserCheck})
]
})
export class AppModule {}
export class AppModule { }
```
### Step 2: Use the icons in templates
Within your templates you may now use one of the following component tags to insert an icon:
```html
<!-- app.component.html -->
<lucide-icon name="alarm-check" class="myicon"></lucide-icon>
<lucide-icon name="edit" class="myicon"></lucide-icon>
<lucide-angular name="file" class="my-icon"></lucide-angular>
<lucide-icon name="home" class="my-icon"></lucide-icon>
<i-lucide name="menu" class="my-icon"></i-lucide>
<span-lucide name="user-check" class="my-icon"></span-lucide>
```
### Method 3: User **Tag** with **img** property
### Props
After install `lucide-angular` change content of file `app.component.html`, `app.component.ts`, `app.component.css` and `app.module.ts`.
You can pass additional props to adjust the icon appearance.
```js
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
| name | type | default |
| ------------------ | --------- | ------------ |
| `size` | _number_ | 24 |
| `color` | _string_ | currentColor |
| `strokeWidth` | _number_ | 2 |
| `absoluteStrokeWidth` | _boolean_ | true |
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LucideAngularModule } from 'lucide-angular';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, LucideAngularModule.pick({})],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
```html
<i-lucide name="home" [size]="48" color="red" [strokeWidth]="1"></i-lucide>
```
```xml
<!-- app.component.html -->
<lucide-icon [img]="ico1" class="myicon"></lucide-icon>
<lucide-icon [img]="ico2" class="myicon"></lucide-icon>
```
### Global configuration
```js
// app.component.ts
import { Component } from '@angular/core';
import { Airplay, Circle } from 'lucide-angular';
You can inject the `LucideIconConfig` service in your root component to globally configure the default property values as defined above.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
ico1 = Airplay;
ico2 = Circle;
### Styling using a custom CSS class
Any extra HTML attribute is ignored, but the `class` attribute
is passed onto the internal SVG image element and it can be used to style it:
```css
svg.my-icon {
width: 12px;
height: 12px;
stroke-width: 3;
}
```
## Notes
## Injecting multiple icon providers
### Import all icons
In `Method 2`: import all icons in `app.module.ts` by:
You may provide additional icons using the `LUCIDE_ICONS` injection token,
which accepts multiple providers of the interface `LucideIconsProviderInterface`
with the utility class `LucideIconsProvider` available for easier usage:
```js
...
import { icons } from 'lucide-angular';
....
LucideAngularModule.pick(icons)
....
import { LUCIDE_ICONS, LucideIconProvider } from 'lucide-angular';
import { MyIcon } from './icons/my-icon';
const myIcons = {MyIcon};
@NgModule({
providers: [
{provide: LUCIDE_ICONS, multi: true, useValue: new LucideIconProvider(myIcons)},
]
})
export class AppModule { }
```
### Tags
To add custom icons, you will first need to convert them to an [svgson format](https://github.com/elrumordelaluz/svgson).
You can use the following tags instead of `lucide-icon`:
## Loading all icons
- lucide-angular
- i-lucide
- span-lucide
> :warning: You may also opt to import all icons if necessary using the following format but be aware that this will significantly increase your application build size.
All of the above are the same
```js
import { icons } from 'lucide-angular';
...
LucideAngularModule.pick(icons)
```

View File

@@ -5,21 +5,24 @@
"projects": {
"lucide-angular": {
"projectType": "library",
"root": "",
"root": ".",
"sourceRoot": "src",
"prefix": "lib",
"prefix": "lucide",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:ng-packagr",
"options": {
"tsConfig": "tsconfig.lib.json",
"project": "ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "tsconfig.lib.json"
}
}
},
"defaultConfiguration": "production"
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
@@ -30,19 +33,19 @@
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"builder": "@angular-eslint/builder:lint",
"options": {
"tsConfig": [
"tsconfig.lib.json",
"tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
"lintFilePatterns": [
"src/**/*.ts",
"src/**/*.html"
]
}
}
}
}
},
"cli": {
"packageManager": "pnpm"
},
"defaultProject": "lucide-angular"
}

View File

@@ -2,6 +2,6 @@
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
"dest": "dist",
"lib": {
"entryFile": "src/index.ts"
"entryFile": "src/public-api.ts"
}
}

View File

@@ -28,44 +28,56 @@
"build": "pnpm clean && pnpm copy:license && pnpm build:icons && pnpm build:ng",
"copy:license": "cp ../../LICENSE ./LICENSE",
"clean": "rm -rf dist && rm -rf ./src/icons/*.ts",
"build:icons": "build-icons --output=./src --templateSrc=./scripts/exportTemplate.mjs --iconFileExtension=.ts --exportFileName=index.ts",
"build:ng": "ng build --configuration production",
"build:icons": "build-icons --output=./src --templateSrc=./scripts/exportTemplate.mjs --iconFileExtension=.ts --exportFileName=lucide-icons.ts",
"build:ng": "ng build --configuration production",
"test": "ng test --no-watch --no-progress --browsers=ChromeHeadlessCI",
"test:watch": "ng test",
"lint": "ng lint",
"lint": "npx eslint 'src/**/*.{js,jsx,ts,tsx,html,css,scss}' --quiet --fix",
"format": "npx prettier 'src/**/*.{js,jsx,ts,tsx,html,css,scss}' --write",
"e2e": "ng e2e",
"version": "pnpm version --git-tag-version=false"
},
"dependencies": {
"tslib": "^2.4.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "~14.2.6",
"@angular/cli": "~14.2.6",
"@angular/common": "~14.2.7",
"@angular/compiler": "~14.2.7",
"@angular/compiler-cli": "~14.2.7",
"@angular/core": "~14.2.7",
"@angular/platform-browser": "~14.2.7",
"@angular/platform-browser-dynamic": "~14.2.7",
"@angular-devkit/build-angular": "~13.3.11",
"@angular-eslint/builder": "~13.0.0",
"@angular-eslint/eslint-plugin": "~13.0.0",
"@angular-eslint/eslint-plugin-template": "~13.0.0",
"@angular-eslint/schematics": "~13.0.0",
"@angular-eslint/template-parser": "~13.0.0",
"@angular/cli": "~13.3.11",
"@angular/common": "~13.3.0",
"@angular/compiler": "~13.3.0",
"@angular/compiler-cli": "~13.3.0",
"@angular/core": "~13.3.0",
"@angular/platform-browser": "~13.3.0",
"@angular/platform-browser-dynamic": "~13.3.0",
"@lucide/build-icons": "workspace:*",
"@types/jasmine": "~4.3.0",
"@types/node": "^18.11.4",
"codelyzer": "^6.0.2",
"jasmine-core": "~3.10.1",
"@types/jasmine": "~3.10.0",
"@types/node": "^12.11.1",
"@typescript-eslint/eslint-plugin": "5.48.2",
"@typescript-eslint/parser": "5.48.2",
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jasmine-core": "~4.0.0",
"jasmine-spec-reporter": "~7.0.0",
"karma": "~6.3.14",
"karma": "~6.3.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.1",
"karma-jasmine-html-reporter": "^1.7.0",
"ng-packagr": "^14.2.1",
"karma-coverage": "~2.1.0",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "~1.7.0",
"ng-packagr": "^13.3.0",
"prettier": "^2.8.4",
"protractor": "~7.0.0",
"puppeteer": "^8.0.0",
"rxjs": "7.5.7",
"rxjs": "~7.5.0",
"ts-node": "~10.9.1",
"tslint": "~6.1.0",
"typescript": "~4.8.4",
"zone.js": "^0.11.8"
"tslib": "^2.3.0",
"typescript": "~4.6.2",
"zone.js": "~0.11.4"
},
"peerDependencies": {
"@angular/common": "13.x - 15.x",
"@angular/core": "13.x - 15.x"
}
}

View File

@@ -1,11 +1,12 @@
export default ({ componentName, children }) => `
import { IconData } from './types';
import { LucideIconData } from './types';
import defaultAttributes from './constants/default-attributes';
const ${componentName}: IconData = [
'svg',
defaultAttributes,
${JSON.stringify(children)}
/* eslint-disable no-shadow-restricted-names */
const ${componentName}: LucideIconData = [
'svg',
defaultAttributes,
${JSON.stringify(children)}
];
export default ${componentName};

View File

@@ -1,26 +0,0 @@
import { IconData } from '../icons/types'
/**
* Creates a new SVGElement from icon node
* @param {string} tag
* @param {object} attrs
* @param {array} children
* @returns {SVGElement}
*/
export const createElement = ([tag, attrs, children = []]: IconData): SVGElement => {
const element = document.createElementNS('http://www.w3.org/2000/svg', tag);
Object.keys(attrs).forEach(name => {
element.setAttribute(name, attrs[name]);
});
if (children.length) {
children.forEach((child: IconData) => {
const childElement = createElement(child);
element.appendChild(childElement);
});
}
return element;
};

View File

@@ -1,11 +1,11 @@
export default {
xmlns: 'http://www.w3.org/2000/svg',
width: 24,
height: 24,
viewBox: '0 0 24 24',
fill: 'none',
stroke: 'currentColor',
'stroke-width': 2,
'stroke-linecap': 'round',
'stroke-linejoin': 'round',
xmlns: 'http://www.w3.org/2000/svg',
width: 24,
height: 24,
viewBox: '0 0 24 24',
fill: 'none',
stroke: 'currentColor',
'stroke-width': 2,
'stroke-linecap': 'round',
'stroke-linejoin': 'round',
};

View File

@@ -2,7 +2,7 @@
"ngPackage": {
"dest": "dist",
"lib": {
"entryFile": "./index.ts"
"entryFile": "../public-api.ts"
}
}
}

View File

@@ -1,3 +1,13 @@
export type IconNode = readonly [string, object];
export type IconData = readonly [string, object, IconNode[]? ];
export type Icons = { [key: string]: IconData }
type HtmlAttributes = { [key: string]: string | number };
export type LucideIconNode = readonly [string, HtmlAttributes];
export type LucideIconData = readonly [string, HtmlAttributes, LucideIconNode[]?];
export type LucideIcons = { [key: string]: LucideIconData };
/** @deprecated Use LucideIconData instead. Will be removed in v1.0. */
export type IconData = LucideIconData;
/** @deprecated Use LucideIconNode instead. Will be removed in v1.0. */
export type IconNode = LucideIconNode;
/** @deprecated Use LucideIcons instead. Will be removed in v1.0. */
export type Icons = LucideIcons;

View File

@@ -1,7 +0,0 @@
import * as icons from './icons';
export * from './lib/lucide-angular.component';
export * from './lib/lucide-angular.module';
export * from './helpers/create-element';
export * from './icons';
export { icons };

3
packages/lucide-angular/src/lib/icons.provider.ts Executable file → Normal file
View File

@@ -1,3 +1,4 @@
/** @deprecated Use the injection token LUCIDE_ICONS instead. Will be removed in v1.0. */
export class Icons {
constructor(private icons: object) {}
constructor(private icons: object) {}
}

View File

@@ -1,28 +1,103 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LucideAngularModule } from './lucide-angular.module';
import { LucideAngularComponent } from './lucide-angular.component';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {LucideAngularModule} from './lucide-angular.module';
import {formatFixed, LucideAngularComponent} from './lucide-angular.component';
import defaultAttributes from '../icons/constants/default-attributes';
import {LucideIcons} from '../icons/types';
describe('LucideAngularComponent', () => {
let component: LucideAngularComponent;
let fixture: ComponentFixture<LucideAngularComponent>;
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
const getSvgAttribute = (attr: string) =>
testHostFixture.nativeElement.querySelector('svg').getAttribute(attr);
const testIcons: LucideIcons = {
Demo: [
'svg',
defaultAttributes,
[['polyline', {points: '1 1 22 22'}]],
],
};
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ LucideAngularComponent ],
imports: [
LucideAngularModule.pick({ })
],
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [LucideAngularComponent, TestHostComponent],
imports: [
LucideAngularModule.pick(testIcons),
]
}).compileComponents();
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
});
it('should create', () => {
testHostFixture.detectChanges();
expect(testHostComponent).toBeTruthy();
});
it('should set color', () => {
const color = 'red';
testHostComponent.setColor(color);
testHostFixture.detectChanges();
expect(getSvgAttribute('stroke')).toBe(color);
});
it('should set size', () => {
const size = 12;
testHostComponent.setSize(size);
testHostFixture.detectChanges();
expect(getSvgAttribute('width')).toBe(size.toString(10));
});
it('should set stroke width', () => {
const strokeWidth = 1.41;
testHostComponent.setStrokeWidth(strokeWidth);
testHostFixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe(strokeWidth.toString(10));
});
it('should adjust stroke width', () => {
const strokeWidth = 2;
const size = 12;
testHostComponent.setStrokeWidth(strokeWidth);
testHostComponent.setSize(12);
testHostComponent.setAbsoluteStrokeWidth(true);
testHostFixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe(
formatFixed(strokeWidth / (size / defaultAttributes.height))
);
});
@Component({
selector: 'lucide-spec-host-component',
template: `
<i-lucide
name="demo"
[color]="color"
[size]="size"
[strokeWidth]="strokeWidth"
[absoluteStrokeWidth]="absoluteStrokeWidth"
></i-lucide>`,
})
.compileComponents();
});
class TestHostComponent {
color?: string;
size?: number;
strokeWidth?: number;
absoluteStrokeWidth = true;
beforeEach(() => {
fixture = TestBed.createComponent(LucideAngularComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
setColor(color: string): void {
this.color = color;
}
it('should create', () => {
expect(component).toBeTruthy();
});
setSize(size: number): void {
this.size = size;
}
setStrokeWidth(strokeWidth: number): void {
this.strokeWidth = strokeWidth;
}
setAbsoluteStrokeWidth(absoluteStrokeWidth: boolean): void {
this.absoluteStrokeWidth = absoluteStrokeWidth;
}
}
});

View File

@@ -1,72 +1,170 @@
import { Component, ElementRef, Input, Inject, ChangeDetectorRef, OnChanges, SimpleChanges } from '@angular/core';
import { Icons } from './icons.provider';
import { IconData } from '../icons/types';
import { createElement } from '../helpers/create-element';
import {ChangeDetectorRef, Component, Renderer2, ElementRef, Inject, Input, OnChanges, SimpleChange} from '@angular/core';
import {LucideIconData} from '../icons/types';
import defaultAttributes from '../icons/constants/default-attributes';
import {LUCIDE_ICONS, LucideIconProviderInterface} from './lucide-icon.provider';
import {LucideIconConfig} from "./lucide-icon.config";
interface TypedChange<T> extends SimpleChange {
previousValue: T;
currentValue: T;
}
type LucideAngularComponentChanges = {
name?: TypedChange<string|LucideIconData>;
color?: TypedChange<string>;
size?: TypedChange<number>;
strokeWidth?: TypedChange<number>;
absoluteStrokeWidth?: TypedChange<boolean>;
};
export function formatFixed(number: number, decimals = 3): string {
return parseFloat(number.toFixed(decimals)).toString(10);
}
@Component({
selector: 'lucide-angular, lucide-icon, i-lucide, span-lucide',
template: `<ng-content></ng-content>`,
styles: [`
:host {
display: inline-block;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
width: 24px;
height: 24px;
}
`]
selector: 'lucide-angular, lucide-icon, i-lucide, span-lucide',
template: '<ng-content></ng-content>',
})
export class LucideAngularComponent implements OnChanges {
@Input() name!: string;
@Input() img!: IconData;
@Input() class?: string;
@Input() name?: string|LucideIconData;
@Input() set img(img: LucideIconData) {
this.name = img;
}
@Input() color?: string;
_size?: number;
get size(): number {
return this._size ?? this.iconConfig.size;
}
constructor(
@Inject(ElementRef) private elem: ElementRef,
@Inject(ChangeDetectorRef) private changeDetector: ChangeDetectorRef,
@Inject(Icons) private icons: Icons
) { }
@Input() set size(value: string | number) {
this._size = this.parseNumber(value);
}
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)] || '';
_strokeWidth?: number;
get strokeWidth(): number {
return this._strokeWidth ?? this.iconConfig.strokeWidth;
}
if (!icoOfName) {
console.warn(
`Icon not found: ${changes.name.currentValue}\n` +
`Please check icon name or \'lucide icon list\'`
@Input() set strokeWidth(value: string | number) {
this._strokeWidth = this.parseNumber(value);
}
@Input() absoluteStrokeWidth = false;
defaultSize: number;
constructor(
@Inject(ElementRef) private elem: ElementRef,
@Inject(Renderer2) private renderer: Renderer2,
@Inject(ChangeDetectorRef) private changeDetector: ChangeDetectorRef,
@Inject(LUCIDE_ICONS) private iconProviders: LucideIconProviderInterface[],
@Inject(LucideIconConfig) private iconConfig: LucideIconConfig,
) {
this.defaultSize = defaultAttributes.height;
}
ngOnChanges(changes: LucideAngularComponentChanges): void {
this.color = this.color ?? this.iconConfig.color;
this.size = this.parseNumber(this.size ?? this.defaultSize);
this.strokeWidth = this.parseNumber(
this.strokeWidth ?? this.iconConfig.strokeWidth
);
} else {
const icoElement = createElement(icoOfName);
icoElement.setAttribute('stroke-width', 'inherit');
icoElement.setAttribute('fill', 'inherit');
icoElement.removeAttribute('width');
icoElement.removeAttribute('height');
this.absoluteStrokeWidth = this.absoluteStrokeWidth ?? false;
if (changes.name) {
const name = changes.name.currentValue;
if (typeof name === 'string') {
const icoOfName = this.getIcon(this.toPascalCase(name));
if (icoOfName) {
this.replaceElement(icoOfName);
} else {
throw new Error(`The "${name}" icon has not been provided by any available icon providers.`);
}
} else {
this.replaceElement(name);
}
}
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();
}
this.changeDetector.markForCheck();
}
replaceElement(img: LucideIconData): void {
const attributes = {
...defaultAttributes,
...img[1],
width:
typeof this.size === 'number'
? this.size.toString(10)
: this.size,
height:
typeof this.size === 'number'
? this.size.toString(10)
: this.size,
stroke: this.color ?? this.iconConfig.color,
'stroke-width': this.absoluteStrokeWidth
? formatFixed(this.strokeWidth / (this.size / this.defaultSize))
: this.strokeWidth.toString(10),
};
const icoElement = this.createElement([img[0], attributes, img[2]]);
icoElement.classList.add('lucide');
if (typeof this.name === 'string') {
icoElement.classList.add(`lucide-${this.name.replace('_', '-')}`);
}
if (this.class) {
icoElement.classList.add(...this.class.split(/ /).map((a) => a.trim()).filter((a) => a.length > 0))
}
const childElements = this.elem.nativeElement.childNodes;
for (let child of childElements) {
this.renderer.removeChild(this.elem.nativeElement, child);
}
this.renderer.appendChild(this.elem.nativeElement, icoElement);
}
toPascalCase(str: string): string {
return str.replace(/(\w)([a-z0-9]*)(_|-|\s*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
}
toPascalCase(str: string): string {
return str.replace(
/(\w)([a-z0-9]*)(_|-|\s*)/g,
(g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()
);
}
private parseNumber(value: string | number): number {
if (typeof value === 'string') {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new Error(`${value} is not numeric.`);
}
return parsedValue;
}
return value;
}
private getIcon(name: string): LucideIconData|null {
for (const iconProvider of Array.isArray(this.iconProviders) ? this.iconProviders : [this.iconProviders]) {
if (iconProvider.hasIcon(name)) {
return iconProvider.getIcon(name);
}
}
return null;
}
private createElement([tag, attrs, children = []]: LucideIconData) {
const element = this.renderer.createElement(tag, 'http://www.w3.org/2000/svg');
Object.keys(attrs).forEach((name) => {
const attrValue: string =
typeof attrs[name] === 'string'
? (attrs[name] as string)
: attrs[name].toString(10);
this.renderer.setAttribute(element, name, attrValue);
});
if (children.length) {
children.forEach((child: LucideIconData) => {
const childElement = this.createElement(child);
this.renderer.appendChild(element, childElement);
});
}
return element;
}
}

View File

@@ -1,30 +1,26 @@
import { NgModule, ModuleWithProviders, Optional } from '@angular/core';
import { LucideAngularComponent } from './lucide-angular.component';
import { Icons } from './icons.provider';
import { IconData } from '../icons/types';
import {ModuleWithProviders, NgModule, Optional} from '@angular/core';
import {LucideAngularComponent} from './lucide-angular.component';
import {LucideIcons} from '../icons/types';
import {LUCIDE_ICONS, LucideIconProvider} from './lucide-icon.provider';
import {Icons} from './icons.provider';
const legacyIconProviderFactory = (icons?: LucideIcons) => {
return new LucideIconProvider(icons ?? {});
}
@NgModule({
declarations: [LucideAngularComponent],
imports: [
],
exports: [LucideAngularComponent]
declarations: [LucideAngularComponent],
imports: [],
exports: [LucideAngularComponent],
})
export class LucideAngularModule {
constructor(@Optional() private icons: Icons) {
if (!this.icons) {
throw new Error(
`No icon provided. Make sure to use 'LucideAngularModule.pick({ ... })' when importing the module\n`
);
}
}
static pick(icons: { [key: string]: IconData }): ModuleWithProviders<LucideAngularModule> {
static pick(icons: LucideIcons): ModuleWithProviders<LucideAngularModule> {
return {
ngModule: LucideAngularModule,
providers: [
{ provide: Icons, multi: true, useValue: icons }
]
{provide: LUCIDE_ICONS, multi: true, useValue: new LucideIconProvider(icons)},
{provide: LUCIDE_ICONS, multi: true, useFactory: legacyIconProviderFactory, deps: [[new Optional(), Icons]]},
],
};
}
}

View File

@@ -0,0 +1,16 @@
import {Injectable} from '@angular/core';
import defaultAttributes from "../icons/constants/default-attributes";
/**
* A configuration service for Lucide icon components.
*
* You can inject this service, typically in AppComponent, and customize its property values in
* order to provide default values for all the icons used in the application.
*/
@Injectable({ providedIn: 'root' })
export class LucideIconConfig {
color: string = defaultAttributes.stroke;
size: number = defaultAttributes.width;
strokeWidth: number = defaultAttributes["stroke-width"];
absoluteStrokeWidth: boolean = false;
}

View File

@@ -0,0 +1,22 @@
import {LucideIconData, LucideIcons} from '../icons/types';
import {InjectionToken} from '@angular/core';
export interface LucideIconProviderInterface {
hasIcon(name: string): boolean;
getIcon(name: string): LucideIconData|null;
}
export const LUCIDE_ICONS = new InjectionToken<LucideIconProviderInterface>('LucideIcons');
export class LucideIconProvider implements LucideIconProviderInterface {
constructor(private icons: LucideIcons) {
}
getIcon(name: string): LucideIconData|null {
return this.hasIcon(name) ? this.icons[name] : null;
}
hasIcon(name: string): boolean {
return typeof this.icons === 'object' && name in this.icons;
}
}

View File

@@ -0,0 +1,7 @@
import * as icons from './icons/lucide-icons';
export * from './lib/lucide-angular.component';
export * from './lib/lucide-angular.module';
export * from './lib/lucide-icon.config';
export * from './icons/lucide-icons';
export { icons };

View File

@@ -1,25 +1,26 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';
import 'zone.js';
import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: {
context(path: string, deep?: boolean, filter?: RegExp): {
keys(): string[];
<T>(id: string): T;
};
context(path: string, deep?: boolean, filter?: RegExp): {
<T>(id: string): T;
keys(): string[];
};
};
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.

View File

@@ -4,30 +4,34 @@
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": true,
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"paths": {
"lucide-angular": [
"dist"
],
"lucide-angular/*": [
"dist/*"
],
]
},
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"target": "es2017",
"module": "es2020",
"lib": [
"es2018",
"es2020",
"dom"
]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

View File

@@ -3,22 +3,10 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/lib",
"target": "es2015",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true,
"fullTemplateTypeCheck": true
"types": []
},
"exclude": [
"src/test.ts",

View File

@@ -5,7 +5,6 @@
"declarationMap": false
},
"angularCompilerOptions": {
"enableIvy": true,
"compilationMode": "partial"
}
}

View File

@@ -1,152 +0,0 @@
{
"extends": "tslint:recommended",
"rulesDirectory": [
"codelyzer"
],
"rules": {
"align": {
"options": [
"parameters",
"statements"
]
},
"array-type": false,
"arrow-return-shorthand": true,
"curly": true,
"deprecation": {
"severity": "warning"
},
"directive-selector": [
true,
"attribute",
"lib",
"camelCase"
],
"component-selector": [
true,
"element",
"",
"kebab-case"
],
"eofline": true,
"import-blacklist": [
true,
"rxjs/Rx"
],
"import-spacing": true,
"indent": {
"options": [
"spaces"
]
},
"max-classes-per-file": false,
"max-line-length": [
true,
140
],
"member-ordering": [
true,
{
"order": [
"static-field",
"instance-field",
"static-method",
"instance-method"
]
}
],
"no-console": [
true,
"debug",
"info",
"time",
"timeEnd",
"trace"
],
"no-empty": false,
"no-inferrable-types": [
true,
"ignore-params"
],
"no-non-null-assertion": true,
"no-redundant-jsdoc": true,
"no-switch-case-fall-through": true,
"no-var-requires": false,
"object-literal-key-quotes": [
true,
"as-needed"
],
"quotemark": [
true,
"single"
],
"semicolon": {
"options": [
"always"
]
},
"space-before-function-paren": {
"options": {
"anonymous": "never",
"asyncArrow": "always",
"constructor": "never",
"method": "never",
"named": "never"
}
},
"typedef": [
true,
"call-signature"
],
"typedef-whitespace": {
"options": [
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
},
{
"call-signature": "onespace",
"index-signature": "onespace",
"parameter": "onespace",
"property-declaration": "onespace",
"variable-declaration": "onespace"
}
]
},
"variable-name": {
"options": [
"ban-keywords",
"check-format",
"allow-pascal-case"
]
},
"whitespace": {
"options": [
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type",
"check-typecast"
]
},
"component-class-suffix": true,
"contextual-lifecycle": true,
"directive-class-suffix": true,
"no-conflicting-lifecycle": true,
"no-host-metadata-property": true,
"no-input-rename": true,
"no-inputs-metadata-property": true,
"no-output-native": true,
"no-output-on-prefix": true,
"no-output-rename": true,
"no-outputs-metadata-property": true,
"template-banana-in-box": true,
"template-no-negated-async": true,
"use-lifecycle-interface": true,
"use-pipe-transform-interface": true
}
}