1 | ![ngx-smart-modal](https://user-images.githubusercontent.com/5319267/28756216-65c335c4-756a-11e7-9ac5-6a0e0cd8ea22.png)
|
2 |
|
3 | [![Join the chat at https://gitter.im/ngx-smart-modal/Lobby](https://badges.gitter.im/ngx-smart-modal/Lobby.svg)](https://gitter.im/ngx-smart-modal/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4 | [![Greenkeeper badge](https://badges.greenkeeper.io/biig-io/ngx-smart-modal.svg)](https://greenkeeper.io/)
|
5 | [![Build Status](https://travis-ci.org/biig-io/ngx-smart-modal.svg?branch=master)](https://travis-ci.org/biig-io/ngx-smart-modal) [![npm version](https://badge.fury.io/js/ngx-smart-modal.svg)](https://badge.fury.io/js/ngx-smart-modal) [![npm downloads](https://img.shields.io/npm/dm/ngx-smart-modal.svg)](https://npmjs.org/ngx-smart-modal) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/8763afb5afe5443bb18c63f7721cd53c)](https://www.codacy.com/app/maximelafarie/ngx-smart-modal?utm_source=github.com&utm_medium=referral&utm_content=biig-io/ngx-smart-modal&utm_campaign=Badge_Coverage) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/8763afb5afe5443bb18c63f7721cd53c)](https://www.codacy.com/app/maximelafarie/ngx-smart-modal?utm_source=github.com&utm_medium=referral&utm_content=biig-io/ngx-smart-modal&utm_campaign=Badge_Grade)
|
6 |
|
7 | `ngx-smart-modal` is a lightweight and very complete Angular library for managing modals inside any Angular project. It was built for modern browsers using TypeScript, HTML5 and Angular >=2.0.0.
|
8 |
|
9 | ## Demo
|
10 | http://biig-io.github.io/ngx-smart-modal/
|
11 |
|
12 |
|
13 | ## No external library, no jQuery! 🤘
|
14 | To avoid imposing you to download a CSS library by using this package, you simply have to use our built-in SCSS/CSS file with custom animations and overridable variables. So get rid of being forced to use a CSS library you don't want to! In addition, it doesn't use jQuery either!
|
15 |
|
16 | ![NgxSmartModal is the CSS frameworks's friend!](src/assets/css_frameworks.png)
|
17 | > #### But... I'm using Bootstrap (or Materialize, Foundation or anything else)!
|
18 | > Don't panic! We already thought about that! And because we want to be as neutral as we can, we made it very flexible for you to style it!
|
19 | > 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 in the `[customClass]="'modal'"` (e.g.: bootstrap). And the rest of the modal DOM elements has just to be set in the `ngx-smart-modal` component (e.g.: modal-dialog, modal-content, modal-header, etc.).
|
20 |
|
21 | Check out the [documentation](https://github.com/biig-io/ngx-smart-modal#parameters--options) & [demo](https://github.com/biig-io/ngx-smart-modal) for more information and examples!
|
22 |
|
23 | See the [changelog](https://github.com/biig-io/ngx-smart-modal/blob/master/CHANGELOG.md) for recent changes.
|
24 |
|
25 | You can take a look on [this diagram](https://github.com/biig-io/ngx-smart-modal#how-it-works) if you are curious about how this library works.
|
26 |
|
27 |
|
28 | ## Features
|
29 | - Handle large quantity of modals anywhere in your app
|
30 | - Customize the style of your modals through custom CSS classes and SCSS variables!
|
31 | - No external CSS library is used so you can easily override the modals default style
|
32 | - Pass data to any modal and retrieve it very simply in the modal view (or anywhere else)
|
33 | - Events on `open`, `close`, `dismiss`, `escape` and more for each modal
|
34 | - Manage all your modal stack and data with very fast methods
|
35 | - Very smart `z-index` computation (no ugly glitches or problems with a modal inside another)
|
36 | - A modal in a modal in a modal in a modal... I guess you got it!
|
37 | - AoT compilation support
|
38 |
|
39 |
|
40 | ## Setup
|
41 | To use `ngx-smart-modal` in your project install it via [npm](https://www.npmjs.com/package/ngx-smart-modal):
|
42 | ```
|
43 | npm i ngx-smart-modal --save
|
44 | ```
|
45 | or with [yarn](https://yarnpkg.com/en/package/ngx-smart-modal):
|
46 | ```
|
47 | yarn add ngx-smart-modal
|
48 | ```
|
49 | ⚠️ If you have the following warning after install **(for NgxSmartModal <= 5.0.0)**:
|
50 | ```
|
51 | npm WARN ngx-smart-modal@x.x.x requires a peer of web-animations-js@>=x.x.x but none was installed.
|
52 | ```
|
53 | or
|
54 | ```
|
55 | warning "ngx-smart-modal@x.x.x" has unmet peer dependency "web-animations-js@>=x.x.x".
|
56 | ```
|
57 | Just run:
|
58 | ```
|
59 | npm i web-animations-js --save
|
60 | ```
|
61 | or
|
62 | ```
|
63 | yarn add web-animations-js
|
64 | ```
|
65 |
|
66 | **If you're using SystemJS**
|
67 | ```
|
68 | System.config({
|
69 | map: {
|
70 | 'ngx-smart-modal': 'node_modules/ngx-smart-modal/bundles/ngx-smart-modal.umd.js'
|
71 | }
|
72 | });
|
73 | ```
|
74 |
|
75 | Then add
|
76 | `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`
|
77 | ```
|
78 | import { BrowserModule } from '@angular/platform-browser';
|
79 | import { NgModule } from '@angular/core';
|
80 | import { NgxSmartModalModule } from 'ngx-smart-modal';
|
81 |
|
82 | import { AppComponent } from './app.component';
|
83 |
|
84 | @NgModule({
|
85 | declarations: [
|
86 | AppComponent
|
87 | ],
|
88 | imports: [
|
89 | BrowserModule,
|
90 | NgxSmartModalModule.forRoot()
|
91 | ],
|
92 | providers: [ ],
|
93 | bootstrap: [ AppComponent ]
|
94 | })
|
95 | export class AppModule { }
|
96 | ```
|
97 |
|
98 | And import `ngx-smart-modal.scss` or `ngx-smart-modal.css` in a global style file (e.g. `styles.scss` or `styles.css` in classic Angular projects or any other scss/css file it imports):
|
99 | Example with **styles.scss**:
|
100 | ```
|
101 | /* You can add global styles to this file, and also import other style files */
|
102 | @import "~ngx-smart-modal/ngx-smart-modal";
|
103 | @import "app/app.component";
|
104 | ...
|
105 | ```
|
106 | [Demo example here](https://github.com/biig-io/ngx-smart-modal/blob/master/src/styles.scss)
|
107 |
|
108 |
|
109 | ## Style & customization
|
110 | ⚠️ **For `ngx-smart-modal` >= 6.0.0 only!**
|
111 | `ngx-smart-modal` provides built-in [SCSS variables](https://sass-lang.com/guide#topic-2) that you can override easily like it (assuming you imported `ngx-smart-modal.scss` as explained above):
|
112 | ```
|
113 | /* You can add global styles to this file, and also import other style files */
|
114 | /* NgxSmartModal variables override */
|
115 | $color-overlay: rgba(0, 0, 0, .7);
|
116 | $dialog-position-top: 20%;
|
117 |
|
118 | @import "~ngx-smart-modal/ngx-smart-modal";
|
119 | ...
|
120 | ```
|
121 | _Note that variables needs to be overridden **before** `@import`!_
|
122 |
|
123 | ### Available SCSS variables
|
124 | The below documentation will use the following pattern:
|
125 | > `parameter/option name` (type) | default value | _description_
|
126 |
|
127 | - `$color-overlay` (hex / rgb / rgba) | `rgba(0, 0, 0, .5)` ― _Modifies the modals overlay background color_
|
128 |
|
129 | - `$dialog-position-top` (px / %) | `5%` ― _Defines the position of the modal from the top of the screen_
|
130 |
|
131 | - `$transition-duration` (duration) | `500ms` ― _Defines the transition effect duration. **Keep in mind you also need to set the same time (in ms) in the `hideDelay` modal option (see below)**_
|
132 |
|
133 | - `$transition-timing-function` (transition-timing-function Property) | `ease-in-out` ― _Specifies the speed curve of the transition effect ([available speed curves here](https://www.w3schools.com/cssref/css3_pr_transition-timing-function.asp))_
|
134 |
|
135 | ### Built-in effects
|
136 | `ngx-smart-modal` can understand several built-in classes to open differently with a sexy effect:
|
137 |
|
138 | To change this effect, you can use the `customClass` option (see below) but you also can define your own class names with dedicated effect and pass them to `customClass`!
|
139 |
|
140 | - ` `: no class. The modal will show without any transition effect
|
141 | - `.nsm-dialog-animation-fade`: default modal effect with a simple fade effect
|
142 | - `.nsm-dialog-animation-ltr `: the modal comes with a left-to-right effect
|
143 | - `.nsm-dialog-animation-rtl`: the modal comes with a right-to-left effect
|
144 | - `.nsm-dialog-animation-ttb`: the modal comes with a top-to-bottom effect
|
145 | - `.nsm-dialog-animation-btt`: the modal comes with a bottom-to-top effect
|
146 |
|
147 |
|
148 | ## Parameters / Options
|
149 | `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>`
|
150 |
|
151 | The below documentation will use the following pattern:
|
152 | > `parameter/option name` (type) | default value | required? ― _description_
|
153 |
|
154 | - `closable` (boolean) | `true` ― _Show / hide the cross icon at the top right corner of the modal_
|
155 |
|
156 | - `escapable` (boolean) | `true` ― _Enable / disable the modal for listening to the escape keypress event (if pressed and this option is set to true, it will close the current opened modal or the latest opened if you have several modals opened at the same time)_
|
157 |
|
158 | - `dismissable` (boolean) | `true` ― _Enable / disable the modal backdrop for listening to the click event (if backdrop is clicked and this option is set to true, it will close the current opened modal or the latest opened if you have several modals opened at the same time)_
|
159 |
|
160 | - `identifier` (string) | `undefined` | **REQUIRED** ― _The identifiant of the modal instance. Retrieve a modal easily by its identifier_
|
161 |
|
162 | - `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._
|
163 |
|
164 | - `customClass` (string) | `'nsm-dialog-animation-fade'` ― _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 class names_
|
165 |
|
166 | - `visible` (boolean) | `false` ― _Define if the modal is shown or not. Automatically toggled with open() and close()/dismiss() methods._
|
167 |
|
168 | - `backdrop` (boolean) | `true` ― _Enable / 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._
|
169 |
|
170 | - `hideDelay` (number) | `500` ― _Opening / closing class delay **in milliseconds**. ⚠️ Only for `NgxSmartModal >= 6.0.0`!_
|
171 |
|
172 |
|
173 | ## Manipulate modals
|
174 | You can use it directly in your component's template like this
|
175 | ```
|
176 | <ngx-smart-modal #myModal identifier="myModal">
|
177 | <h1>Title</h1>
|
178 | <p>Some stuff...</p>
|
179 |
|
180 | <button (click)="myModal.close()">Close</button>
|
181 | </ngx-smart-modal>
|
182 | ```
|
183 | 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 :
|
184 | ```
|
185 | import { Component } from '@angular/core';
|
186 | import { NgxSmartModalService } from 'ngx-smart-modal';
|
187 |
|
188 | @Component({
|
189 | ...
|
190 | })
|
191 | export class AppComponent {
|
192 | constructor(public ngxSmartModalService: NgxSmartModalService) {
|
193 | }
|
194 | }
|
195 | ```
|
196 | Then in the AppComponent view you can open any modal with no need to be in the same view:
|
197 | ```
|
198 | <button (click)="ngxSmartModalService.getModal('myModal').open()">Open myModal</button>
|
199 | ```
|
200 |
|
201 | ## Manipulate data
|
202 | You can associate data with any created modal. To do that, simply use the `setModalData()` from the `NgxSmartModalService`:
|
203 | ```
|
204 | import { AfterViewInit, Component } from '@angular/core';
|
205 | import { NgxSmartModalService } from 'ngx-smart-modal';
|
206 |
|
207 | @Component({
|
208 | ...
|
209 | })
|
210 | export class AppComponent implements AfterViewInit {
|
211 | constructor(public ngxSmartModalService: NgxSmartModalService) {
|
212 | }
|
213 |
|
214 | ngAfterViewInit() {
|
215 | const obj: Object = {
|
216 | prop1: 'test',
|
217 | prop2: true,
|
218 | prop3: [{a: 'a', b: 'b'}, {c: 'c', d: 'd'}],
|
219 | prop4: 327652175423
|
220 | };
|
221 |
|
222 | this.ngxSmartModalService.setModalData(obj, 'myModal');
|
223 | }
|
224 | }
|
225 | ```
|
226 | 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`):
|
227 | ```
|
228 | <ngx-smart-modal #myModal identifier="myModal">
|
229 | <div *ngIf="myModal.hasData()">
|
230 | <pre>{{ myModal.getData() | json }}</pre>
|
231 | </div>
|
232 |
|
233 | <button (click)="myModal.close()">Close</button>
|
234 | </ngx-smart-modal>
|
235 | ```
|
236 |
|
237 | ## Handle events
|
238 | `ngx-smart-modal` comes with several built-in events:
|
239 |
|
240 | - `onOpen`: modal is opening
|
241 | - `onClose`: modal is closing
|
242 | - `onCloseFinished`: modal has been closed
|
243 | - `onDismiss`: modal is closing by clicking on its backdrop
|
244 | - `onDismissFinished`: modal has been closed by clicking on its backdrop
|
245 | - `onEscape`: modal has been closed by escape key
|
246 | - `onAnyCloseEvent`: modal is closing whatever the kind of event (close / escape / dismiss)
|
247 | - `onAnyCloseEventFinished`: modal has been closed whatever the kind of event (close / escape / dismiss)
|
248 | - `visibleChange`: modal visibility has changed (regardless of the modal visibility state)
|
249 | - `onDataAdded`: data were added to the modal (using `setData()`)
|
250 | - `onDataRemoved` data were removed from the modal (using `removeData()`)
|
251 |
|
252 | You can handle events directly from the view...
|
253 | ```
|
254 | <ngx-smart-modal #myModal identifier="myModal" (onOpen)="log('Modal opened!')" (onClose)="log('Modal closed!')" (onDismiss)="log('Modal dismissed!')">
|
255 | <h1>Title</h1>
|
256 | <p>Some stuff...</p>
|
257 |
|
258 | <button (click)="myModal.close()">Close</button>
|
259 | </ngx-smart-modal>
|
260 | ```
|
261 | ...and execute component's functions:
|
262 | ```
|
263 | @Component({
|
264 | ...
|
265 | })
|
266 | export class AppComponent {
|
267 | constructor() {
|
268 | }
|
269 |
|
270 | public log(msg: string) {
|
271 | console.log(msg);
|
272 | }
|
273 | }
|
274 | ```
|
275 |
|
276 | Or you also can declare modal in any template (e.g.: the Rickroll demo modal)...
|
277 | ```
|
278 | <ngx-smart-modal #videoModal identifier="videoModal" customClass="medium-modal">
|
279 | <h1>Hey, I Rickrolled You!</h1>
|
280 | <iframe #rickroll width="1280" height="720"
|
281 | src="https://www.youtube.com/embed/dQw4w9WgXcQ?rel=0&autoplay=1&controls=0&showinfo=0&ecver=1&enablejsapi=1"
|
282 | frameborder="0" allowfullscreen></iframe>
|
283 |
|
284 | <button class="button -dark" (click)="videoModal.close()">Close</button>
|
285 | </ngx-smart-modal>
|
286 | ```
|
287 | ... and listen to its events from any component:
|
288 | ```
|
289 | export class AppComponent implements AfterViewInit {
|
290 | ...
|
291 | constructor(public ngxSmartModalService: NgxSmartModalService) {
|
292 | }
|
293 |
|
294 | ngAfterViewInit() {
|
295 | this.ngxSmartModalService.getModal('videoModal').onOpen.subscribe((modal: NgxSmartModalComponent) => {
|
296 | console.log('Rickroll modal opened!', modal);
|
297 | });
|
298 | }
|
299 | }
|
300 | ```
|
301 |
|
302 |
|
303 | ## API
|
304 | `ngx-smart-modal` also comes with the `NgxSmartModalService` that you can use in any component like this:
|
305 | ```
|
306 | import { Component } from '@angular/core';
|
307 | import { NgxSmartModalService } from 'ngx-smart-modal';
|
308 |
|
309 | @Component({
|
310 | ...
|
311 | })
|
312 | export class AppComponent {
|
313 | constructor(public ngxSmartModalService: NgxSmartModalService) {
|
314 | }
|
315 | }
|
316 | ```
|
317 | **List of available methods**:
|
318 | - `addModal(modalInstance: ModalInstance, force?: boolean)`: add a new modal instance
|
319 | - `getModal(id: string)`: retrieve a modal instance by its identifier
|
320 | - `open(id: string, force?: boolean)`: open a given modal
|
321 | - `close(id: string)`: close a given modal
|
322 | - `getModalStack()`: retrieve all the created modals
|
323 | - `getOpenedModals()`: retrieve all the opened modals
|
324 | - `getHigherIndex()`: get the higher `z-index` value between all the modal instances
|
325 | - `getModalStackCount()`: it gives the number of modal instances
|
326 | - `removeModal(id: string)`: remove a modal instance from the modal stack
|
327 | - `setModalData(data: any, id: string, force?: boolean)`: associate data to an identified modal
|
328 | - `getModalData(id: string)`: retrieve modal data by its identifier
|
329 | - `resetModalData(id: string)`: reset the data attached to a given modal
|
330 | - `closeLatestModal()`: Close the latest opened modal **if it has been declared as escapable**
|
331 |
|
332 | To get more details about the available methods, their parameters and what they return, please take a look at **[ngx-smart-modal.service.ts](https://github.com/biig-io/ngx-smart-modal/blob/master/src/ngx-smart-modal/src/services/ngx-smart-modal.service.ts)** file (well documented).
|
333 |
|
334 |
|
335 | ## Author and Maintainer
|
336 | * [Maxime LAFARIE](https://github.com/maximelafarie)
|
337 |
|
338 |
|
339 | ## Issues
|
340 | If you wish to submit an issue, please use the available template to facilitate reading and comprehension of all issues encountered. You can find this template in `./github/issue_template.md`.
|
341 |
|
342 |
|
343 | ## Contribute
|
344 | Firstly fork this repo, then clone your fork and go inside the root of the freshly forked project.
|
345 | Run `npm i` or `yarn` to install dependencies then `yarn start` to start the angular-cli demo.
|
346 | To modify the library, go into `src/ngx-smart-modal` and do some code (and some tests)! 🤓
|
347 | When you've finished, commit and push it to your forked repo, and make a PR to the official `ngx-smart-modal` repo!
|
348 | Thank you for your support, you rock! 🤘🎸
|
349 |
|
350 |
|
351 | ## How it works
|
352 | 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.
|
353 |
|
354 | ![Sequence diagram](src/assets/sequence_diagram.png)
|
355 |
|
\ | No newline at end of file |