Vuejs Dialog Plugin
A lightweight, promise based alert, prompt and confirm dialog.
Demo
https://godofbrowser.github.io/vuejs-dialog/
Important updates in version v1.x.x
- Dialog will always resolve with an object. (i.e callback for proceed always will receive an object)
- For directives usage, the object returned in (1) above will include a node. The node is the element the directive was bound to (see issue #5
- Styles will have to be included explicitly as they have been extracted into a separate file (see issue #28)
- If loader is enabled globally, and a dialog is triggered via a directive without a callback, the loader is ignored for clicks on proceed
- Custom class injection on parent node (see issue #25)
- Ability to register custom views. This allows for custom logic, custom buttons, etc (see issue #13, #14, #33)
- For installation via HTML script tag
- The library has been namespaced as it has been split into two. The main library which is the plugin and the mixin which is useful in custom components
- To this effect, the new way to install the plugin is slightly dufferent:
window.Vue.use(VuejsDialog.main.default)
- And the mixin can be added to components like so:
mixins: [VuejsDialog.mixin.default, ...otherMixins]
Installation
HTML
// Include vuejs
<script type="text/javascript" src="./path/to/vue.min.js"></script>
// Include vuejs-dialog plugin
<link href="./path/to/vuejs-dialog.min.css" rel="stylesheet">
<script type="text/javascript" src="./path/to/vuejs-dialog.min.js"></script>
<script>
window.Vue.use(VuejsDialog.main.default)
</script>
Package Manager
npm i -S vuejs-dialog
yarn add vuejs-dialog
import Vue from "vue"
import VuejsDialog from "vuejs-dialog"
import 'vuejs-dialog/vuejs-dialog.min.css'
Vue.use(VuejsDialog)
Basic Usage inside a vuejs application
this.$dialog.alert('Request completed!')
.then(function (dialog) {
console.log('Closed')
})
this.$dialog.confirm('Please confirm to continue')
.then(function (dialog) {
console.log('Clicked on proceed')
})
.catch(function () {
console.log('Clicked on cancel')
});
Basic Usage outside a vuejs application
Vue.dialog.alert('Request completed!')
.then(function (dialog) {
console.log('Closed')
})
Vue.dialog.confirm('Please confirm to continue')
.then(function (dialog) {
console.log('Clicked on proceed')
})
.catch(function () {
console.log('Clicked on cancel')
});
Return value on success
{
close: function | sometimes | A method that can be used to close the dialog if it's in a loading state
loading: function | sometimes | A method that can be used to stop the dialog loader
node: DOMElement | sometimes | A DOM element which the directive was bound to, when triggered via a directive
data: any | always | Data sent with the positive action. Useful in prompts or custom components where you have multiple proceed buttons
}
// Example:
<button class="btn-danger"
v-confirm="{
loader: true,
ok: okCallback,
cancel: cancelcallback,
message: 'Some confirmation message'}"
>
okCallback: function (dialog) {
dialog.loaging(false) // stop the loader (you won't be needing this)
dialog.close()
dialog.node.className
dialog.data
}
Usage with ajax - Loader enabled
this.$dialog.confirm("If you delete this record, it'll be gone forever.", {
loader: true
})
.then((dialog) => {
setTimeout(() => {
console.log('Delete action completed ');
dialog.close();
}, 2500);
})
.catch(() => {
console.log('Delete aborted');
});
Usage as a directive
If you don't pass a message, the global/default message would be used.
<button type="submit" v-confirm="">submit</button>
// Callbacks can be provided
// Note: If "loader" is set to true, the makeAdmin callback will receive a "dialog" object
// Which is useful for closing the dialog when transaction is complete.
<button v-confirm="{ok: makeAdmin, cancel: doNothing, message: 'User will be given admin privileges. Make user an Admin?'}">Make Admin</button>
methods: {
makeAdmin: function() {
},
doNothing: function() {
}
}
A more practical use of ths v-confirm
directive with multiple triggers - Solution 1
// While looping through users
<button v-for="user in users"
v-confirm="{
loader: true,
ok: dialog => makeAdmin(dialog, user),
cancel: doNothing,
message: 'User will be given admin privileges. Make user an Admin?'}"
>
Make Admin
</button>
methods: {
makeAdmin: function(dialog, user) {
},
doNothing: function() {
}
}
( new ) A more practical use of ths v-confirm
directive with multiple triggers - Solution 2
// While looping through users
<button v-for="user in users"
:data-user="user"
v-confirm="{
loader: true,
ok: makeAdmin,
cancel: doNothing,
message: 'User will be given admin privileges. Make user an Admin?'}"
>
Make Admin
</button>
methods: {
makeAdmin: function(dialog) {
let button = dialog.node
let user = button.dataset.user
},
doNothing: function() {
}
}
For v-confirm directive, if an "OK" callback is not provided, the default event would be triggered.
// Default Behaviour when used on links
<a href="http://example.com" v-confirm="'This will take you to http://example.com. Proceed with caution'">Go to example.com</a>
Setting a dialog title (new)
You can now set a dialog title by passing your message as an object instead of a string.
The message object should contain a title
and body
let message = {
title: 'Vuejs Dialog Plugin',
body: 'A lightweight, promise based alert, prompt and confirm dialog'
}
this.$dialog.confirm(message)
Options
let message = "Are you sure?";
let options = {
html: false,
loader: false,
reverse: false,
okText: 'Continue',
cancelText: 'Close',
animation: 'zoom',
type: 'basic',
verification: 'continue',
verificationHelp: 'Type "[+:verification]" below to confirm',
clicksCount: 3,
backdropClose: false
customClass: ''
};
this.$dialog.confirm(message, options)
.then(function () {
})
.catch(function () {
});
Global Configuration
Vue.use(VuejsDialog, {
html: true,
loader: true,
okText: 'Proceed',
cancelText: 'Cancel',
animation: 'bounce',
})
CSS Override
If you have included the plugin's style and wish to make a few overides, you can do so with basic css, ex:
.dg-btn--ok {
border-color: green;
}
.dg-btn-loader .dg-circle {
background-color: green;
}
Useful tip for customization
You can use any of the options in your verification help text. Example:
this.$dialog.confirm($message, {
verificationHelp: 'Enter "[+:verification]" below and click on "[+:okText]"',
type: 'hard'
})
More flexibility with Custom components
/* File: custom-component.vue */
<template>
<div class="custom-view-wrapper">
<template v-if=messageHasTitle>
<h2 v-if="options.html" class="dg-title" v-html="messageTitle"></h2>
<h2 v-else class="dg-title">{{ messageTitle }}</h2>
</template>
<template v-else>
<h2>Share with friends</h2>
</template>
<div v-if="options.html" class="dg-content" v-html="messageBody"></div>
<div v-else class="dg-content">{{ messageBody }}</div>
<br/>
<ok-btn @click="handleShare('facebook')" :options="options">Facebook</ok-btn>
<ok-btn @click="handleShare('twitter')" :options="options">Twitter</ok-btn>
<ok-btn @click="handleShare('googleplus')" :options="options">Google+</ok-btn>
<ok-btn @click="handleShare('linkedin')" :options="options">LinkedIn</ok-btn>
<cancel-btn @click="handleDismiss()" :options="options">Dismiss</cancel-btn>
</div>
</template>
<script>
import DialogMixin from 'vuejs-dialog/vuejs-dialog-mixin.min.js' // Include mixin
import OkBtn from 'path/to/components/ok-btn.vue'
import CancelBtn from 'path/to/components/cancel-btn.vue'
export default {
mixins: [ DialogMixin ],
methods: {
handleShare(platform) {
this.proceed(platform) // included in DialogMixin
},
handleDismiss() {
this.cancel() // included in DialogMixin
}
},
components: { CancelBtn, OkBtn }
}
</script>
<style scoped="">
button {
width: 100%;
margin-bottom: 10px;
float: none;
}
</style>
import Vue from 'vue'
import CustomView from './path/to/file/custom-component.vue'
const VIEW_NAME = 'my-unique-view-name'
Vue.dialog.registerComponent(VIEW_NAME, CustomView)
let vm = new Vue({
methods: {
showCustomView(){
this.$dialog.alert(trans('messages.html'), {
view: VIEW_NAME,
html: true,
animation: 'fade',
backdropClose: true
})
}
}
})
... and you get your custom view
License
MIT
Contributing
- Fork it!
- Create your feature branch: git checkout -b my-new-feature
- Commit your changes: git commit -am 'Add some feature'
- Push to the branch: git push origin my-new-feature
- Submit a pull request :)