Skip to content

Commit 504d8a3

Browse files
committed
Merge branch 'dev' into next
t st git # Please enter the commit message for your changes. Lines starting
2 parents cbf243c + e36cd9b commit 504d8a3

File tree

8 files changed

+75
-32
lines changed

8 files changed

+75
-32
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,15 @@ toggle modal by name.
300300
301301
### `@before-open`
302302

303-
- Emits while modal is still invisible, but before transition starting.
303+
- 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()` .
304304

305305
### `@opened`
306306

307307
- Emits after modal became visible and transition ended.
308308

309309
### `@before-close`
310310

311-
- Emits before modal is going to be closed.
311+
- Emits before modal is going to be closed. Further closing of the modal can be blocked from this event listener by calling `event.stop()` .
312312

313313
### `@closed`
314314

dist/VueFinalModal.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/VueFinalModal.esm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/VueFinalModal.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/VueFinalModal.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/content/en/index.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Introduction
33
description: 'Vue Final Modal is a renderless, stackable, detachable and lightweight modal component.'
44
position: 0
55
category: Getting Start
6-
version: 0.19
6+
version: 0.20
77
features:
88
- Support Vue 3 and Vue 2
99
- Tailwind CSS friendly
@@ -489,20 +489,41 @@ If prop `clickToClose` is `false`, the event will still be emitted.
489489

490490
### `@before-open`
491491

492-
- Emits while modal is still invisible, but before transition starting.
492+
- 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()` .
493493

494494
### `@opened`
495495

496496
- Emits after modal became visible and transition ended.
497497

498498
### `@before-close`
499499

500-
- Emits before modal is going to be closed.
500+
- Emits before modal is going to be closed. Further closing of the modal can be blocked from this event listener by calling `event.stop()` .
501+
502+
#
501503

502504
### `@closed`
503505

504506
- Emits right before modal is destroyed.
505507

506508
## Contribution
507509

508-
👋 Hi I'm Hunter. Author of `vue-final-modal`. There is no perfect library even the `final` of vue modal. If you have any ideas for optimization of `vue-final-modal`, feel free to open [issues](https://github.com/hunterliu1003/vue-final-modal/issues) or [pull requests](https://github.com/hunterliu1003/vue-final-modal/pulls).
510+
👋 Hi I'm Hunter, the author of `vue-final-modal`.
511+
512+
To develop vue-final-modal, I learn a lot from these awesome libraries:
513+
514+
- [Vuetify](https://vuetifyjs.com/en/)
515+
- attach
516+
- [Element UI](https://element.eleme.io/)
517+
- stackable modal
518+
- zIndex
519+
- zIndexBase
520+
- [vue-js-modal](https://github.com/euvl/vue-js-modal)
521+
- dynamic modal
522+
- transition
523+
- focusTrap for A11y
524+
- [Bootstrap Vue](https://bootstrap-vue.org/)
525+
- lockScroll
526+
527+
> There is no perfect library even the `final` of vue modal.
528+
529+
If you have any ideas for optimization of `vue-final-modal`, feel free to open [issues](https://github.com/hunterliu1003/vue-final-modal/issues) or [pull requests](https://github.com/hunterliu1003/vue-final-modal/pulls).

lib/PluginCore.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ function createVfm(options) {
77
return function() {
88
vfm = {
99
isScrollLocked: false,
10-
openedModals: [],
10+
get openedModals() {
11+
return this.modals.filter(modal => modal.props.modelValue)
12+
},
1113
modals: [],
1214
show(name) {
1315
this.toggle(name, true)
@@ -21,9 +23,9 @@ function createVfm(options) {
2123
})
2224
},
2325
toggle(name, show) {
24-
const modal = this.modals.find(modal => modal.name === name)
26+
const modal = this.modals.find(modal => modal.props.name === name)
2527
if (modal !== undefined) {
26-
modal.emit('update:modelValue', show === undefined ? !modal.value : show)
28+
modal.emit('update:modelValue', show === undefined ? !modal.props.modelValue : show)
2729
}
2830
},
2931
lockScroll() {

lib/VueFinalModal.vue

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export const visibility = reactive({
120120
})
121121
const overlayTransitionState = ref(null)
122122
const modalTransitionState = ref(null)
123+
const _stopEvent = ref(false)
123124
124125
const isComponentReadyToBeDestroyed = computed(() => {
125126
return (
@@ -143,6 +144,10 @@ export const calculateZIndex = computed(() => {
143144
watch(
144145
() => props.modelValue,
145146
value => {
147+
if (_stopEvent.value) {
148+
_stopEvent.value = false
149+
return
150+
}
146151
mounted()
147152
if (!value) {
148153
close()
@@ -186,17 +191,14 @@ onBeforeUnmount(() => {
186191
function getModalInfo() {
187192
return {
188193
uid,
189-
name: props.name,
194+
props,
190195
emit,
191196
vfmContainer,
192197
vfmContent,
193198
getAttachElement,
194199
modalStackIndex,
195200
visibility,
196201
handleLockScroll,
197-
hideOverlay: props.hideOverlay,
198-
focusRetain: props.focusRetain,
199-
focusTrap: props.focusTrap,
200202
$focusTrap
201203
}
202204
}
@@ -205,13 +207,7 @@ function mounted() {
205207
let target = getAttachElement()
206208
if (target || props.attach === false) {
207209
props.attach !== false && target.appendChild(root.value)
208-
let index = $vfm.openedModals.findIndex(vm => vm.uid === uid)
209210
210-
if (index !== -1) {
211-
// if this is already exist in modalStack, delete it
212-
$vfm.openedModals.splice(index, 1)
213-
}
214-
$vfm.openedModals.push(getModalInfo())
215211
modalStackIndex.value = $vfm.openedModals.length - 1
216212
217213
handleLockScroll()
@@ -226,6 +222,10 @@ function mounted() {
226222
}
227223
})
228224
225+
if (emitEvent('before-open', false)) {
226+
return
227+
}
228+
229229
visible.value = true
230230
nextTick(() => {
231231
startTransitionEnter()
@@ -236,20 +236,18 @@ function mounted() {
236236
}
237237
}
238238
function close() {
239-
let index = $vfm.openedModals.findIndex(vm => vm.uid === uid)
240-
if (index !== -1) {
241-
// remove this in modalStack
242-
$vfm.openedModals.splice(index, 1)
239+
if (emitEvent('before-close', true)) {
240+
return
243241
}
244242
if ($vfm.openedModals.length > 0) {
245243
// If there are still nested modals opened
246244
const $_vm = $vfm.openedModals[$vfm.openedModals.length - 1]
247245
$_vm.handleLockScroll()
248-
$_vm.focusTrap && $_vm.$focusTrap.firstElement().focus()
249-
if ($_vm.focusRetain || $_vm.focusTrap) {
246+
$_vm.props.focusTrap && $_vm.$focusTrap.firstElement().focus()
247+
if ($_vm.props.focusRetain || $_vm.props.focusTrap) {
250248
$_vm.vfmContainer.value.focus()
251249
}
252-
!$_vm.hideOverlay && ($_vm.visibility.overlay = true)
250+
!$_vm.props.hideOverlay && ($_vm.visibility.overlay = true)
253251
}
254252
startTransitionLeave()
255253
}
@@ -297,7 +295,6 @@ export function afterOverlayLeave() {
297295
overlayTransitionState.value = TransitionState.Leave
298296
}
299297
export function beforeModalEnter() {
300-
emit('before-open')
301298
modalTransitionState.value = TransitionState.Entering
302299
}
303300
export function afterModalEnter() {
@@ -311,7 +308,6 @@ export function afterModalEnter() {
311308
emit('opened')
312309
}
313310
export function beforeModalLeave() {
314-
emit('before-close')
315311
modalTransitionState.value = TransitionState.Leaving
316312
317313
if ($focusTrap.enabled()) {
@@ -335,6 +331,30 @@ export function onEsc(evt) {
335331
emit('update:modelValue', false)
336332
}
337333
}
334+
function createModalEvent(params = {}) {
335+
return {
336+
ref: getModalInfo(),
337+
...params
338+
}
339+
}
340+
function emitEvent(eventType, value) {
341+
let stopEvent = false
342+
const event = createModalEvent({
343+
type: eventType,
344+
stop() {
345+
stopEvent = true
346+
}
347+
})
348+
emit(eventType, event)
349+
if (stopEvent) {
350+
_stopEvent.value = true
351+
nextTick(() => {
352+
emit('update:modelValue', value)
353+
})
354+
return true
355+
}
356+
return false
357+
}
338358
</script>
339359

340360
<style lang="css" scoped>

0 commit comments

Comments
 (0)