Skip to content

Commit 503c1f1

Browse files
committed
fix: should focus vfmContent element when click outside
1 parent 7b9152a commit 503c1f1

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const {
5151
} as any as InternalVfm)
5252
5353
const vfmRootEl = ref<HTMLDivElement>()
54+
const vfmContentEl = ref<HTMLDivElement>()
5455
5556
const { focus, blur } = useFocusTrap(props, { focusEl: vfmRootEl })
5657
const { zIndex, refreshZIndex, resetZIndex } = useZIndex(props)
@@ -89,14 +90,13 @@ const {
8990
},
9091
})
9192
92-
const { onEsc, onMouseupRoot, onMousedown } = useToClose(props, emit, { vfmRootEl, visible, modelValueLocal })
93+
const { onEsc, onMouseupRoot, onMousedown } = useToClose(props, emit, { vfmRootEl, vfmContentEl, visible, modelValueLocal })
9394
9495
const {
95-
vfmContentEl,
9696
swipeBannerEl,
9797
bindSwipe,
9898
onTouchStartSwipeBanner,
99-
} = useSwipeToClose(props, { modelValueLocal })
99+
} = useSwipeToClose(props, { vfmContentEl, modelValueLocal })
100100
101101
const hideOverlay = toRef(props, 'hideOverlay')
102102
const modalInstance = computed<Modal>(() => ({
@@ -175,15 +175,14 @@ onBeforeUnmount(() => {
175175
:style="{ zIndex }"
176176
role="dialog"
177177
aria-modal="true"
178-
@keydown.esc="onEsc"
178+
@keydown.esc="() => onEsc()"
179179
@mouseup.self="() => onMouseupRoot()"
180180
@mousedown.self="e => onMousedown(e)"
181181
>
182182
<Transition v-if="!hideOverlay" v-bind="overlayTransition" appear v-on="overlayListeners">
183183
<div
184184
v-if="overlayVisible"
185185
class="vfm__overlay vfm--overlay vfm--absolute vfm--inset vfm--prevent-none"
186-
style="z-index: -1"
187186
:class="overlayClass"
188187
:style="overlayStyle"
189188
aria-hidden="true"
@@ -240,6 +239,7 @@ onBeforeUnmount(() => {
240239
left: 0;
241240
}
242241
.vfm--overlay {
242+
z-index: -1;
243243
background-color: rgba(0, 0, 0, 0.5);
244244
}
245245
.vfm--prevent-none {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ export function useToClose(
77
emit: InstanceType<typeof CoreModal>['$emit'],
88
options: {
99
vfmRootEl: Ref<HTMLDivElement | undefined>
10+
vfmContentEl: Ref<HTMLDivElement | undefined>
1011
visible: Ref<boolean>
1112
modelValueLocal: Ref<boolean>
1213
}) {
13-
const { vfmRootEl, visible, modelValueLocal } = options
14+
const { vfmRootEl, vfmContentEl, visible, modelValueLocal } = options
1415
const lastMousedownEl = ref<EventTarget | null>()
1516

1617
function onEsc() {
1718
if (visible.value && props.escToClose)
18-
(modelValueLocal.value = false)
19+
modelValueLocal.value = false
1920
}
2021

2122
function onMousedown(e?: MouseEvent) {
@@ -27,10 +28,13 @@ export function useToClose(
2728
if (lastMousedownEl.value !== vfmRootEl.value)
2829
return
2930

30-
if (props.clickToClose)
31+
if (props.clickToClose) {
3132
modelValueLocal.value = false
32-
else
33+
}
34+
else {
35+
vfmContentEl.value?.focus()
3336
emit('clickOutside')
37+
}
3438
}
3539

3640
return {

packages/vue-final-modal/src/useSwipeToClose.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { clamp, noop } from './utils'
88
export function useSwipeToClose(
99
props: InstanceType<typeof CoreModal>['$props'],
1010
options: {
11+
vfmContentEl: Ref<HTMLDivElement | undefined>
1112
modelValueLocal: Ref<boolean>
1213
},
1314
) {
14-
const { modelValueLocal } = options
15+
const { vfmContentEl, modelValueLocal } = options
1516
const LIMIT_DISTANCE = 0.1
1617
const LIMIT_SPEED = 300
1718

18-
const vfmContentEl = ref<HTMLDivElement>()
1919
const swipeBannerEl = ref<HTMLDivElement>()
2020
const swipeEl = computed(() => {
2121
if (props.swipeToClose === undefined || props.swipeToClose === 'none')

0 commit comments

Comments
 (0)