Skip to content

Commit 94dba28

Browse files
authored
feat: Add provider function, convert to standalone and use inject (#991)
1 parent ca20a91 commit 94dba28

9 files changed

+75
-54
lines changed

src/app/app.module.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
55

66
import { GhButtonModule } from '@ctrl/ngx-github-buttons';
77

8-
import { ToastrModule, ToastContainerModule, ToastNoAnimationModule } from '../lib/public_api';
8+
import { ToastNoAnimationModule, ToastContainerDirective } from '../lib/public_api';
99

1010
import { AppComponent } from './app.component';
1111
import { FooterComponent } from './footer/footer.component';
@@ -14,6 +14,7 @@ import { HomeComponent } from './home/home.component';
1414
import { NotyfToast } from './notyf.toast';
1515
import { PinkToast } from './pink.toast';
1616
import { BootstrapToast } from './bootstrap.toast';
17+
import { provideToastr } from '../lib/toastr/toast.provider';
1718

1819
@NgModule({
1920
declarations: [
@@ -30,10 +31,10 @@ import { BootstrapToast } from './bootstrap.toast';
3031
FormsModule,
3132
BrowserAnimationsModule,
3233
ToastNoAnimationModule,
33-
ToastrModule.forRoot(),
34-
ToastContainerModule,
34+
ToastContainerDirective,
3535
GhButtonModule,
3636
],
37+
providers: [provideToastr()],
3738
bootstrap: [AppComponent],
3839
})
3940
export class AppModule {}

src/lib/overlay/overlay-container.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { DOCUMENT } from '@angular/common';
2-
import { Inject, Injectable, OnDestroy } from '@angular/core';
2+
import { inject, Injectable, OnDestroy } from '@angular/core';
33

44
/** Container inside which all toasts will render. */
55
@Injectable({ providedIn: 'root' })
66
export class OverlayContainer implements OnDestroy {
7+
protected _document = inject(DOCUMENT);
78
protected _containerElement!: HTMLElement;
89

9-
constructor(@Inject(DOCUMENT) protected _document: any) {}
10-
1110
ngOnDestroy() {
1211
if (this._containerElement && this._containerElement.parentNode) {
1312
this._containerElement.parentNode.removeChild(this._containerElement);

src/lib/overlay/overlay.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
import { DOCUMENT } from '@angular/common';
3-
import { ApplicationRef, ComponentFactoryResolver, Inject, Injectable } from '@angular/core';
3+
import { ApplicationRef, ComponentFactoryResolver, inject, Inject, Injectable } from '@angular/core';
44

55
import { DomPortalHost } from '../portal/dom-portal-host';
66
import { ToastContainerDirective } from '../toastr/toast.directive';
@@ -17,15 +17,14 @@ import { OverlayRef } from './overlay-ref';
1717
*/
1818
@Injectable({ providedIn: 'root' })
1919
export class Overlay {
20+
private _overlayContainer = inject(OverlayContainer);
21+
private _componentFactoryResolver = inject(ComponentFactoryResolver);
22+
private _appRef = inject(ApplicationRef);
23+
private _document = inject(DOCUMENT);
24+
2025
// Namespace panes by overlay container
2126
private _paneElements: Map<ToastContainerDirective, Record<string, HTMLElement>> = new Map();
2227

23-
constructor(
24-
private _overlayContainer: OverlayContainer,
25-
private _componentFactoryResolver: ComponentFactoryResolver,
26-
private _appRef: ApplicationRef,
27-
@Inject(DOCUMENT) private _document: any,
28-
) {}
2928
/**
3029
* Creates an overlay.
3130
* @returns A reference to the created overlay.

src/lib/public_api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './toastr/toast.component';
33
export * from './toastr/toastr.service';
44
export * from './toastr/toastr-config';
55
export * from './toastr/toastr.module';
6+
export * from './toastr/toast.provider';
67
export * from './toastr/toast-ref';
78
export * from './toastr/toast-noanimation.component';
89

src/lib/toastr/toast-noanimation.component.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommonModule } from '@angular/common';
1+
import { NgIf } from '@angular/common';
22
import { ModuleWithProviders } from '@angular/core';
33
import {
44
ApplicationRef,
@@ -40,6 +40,8 @@ import { ToastrService } from './toastr.service';
4040
<div class="toast-progress" [style.width]="width + '%'"></div>
4141
</div>
4242
`,
43+
standalone: true,
44+
imports: [NgIf]
4345
})
4446
export class ToastNoAnimation implements OnDestroy {
4547
message?: string | null;
@@ -220,8 +222,7 @@ export const DefaultNoAnimationsGlobalConfig: GlobalConfig = {
220222
};
221223

222224
@NgModule({
223-
imports: [CommonModule],
224-
declarations: [ToastNoAnimation],
225+
imports: [ToastNoAnimation],
225226
exports: [ToastNoAnimation],
226227
})
227228
export class ToastNoAnimationModule {

src/lib/toastr/toast.component.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import {
1010
HostBinding,
1111
HostListener,
1212
NgZone,
13-
OnDestroy
13+
OnDestroy,
1414
} from '@angular/core';
15+
import { NgIf } from '@angular/common';
1516
import { Subscription } from 'rxjs';
1617
import { IndividualConfig, ToastPackage } from './toastr-config';
1718
import { ToastrService } from './toastr.service';
@@ -43,15 +44,17 @@ import { ToastrService } from './toastr.service';
4344
state('removed', style({ opacity: 0 })),
4445
transition(
4546
'inactive => active',
46-
animate('{{ easeTime }}ms {{ easing }}')
47+
animate('{{ easeTime }}ms {{ easing }}'),
4748
),
4849
transition(
4950
'active => removed',
50-
animate('{{ easeTime }}ms {{ easing }}')
51-
)
52-
])
51+
animate('{{ easeTime }}ms {{ easing }}'),
52+
),
53+
]),
5354
],
54-
preserveWhitespaces: false
55+
preserveWhitespaces: false,
56+
standalone: true,
57+
imports: [ NgIf ],
5558
})
5659
export class Toast<ConfigPayload = any> implements OnDestroy {
5760
message?: string | null;
@@ -206,7 +209,7 @@ export class Toast<ConfigPayload = any> implements OnDestroy {
206209
clearTimeout(this.timeout);
207210
this.options.timeOut = 0;
208211
this.hideTime = 0;
209-
212+
210213
// disable progressBar
211214
clearInterval(this.intervalId);
212215
this.width = 0;

src/lib/toastr/toast.directive.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
import {
2-
Directive,
3-
ElementRef,
4-
NgModule,
5-
} from '@angular/core';
1+
import { Directive, ElementRef } from '@angular/core';
62

73
@Directive({
84
selector: '[toastContainer]',
95
exportAs: 'toastContainer',
6+
standalone: true
107
})
118
export class ToastContainerDirective {
129
constructor(private el: ElementRef) { }
1310
getContainerElement(): HTMLElement {
1411
return this.el.nativeElement;
1512
}
1613
}
17-
18-
@NgModule({
19-
declarations: [ToastContainerDirective],
20-
exports: [ToastContainerDirective],
21-
})
22-
export class ToastContainerModule {}

src/lib/toastr/toast.provider.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { DefaultNoComponentGlobalConfig, GlobalConfig, TOAST_CONFIG } from './toastr-config';
2+
import { EnvironmentProviders, makeEnvironmentProviders, Provider } from '@angular/core';
3+
import { Toast } from './toast.component';
4+
5+
export const DefaultGlobalConfig: GlobalConfig = {
6+
...DefaultNoComponentGlobalConfig,
7+
toastComponent: Toast,
8+
};
9+
10+
/**
11+
* @description
12+
* Provides the `TOAST_CONFIG` token with the given config.
13+
*
14+
* @param config The config to configure toastr.
15+
* @returns The environment providers.
16+
*
17+
* @example
18+
* ```ts
19+
* import { provideToastr } from 'ngx-toastr';
20+
*
21+
* bootstrap(AppComponent, {
22+
* providers: [
23+
* provideToastr({
24+
* timeOut: 2000,
25+
* positionClass: 'toast-top-right',
26+
* }),
27+
* ],
28+
* })
29+
*/
30+
export const provideToastr = (config: Partial<GlobalConfig> = {}): EnvironmentProviders => {
31+
const providers: Provider[] = [
32+
{
33+
provide: TOAST_CONFIG,
34+
useValue: {
35+
default: DefaultGlobalConfig,
36+
config,
37+
}
38+
}
39+
];
40+
41+
return makeEnvironmentProviders(providers);
42+
};

src/lib/toastr/toastr.module.ts

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import { CommonModule } from '@angular/common';
21
import { ModuleWithProviders, NgModule } from '@angular/core';
32

43
import { Toast } from './toast.component';
54
import { DefaultNoComponentGlobalConfig, GlobalConfig, TOAST_CONFIG } from './toastr-config';
6-
7-
export const DefaultGlobalConfig: GlobalConfig = {
8-
...DefaultNoComponentGlobalConfig,
9-
toastComponent: Toast,
10-
};
5+
import { provideToastr } from './toast.provider';
116

127
@NgModule({
13-
imports: [CommonModule],
14-
declarations: [Toast],
8+
imports: [Toast],
159
exports: [Toast],
1610
})
1711
export class ToastrModule {
1812
static forRoot(config: Partial<GlobalConfig> = {}): ModuleWithProviders<ToastrModule> {
1913
return {
2014
ngModule: ToastrModule,
21-
providers: [
22-
{
23-
provide: TOAST_CONFIG,
24-
useValue: {
25-
default: DefaultGlobalConfig,
26-
config,
27-
},
28-
},
29-
],
15+
providers: [provideToastr(config)],
3016
};
3117
}
3218
}
3319

34-
@NgModule({
35-
imports: [CommonModule],
36-
})
20+
@NgModule({})
3721
export class ToastrComponentlessModule {
3822
static forRoot(config: Partial<GlobalConfig> = {}): ModuleWithProviders<ToastrModule> {
3923
return {

0 commit comments

Comments
 (0)