Gathering detailed insights and metrics for ngx-url-modal
Gathering detailed insights and metrics for ngx-url-modal
Gathering detailed insights and metrics for ngx-url-modal
Gathering detailed insights and metrics for ngx-url-modal
npm install ngx-url-modal
Typescript
Module System
Node Version
NPM Version
TypeScript (97.62%)
HTML (1.63%)
SCSS (0.75%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2 Stars
13 Commits
2 Watchers
1 Branches
1 Contributors
Updated on May 29, 2023
Latest Version
1.0.0
Package Id
ngx-url-modal@1.0.0
Unpacked Size
250.74 kB
Size
57.15 kB
File Count
44
NPM Version
8.5.0
Node Version
16.14.1
Published on
May 30, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
NgxUrlModal is an Angular library that enables you to save and restore the state of modal windows in the URL query parameters. It provides a seamless way to store data in the query parameters, allowing the modal state to be preserved even after page reloads. With NgxUrlModal, you can easily create and manage modals with dynamic parameters, providing a consistent user experience and easy sharing of modal states via URLs
Live demo on Stackblitz
1npm install ngx-url-modal
Library uses Dialog by default. You must be sure that you have installed necessary packages.
1npm install @angular/cdk
After packages installation you should import NgxUrlModalModule
module to your app
1@NgModule({ 2 declarations: [ 3 AppComponent 4 ], 5 imports: [ 6 DialogModule, 7 NgxUrlModalModule 8 ], 9 bootstrap: [AppComponent] 10}) 11export class AppModule { } 12
Note If you use MatDialog
you can import only MatDialogModule
and that will work.
BaseModal
class with modal required data as first parameter1interface ModalData { 2 id: string; 3} 4 5@Component({...}) 6class MyComponent extends BaseModal<ModalData> { 7 constructor( 8 @Inject(DIALOG_DATA) 9 public readonly data: ModalData, 10 ) { 11 super(); 12 } 13}
UrlModalService
UrlModalService
in component (recommended way) or in module. Important if you provide in module, clear this service in ngOnDestroy
registerModal
method call
addUrlContext
addStaticContext
;ActivatedRoute
by registerPage
method call1@Component({
2 ...
3 providers: [UrlModalService]
4})
5export class PageComponent implements OnInit {
6
7 constructor(
8 public readonly users: UserService,
9 public readonly modal: UrlModalService,
10 private readonly route: ActivatedRoute,
11 ) {
12 }
13
14 ngOnInit(): void {
15 const urlContext = new UrlContext()
16 .declareParam('userId')
17 .declareComputedParam('user', params => {
18 return this.users.getUserById(params.userId);
19 });
20
21 this.modal.registerModal('user', UrlComponent)
22 .addUrlContext(urlContext)
23 .addStaticContext({
24 width: '400px',
25 height: '800px',
26 });
27
28 this.modal.registerPage(this.route);
29 }
30
Service for controlling URL modals. It allows registering URL modals and pages, opening and closing URL modals, and subscribing to modal events
clear()
method.BaseModal<T>
, where T
is the data that will be injected into the component via the configuration.registerModal
method.addStaticContext
and addUrlContext
methods of the UrlModal
class.registerPage
method with the ActivatedRoute
.1@Component({ 2 providers: [UrlModalService] 3}) 4class MyPage implements OnInit { 5 6 constructor( 7 private modals: UrlModalService, 8 private route: ActivatedRoute, 9 ) {} 10 11 public ngOnInit() { 12 const staticContext = {}; 13 const urlContext = new UrlParamsContext().declareParam('id'); 14 this.modals.registerModal('modal', MyModal) 15 .addUrlContext(urlContext) 16 .addStaticContext(staticContext); 17 18 this.modals.registerPage(this.route); 19 } 20 21}
registerPage(route: ActivatedRoute): UrlModalService
Registers the page. This method must be called after modal registration. If called before modal registration, there is no guarantee that modals will open via URL after initialization.
registerModal<ReturnData, ConfigType>(modalName: string, component: Type<BaseModal<ConfigType>>): UrlModal
Registers a modal by name and component. It creates a new UrlModal and stores it in the service.
modalName
: The name of the modal.component
: The component class representing the modal.open<T>(modalName: string, params: T): Promise<UrlModal<T> | undefined>
Manually opens a modal with parameters.
modalName
: The name of the modal.params
: The parameters to pass to the modal.
Throws an error if the page is not registered before calling any modal actions.close<Data>(modalName: string, data?: Data): Promise<UrlModal<Data>> | undefined
Manually closes a modal with optional data.
modalName
: The name of the modal.data (optional)
: The data to pass to the modal on closing.
Throws an error if the page is not registered before calling any modal actions.on<Data>(modalName: string, event: UrlModalEventType): Observable<UrlModalEvent<Data>> | undefined
Subscribes to a specific modal event.
modalName
: The name of the modal.event
: The event type to subscribe to.clear(): void
Clears all resources used by the UrlModalService.
Class for providing parametres declarations. Used for declaring and handling URL query param
The declareParam
and declareComputedParam
methods are used to declare parameters in the UrlContext
. The declareParam
method is used for declaring URL qury parameters, while the declareComputedParam
method is used for declaring parametes that should be computed with some function from another params. By using these methods, you can define the parameters that will be used in the URL and specify their optional or required status.
declareParam(param: string, optional?: boolean): UrlContext<ParamsDeclaration, ComputedParams>
Declares a query parameter.
param
: The name of the parameter.optional
(optional): Specifies whether the parameter is optional. Defaults to true
declareComputedParam(param: string, selectFunction: (params: ComputedParams) => ParamType): UrlContext<ParamsDeclaration, ComputedParams>
Declares a computed parameter.
param
: The name of the parameter.selectFunction
: The function that will be executed after calling computeParams
. It takes the computed parameters as input.Note that when declaring computed parameters, you need to provide a function that will be executed after calling computeParams
. This function takes the computed parameters as input and returns the computed parameter value.
The StaticContext is similar to modal config DialogConfig but with additional features such as field computing. It allows you to provide static configuration options for your modal, such as width, height, or any other properties that you want to set for the modal window.
You can:
data
field.Full example with modal from previous example
1this.modal.registerModal('user', UrlComponent)
2 .addUrlContext(urlContext)
3 .addStaticContext({
4 width: '400px',
5 height: '800px',
6 panelClass: params => params.user ? 'edit' : 'create',
7 data: {
8 userNumber: 1
9 }
10 });
No vulnerabilities found.
No security vulnerabilities found.