Skip to content

Commit 319f45d

Browse files
committed
feat: modernise fast svg library by using inject, standalone and provide function
1 parent 0fcd1b4 commit 319f45d

File tree

6 files changed

+77
-52
lines changed

6 files changed

+77
-52
lines changed

packages/ngx-fast-icon-demo/src/app/app.module.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DescriptionComponent } from './routes/description/description.component
77
import { CommonModule } from '@angular/common';
88
import { IonicModule } from '@ionic/angular';
99
import { TransferHttpCacheModule } from '@nguniversal/common';
10-
import { FastSvgModule } from '@push-based/ngx-fast-svg';
10+
import { provideFastSVG } from '@push-based/ngx-fast-svg';
1111

1212
@NgModule({
1313
declarations: [AppComponent],
@@ -16,9 +16,9 @@ import { FastSvgModule } from '@push-based/ngx-fast-svg';
1616
BrowserModule.withServerTransition({ appId: 'ngx-fast-icon-demo' }),
1717
HttpClientModule,
1818
TransferHttpCacheModule,
19-
FastSvgModule.forRoot({
20-
url: (name: string) => `assets/svg-icons/${name}.svg`,
21-
}),
19+
// FastSvgModule.forRoot({
20+
// url: (name: string) => `assets/svg-icons/${name}.svg`,
21+
// }),
2222
IonicModule.forRoot(),
2323
RouterModule.forRoot(
2424
[
@@ -93,7 +93,11 @@ import { FastSvgModule } from '@push-based/ngx-fast-svg';
9393
{ initialNavigation: 'enabledBlocking' }
9494
),
9595
],
96-
providers: [],
96+
providers: [
97+
provideFastSVG({
98+
url: (name: string) => `assets/svg-icons/${name}.svg`,
99+
}),
100+
],
97101
bootstrap: [AppComponent],
98102
})
99103
export class AppModule {}

packages/ngx-fast-icon-demo/src/app/comparison/fast-icon/fast-icon.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CommonModule } from '@angular/common';
22
import { NgModule } from '@angular/core';
33
import { RouterModule } from '@angular/router';
4-
import { FastSvgModule } from '@push-based/ngx-fast-svg';
4+
import { FastSvgComponent } from '@push-based/ngx-fast-svg';
55
import { FastIconRouteComponent } from './fast-icon.component';
66

77
@NgModule({
@@ -14,7 +14,7 @@ import { FastIconRouteComponent } from './fast-icon.component';
1414
},
1515
]),
1616
CommonModule,
17-
FastSvgModule,
17+
FastSvgComponent,
1818
],
1919
})
2020
export class FastIconRouteModule {}

packages/ngx-fast-lib/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export * from './lib/svg-registry.service';
99
export * from './lib/fast-svg.component';
1010
// module
1111
export * from './lib/fast-svg.module';
12+
// provider
13+
export * from './lib/fast-svg.provider';

packages/ngx-fast-lib/src/lib/fast-svg.component.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
ChangeDetectionStrategy,
66
Component,
77
ElementRef,
8-
Inject,
98
Input,
109
OnDestroy,
1110
PLATFORM_ID,
12-
Renderer2
11+
Renderer2,
12+
inject,
1313
} from '@angular/core';
1414
import { Subscription } from 'rxjs';
1515
import { getZoneUnPatchedApi } from './internal/get-zone-unpatched-api';
@@ -91,38 +91,34 @@ function createGetImgFn(renderer: Renderer2): (src: string) => HTMLElement {
9191
}
9292
`,
9393
],
94+
standalone: true,
9495
changeDetection: ChangeDetectionStrategy.OnPush,
9596
})
9697
export class FastSvgComponent implements AfterViewInit, OnDestroy {
98+
private readonly platform = inject(PLATFORM_ID);
99+
private readonly renderer = inject(Renderer2);
100+
private readonly registry = inject(SvgRegistry);
101+
private readonly element = inject<ElementRef<HTMLElement>>(ElementRef);
102+
97103
private readonly sub = new Subscription();
98104
private readonly getImg = createGetImgFn(this.renderer);
99105

100-
@Input()
101-
name = '';
102-
@Input()
103-
size: string = this.registry.defaultSize;
104-
@Input()
105-
width = '';
106-
@Input()
107-
height = '';
106+
@Input() name = '';
107+
@Input() size: string = this.registry.defaultSize;
108+
@Input() width = '';
109+
@Input() height = '';
110+
108111
// When the browser loaded the svg resource we trigger the caching mechanism
109112
// re-fetch -> cache-hit -> get SVG -> cache in DOM
110113
loadedListener = () => {
111114
this.registry.fetchSvg(this.name);
112115
};
113116

114-
constructor(
115-
@Inject(PLATFORM_ID)
116-
private platform: Record<string, unknown>,
117-
private renderer: Renderer2,
118-
private registry: SvgRegistry,
119-
private element: ElementRef<HTMLElement>
120-
) {}
121-
122117
ngAfterViewInit() {
123118
if (!this.name) {
124119
throw new Error('svg component needs a name to operate');
125120
}
121+
126122
// Setup view refs and init them
127123
const elem = this.element.nativeElement;
128124

packages/ngx-fast-lib/src/lib/fast-svg.module.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
import { ModuleWithProviders, NgModule } from '@angular/core';
22
import { FastSvgComponent } from './fast-svg.component';
3-
import { SvgRegistry } from './svg-registry.service';
4-
import { CommonModule } from '@angular/common';
53
import { FastSvgProviderOptions } from './provider-config.interface';
6-
import { SvgLoadStrategyImpl } from './token/svg-load.strategy';
7-
import { SvgLoadStrategy } from './token/svg-load.strategy.model';
8-
import { SvgOptionsToken } from './token/svg-options.token';
9-
import { SvgOptions } from './token/svg-options.model';
4+
import { provideFastSVG } from './fast-svg.provider';
105

116
@NgModule({
12-
imports: [CommonModule],
13-
declarations: [FastSvgComponent],
7+
imports: [FastSvgComponent],
148
exports: [FastSvgComponent],
159
})
1610
export class FastSvgModule {
1711
static forRoot(
18-
providers: FastSvgProviderOptions
12+
options: FastSvgProviderOptions
1913
): ModuleWithProviders<FastSvgModule> {
20-
const svgOptions: SvgOptions = {
21-
url: providers.url,
22-
};
23-
providers?.suspenseSvgString &&
24-
(svgOptions.suspenseSvgString = providers.suspenseSvgString);
25-
providers?.defaultSize && (svgOptions.defaultSize = providers.defaultSize);
26-
2714
return {
2815
ngModule: FastSvgModule,
29-
providers: [
30-
{
31-
provide: SvgLoadStrategy,
32-
useClass: providers.svgLoadStrategy || SvgLoadStrategyImpl,
33-
},
34-
{
35-
provide: SvgOptionsToken,
36-
useValue: svgOptions,
37-
},
38-
SvgRegistry,
39-
],
16+
providers: [provideFastSVG(options)],
4017
};
4118
}
4219

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Provider, makeEnvironmentProviders } from '@angular/core';
2+
import {
3+
SvgLoadStrategy,
4+
SvgLoadStrategyImpl,
5+
SvgOptions,
6+
SvgOptionsToken,
7+
SvgRegistry,
8+
} from '..';
9+
import { FastSvgProviderOptions } from './provider-config.interface';
10+
11+
/**
12+
* @description
13+
* Use this function to register the FastSvg providers in your application.
14+
*
15+
* @param options {FastSvgProviderOptions} - The options for the FastSvg providers.
16+
* @return {EnvironmentProviders} - The providers for the FastSvg module.
17+
*
18+
* @example
19+
*
20+
* ```ts
21+
* bootstrapApplication(AppComponent, {
22+
* providers: [
23+
* provideFastSVG({
24+
* url: (name: string) => `path/to/svg-assets/${name}.svg`,
25+
* })
26+
* ]});
27+
* ```
28+
*/
29+
export const provideFastSVG = (options: FastSvgProviderOptions) => {
30+
const svgOptions: SvgOptions = {
31+
url: options.url,
32+
suspenseSvgString: options.suspenseSvgString || undefined,
33+
defaultSize: options.defaultSize || undefined,
34+
};
35+
36+
const providers: Provider[] = [
37+
SvgRegistry,
38+
{
39+
provide: SvgLoadStrategy,
40+
useClass: options.svgLoadStrategy || SvgLoadStrategyImpl,
41+
},
42+
{ provide: SvgOptionsToken, useValue: svgOptions },
43+
];
44+
45+
return makeEnvironmentProviders(providers);
46+
};

0 commit comments

Comments
 (0)