Skip to content

Commit 97cfc17

Browse files
authored
feat: modernise fast svg library by using inject, standalone and provide function
2 parents 0fcd1b4 + f949780 commit 97cfc17

File tree

8 files changed

+142
-58
lines changed

8 files changed

+142
-58
lines changed

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

Lines changed: 6 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,6 @@ 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-
}),
2219
IonicModule.forRoot(),
2320
RouterModule.forRoot(
2421
[
@@ -93,7 +90,11 @@ import { FastSvgModule } from '@push-based/ngx-fast-svg';
9390
{ initialNavigation: 'enabledBlocking' }
9491
),
9592
],
96-
providers: [],
93+
providers: [
94+
provideFastSVG({
95+
url: (name: string) => `assets/svg-icons/${name}.svg`,
96+
}),
97+
],
9798
bootstrap: [AppComponent],
9899
})
99100
export class AppModule {}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
} from '@angular/platform-server';
66
import { AppModule } from './app.module';
77
import { AppComponent } from './app.component';
8-
import { FastSvgModule } from '@push-based/ngx-fast-svg';
8+
import { provideFastSVG } from '@push-based/ngx-fast-svg';
99
import { SvgLoadStrategySsr } from './ngx-fast-icon-ssr/icon-load.ssr.strategy';
10+
1011
@NgModule({
1112
declarations: [],
1213
imports: [
@@ -18,12 +19,13 @@ import { SvgLoadStrategySsr } from './ngx-fast-icon-ssr/icon-load.ssr.strategy';
1819
*/
1920
ServerModule,
2021
ServerTransferStateModule,
21-
FastSvgModule.forRoot({
22+
],
23+
providers: [
24+
provideFastSVG({
2225
svgLoadStrategy: SvgLoadStrategySsr,
2326
url: (name: string) => `assets/svg-icons/${name}.svg`,
2427
}),
2528
],
26-
providers: [],
2729
bootstrap: [AppComponent],
2830
})
2931
export class AppServerModule {}

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/README.md

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,45 @@ yarn add @push-based/ngx-fast-svg
2626

2727
### Setup
2828

29+
#### Setup the library in your standalone application:
30+
31+
**main.ts**
32+
33+
```typescript
34+
import { provideFastSVG } from '@push-based/ngx-fast-svg';
35+
36+
bootstrapApplication(AppComponent, {
37+
providers: [
38+
// ... other providers
39+
provideFastSVG({
40+
url: (name: string) => `path/to/svg-assets/${name}.svg`,
41+
})
42+
]
43+
});
44+
```
45+
46+
#### Setup the library in your Angular application using NgModules:
47+
2948
**app.module.ts**
3049

50+
```typescript
51+
// ...
52+
import { provideFastSVG } from '@push-based/ngx-fast-svg';
53+
54+
@NgModule({
55+
declarations: [AppComponent],
56+
providers: [
57+
provideFastSVG({
58+
url: (name: string) => `path/to/svg-assets/${name}.svg`,
59+
})
60+
],
61+
bootstrap: [AppComponent]
62+
})
63+
export class AppModule {}
64+
```
65+
66+
or if you're using an older version of the library, you can still do:
67+
3168
```typescript
3269
// ...
3370
import { FastSvgModule } from '@push-based/ngx-fast-svg';
@@ -38,7 +75,7 @@ import { FastSvgModule } from '@push-based/ngx-fast-svg';
3875
FastSvgModule.forRoot({
3976
url: (name: string) => `path/to/svg-assets/${name}.svg`,
4077
})
41-
]
78+
],
4279
providers: [],
4380
bootstrap: [AppComponent]
4481
})
@@ -98,6 +135,28 @@ import { HttpClientFetchStrategy } from './fetch-strategy';
98135
export class AppModule {}
99136
```
100137

138+
or in a standalone application:
139+
140+
**main.ts**
141+
142+
```typescript
143+
import { provideFastSVG } from '@push-based/ngx-fast-svg';
144+
import { loaderSvg } from './assets';
145+
import { HttpClientFetchStrategy } from './fetch-strategy';
146+
147+
bootstrapApplication(AppComponent, {
148+
providers: [
149+
// ... other providers
150+
provideFastSVG({
151+
url: (name: string) => `path/to/svg-assets/${name}.svg`,
152+
defaultSize: '32',
153+
suspenseSvgString: loaderSvg,
154+
svgLoadStrategy: HttpClientFetchStrategy
155+
})
156+
]
157+
});
158+
```
159+
101160
#### SSR Usage
102161

103162
You can provide your own SSR loading strategy that can look like this:
@@ -124,12 +183,13 @@ And then just provide it in you server module.
124183
AppModule,
125184
ServerModule,
126185
ServerTransferStateModule,
127-
FastSvgModule.forRoot({
186+
],
187+
providers: [
188+
provideFastSVG({
128189
svgLoadStrategy: SvgLoadStrategySsr,
129190
url: (name: string) => `assets/svg-icons/${name}.svg`,
130191
}),
131192
],
132-
providers: [],
133193
bootstrap: [AppComponent],
134194
})
135195
export class AppServerModule {}

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)