/ngx-smart-modal

Angular smart light and fast modal handler to manage modals and data everywhere. 🌎

Primary LanguageTypeScriptMIT LicenseMIT

ngx-smart-modal

Join the chat at https://gitter.im/ngx-smart-modal/Lobby Greenkeeper badge Build Status npm version npm downloads codecov

ngx-smart-modal is a lightweight and very complete Angular component for managing modal inside any Angular project. It was built for modern browsers using TypeScript, HTML5 and Angular >=2.0.0.

Demo

http://biig-io.github.io/ngx-smart-modal/

No external library, no jQuery! 🤘

To avoid imposing you to download a CSS library by using this package, this one is only using Angular animations. So get rid off to be forced to use a CSS library you don't want to! In addition, it doesn't use jQuery too!

But... I'm using Bootstrap (or Materialize, Foundation or anything else)!

Don't panic! We already thought about that! And because we want to be the more neutral as we can, we made it very flexible for you to style it! So if your app uses a CSS framework that has some modal styles, you simply have to pick up its class names and set the main class it in the [customClass]="'modal'" (e.g.: bootstrap). And the rest of the modal DOM elements simply have to be set in the ngx-smart-modal component (e.g.: modal-dialog, modal-content, modal-header, etc.).

Check out the documentation & demos for more information and tutorials!

See the changelog for recent changes.

Features

  • Handle large quantity of modals anywhere in your app
  • Customize the style of your modals through custom CSS classes
  • No external CSS library is used so you can easily override the modals default style
  • Pass data to any modal and retrieve it very simply in the modal view (or anywhere else)
  • Events on open, close and dismiss for each modal
  • Manage all your modal stack and data with very fast methods
  • Very smart z-index computation (no ugly glitches or problems with a modal inside another)
  • A modal in a modal in a modal in a modal... I guess you got it!
  • AoT compilation support

Setup

To use ngx-smart-modal in your project install it via npm:

npm i ngx-smart-modal --save

⚠️ If you have the following warning after install:

npm WARN ngx-smart-modal@2.0.1 requires a peer of web-animations-js@>=2.0.0 but none was installed.

or

warning "ngx-smart-modal@2.0.1" has unmet peer dependency "web-animations-js@>=2.0.0".

Just run:

npm i web-animations-js --save

or

yarn add web-animations-js

Then add NgxSmartModalModule (with .forRoot() or .forChild() depending if the module which you import the library into is the main module of your project or a nested module) and NgxSmartModalService to your project NgModule

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgxSmartModalModule, NgxSmartModalService } from 'ngx-smart-modal';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgxSmartModalModule.forRoot()
  ],
  providers: [ NgxSmartModalService ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Parameters / Options

ngx-smart-modal comes with some parameters / options in order to make it fit your needs. The following parameters / options needs to be used like this: <ngx-smart-modal [parameter-or-option-name]="value"></ngx-smart-modal>

The below documentation will use the following pattern:

parameter/option name (type) | default value | required? ― description

  • closable (boolean) | trueShow / hide the cross icon at the top right corner of the modal

  • escapeAble (boolean) | trueEnable / disable the modal for listening to the escape keypress event (if pressed and this option is true, it will close the current opened modal or the latest opened if you have several modals opened at the same time

  • identifier (string) | undefined | REQUIREDThe identifiant of the modal instance. Retrieve a modal easily by its identifier

  • force (boolean) | true ― If true and if you declare another modal instance with the same identifier that another, the service will override it by the new you declare in the modal stack.

  • customClass (string) | ''All the additionnal classes you want to add to the modal (e.g.: any bootstrap modal class). You can add several classes by giving a string with space-separated classnames

  • visible (boolean) | falseDefine if the modal is shown or not. Automatically toggled with open() and close()/dismiss() methods.

  • backdrop (boolean) | trueEnable / disable the backdrop of a modal. Tip: when you want to encapsulate several modals, set this options at true for the parent modal and false for the others.

Manipulate modals

You can use it directly in your component's template like this

<ngx-smart-modal #myModal [identifier]="'myModal'">
  <h1>Title</h1>
  <p>Some stuff...</p>

  <button (click)="myModal.close()">Close</button>
</ngx-smart-modal>

At this point, the modal instance is stored in the NgxSmartModalService. You can do absolutely what you want with it, anywhere in your app. For example, from a component :

import {Component} from '@angular/core';
import {NgxSmartModalService} from 'ngx-smart-modal';

@Component({
  ...
})
export class AppComponent {
  constructor(public ngxSmartModalService: NgxSmartModalService) {
  }
}

Then in the AppComponent view you can open any modal with no need to be in the same view:

<button (click)="ngxSmartModalService.getModal('myModal').open()">Open myModal</button>

Manipulate data

You can associate data with any created modal. To do that, simply use the setModalData() from the NgxSmartModalService:

import {AfterViewInit, Component} from '@angular/core';
import {NgxSmartModalService} from 'ngx-smart-modal';

@Component({
  ...
})
export class AppComponent implements AfterViewInit {
  constructor(public ngxSmartModalService: NgxSmartModalService) {
  }

  ngAfterViewInit() {
    const obj: Object = {
      prop1: 'test',
      prop2: true,
      prop3: [{a: 'a', b: 'b'}, {c: 'c', d: 'd'}],
      prop4: 327652175423
    };

    this.ngxSmartModalService.setModalData(obj, 'myModal');
  }
}

After that, you can retrieve the modal data directly from the view with the getData() modal property. To avoid any errors with unavailable data, you can use the hasData() modal property (It's dynamic. If data comes after a certain time its value will automatically change to true):

<ngx-smart-modal #myModal [identifier]="'myModal'">
  <div *ngIf="myModal.hasData()">
    <pre>{{ myModal.getData() | json }}</pre>
  </div>

  <button (click)="myModal.close()">Close</button>
</ngx-smart-modal>

Handle events

ngx-smart-modal comes with three built-in events: onOpen, onClose and onDismiss.

  • onOpen: modal has been opened
  • onClose: modal has been closed
  • onDismiss: modal has been closed by clicking on its backdrop

You can handle events directly from the view...

<ngx-smart-modal #myModal [identifier]="'myModal'" (onOpen)="log('Modal opened!')" (onClose)="log('Modal closed!')" (onDismiss)="log('Modal dismissed!')">
  <h1>Title</h1>
  <p>Some stuff...</p>

  <button (click)="myModal.close()">Close</button>
</ngx-smart-modal>

...and execute component's functions:

@Component({
  ...
})
export class AppComponent {
  constructor() {
  }

  public log(msg: string) {
    console.log(msg);
  }
}

Or you also can declare modal in any template (e.g.: the Rickroll demo modal)...

<ngx-smart-modal #videoModal [identifier]="'videoModal'" [customClass]="'medium-modal'">
  <h1>Hey, I Rickrolled You!</h1>
  <iframe #rickroll width="1280" height="720"
          src="https://www.youtube.com/embed/dQw4w9WgXcQ?rel=0&autoplay=1&controls=0&showinfo=0&ecver=1&enablejsapi=1"
          frameborder="0" allowfullscreen></iframe>

  <button class="button -dark" (click)="videoModal.close()">Close</button>
</ngx-smart-modal>

... and listen to its events from any component:

export class AppComponent implements AfterViewInit {
  ...
  constructor(public ngxSmartModalService: NgxSmartModalService) {
  }

  ngAfterViewInit() {
    this.ngxSmartModalService.getModal('videoModal').onOpen.subscribe((event: Event) => {
      console.log('Rickroll modal opened!', event);
    });
  }
}

Contribute

Firstly fork this repo, then clone it and go inside the root of the freshly forked project. ng serve to start the angular-cli demo. To modify the package, go into ./src/lib/core and do some code! 🤓 When you finished commit and push it to your fork repo, make a PR! Thank you for your support, you rock! 🤘🎸

How it works

Basically, imagine that the component is based on a service that stores any modals you create in order to let you pick them up and manage them anywhere in your app at any time.

Sequence diagram