Giter Club home page Giter Club logo

vue-modal's Introduction

Vue Modal

npm (scoped) npm MIT

A flexible & responsive modal component for Vue.js 2. Looks nice and clean on all devices.

Live Demo

Installation via NPM

npm i @voerro/vue-modal --save-dev

or

npm i @voerro/vue-modal --save

Then import the component:

import { Modal, VoerroModal } from '@voerro/vue-modal';

Vue.component('modal', Modal);
window.VoerroModal = VoerroModal;

Usage

Add an empty component somewhere inside your template.

<modal></modal>

Generating Modals Dynamically

Call VoerroModal.show() from anywhere in your script/Vue code. Example call:

VoerroModal.show({
    title: 'Modal Title',
    body: 'Modal Body (HTML is supported here)',
    buttons: [
        {
            text: 'Ok',
            handler: () => {
                alert('You pressed Ok');
            }
        },
        {
            text: 'Cancel'
        }
    ]
});

You can add as many buttons as you need. When you press a button, the button's handler gets called and then the modal closes. If there's no handler, the modal will close immediately without any additional actions taking place.

Buttons - Options

To add buttons to a modal include an array of button objects. Each object can have the following properties (options):

Property Type Default Required Description
text String undefined yes The text of the button
handler function undefined no The function which will be called on button press. The default action is to hide the modal, so you don't have to include anything if the button click should only close the modal.
preventDefault Boolean undefined (false) no Even if you provide a handler, the modal will be closed after the action is done. To prevent that set this property to true.

Ajax

You can fetch the modal's body via Ajax. To do so provide a bodyUrl URL instead of a body. Example:

VoerroModal.show({
    title: 'Modal Title',
    bodyUrl: 'http://example.com/add-user-modal',
    buttons: [
        {
            text: 'Submit',
            handler: () => {
                alert('Submitting the form...');
            }
        },
        {
            text: 'Cancel'
        }
    ]
});

Inline Modals

You can have multiple inline modals. "Inline" means a modal's content is stored within the HTML code of a page. Each modal should have a unique id. See an example below.

HTML:

<modal id="inline-modal" v-cloak>
    <template slot="title">Inline Title</template>

    <div slot="body">
        This body, as well as the title, are stored directly within the HTML of your page.
    </div>

    <div slot="footer">
        You'll have to implement the buttons completely yourself, in case you need them of course.
    </div>
</modal>

<modal id="inline-modal-two" v-cloak>
    <template slot="title">Inline 2</template>

    <div slot="body">This is a different inline modal with a different id.</div>
</modal>

JS/ES:

// Show the modal with the id = 'inline-modal'
VoerroModal.show('inline-modal');

// Show the modal with the id = 'inline-modal-two'
VoerroModal.show('inline-modal-two');

You can still include an object with options (including dynamically generated buttons) in the method call.

VoerroModal.show('inline-modal', {
    buttons: [
        {
            text: 'Ok',
            handler: () => {
                alert('You pressed ok');
            }
        },
        { text: 'CLose' }
    ]
});

Note: don't delete the empty <modal></modal> component from your page if you also want to use dynamically generated modals.

<modal> Properties

Property Type Default Required Description
id String undefined no Id of the modal
dismissible Boolean true no Whether a modal can be closed by clicking on the close button or on the overlay outside the modal.
When applied to the global <modal> component becomes the default for all the dynamically generated modals. Can be overridden for individual modals.
header Boolean true no Whether to show the header.
When applied to the global <modal> component becomes the default for all the dynamically generated modals. Can be overridden for individual modals.

VoerroModal Methods

Method Description Arguments
show (options) Generate and show a modal options - an object with options

Available Options:
title - String - modal's title
body - String - modal's body (HTML is supported)
bodyUrl - String - URL to fetch modal's body from (AJAX)
buttons - Array - an array of objects describing buttons
dismissible - Boolean - Whether the modal can be closed by clicking on the close button or on the overlay outside the modal.
header - Boolean - Whether to show the header.
show (id) Show an inline modal by id id - the id of a modal to be shown
hide (id) Hide a modal by id id - the id of a modal to be hidden.
hide () Hide the currently open dynamically generated modal -

License

This is open-sourced software licensed under the MIT license.

vue-modal's People

Contributors

alexmordred avatar

Watchers

Fufutu avatar James Cloos 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.