Git Product home page Git Product logo

ngsweetalert2's Introduction

SweetAlert2 integration for Angular. This is not a regular API wrapper for SweetAlert (which already works very well alone), it intends to provide Angular-esque utilities on top of it.

Before posting an issue, please check that the problem isn't on SweetAlert's side. This is just a directive/component wrapper around Swal2.

📦 Installation & Usage

  1. Install NgSweetAlert2 and SweetAlert2 via the npm registry:
npm install --save sweetalert2 @toverux/ngsweetalert2
  1. Then, import SweetAlert's CSS (or SCSS) file, exactly like you're doing usually with vendor styles. Could be a TypeScript import with Webpack, a SASS @import, or even a <link> tag: that depends of you build system.

    Its path is node_modules/sweetalert2/dist/sweetalert2.css.

  2. Finally, import the module:

import { SweetAlert2Module } from '@toverux/ngsweetalert2';

@NgModule({
    //=> Basic usage
    imports: [SweetAlert2Module.forRoot()],
    
    //=> Or provide default SweetAlert2-native options
    //   (here we make Swal more Bootstrap-friendly)
    imports: [
        SweetAlert2Module.forRoot({
            buttonsStyling: false,
            customClass: 'modal-content',
            confirmButtonClass: 'btn btn-lg btn-primary',
            cancelButtonClass: 'btn btn-lg'
        })
    ],
    
    //=> In submodules only:
    imports: [SweetAlert2Module]
})
export class AppModule {
}

🔗 API

SwalDirective

Adding the [swal] attribute to an element will attach the directive to it.

The directive will listen for click events and display a SweetAlert modal, configured using the options you pass to the attribute. The options are of type SweetAlertOptions (provided by sweetalert2), or a simple array of strings, of format [title: string, text: string (, type: string)].

class __API__ {
    @Input() public set swal(options: SweetAlertOptions|SimpleSweetAlertOptions);

    @Output() public confirm: EventEmitter<any>;
    @Output() public cancel: EventEmitter<any>;
}

Simple confirmation dialog:

<button [swal]="['Delete?', 'This cannot be undone.', 'warning']" (confirm)="deleteFile(file)">
  Delete {{ file.name }}
</button>

More advanced (input in dialog, dismissal handling):

<button [swal]="{ title: 'Enter your email', input: 'email' }"
        (confirm)="saveEmail($event)"
        (cancel)="handleRefusalToSetEmail($event)">
  Set my e-mail address
</button>
export class MyComponent {
    public saveEmail(email: string): void {
        // ... save user email
    }

    public handleRefusalToSetEmail(dismissMethod: string): void {
        // dismissMethod can be 'cancel', 'overlay', 'close', and 'timer'
        // ... do something
    }
}

SwalComponent

The library also provides a component, that can be useful for displaying other dialogs than confirmation ones. Others can prefer to use that to avoid having dialog-related logic in their codebehind.

class __API__ {
    @Input() public type: SweetAlertType;
    @Input() public title: string;
    @Input() public text: string;
    @Input() public html: string;
    @Input() public options: SweetAlertOptions;
    
    @Output() public confirm: EventEmitter<any>;
    @Output() public cancel: EventEmitter<any>;
    
    public show(): Promise<any>;
}

Simple example:

<swal #dialog title="..." type="info"></swal>
<button (click)="dialog.show().then(goToProfile)">Go to my profile</button>

Or:
<swal #dialog title="..." type="info" (confirm)="goToProfile()" (cancel)="doSomethingElse()"></swal>
<button (click)="dialog.show()">Go to my profile</button>

If you decide to use the show().then(...) form, remember that you'll have to handle the promise rejection if the modal is dismissable, or you'll get an "uncaught promise rejection" in your console.

If you use the (confirm)="handler()" form, (cancel) is optional.

You can also access the dialog from your codebehind:

class MyComponent {
    @ViewChild('dialog') private swalDialog: SwalComponent;
}

You can pass more SweetAlert2-native options via the options input:

<swal #dialog [options]="{ confirmButtonText: 'I understand' }"></swal>

ngsweetalert2's People

Contributors

toverux avatar cassmtnr avatar fklingler avatar

Watchers

 avatar James Cloos avatar  avatar

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.