vf-modal
vf-modal
A simple modal framework for vue3.
only 3KB gzip size
Todo
- add tests
- build a docs
- more demos
Install
$ npm install vf-modal@next
$ yarn add vf-modal@next
Start
It is compiled to es6 by default. If you need compatibility with lower versions of browsers, you can refer to the following configuration.
- Config Webpack or Vue CLI
- Vue CLI
module.exports = { transpileDependencies: ['vf-modal'] } - Webpack
module.exports = { modules: { rules: [ test: /\.m?js$/, use: { loader: 'babel-loader', }, include: [/vf-modal/] ] } }
- Vue CLI
Usage
Create a VfModal
-
create a file like
modal.jsimport { createVfModal, } from 'vf-modal/index' import NetError from './modals/NetError.vue' import 'vf-modal/dist/index.css' const { VfModal, Controller } = createVfModal({ // define some modals modals: { net: { component: NetError }, hello: { component: defineComponent({ props: { msg: { default: 'hello', required: true, type: String } }, setup (props) { const { msg } = toRefs(props) return <> <p>Hello World</p> <p> msg:{msg.value}</p> </> } }) } } }) export { VfModal, Controller } -
import
VfModalinApp.vue<template> <router-view /> <vf-modal /> </template> <script> import {VfModal} from './modal' export default { name: 'App', components: { VfModal } } </script> -
Open a modal anywhere
import { Controller } from './modal' /** * open a modal with key * @param key {ModalKey} * @param opt { * props: {Record<string, any>} props of the modal component * zIndex: {number} default is 1 * on: {EventMap} event listener * } * @return { * renderList: RenderList * isClosed: () => Promise<void> * close: () => void * } */ Controller.open('net') const { close, isClosed } = Controller.open('hello', { props: { msg: 'haha' }, on: { // like <Component @xxx="function" /> xxx: (...args) => { // do something } }, // maybe useful when stacking multiple modals zIndex: 10 }) // ```only``` close the 'hello' modal close() // ```only``` await the 'hello' modal closed isClosed() .then(() => { // do something }) /** * close a modal * @param key key of modal that you would close , if not provided, will close modal directly */ Controller.close() Controller.close('hello') /** * returns a promise that will be resolved when the ```VFModal``` component is invisible * that means it will be resolved on the ```onAfterEnter``` hook is triggered. */ Controller.isClosed() .then(() => { // do something })
More Optiions
create config
-
modals: The map object of modal.-
type:interface ModalObj { component: any zIndex?: number props?: Record<string, any>, on?: EventMap } -
component: vue componet -
zIndex: style z-index, which can be overridden on open -
props: props of component, which can be overridden on open -
on: listener of component, which can be overridden on opentype Handler<T = any> = (event?: T) => void; interface EventMap { [ x: string ]: Handler }
-
-
provide: the function called in setup -
mask: mask of modal-
clickHandler: the function called when mask is clickedtype clickHandler = (controller: Controller, emiiter: Emitter, instance: VfModal) => voidThe
emittercan also be obtained in modal through the injection -
autoCloseModal: if true modal will be closed when clicking mask, default is false -
classname: classname for mask, default is 'vf-modal-mask-wrapper'
-
-
transition: Vue's transition for modalname: Vue's transition name, default is 'vf-modal-fade'type: Vue's transition name, default is 'transition'
-
on: modal open/close hooksinterface on { modalOpen?: Function modalClose?: Function } -
multipleModal: control whether the same modal can open multiple -
closeWhenRouteChanges: close modal when route changed. -
container: the component that wrap the modal, it's classname is 'vf-modal-container-wrapper', default is 'div'
Injections
You can provide your own injection using the provide property in createConfig, but vf-modal also provides an injection with the basic state of current context.
-
typeinterface VfModalInstanceState { renderList: RenderList close: (key?: string) => void emitter: Emitter } -
renderList: list of currently rendered modal -
close: same close method as provided by theController -
emitter: a emitter create by mitt
Related Packages
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 0.2.14 | ^3.2.33 | MIT | |
| 0.2.13 | ^3.2.33 | MIT | |
| 0.1.0 | ^3.0.0-rc.5 | MIT | |
| 0.0.14 | ^2.6.11 | MIT | |
| 0.0.13 | ^2.6.11 | MIT | |
| 0.0.12 | ^2.6.11 | MIT | |
| 0.0.11 | ^2.6.11 | MIT | |
| 0.0.10 | ^2.6.11 | MIT | |
| 0.0.9 | ^2.6.11 | MIT | |
| 0.0.8 | ^2.6.11 | MIT | |
| 0.0.7 | ^2.6.11 | MIT | |
| 0.0.6 | ^2.6.11 | MIT | |
| 0.0.5 | ^2.6.11 | MIT | |
| 0.0.4 | ^2.6.11 | MIT | |
| 0.0.3 | ^2.6.11 | MIT | |
| 0.0.2 | ^2.6.11 | MIT | |
| 0.0.1 | ^2.6.11 | MIT |
Pre-releases (38)
| Version | Released | Vue | License |
|---|---|---|---|
| 0.2.12-next | ^3.2.33 | MIT | |
| 0.2.11-next | ^3.2.33 | MIT | |
| 0.2.10-next | ^3.2.33 | MIT | |
| 0.2.9-next | ^3.0.7 | MIT | |
| 0.2.8-next | ^3.0.7 | MIT | |
| 0.2.7-next | ^3.0.7 | MIT | |
| 0.2.6-next | ^3.0.7 | MIT | |
| 0.2.5-next | ^3.0.7 | MIT | |
| 0.2.3-next | ^3.0.7 | MIT | |
| 0.2.2-next | ^3.0.5 | MIT | |
| 0.2.1-next | ^3.0.5 | MIT | |
| 0.2.0-next | 3.0.3 | MIT | |
| 0.1.0-rc.3 | ^3.0.4 | MIT | |
| 0.1.0-rc.2 | ^3.0.4 | MIT | |
| 0.1.0-rc.1 | ^3.0.4 | MIT | |
| 0.1.0-beta.16 | ^3.0.0 | MIT | |
| 0.1.0-beta.15 | ^3.0.0 | MIT | |
| 0.1.0-beta.14 | ^3.0.0-rc.10 | MIT | |
| 0.1.0-beta.13 | ^3.0.0-rc.10 | MIT | |
| 0.1.0-beta.12 | ^3.0.0-rc.10 | MIT | |
| 0.1.0-beta.11 | ^3.0.0-rc.10 | MIT | |
| 0.1.0-beta.10 | ^3.0.0-rc.10 | MIT | |
| 0.1.0-beta.9 | ^3.0.0-rc.9 | MIT | |
| 0.1.0-beta.8 | ^3.0.0-rc.9 | MIT | |
| 0.1.0-beta.7 | ^3.0.0-rc.9 | MIT | |
| 0.1.0-beta.6 | ^3.0.0-rc.9 | MIT | |
| 0.1.0-beta.5 | ^3.0.0-rc.8 | MIT | |
| 0.1.0-beta.4 | ^3.0.0-rc.5 | MIT | |
| 0.1.0-beta.3 | ^3.0.0-rc.2 | MIT | |
| 0.1.0-beta.2 | ^3.0.0-beta.21 | MIT |
Showing the latest 30 of 38. See every release on npm