feat(packages/angular): add new @lucide/angular package (#3897)

* Add new lucide angular package

* feat(packages/angular): added initial @lucide/angular package

* feat(packages/angular): update readme

* feat(packages/angular): update angular.json

* docs(packages/angular): added (for now) full documentation for @lucide/angular

* docs(packages/angular): added migration guide from lucide-angular

* fix(github): fix package label syntax 😅

* fix(lint): fix linting issues

* fix(github/angular): add prebuild stage

* fix(github/angular): add prebuild stage & fix tests

* fix(github/angular): fix LucideIconComponentType, update with _real_ public members

* fix(github/angular): add prebuild to build step manually

* fix(github/angular): downgrade vitest

* fix(packages/angular): fix migration guide code example

* fix(packages): add vitest + @vitest/* to pnpm overrides

* fix(packages): update pnpm-lock with merged version

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix(packages): fix aria-hidden logic

* fix(packages): update pnpm-lock

* fix(packages): extract vitest and jsdom to root devDependencies

* Fix copy utils script

* Format code

* feat(packages/angular): switched to self-describing IconData object from separate node+name – no more toKebabCase hackery
feat(packages/angular): renamed LucideIconComponentType => LucideIcon, and LucideIcon => LucideDynamicIcon
feat(packages/angular): added backwards compatible CSS class support
feat(packages/angular): switched to vector-effect: non-scaling-stroke implementation from computed stroke width
feat(packages/angular): rewrote icon provider to only accept a list of self-described icons – no more toKebabCase hackery & as an added bonus automatic backwards compatible alias support 🚀
feat(packages/angular): added legacy icon node helper function for passing legacy icons to providers
test(packages/angular): added unit tests on LUCIDE_CONFIG provider usage

* fix(packages/angular): fix linting issues

* feat(packages/angular): extract createLucideIcon logic into helper function, refactor export template to use the iconData object as defined in ExportTemplate

* Replace author

* Remove private field

* fix(packages/angular): remove createLucideIcon, it breaks the package :'(

* fix(packages/angular): fix rendering order of child elements (_before_ projected content)

* Format package.json

* Update docs/guide/packages/angular.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/angular/MIGRATION.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Eric Fennis <eric.fennis@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Karsa
2026-03-20 15:31:34 +01:00
committed by GitHub
co-authored by Copilot Eric Fennis
parent d4424888de
commit a0e202d759
54 changed files with 6672 additions and 462 deletions
@@ -0,0 +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',
};
@@ -0,0 +1,25 @@
import { TestBed } from '@angular/core/testing';
import { LUCIDE_CONFIG, lucideDefaultConfig, provideLucideConfig } from './lucide-config';
describe('Lucide config', () => {
describe('LUCIDE_CONFIG', () => {
it('should use default', () => {
expect(TestBed.inject(LUCIDE_CONFIG)).toBe(lucideDefaultConfig);
});
});
describe('provideLucideConfig', () => {
it('should use defaults', () => {
TestBed.configureTestingModule({
providers: [
provideLucideConfig({
size: 18,
}),
],
});
expect(TestBed.inject(LUCIDE_CONFIG)).toEqual({
...lucideDefaultConfig,
size: 18,
});
});
});
});
+67
View File
@@ -0,0 +1,67 @@
import { InjectionToken, Provider } from '@angular/core';
/**
* Lucide icon configuration options.
*/
export interface LucideConfig {
/**
* Stroke color.
* @default currentColor
*/
color: string;
/**
* Width and height.
* @default 24
*/
size: number;
/**
* Stroke width
* @default 2
*/
strokeWidth: number;
/**
* Whether stroke width should be scaled to appear uniform regardless of icon size.
* @default false
*
* @remarks
* Use CSS to set on SVG paths instead:
* ```css
* .lucide * {
* vector-effect: non-scaling-stroke;
* }
* ```
*/
absoluteStrokeWidth: boolean;
}
/**
* Default icon configuration options.
*/
export const lucideDefaultConfig: LucideConfig = {
color: 'currentColor',
size: 24,
strokeWidth: 2,
absoluteStrokeWidth: false,
};
/**
* Injection token for providing default configuration options.
*
* @internal Use {@link provideLucideConfig}
*/
export const LUCIDE_CONFIG = new InjectionToken<LucideConfig>('Lucide icon config', {
factory: () => lucideDefaultConfig,
});
/**
* Provider for default icon configuration options.
*/
export function provideLucideConfig(config: Partial<LucideConfig>): Provider {
return {
provide: LUCIDE_CONFIG,
useValue: {
...lucideDefaultConfig,
...config,
},
};
}
@@ -0,0 +1,132 @@
import { Component, input, inputBinding, signal, WritableSignal } from '@angular/core';
import { LucideDynamicIcon } from './lucide-dynamic-icon';
import { LucideIconData, LucideIconInput } from './types';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideLucideIcons } from './lucide-icons';
import { LucideActivity } from './icons/activity';
import { By } from '@angular/platform-browser';
@Component({
template: `@if (icon(); as iconData) {
<svg [lucideIcon]="iconData">
<rect x="1" y="1" width="22" height="22" />
</svg>
}`,
imports: [LucideDynamicIcon],
})
class TestHostComponent {
readonly icon = input<LucideIconData>();
}
describe('LucideDynamicIcon', () => {
let component: LucideDynamicIcon;
let fixture: ComponentFixture<LucideDynamicIcon>;
let icon: WritableSignal<LucideIconInput | null | undefined>;
const getSvgAttribute = (attr: string) => fixture.nativeElement.getAttribute(attr);
const testIcon: LucideIconData = {
name: 'demo',
node: [['polyline', { points: '1 1 22 22' }]],
};
const testIcon2: LucideIconData = {
name: 'demo-other',
node: [
['circle', { cx: 12, cy: 12, r: 8 }],
['polyline', { points: '1 1 22 22' }],
],
aliases: ['demo-2'],
};
function createComponent() {
return TestBed.createComponent(LucideDynamicIcon, {
inferTagName: true,
bindings: [inputBinding('lucideIcon', icon)],
});
}
beforeEach(async () => {
TestBed.configureTestingModule({
providers: [provideLucideIcons(testIcon)],
});
icon = signal('demo');
fixture = createComponent();
component = fixture.componentInstance;
});
it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
it('should render children', () => {
icon.set(testIcon2);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe(
'<!--container--><circle cx="12" cy="12" r="8"></circle><polyline points="1 1 22 22"></polyline><!--ng-container-->',
);
});
it('should remove children on change', () => {
icon.set(null);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<!--container--><!--ng-container-->');
});
describe('iconInput', () => {
it('should support LucideIconData input', () => {
icon.set(testIcon);
fixture.detectChanges();
expect(component['icon']()).toBe(testIcon);
expect(fixture.nativeElement.innerHTML).toBe(
'<!--container--><polyline points="1 1 22 22"></polyline><!--ng-container-->',
);
});
it('should support LucideIcon input', () => {
icon.set(LucideActivity);
fixture.detectChanges();
expect(component['icon']()).toBe(LucideActivity.icon);
});
it('should support string icon name', () => {
icon.set('demo');
fixture.detectChanges();
expect(component['icon']()).toBe(testIcon);
});
it('should throw error if no icon found', () => {
icon.set('invalid');
expect(() => fixture.detectChanges()).toThrowError(`Unable to resolve icon 'invalid'`);
});
});
describe('class', () => {
it('should add all classes', () => {
fixture.detectChanges();
expect(getSvgAttribute('class')).toBe('lucide lucide-demo');
});
it('should add backwards compatible classes from aliases', () => {
icon.set(testIcon2);
fixture.detectChanges();
expect(getSvgAttribute('class')).toBe('lucide lucide-demo-other lucide-demo-2');
});
it('should add class icon if available', () => {
icon.set(LucideActivity);
fixture.detectChanges();
expect(getSvgAttribute('class')).toBe('lucide lucide-activity');
});
it('should remove class on change', () => {
icon.set(null);
fixture.detectChanges();
expect(getSvgAttribute('class')).toBe('lucide');
});
});
describe('content projection', () => {
it('should project content', () => {
const hostFixture = TestBed.createComponent(TestHostComponent);
hostFixture.componentRef.setInput('icon', testIcon);
hostFixture.detectChanges();
hostFixture.componentRef.setInput('icon', testIcon2);
hostFixture.detectChanges();
const rect = hostFixture.debugElement.query(By.css('svg :last-child')).nativeElement;
expect(rect).toBeInstanceOf(SVGElement);
expect(rect.outerHTML).toBe('<rect x="1" y="1" width="22" height="22"></rect>');
});
});
});
@@ -0,0 +1,43 @@
import {
ChangeDetectionStrategy,
Component,
computed,
inject,
input,
ViewEncapsulation,
} from '@angular/core';
import { isLucideIconComponent, isLucideIconData, LucideIconInput } from './types';
import { LucideIconBase } from './lucide-icon-base';
import { LUCIDE_ICONS } from './lucide-icons';
import { lucideIconTemplate } from './lucide-icon-template';
/**
* Generic icon component for rendering LucideIconData.
*/
@Component({
selector: 'svg[lucideIcon]',
template: lucideIconTemplate,
standalone: true,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LucideDynamicIcon extends LucideIconBase {
protected readonly icons = inject(LUCIDE_ICONS);
public readonly lucideIcon = input.required<LucideIconInput | null>();
protected override readonly icon = computed(() => {
const icon = this.lucideIcon();
if (isLucideIconData(icon)) {
return icon;
} else if (isLucideIconComponent(icon)) {
return icon.icon;
} else if (typeof icon === 'string') {
if (icon in this.icons) {
return this.icons[icon];
} else {
throw new Error(`Unable to resolve icon '${icon}'`);
}
}
return null;
});
}
@@ -0,0 +1,249 @@
import {
ChangeDetectionStrategy,
Component,
inputBinding,
signal,
ViewEncapsulation,
WritableSignal,
} from '@angular/core';
import { provideLucideConfig } from './lucide-config';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { lucideIconTemplate } from './lucide-icon-template';
import { LucideIconBase } from './lucide-icon-base';
import { LucideIconData } from './types';
@Component({
selector: 'svg[lucideCircleCheck], svg[lucideCheckCircle2]',
template: lucideIconTemplate,
standalone: true,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LucideCircleCheck extends LucideIconBase {
static readonly icon: LucideIconData = {
name: 'circle-check',
size: 24,
node: [
['circle', { cx: '12', cy: '12', r: '10' }],
['path', { d: 'm9 12 2 2 4-4' }],
],
aliases: ['check-circle-2'],
};
protected override readonly icon = signal(LucideCircleCheck.icon);
}
describe('LucideIconBase', () => {
let component: LucideCircleCheck;
let fixture: ComponentFixture<LucideCircleCheck>;
let title: WritableSignal<string | undefined>;
let color: WritableSignal<string | undefined>;
let size: WritableSignal<string | number | undefined>;
let strokeWidth: WritableSignal<string | number | undefined>;
let absoluteStrokeWidth: WritableSignal<boolean | undefined>;
const getSvgAttribute = (attr: string) => fixture.nativeElement.getAttribute(attr);
function createComponent() {
return TestBed.createComponent(LucideCircleCheck, {
inferTagName: true,
bindings: [
inputBinding('title', title),
inputBinding('color', color),
inputBinding('size', size),
inputBinding('strokeWidth', strokeWidth),
inputBinding('absoluteStrokeWidth', absoluteStrokeWidth),
],
});
}
beforeEach(async () => {
title = signal(undefined);
color = signal(undefined);
size = signal(undefined);
strokeWidth = signal(undefined);
absoluteStrokeWidth = signal(undefined);
fixture = createComponent();
component = fixture.componentInstance;
});
it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
it('should render children', () => {
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe(
'<!--container--><circle cx="12" cy="12" r="10"></circle><path d="m9 12 2 2 4-4"></path><!--ng-container-->',
);
});
describe('class', () => {
it('should add all classes', () => {
fixture.detectChanges();
expect(getSvgAttribute('class')).toBe('lucide lucide-circle-check lucide-check-circle-2');
});
});
describe('color', () => {
it('should default to currentColor', () => {
fixture.detectChanges();
expect(getSvgAttribute('stroke')).toBe('currentColor');
});
it('should set color', () => {
color.set('red');
fixture.detectChanges();
expect(getSvgAttribute('stroke')).toBe('red');
});
});
describe('size', () => {
it('should default to 24', () => {
fixture.detectChanges();
expect(getSvgAttribute('width')).toBe('24');
expect(getSvgAttribute('height')).toBe('24');
});
it('should set size', () => {
size.set(12);
fixture.detectChanges();
expect(getSvgAttribute('width')).toBe('12');
expect(getSvgAttribute('height')).toBe('12');
});
it('should allow string size', () => {
size.set('18');
fixture.detectChanges();
expect(getSvgAttribute('width')).toBe('18');
expect(getSvgAttribute('height')).toBe('18');
});
it('should use default on invalid string', () => {
size.set('large');
fixture.detectChanges();
expect(getSvgAttribute('width')).toBe('24');
expect(getSvgAttribute('height')).toBe('24');
});
});
describe('strokeWidth', () => {
it('should default to 2', () => {
fixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe('2');
});
it('should set stroke width', () => {
strokeWidth.set(1.41);
fixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe('1.41');
});
it('should allow string stroke width', () => {
strokeWidth.set('1px');
fixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe('1');
});
});
describe('absoluteStrokeWidth', () => {
it('should not adjust stroke width', () => {
strokeWidth.set(2);
size.set(12);
absoluteStrokeWidth.set(true);
fixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe('2');
});
it('should not set vector-effect on children', () => {
absoluteStrokeWidth.set(false);
for (const child of fixture.nativeElement.children) {
expect(child.getAttribute('vector-effect')).toBeNull();
}
});
it('should set vector-effect on children', () => {
absoluteStrokeWidth.set(true);
for (const child of fixture.nativeElement.children) {
expect(child.getAttribute('vector-effect')).toBe('non-scaling-stroke');
}
});
});
describe('title', () => {
it('should set title if provided', () => {
title.set('Foobar');
fixture.detectChanges();
const titleEl = fixture.debugElement.query(By.css('title')).nativeElement;
expect(titleEl).toBeDefined();
expect(titleEl.textContent).toBe('Foobar');
});
it('should not set aria-hidden when title is set', () => {
title.set('Foobar');
fixture.detectChanges();
expect(getSvgAttribute('aria-hidden')).toBe('false');
});
it('should set aria-hidden if no title is provided', () => {
title.set(undefined);
fixture.detectChanges();
expect(getSvgAttribute('aria-hidden')).toBe('true');
});
});
describe('LUCIDE_CONFIG', () => {
beforeEach(async () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [
provideLucideConfig({
color: 'red',
strokeWidth: 1,
size: 12,
absoluteStrokeWidth: true,
}),
],
});
await TestBed.compileComponents();
fixture = createComponent();
component = fixture.componentInstance;
});
describe('color', () => {
it('should use color from config', () => {
fixture.detectChanges();
expect(getSvgAttribute('stroke')).toBe('red');
});
it('should use override color from config', () => {
color.set('pink');
fixture.detectChanges();
expect(getSvgAttribute('stroke')).toBe('pink');
});
});
describe('strokeWidth', () => {
it('should use stroke width from config', () => {
fixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe('1');
});
it('should use override stroke width from config', () => {
strokeWidth.set(3);
fixture.detectChanges();
expect(getSvgAttribute('stroke-width')).toBe('3');
});
});
describe('size', () => {
it('should use size from config', () => {
fixture.detectChanges();
expect(getSvgAttribute('width')).toBe('12');
expect(getSvgAttribute('height')).toBe('12');
});
it('should use override size from config', () => {
size.set('48');
fixture.detectChanges();
expect(getSvgAttribute('width')).toBe('48');
expect(getSvgAttribute('height')).toBe('48');
});
});
describe('absoluteStrokeWidth', () => {
it('should use absoluteStrokeWidth from config', () => {
for (const child of fixture.nativeElement.children) {
expect(child.getAttribute('vector-effect')).toBe('non-scaling-stroke');
}
});
it('should override absoluteStrokeWidth', () => {
absoluteStrokeWidth.set(false);
for (const child of fixture.nativeElement.children) {
expect(child.getAttribute('vector-effect')).toBeNull();
}
});
});
});
});
+134
View File
@@ -0,0 +1,134 @@
import {
Component,
effect,
ElementRef,
inject,
input,
Renderer2,
Signal,
viewChild,
} from '@angular/core';
import { LUCIDE_CONFIG } from './lucide-config';
import { LucideIconData, Nullable } from './types';
import defaultAttributes from './default-attributes';
import { lucideIconTemplate } from './lucide-icon-template';
function transformNumericStringInput(
value: Nullable<string | number>,
defaultValue: number,
): number {
if (typeof value === 'string') {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
return defaultValue;
}
return parsedValue;
}
return value ?? defaultValue;
}
/**
* @internal
*/
@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'svg[lucideIcon]',
template: lucideIconTemplate,
host: {
...defaultAttributes,
class: 'lucide',
'[attr.width]': 'size().toString(10)',
'[attr.height]': 'size().toString(10)',
'[attr.stroke]': 'color()',
'[attr.stroke-width]': 'strokeWidth().toString(10)',
'[attr.aria-hidden]': '!title()',
},
})
export abstract class LucideIconBase {
protected abstract readonly icon: Signal<Nullable<LucideIconData>>;
protected readonly iconConfig = inject(LUCIDE_CONFIG);
protected readonly elRef = inject(ElementRef);
protected readonly renderer = inject(Renderer2);
protected readonly contentRef = viewChild.required<ElementRef>('contentRef');
/**
* An optional accessible label for the icon.
* - If provided, it will add the title as an [`<svg:title>` element](https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/title).
* - If not provided, the component will add an `aria-hidden="true"` attribute automatically.
*
* @remarks
* Please refer to our [Accessibility guide](https://lucide.dev/guide/advanced/accessibility) regarding this matter.
* Adding accessible labels to icons is normally not necessary:
* - If your icon is decorative (as most icons are) just leave it as hidden from screen readers.
* - If your icon is interactive, it should be contained within an interactive element (e.g. button), and you should probably set your accessible label on that element.
* - If your icon is functional (e.g. used in place of a label), feel free to use this property.
*/
readonly title = input<Nullable<string>>();
/**
* Width and height.
* @default 24
*/
readonly size = input(this.iconConfig.size, {
transform: (value: Nullable<string | number>) =>
transformNumericStringInput(value, this.iconConfig.size),
});
/**
* Stroke color.
* @default currentColor
*/
readonly color = input(this.iconConfig.color, {
transform: (value: Nullable<string>) => value ?? this.iconConfig.color,
});
/**
* Stroke width
* @default 2
*/
readonly strokeWidth = input(this.iconConfig.strokeWidth, {
transform: (value: Nullable<string | number>) =>
transformNumericStringInput(value, this.iconConfig.strokeWidth),
});
/**
* If set to true, it adds [`vector-effect="non-scaling-stroke"`](https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/vector-effect) to child elements.
*/
readonly absoluteStrokeWidth = input(this.iconConfig.absoluteStrokeWidth, {
transform: (value: Nullable<boolean>) => value ?? this.iconConfig.absoluteStrokeWidth,
});
constructor() {
effect((onCleanup) => {
const icon = this.icon();
if (icon) {
const absoluteStrokeWidth = this.absoluteStrokeWidth();
const { name, node, aliases = [] } = icon;
const classes = [name, ...aliases].map((item) => `lucide-${item}`);
for (const cssClass of classes) {
this.renderer.addClass(this.elRef.nativeElement, cssClass);
}
const contentRef = this.contentRef();
const refChild = contentRef.nativeElement;
const elements = node.map(([name, attrs]) => {
const element = this.renderer.createElement(name, 'http://www.w3.org/2000/svg');
if (absoluteStrokeWidth) {
this.renderer.setAttribute(element, 'vector-effect', 'non-scaling-stroke');
}
Object.entries(attrs).forEach(([name, value]) =>
this.renderer.setAttribute(
element,
name,
typeof value === 'number' ? value.toString(10) : value,
),
);
this.renderer.insertBefore(this.elRef.nativeElement, element, refChild);
return element;
});
onCleanup(() => {
elements.forEach((element) =>
this.renderer.removeChild(this.elRef.nativeElement, element),
);
for (const cssClass of classes) {
this.renderer.removeClass(this.elRef.nativeElement, cssClass);
}
});
}
});
}
}
@@ -0,0 +1,10 @@
/**
* @internal
* The template of all Lucide icon components.
*/
export const lucideIconTemplate = `@if (title(); as titleValue) {
<title>{{ titleValue }}</title>
}
<ng-content select="title"></ng-content>
<ng-container #contentRef></ng-container>
<ng-content />`;
+64
View File
@@ -0,0 +1,64 @@
import { TestBed } from '@angular/core/testing';
import {
LUCIDE_ICONS,
lucideLegacyIcon,
lucideLegacyIconMap,
provideLucideIcons,
} from './lucide-icons';
import { LucideIconData } from './types';
import { LucideCircle } from './icons/circle';
describe('Lucide icons', () => {
describe('LUCIDE_ICONS', () => {
it('should default to empty map', () => {
expect(TestBed.inject(LUCIDE_ICONS)).toEqual({});
});
});
describe('provideLucideIcons', () => {
const mockIcon: LucideIconData = {
name: 'mock-icon',
node: [['polyline', { points: '1 1 22 22' }]],
};
const mockIcon2: LucideIconData = {
name: 'mock-icon-circle',
node: [['circle', { cx: 12, cy: 12, r: 8 }]],
aliases: ['mock-icon-2'],
};
const legacyIconNode: LucideIconData['node'] = [['circle', { cx: 12, cy: 12, r: 8 }]];
const legacyAlias = 'legacy-old-name';
const OtherLegacyIcon = legacyIconNode;
it('should accept list of icon object, icon components or legacy icons', () => {
TestBed.configureTestingModule({
providers: [
provideLucideIcons(
mockIcon,
mockIcon2,
LucideCircle,
lucideLegacyIcon('legacy-icon', legacyIconNode, [legacyAlias]),
...lucideLegacyIconMap({ OtherLegacyIcon }),
),
],
});
const legacyIconData = {
name: 'legacy-icon',
node: legacyIconNode,
aliases: [legacyAlias],
};
const otherLegacyIconData = {
name: 'other-legacy-icon',
node: legacyIconNode,
aliases: ['OtherLegacyIcon'],
};
expect(TestBed.inject(LUCIDE_ICONS)).toEqual({
'mock-icon': mockIcon,
'mock-icon-circle': mockIcon2,
'mock-icon-2': mockIcon2,
'legacy-icon': legacyIconData,
'legacy-old-name': legacyIconData,
'other-legacy-icon': otherLegacyIconData,
OtherLegacyIcon: otherLegacyIconData,
['circle']: LucideCircle.icon,
});
});
});
});
+105
View File
@@ -0,0 +1,105 @@
import { InjectionToken, Provider } from '@angular/core';
import { isLucideIconComponent, LucideIcon, LucideIconData, LucideIcons } from './types';
/**
* Injection token for providing Lucide icons by name.
*
* @internal Use {@link provideLucideIcons}
*/
export const LUCIDE_ICONS = new InjectionToken<LucideIcons>('Lucide icons', {
factory: () => ({}),
});
/**
* Provide Lucide icons by name.
*
* @remarks
* Warning! This provider will convert dictionary keys to lower-kebab-case.
*
* @param icons Either a dictionary of icons or a list of Angular icon components.
*
* @usage
* ```ts
* import { provideLucideIcons, SquareCheck } from '@lucide/angular';
* import { MyCustomIcon } from './custom-icons/my-custom-icon';
*
* providers: [
* provideLucideIcons({
* SquareCheck,
* MyCustomIcon, // LucideIconData
* }),
* ]
* ```
*
* ```html
* <svg lucideIcon="my-custom-icon" />
* ```
*/
export function provideLucideIcons(...icons: Array<LucideIcon | LucideIconData>): Provider {
return {
provide: LUCIDE_ICONS,
useValue: icons.reduce((acc, icon) => {
const iconData = isLucideIconComponent(icon) ? icon.icon : icon;
acc[iconData.name] = iconData;
for (const alias of iconData.aliases ?? []) {
acc[alias] = iconData;
}
return acc;
}, {} as LucideIcons),
};
}
/**
* Converts a legacy icon node to the new format, for custom icon (e.g. `@lucide/lab`) support.
*
* @usage
* ```ts
* import { provideLucideIcons, lucideLegacyIcon } from '@lucide/angular';
* import { UserRoundX } from 'lucide-angular';
* import { burger } from '@lucide/lab';
*
* provideLucideIcons(
* lucideLegacyIcon('user-round-x', UserRoundX, ['user-circle-x']),
* lucideLegacyIcon('burger', burger, ['hamburger']),
* ),
* ```
*/
export function lucideLegacyIcon(
name: string,
node: LucideIconData['node'],
aliases: string[] = [],
): LucideIconData {
return {
name,
node,
aliases,
};
}
/**
* Converts a map of legacy icon nodes to a list of icon data objects.
*
* @usage
* ```ts
* import { provideLucideIcons, lucideLegacyIconMap, LucideCircle } from '@lucide/angular';
* import { UserRoundX } from 'lucide-angular';
* import { burger } from '@lucide/lab';
*
* provideLucideIcons(
* LucideCircle,
* ...lucideLegacyIconMap({ UserRoundX, burger }),
* ),
* ```
*/
export function lucideLegacyIconMap(
icons: Record<string, LucideIconData['node']>,
): LucideIconData[] {
return Object.entries(icons).map(([pascalName, node]) => {
const name: string = pascalName.replaceAll(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
return {
name,
node,
aliases: [pascalName],
};
});
}
+10
View File
@@ -0,0 +1,10 @@
import * as icons from './icons/lucide-angular';
export * from './lucide-config';
export * from './lucide-dynamic-icon';
export * from './lucide-icon-base';
export * from './lucide-icon-template';
export * from './lucide-icons';
export * from './types';
export * from './icons/lucide-angular';
export { icons };
+61
View File
@@ -0,0 +1,61 @@
import { Signal, Type } from '@angular/core';
type HtmlAttributes = { [key: string]: string | number };
export type LucideIconNode = readonly [string, HtmlAttributes];
export type LucideIcons = { [key: string]: LucideIconData };
/**
* A Lucide icon object that fully describes an icon to be displayed.
*/
export type LucideIconData = {
name: string;
size?: number;
node: LucideIconNode[];
aliases?: string[];
};
/**
* Input signal map of Lucide icon components.
*/
interface LucideIconProps {
title: Signal<Nullable<string>>;
size: Signal<Nullable<number>>;
color: Signal<Nullable<string>>;
strokeWidth: Signal<Nullable<number>>;
absoluteStrokeWidth: Signal<Nullable<boolean>>;
}
/**
* Represents a Lucide icon component type that has `iconName` and `iconData` signals inherited from `LucideIconBase` and respective static members accessible without instantiating the component.
*/
export interface LucideIcon extends Type<LucideIconProps> {
icon: LucideIconData;
}
/**
* Type guard for {@link LucideIconData}
*/
export function isLucideIconData(icon: unknown): icon is LucideIconData {
return (
!!icon &&
typeof icon === 'object' &&
'name' in icon &&
typeof icon.name === 'string' &&
'node' in icon &&
Array.isArray(icon.node)
);
}
/**
* Type guard for {@link LucideIcon}
*/
export function isLucideIconComponent(icon: unknown): icon is LucideIcon {
return icon instanceof Type && 'icon' in icon && isLucideIconData(icon.icon);
}
export type LucideIconInput = LucideIcon | LucideIconData | string;
/**
* @internal
*/
export type Nullable<T> = T | null | undefined;