Code Monkey home page Code Monkey logo

ngx-modal's Introduction

ngx-modal

Open modal window for your Angular X application

INSTALL

npm i -S <package-name>

USAGE

Example 1: Route modal

  1. Add <router-outlet name="modal"></router-outlet> to AppComponent template
  2. Configure AppModule
// app.module.ts
@NgModule({
    imports: [
        ModalModule.forRoot(options),
        RouterModule.forRoot([
            { path: 'greet/:user', outlet: 'modal', component: GreetingsModalComponent },
        ]),
    ]
})
export class AppModule { }
  1. Add link, e.g. <a [routerLink]="['.', { outlets: { 'modal': 'greet/joe'} }]">Greetings, Joe!</a>
  2. Create GreetingsModalComponent
@Component({
    template: `
<modal [isOpen]="true">
    <modal-header title="Greetings"></modal-header>
    <modal-content>Example Modal Content</modal-content>
</modal>
    `
})
export class GreetingsModalComponent {

    @ViewChild(ModalComponent) protected modal: ModalComponent;

    constructor() { }

}

Example 2: Confirm

  1. Import ModalConfirmComponent
  2. Add following markup to your component template
<modal-confirm #confirm 
    title="Confirmation" 
    content="Are you are sure?"
    (closemodal)="onCloseConfirm()"
></modal-confirm>
<a (click)="openConfirm()">Confirm</a>
  1. Add openConfirm method
export class AppComponent {

    @ViewChild(ModalConfirmComponent) private confirm: ModalConfirmComponent;
    confirmSubscription: Subscription;

    protected openConfirm() {
        this.confirm.open();
        this.confirmSubscription = this.confirm.okay.subscribe(() => {
            console.log('Okay...');
        });
    }

    protected onCloseConfirm() {
        this.confirmSubscription.unsubscribe();
    }
}

CONFIGURATION

ModalModule has some configuration

@NgModule({
    imports: [
        ModalModule.forRoot(modalOptions),
    ]
})

Where modalOptions is defaults of following:

export const defaultOptions: ModalOptions = {
    popupOpenedClass: 'ngx-modal-popup-opened',
    isOpenClass: 'ngx-modal-open',
    isNotificationClass: 'ngx-modal-notification',
    popupClass: 'ngx-modal-popup',
    bodyClass: 'ngx-modal-body',
    headerClass: 'ngx-modal-header',
    footerClass: 'ngx-modal-footer',
    contentClass: 'ngx-modal-content',
    /**
     * Class for close button in modal-header component.
     */
    buttonCloseClass: 'ngx-modal-button-close',
    /**
     * Content in close button tag.
     */
    buttonCloseContent: '&times;',
    /**
     * Navigate back when modal close
     */
    backOnClose: true,
    hasCloseButton: true,
    confirmFooterToolbarClass: 'ngx-modal-confirm-footer-toolbar',
    /**
     * Class wrapper for modal-confirm2 component buttons toolbar.
     */
    confirmFooterButtonsClass: '',
    confirmFooterButtonItemClass: '',
    confirmOkayButtonClass: '',
    confirmCancelButtonClass: '',
    /**
     * When true, when modal closes router.navigate() will be called with options relativeTo: activatedRoute.parent
     */
    closeRelativeToParent: false,
    /**
     * Content box class for modal-confirm3 component
     */
    confirmContentBoxClass: '',
};

API

ModalModule

  • ModalModule.forRoot(modalOptions) override default options
  • ModalModule.forChild(modalOptions) set modal options for this module

ModalComponent

Selector: modal

Inputs:

  • routed: boolean Set flag indicating that modal is routed
  • isOpen: boolean Open modal when component initialized
  • isNotification: boolean
  • settings: Partial<ModalOptions> Custom settings for modal, supported settings: routeOnClose, routeOutlets, backOnClose, isOpenClass, isNotificationClass, popupOpenedClass

Outputs:

  • closemodal: EventEmitter<void>
  • openmodal: EventEmitter<void>

Methods:

  • open: void Open modal
  • close: void Close modal

ModalConfirmComponent

Selector: modal-confirm

Inputs:

  • title: string
  • content: string
  • isNotification: boolean If true add notification class popup
  • settings: Partial<ModalOptions>
  • okayLabel: string = 'Okay' Okay button label
  • cancelLabel: string = 'Cancel'

Outputs:

  • closemodal: EventEmitter<void>

Properties:

  • result: readonly Subject` Result of confirm
  • isOpen: readonly boolean
  • okay: readonly Observable<boolean> Observable of result filtered to true

Methods:

  • open Open modal confirm

ModalConfirm2Component

Selector: modal-confirm2

Inputs: See ModalConfirmComponent

Properties: See ModalConfirmComponent

Methods: See ModalConfirmComponent

ModalConfirm2Component inherits ModalConfirmComponent, the only difference is markup. In ModalConfirm2Component modal-footer is not used.

ModalConfirm3Component

Selector: modal-confirm3

Inputs: See ModalConfirmComponent

Properties: See ModalConfirmComponent

Methods: See ModalConfirmComponent

ModalConfirm3Component inherits ModalConfirmComponent, the only difference is markup.
Used more advanced markup in footer.

ModalHeaderComponent

Selector: modal-header

Inputs:

  • title: string
  • hasCloseButton: boolean
  • closeButtonId: string

Properties:

  • closeEventEmitter: EventEmitter<any>

ModalFooterComponent

Selector: modal-footer

ModalContentComponent

Selector: modal-content

ModalConfirmService

Create modal confirm popup dynamically.

Methods:

  • open(viewContainerRef: ViewContainerRef, componentType: Type<ModalConfirmComponent>, settings?: Partial<ModalConfirmComponent>): Observable<boolean>

Example usage:

export class AppComponent {

    constructor(
        private modalConfirmService: ModalConfirmService,
        private viewContainerRef: ViewContainerRef,
    ) { }

    openModalConfirmService() {
        this.modalConfirmService.open(this.viewContainerRef, ModalConfirmComponent)
            .pipe(take(1))
            .subscribe(result => {
                // true - ok, false - cancel
                console.log('confirm result', result);
            });
    }
}

Note: You MUST subscribe to observable, otherwise modal will not be closed.

DEVELOPMENT

  • npm run dev

ngx-modal's People

Contributors

semantic-release-bot avatar unlight avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

ngx-modal's Issues

Production build failed

When I try to build with AOT by ng build --prod , the compilation fails.

ERROR in ./node_modules/ngx-modal2/src/modal-content.component.ts
Module build failed: Error: C:\Users\plageoj\Documents\project\project\node_modules\ngx-modal2\src\modal-content.component.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format (https://goo.gl/jB3GVv).
    at AngularCompilerPlugin.getCompiledFile (C:\Users\plageoj\Documents\project\project\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:708:23)
    at plugin.done.then (C:\Users\plageoj\Documents\project\project\node_modules\@ngtools\webpack\src\loader.js:41:31)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)

My environment is:

Angular CLI: 6.0.1
Node: 9.11.1
OS: win32 x64
Angular: 6.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.0
@angular-devkit/build-angular     0.6.0
@angular-devkit/build-optimizer   0.6.0
@angular-devkit/core              0.6.0
@angular-devkit/schematics        0.6.1
@angular/cli                      6.0.1
@angular/pwa                      0.6.1
@angular/service-worker           6.0.1
@ngtools/webpack                  6.0.0
@schematics/angular               0.6.1
@schematics/update                0.6.1
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.6.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.