xmodal-vue14 v0.1.0
š Xmodal-Vue
get rid of those ugly š© predefined modals
xmodal will let you define custom components and show them as modal. YOUR APP YOUR RULE!. very simple and easy
this little component comes with customization tools that will make your life a little bit easier.
š installation :
npm i xmodal-vue
or
yarn add xmodal-vue
š How to use : as a plugin:
import Vue from "vue";
import xmodal from "xmodal-vue";
// install xmodal
Vue.use(xmodal);
for single component:
import xmodal from "xmodal-vue";
export default {
components: {
xmodal
}
------
}
create modal:
<xmodal v-model="isModal" :params="your options here" />
xmodal can have 2 props as input.
- 1- params
- 2- v-model
š Params
params prop is an object that will pass data to the modal. you can see available options for params in the list below.
option | type | default | description |
---|---|---|---|
component | component | null | a reference to your component |
backgroundColor | String | #000000 | control the color of modal backdrop |
opacity | String | 0.7 | control transparency of the modal backdrop |
hasTimer | String,Number | false | you can add timer to your modal by this option |
animation | String | fade | change animation of modal |
isDisable | Boolean | false | disable click events on modal |
props | Object | null | by this option, you can send props to the mounted component |
ā ļø you need to specify a component for each modal instance. all other options are not necessary
š v-model
you can bind xmodal to a boolean value and control it. this boolean value can be a vuex getter, a function etc... it's an optional property.
š” How to use this in your project : you have to use this modal in routes not inside of other components. and you only need one instance on each route. ā for example you can do something like this:
<template>
<div id="#app">
<!-- your code here -->
<xmodal />
</div>
</template>
ā and this is wrong and not supported
<template>
<div id="#app">
<!-- your code here -->
<xmodal v-model="modal1" />
<xmodal v-model="modal2"/>
</div>
</template>
ā ļø you can open different modals with just one instance, so you don't need to create multiple instances for multiple modals!
also, every modal that you creating, needs a default params as options. this params will act as a parent for all other modals that you going to create in that specific page
example: app.vue
<template>
<div id="app">
<h1>Toilet Paper</h1>
<button @click="isModal = !isModal">Open my Modal now !</button>
<xmodal :params="options" />
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
isModal: false,
// basic modal options
options: {
component: require("./components/no.vue").default,
backgroundColor: "#000000",
opacity: "0.7",
animation: "scaleLeft",
}
};
}
};
</script>
Params prop options
components
you need to pass a reference of your component to modal. in that way modal can render your component on the page.
params: {
// you have to require it like this !
component: require("./components/no.vue").default,
}
š“ Don't forget to add .default to end of your require function. if you don't put .default at the end of require, xmodal can't render your component and show it on the page!!
backgroundColor and opacity
you can control modal backdrop color and opacity like this:
params: {
opacity: "0.5" // need to be between 0 - 1
backgroundColor: "#00fffdf"
}
animations
you can set animations for modal by defining its name
animation name |
---|
fade |
------ |
scaleIn |
scaleOut |
scaleBottom |
scaleTop |
scaleLeft |
scaleRight |
------ |
slideBottom |
slideTop |
slideLeft |
slideRight |
hasTimer
it will set a timer based on seconds for your modal hasTimer comes with an indicator on top of the page default color is white. š¢ hasTimer can be a number,string or object. if you want to change indicator color you can pass an object for hasTimer.
example:
params: {
hasTimer: "10s" // 10 seconds as string
hasTimer: 10 // 10 seconds as number
// customize indicator
hasTimer: {
duration: 10 // you can send String too !
color: "#00ff4d" // change color of indicator
}
}
props
you can send props to component that you mounted to show as modal.
params: {
props: {
firstName: "myName",
lastName: "Senpai"
....
// more props
}
}
you can send props to the component, as many as you want.
isDisable
you can disable modal click events to preventing users to close the modal. š¢ isDisable is useful when you want to close modal based on some condition. ( like : checking if input is correct or etc... )
example:
params: {
isDisable: true // prevent user click
}
ā ļø hasTimer will not work with isDisable
Global Methods
you have access to 2 global methods for controlling the modal instance. you can call them by calling this.$xmodal
Open | Close |
---|---|
this.$xmodal.open(params) | this.$xmodal.close() |
this.$xmodal.open(params)
this is where you can open multiple modals ! open function can take params as its argument.
by default, open() is inheriting from default options. you can overwrite it, by passing options that you want to change
š” for example we want to open new modal on click with default options
<template>
<div id="app">
<button @click="Open">Open modal with global fnction</button>
<xmodal :params="options" />
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
// default options
options: {
component: require("./components/no.vue").default,
backgroundColor: "#000000",
opacity: "0.7",
animation: "scaleLeft",
props: {
name: "Mahdi Fakhr",
text: "Onii-chan help me"
},
hasTimer: "10s"
}
};
},
methods: {
Open() {
this.$xmodal.open({
// change default component and show new one
component: require("./components/newComponent.vue").default,
})
}
}
};
</script>
you literally can open new modals by this.modal.open() with brand new options, or customize it based on default options
š“ Don't forget params in this.$xmodal.open() accept an object
this.$xmodal.close()
it will close all open modals in current view(route) at once
like:
<template>
<div id="app">
<img alt="Modal logo" src="./assets/Modal.png" />
<button @click="Close">Open modal with global fnction</button>
<h1>i'm about to end this man's whole career !</h1>
</div>
</template
<script>
export default {
name: "close",
methods: {
Close() {
this.$xmodal.close();
}
}
};
</script>
Modal shortkeys
there are some shortcuts that you can use to modify xmodal
Short keys | Usage |
---|---|
ESC | if user press ESC key, modal will be closed |
feel free to fork or edit this project
4 years ago