手動
基本用法
透過點擊事件將 v-model 的資料設定為 true
<vue-final-modal v-model="showModal">
Modal Content Here
</vue-final-modal>
<button @click="showModal = true">Launch</button>
使用 API 開啟 modal
當使用
$vfm.show(name)
API 開啟,
v-model
為必要的屬性。
<vue-final-modal v-model="showModal" name="example">
Modal Content Here
</vue-final-modal>
this.$vfm.show('example')
範例
試著切換下列的複選框,並點擊 Open Modal 按鈕。
Prop Options:
Attach area:
click will:
- set "attach" to "true"
- set "lockScroll" to "false"
一步步使用
開始 vue-final-modal
Show code
<template>
<div>
<vue-final-modal v-model="showModal">
<span class="modal__title">Hello, vue-final-modal</span>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
.modal__title {
font-size: 1.5rem;
font-weight: 700;
}
</style>
加上 background-color, border-radius
Show code
<template>
<div>
<vue-final-modal v-model="showModal" content-class="modal-content">
<span class="modal__title">Hello, vue-final-modal</span>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
::v-deep .modal-content {
display: inline-block;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
font-size: 1.5rem;
font-weight: 700;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
置中 modal
Show code
<template>
<div>
<vue-final-modal
v-model="showModal"
classes="modal-container"
content-class="modal-content"
>
<span class="modal__title">Hello, vue-final-modal</span>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
::v-deep .modal-container {
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .modal-content {
display: flex;
flex-direction: column;
margin: 0 1rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
font-size: 1.5rem;
font-weight: 700;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
加上內容
Show code
<template>
<div>
<vue-final-modal
v-model="showModal"
classes="modal-container"
content-class="modal-content"
>
<span class="modal__title">Hello, vue-final-modal</span>
<div class="modal__content">
<p>
Vue Final Modal is a renderless, stackable, detachable and lightweight
modal component.
</p>
</div>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
::v-deep .modal-container {
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .modal-content {
display: flex;
flex-direction: column;
margin: 0 1rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
font-size: 1.5rem;
font-weight: 700;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
加上關閉的按鈕
Show code
<template>
<div>
<vue-final-modal
v-model="showModal"
classes="modal-container"
content-class="modal-content"
>
<button class="modal__close" @click="showModal = false">
<mdi-close></mdi-close>
</button>
<span class="modal__title">Hello, vue-final-modal</span>
<div class="modal__content">
<p>
Vue Final Modal is a renderless, stackable, detachable and lightweight
modal component.
</p>
</div>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
::v-deep .modal-container {
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .modal-content {
position: relative;
display: flex;
flex-direction: column;
margin: 0 1rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
margin: 0 2rem 0 0;
font-size: 1.5rem;
font-weight: 700;
}
.modal__close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
使內容是可以滾動的(Scrollable)
Show code
<template>
<div>
<vue-final-modal
v-model="showModal"
classes="modal-container"
content-class="modal-content"
>
<button class="modal__close" @click="showModal = false">
<mdi-close></mdi-close>
</button>
<span class="modal__title">Hello, vue-final-modal</span>
<div class="modal__content">
<p v-for="i in 100" :key="i">
Vue Final Modal is a renderless, stackable, detachable and lightweight
modal component.
</p>
</div>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
::v-deep .modal-container {
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .modal-content {
position: relative;
display: flex;
flex-direction: column;
max-height: 90%;
margin: 0 1rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
margin: 0 2rem 0 0;
font-size: 1.5rem;
font-weight: 700;
}
.modal__content {
flex-grow: 1;
overflow-y: auto;
}
.modal__close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
加上確認(confirm) 以及取消(cancel)按鈕
Show code
<template>
<div>
<vue-final-modal
v-model="showModal"
classes="modal-container"
content-class="modal-content"
>
<button class="modal__close" @click="showModal = false">
<mdi-close></mdi-close>
</button>
<span class="modal__title">Hello, vue-final-modal</span>
<div class="modal__content">
<p v-for="i in 100" :key="i">
Vue Final Modal is a renderless, stackable, detachable and lightweight
modal component.
</p>
</div>
<div class="modal__action">
<button class="vfm-btn" @click="showModal = false">confirm</button>
<button class="vfm-btn" @click="showModal = false">cancel</button>
</div>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false
})
}
</script>
<style scoped>
::v-deep .modal-container {
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .modal-content {
position: relative;
display: flex;
flex-direction: column;
max-height: 90%;
margin: 0 1rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
margin: 0 2rem 0 0;
font-size: 1.5rem;
font-weight: 700;
}
.modal__content {
flex-grow: 1;
overflow-y: auto;
}
.modal__action {
display: flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
padding: 1rem 0 0;
}
.modal__close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
建立可堆疊的(Stackable) modal
Show code
<template>
<div>
<!-- First modal -->
<vue-final-modal
v-model="showModal"
classes="modal-container"
content-class="modal-content"
>
<button class="modal__close" @click="showModal = false">
<mdi-close></mdi-close>
</button>
<span class="modal__title">Hello, vue-final-modal</span>
<div class="modal__content">
<p v-for="i in 5" :key="i">
Vue Final Modal is a renderless, stackable, detachable and lightweight
modal component.
</p>
</div>
<div class="modal__action">
<span>Try click →&nbsp;&nbsp;&nbsp;&nbsp;</span>
<button class="vfm-btn" @click="showConfirmModal = true">
confirm
</button>
<button class="vfm-btn" @click="showModal = false">cancel</button>
</div>
</vue-final-modal>
<!-- Second modal -->
<vue-final-modal
v-model="showConfirmModal"
classes="modal-container"
content-class="modal-content"
>
<button class="modal__close" @click="showConfirmModal = false">
<mdi-close></mdi-close>
</button>
<span class="modal__title">Confirm</span>
<div class="modal__content">
Confirm to submit.
</div>
<div class="modal__action">
<button class="vfm-btn" @click="confirm">
confirm
</button>
<button class="vfm-btn" @click="showConfirmModal = false">
cancel
</button>
</div>
</vue-final-modal>
<button class="vfm-btn" @click="showModal = true">Open modal</button>
</div>
</template>
<script>
export default {
data: () => ({
showModal: false,
showConfirmModal: false
}),
methods: {
confirm() {
this.showConfirmModal = false
this.showModal = false
}
}
}
</script>
<style scoped>
::v-deep .modal-container {
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .modal-content {
position: relative;
display: flex;
flex-direction: column;
max-height: 90%;
margin: 0 1rem;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
background: #fff;
}
.modal__title {
margin: 0 2rem 0.5rem 0;
font-size: 1.5rem;
font-weight: 700;
}
.modal__content {
flex-grow: 1;
overflow-y: auto;
}
.modal__action {
display: flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
padding: 1rem 0 0;
}
.modal__close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
</style>
<style scoped>
.dark-mode div::v-deep .modal-content {
border-color: #2d3748;
background-color: #1a202c;
}
</style>
無障礙
Show code
<template>
<vue-final-modal
...
focus-trap
>
...modal content
</vue-final-modal>
</template>