Looking for a Vue 3 version? It's over here
🎉 Documentation
🙌 Examples
You can create a higher-order component easily and can customize template, script and style based on your needs.
features:
- Support Vue 3 and Vue 2
- Tailwind CSS friendly
- Renderless component
- SSR support
- Stackable
- Detachable
- Scrollable
- Transition support
- Mobile friendly
- Tiny bundle size
- Accessibility support
Vue 3.0
NPM:
npm install vue-final-modal@next --saveYarn:
yarn add vue-final-modal@nextVue 2.0
NPM:
npm install vue-final-modal --saveYarn:
yarn add vue-final-modalimport VueFinalModal from 'vue-final-modal'
Vue.use(VueFinalModal())- Write a plugin
vue-final-modal.js
// plugins/vue-final-modal.js
import VueFinalModal from 'vue-final-modal/lib'
Vue.use(VueFinalModal())- Add plugin into
nuxt.config.js
// nuxt.config.js
export default {
plugins: [
{ src: '~plugins/vue-final-modal.js' }
],
build: {
transpile: ['vue-final-modal']
}
}- jsDelivr
<script src="https://cdn.jsdelivr.net/npm/vue-final-modal"></script>- Unpkg
<script src="https://unpkg.com/vue-final-modal"></script><vue-final-modal v-model="showModal">
Modal Content Here
</vue-final-modal>
<button @click="showModal = true">Launch</button>
v-modelis necessary when you open a modal with$vfm.show(name)API.
<vue-final-modal v-model="showModal" name="example">
Modal Content Here
</vue-final-modal>this.$vfm.show('example')Only work in v0.18+
Configuration options can be passed as a second argument to Vue.use.
import VueFinalModal from 'vue-final-modal'
Vue.use(VueFinalModal(), { ... })- Type:
String - default:
'VueFinalModal'
Changes component name from "VueFinalModal" to any other string value. It is useful when there is already a global "modal" component.
- Type:
String - default:
'$vfm'
Changes API name from "$vfm" to any other string value. It is useful when you use vue-final-modal as spinner, toast, notify, etc.
- In Options API:
Plugin API can be called within any component through this.$vfm.
- In Composition API (Only in Vue 3 version):
Only support in Vue 3
import { inject } from 'vue'
export default {
setup() {
const $vfm = inject('$vfm')
}
}- Type:
Function - Arguments:
- name:
String- Name of the modal - params:
?: object- Any data that you would want to pass into the modal (@before-open event handler will containparamsin the event). You can also using scoped-slot to getparamsin template:
- name:
<template v-slot="{ params }">
<!-- modal content -->
</template>Or get params on @beforeOpen event:
<vue-final-modal @beforeOpen="e => e.ref.params">
<!-- modal content -->
</vue-final-modal>
parmaswill be reset to{}automatically afterclosedevent. You can avoid the modal to reset theparamsto empty object by callingevent.stop().
- Example:
<template>
<vue-final-modal v-model="show" name="example">
<template v-slot="{ params }">
Hi {{ params.userName }}
</template>
</vue-final-modal>
</template>
<script>
export default {
data: () => ({
show: false
}),
mounted() {
this.$vfm.show('example', { userName: 'vue-final-modal' })
}
}
</script>
v-modelis necessary when you open a modal with$vfm.show(name)API.
- Type:
Function - Arguments:
- name:
String- Name of the modal
- name:
- Example:
<template>
<vue-final-modal v-model="show" name="example">Vue Final Modal is awesome</vue-final-modal>
</template>
<script>
export default {
data: () => ({
show: true
}),
mounted () {
this.$vfm.hide('example')
}
}
</script>hide all modals.
- Type:
Function - Arguments:
- name:
String- Name of the modal - show:
?: Boolean- Show modal or not - params:
?: object- Any data that you would want to pass into the modal (@before-open event handler will contain params in the event). You can also using scoped-slot to getparamsin template:
- name:
<template v-slot="{ params }">
<!-- modal content -->
</template>Or get params on @beforeOpen event:
<vue-final-modal @beforeOpen="e => e.ref.params">
<!-- modal content -->
</vue-final-modal>
parmaswill be reset to{}automatically afterclosedevent. You can avoid the modal to reset theparamsto empty object by callingevent.stop().
toggle modal by name.
- Type:
Function - Arguments:
- name:
String- Name of the modal
- name:
return the modal comopnent instance.
- Type:
Array
A stack array store the opened modal's vue component instance.
You can use:
$vfm.openedModals[0]to get the first opened modal instance.$vfm.openedModals.lengthto get how many modals is opened.
- Type:
Array
All modal instances include show and hide.
{
value: { type: Boolean, default: false },
name: { type: String, default: null },
ssr: { type: Boolean, default: true },
classes: { type: [String, Object, Array], default: '' },
overlayClass: { type: [String, Object, Array], default: '' },
contentClass: { type: [String, Object, Array], default: '' },
styles: { type: [String, Object, Array], default: '' },
overlayStyle: { type: [String, Object, Array], default: '' },
contentStyle: { type: [String, Object, Array], default: '' },
lockScroll: { type: Boolean, default: true },
hideOverlay: { type: Boolean, default: false },
clickToClose: { type: Boolean, default: true },
escToClose: { type: Boolean, default: false },
preventClick: { type: Boolean, default: false },
attach: { type: null, default: false, validator: validateAttachTarget },
transition: { type: String, default: 'vfm' },
overlayTransition: { type: String, default: 'vfm' },
zIndexAuto: { type: Boolean, default: true },
zIndexBase: { type: [String, Number], default: 1000 },
zIndex: { type: [Boolean, String, Number], default: false },
focusRetain: { type: Boolean, default: true },
focusTrap: { type: Boolean, default: false }
}- Emits while modal container on click.
If prop
clickToCloseisfalse, the event will still be emitted.
- Emits while modal is still invisible, but before transition starting. Further opening of the modal can be blocked from this event listener by calling
event.stop().
- Emits after modal became visible and transition ended.
- Emits before modal is going to be closed. Further closing of the modal can be blocked from this event listener by calling
event.stop().
- Emits right before modal is destroyed. Further after the modal was closed, you can avoid the modal to reset the
paramsto empty object by callingevent.stop().
If you open a modal though API show(name, params), You can using scoped-slot to get params in template:
<template v-slot="{ params }">
<!-- modal content -->
</template>Or get params on @beforeOpen event:
<vue-final-modal @beforeOpen="e => e.ref.params">
<!-- modal content -->
</vue-final-modal>
parmaswill be reset to{}automatically afterclosedevent. You can avoid the modal to reset theparamsto empty object by callingevent.stop().
đź‘‹ Hi I'm Hunter, the author of vue-final-modal.
To develop vue-final-modal, I learn a lot from these awesome libraries:
- Vuetify
- attach
- Element UI
- stackable modal
- zIndex
- zIndexBase
- vue-js-modal
- dynamic modal
- transition
- focusTrap for A11y
- Bootstrap Vue
- lockScroll
There is no perfect library even the
finalof vue modal.
If you have any ideas for optimization of vue-final-modal, feel free to open issues or pull requests.
