Skip to content

Commit a060fae

Browse files
committed
fix: setModelValueLocal and add StopEvent example
1 parent 30c2832 commit a060fae

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

packages/vue-final-modal/src/components/CoreModal/useModelValue.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, ref, watch } from 'vue'
1+
import { computed, nextTick, ref, watch } from 'vue'
22
import type CoreModal from './CoreModal.vue'
33

44
export function useModelValue(
@@ -9,6 +9,7 @@ export function useModelValue(
99
close: () => boolean
1010
},
1111
) {
12+
let skip = false
1213
const { open, close } = options
1314

1415
/** The truth of modal open or close */
@@ -34,11 +35,17 @@ export function useModelValue(
3435
emit('update:modelValue', val)
3536
}
3637
else {
38+
skip = true
3739
emit('update:modelValue', !val)
40+
nextTick(() => {
41+
skip = false
42+
})
3843
}
3944
}
4045

4146
watch(() => props.modelValue, (val) => {
47+
if (skip)
48+
return
4249
modelValueLocal.value = !!val
4350
})
4451

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<script setup lang="ts">
2+
import { ModalsContainer, VueFinalModal, useModal, useModalSlot } from 'vue-final-modal'
3+
import DefaultSlot from '../DefaultSlot.vue'
4+
5+
let count = 1
6+
7+
const modal = useModal({
8+
keepAlive: true,
9+
component: VueFinalModal,
10+
attrs: {
11+
background: 'interactive',
12+
contentStyle: { backgroundColor: '#fff' },
13+
onClosed() {
14+
count = 1
15+
},
16+
onOpened() {
17+
count = 1
18+
},
19+
onBeforeClose({ stop }) {
20+
count += 1
21+
if (count <= 5) {
22+
console.log('Modal close stopped')
23+
stop()
24+
}
25+
},
26+
onBeforeOpen({ stop }) {
27+
count += 1
28+
if (count <= 5) {
29+
console.log('Modal Open stopped')
30+
stop()
31+
}
32+
},
33+
},
34+
slots: {
35+
default: useModalSlot({
36+
component: DefaultSlot,
37+
attrs: {
38+
text: '123',
39+
onCreate() {
40+
// console.log('onCreated')
41+
},
42+
},
43+
}),
44+
},
45+
})
46+
</script>
47+
48+
<template>
49+
<div style="padding-top: 100px">
50+
<button @click="() => modal.open()">
51+
open a modal
52+
</button>
53+
</div>
54+
55+
<ModalsContainer />
56+
</template>
57+
58+
<docs lang="md">
59+
### Markdown docs for Basic example
60+
</docs>

0 commit comments

Comments
 (0)