Gathering detailed insights and metrics for @ziflow/ngx-simple-modal
Gathering detailed insights and metrics for @ziflow/ngx-simple-modal
Gathering detailed insights and metrics for @ziflow/ngx-simple-modal
Gathering detailed insights and metrics for @ziflow/ngx-simple-modal
A simple unopinionated framework to implement simple modal based behaviour in angular (v2+) projects.
npm install @ziflow/ngx-simple-modal
Typescript
Module System
Node Version
NPM Version
TypeScript (81.95%)
HTML (10.42%)
JavaScript (5.13%)
CSS (1.27%)
SCSS (1.22%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
52 Stars
280 Commits
28 Forks
8 Watchers
3 Branches
15 Contributors
Updated on Jul 24, 2024
Latest Version
16.0.0
Package Id
@ziflow/ngx-simple-modal@16.0.0
Unpacked Size
151.40 kB
Size
36.68 kB
File Count
22
NPM Version
9.5.1
Node Version
16.18.1
Published on
Jun 12, 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
It is a library to makes modals easier in Angular (2+), has no dependencies, but plays well with bootstrap or other frameworks.
You can see the demo in action here - https://ngx-simple-modal-demo.stackblitz.io You can have a play with this demo code here - https://stackblitz.com/edit/ngx-simple-modal-demo?embed=1&file=styles.css
NG9 Preliminary test can be found here - https://codesandbox.io/s/ngx-simple-modal-ng9-durdl
1npm install ngx-simple-modal
To create a custom modal box, you can start with the following example, wich is going to create a modal with a header, body and footer. The css already provide a transparency overlay, opacity and slide animation.
Inside your angular-cli.json update your styles sections to include our CSS
"styles": [
"styles.css",
"../node_modules/ngx-simple-modal/styles/simple-modal.css"
],
We got you covered, you can @import '../node_modules/ngx-simple-modal/styles/simple-modal.scss'
into what ever root based scss global style you want. Update the relative path depending on where you want to pull it in.
1<div class="modal-content"> 2 <div class="modal-header"> 3 <!-- Your Title --> 4 </div> 5 <div class="modal-body"> 6 <!-- Modal custom content --> 7 </div> 8 <div class="modal-footer"> 9 <!-- 10 Footer to add button control 11 ex.: <button (click)="close()">Cancel</button> 12 --> 13 </div> 14</div>
app.module.ts:
1import { NgModule} from '@angular/core'; 2import { CommonModule } from "@angular/common"; 3import { BrowserModule } from '@angular/platform-browser'; 4import { SimpleModalModule } from 'ngx-simple-modal'; 5import { AppComponent } from './app.component'; 6@NgModule({ 7 declarations: [ 8 AppComponent 9 ], 10 imports: [ 11 CommonModule, 12 BrowserModule, 13 SimpleModalModule 14 ], 15 bootstrap: [AppComponent] 16}) 17export class AppModule {}
By default, modal placeholder will be added to AppComponent. But you can select custom placeholder (i.e. document body):
1imports: [ 2 ... 3 SimpleModalModule.forRoot({container:document.body}) 4 ]
If you want a container that is not yet in the DOM during the intial load you can pass a promiseLike function instead to container e.g.
1imports: [ 2 ... 3 SimpleModalModule.forRoot({container: elementPromisingFn()}) 4 ]
where elementPromisingFn
is anything you want as long as its resolvement returns a nativeElement node from the DOM.
An optional second parameter takes a global object of type SimpleModalOptions (all fields required).. you can spread these with the defaultSimpleModalOptions if you like.
1imports: [ 2 ... 3 SimpleModalModule.forRoot({container: 'modal-container'}, {...defaultSimpleModalOptions, ...{ 4 closeOnEscape: true, 5 closeOnClickOutside: true, 6 wrapperDefaultClasses: 'o-modal o-modal--fade', 7 wrapperClass: 'o-modal--fade-in', 8 animationDuration: 300, 9 autoFocus: true 10 }}) 11 12 ]
OR, if you need to control behaviour more granularly, you can provide the configuration in modules or locally like so
1provide:[ 2 { 3 provide: DefaultSimpleModalOptionConfig, 4 useValue: {...defaultSimpleModalOptions, ...{ closeOnEscape: true, closeOnClickOutside: true }} 5 } 6]
Your modal is expected to be extended from SimpleModalComponent. SimpleModalService is generic class with two arguments:
Therefore SimpleModalService is supposed to be a constructor argument of SimpleModalComponent.
confirm.component.ts:
1import { Component } from '@angular/core'; 2import { SimpleModalComponent } from "ngx-simple-modal"; 3export interface ConfirmModel { 4 title:string; 5 message:string; 6} 7@Component({ 8 selector: 'confirm', 9 template: ` 10 <div class="modal-content"> 11 <div class="modal-header"> 12 <h4>{{title || 'Confirm'}}</h4> 13 </div> 14 <div class="modal-body"> 15 <p>{{message || 'Are you sure?'}}</p> 16 </div> 17 <div class="modal-footer"> 18 <button type="button" class="btn btn-outline-danger" (click)="close()" >Cancel</button> 19 <button type="button" class="btn btn-primary" (click)="confirm()">OK</button> 20 </div> 21 </div> 22 ` 23}) 24export class ConfirmComponent extends SimpleModalComponent<ConfirmModel, boolean> implements ConfirmModel { 25 title: string; 26 message: string; 27 constructor() { 28 super(); 29 } 30 confirm() { 31 // we set modal result as true on click on confirm button, 32 // then we can get modal result from caller code 33 this.result = true; 34 this.close(); 35 } 36}
Add component to declarations and entryComponents section, because the component will be created dynamically.
app.module.ts:
1 import { NgModule} from '@angular/core'; 2 import { CommonModule } from "@angular/common"; 3 import { BrowserModule } from '@angular/platform-browser'; 4 import { SimpleModalModule } from 'ngx-simple-modal'; 5 import { ConfirmComponent } from './confirm.component'; 6 import { AppComponent } from './app.component'; 7 @NgModule({ 8 declarations: [ 9 AppComponent, 10 ConfirmComponent 11 ], 12 imports: [ 13 CommonModule, 14 BrowserModule, 15 SimpleModalModule 16 ], 17 //Don't forget to add the component to entryComponents section 18 entryComponents: [ 19 ConfirmComponent 20 ], 21 bootstrap: [AppComponent] 22 }) 23 export class AppModule {}
app.component.ts
1 import { Component } from '@angular/core'; 2 import { ConfirmComponent } from './confirm.component'; 3 import { SimpleModalService } from "ngx-simple-modal"; 4 5 @Component({ 6 selector: 'app', 7 template: ` 8 <div class="modal-content"> 9 <div class="modal-header"> 10 <h4>Confirm</h4> 11 </div> 12 <div class="modal-body"> 13 <p>Are you sure?</p> 14 </div> 15 <div class="modal-footer"> 16 <button type="button" class="btn btn-primary" (click)="showConfirm()">Show confirm</button> 17 </div> 18 </div> 19 ` 20 }) 21 export class AppComponent { 22 constructor(private simpleModalService:SimpleModalService) {} 23 showConfirm() { 24 let disposable = this.simpleModalService.addModal(ConfirmComponent, { 25 title: 'Confirm title', 26 message: 'Confirm message' 27 }) 28 .subscribe((isConfirmed)=>{ 29 //We get modal result 30 if(isConfirmed) { 31 alert('accepted'); 32 } 33 else { 34 alert('declined'); 35 } 36 }); 37 //We can close modal calling disposable.unsubscribe(); 38 //If modal was not closed manually close it by timeout 39 setTimeout(()=>{ 40 disposable.unsubscribe(); 41 },10000); 42 } 43 }
We got you! An example boostrap alert modal component.
1import { Component } from '@angular/core'; 2import { SimpleModalComponent } from 'ngx-simple-modal'; 3 4export interface AlertModel { 5 title: string; 6 message: string; 7} 8 9@Component({ 10 selector: 'alert', 11 template: `<div class="modal-dialog"> 12 <div class="modal-content"> 13 <div class="modal-header"> 14 <button type="button" class="close" (click)="close()" >×</button> 15 <h4 class="modal-title">{{title || 'Alert!'}}</h4> 16 </div> 17 <div class="modal-body"> 18 <p>{{message || 'TADAA-AM!'}}</p> 19 </div> 20 <div class="modal-footer"> 21 <button type="button" class="btn btn-primary" (click)="close()">OK</button> 22 </div> 23 </div> 24 </div>` 25}) 26export class AlertComponent extends SimpleModalComponent<AlertModel, null> implements AlertModel { 27 title: string; 28 message: string; 29 constructor() { 30 super(); 31 } 32}
As you can see, the implementation is completely in your control. We're just here to help you create, configure, add, track inputs, and remove.
Super class of all modal components.
1/** 2* Dialog abstract class 3* @template T1 - input modal data 4* @template T2 - modal result 5*/ 6abstract abstract class SimpleModalComponent<T1, T2> implements T1, OnDestroy { 7 /** 8 * Constructor 9 * @param {SimpleModalService} simpleModalService - instance of SimpleModalService 10 */ 11 constructor(simpleModalService: SimpleModalService) 12 13 /** 14 * Dialog result 15 * @type {T2} 16 */ 17 protected result:T2 18 19 /** 20 * Closes modal 21 */ 22 public close:Function 23 24 /** 25 * OnDestroy handler 26 * Sends modal result to observer 27 */ 28 public ngOnDestroy:Function 29}
1interface SimpleModalOptions { 2 3 /** 4 * clicking outside your content will be close the modal. 5 * @default false 6 * @type {boolean} 7 */ 8 closeOnClickOutside?: boolean; 9 10 /** 11 * Flag to close modal by click on backdrop (outside modal) 12 * @default false 13 * @type {boolean} 14 */ 15 closeOnEscape: boolean; 16 17 /** 18 * Class to put in document body while modal is open 19 * @default 'modal-open' 20 * @type {string} 21 */ 22 bodyClass: string; 23 24 25 /** 26 * Default classes which live in modal wrapper. Change if you need to for your own css requirements 27 * @default 'modal fade' 28 * @type {string} 29 */ 30 wrapperDefaultClasses: string, 31 32 /** 33 * Class we add and remove from modal when we add it/ remove it 34 * @default 'in' 35 * @type {string} 36 */ 37 wrapperClass: string, 38 /** 39 * Time we wait while adding and removing to let animation play 40 * @type {string} 41 * @default 300 42 */ 43 animationDuration: number; 44 45 /** 46 * FInds teh first focusable element in the page and applies focus on open after closing restores focus back to previous 47 * @type {boolean} 48 * @default false 49 */ 50 autoFocus: number; 51}
Service to show and hide modals
1class SimpleModalService { 2 /** 3 * Adds modal 4 * @param {Type<SimpleModalComponent<T1, T2>} component - modal component 5 * @param {T1?} data - Initialization data for component (optional) to add to component instance and can be used in component code or template 6 * @param {SimpleModalOptions?} Dialog options 7 * @return {Observable<T2>} - returns Observable to get modal result 8 */ 9 public addModal<T1, T2>(component:Type<SimpleModalComponent<T1, T2>>, data?:T1, options: SimpleModalOptions): Observable<T2> => {} 10 11 /** 12 * Remove a modal externally 13 * @param [SimpleModalComponent} component 14 */ 15 public removeModal(component: SimpleModalComponent<any, any>): void; 16 17 /** 18 * Removes all open modals in one go 19 */ 20 public removeAll(): void { 21 22}
This project was seeded by https://github.com/ankosoftware/ng2-bootstrap-modal because the author was not responding to pull requests so decided to take what they had started and update and run with it. Along the way we put some of our own opinions on it and added some tests, enough changes that it stopped being a viable PR for the original without serious breaking changes.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More