Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

モーダル内でmousedownしてモーダル外でmouseupしたときはモーダルを閉じないように #4119

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/components/UI/ClickOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
cloneVNode,
shallowRef,
onMounted,
onBeforeUnmount
onBeforeUnmount,
ref
} from 'vue'
import { isIOS } from '/@/lib/dom/browser'
import { useModalStore } from '/@/store/ui/modal'
Expand All @@ -23,7 +24,8 @@ const filterChildren = <T extends VNode>(vnodes: T[]) =>
return true
})

const eventName = isIOS() ? 'touchend' : 'click'
const startEventName = isIOS() ? 'touchstart' : 'mousedown'
const endEventName = isIOS() ? 'touchend' : 'mouseup'

/**
* そのデフォルトスロットに指定した要素の外でクリックされたときにclickOutsideイベントを発火する
Expand Down Expand Up @@ -51,8 +53,9 @@ export default defineComponent({
const element = shallowRef<Element | ComponentPublicInstance>()

const { shouldShowModal } = useModalStore()
const isMouseDown = ref(false)

const onClick = (e: MouseEvent | TouchEvent) => {
const onMouseDown = (e: MouseEvent | TouchEvent) => {
if (!element.value) return

if (props.unableWhileModalOpen && shouldShowModal.value) return
Expand All @@ -64,17 +67,40 @@ export default defineComponent({
return
}

isMouseDown.value = true
if (props.stop) {
e.stopPropagation()
}
}
const onMouseUp = (e: MouseEvent | TouchEvent) => {
if (!isMouseDown.value) return
isMouseDown.value = false

if (!element.value) return
const ele =
element.value instanceof Element ? element.value : element.value.$el

if (ele === e.target || e.composedPath().includes(ele)) {
return
}

emit('clickOutside', e)
if (props.stop) {
e.stopPropagation()
}
}

onMounted(() => {
window.addEventListener(eventName, onClick, { capture: true })
window.addEventListener(startEventName, onMouseDown, { capture: true })
})
onMounted(() => {
window.addEventListener(endEventName, onMouseUp, { capture: true })
})
onBeforeUnmount(() => {
window.removeEventListener(startEventName, onMouseDown, { capture: true })
})
onBeforeUnmount(() => {
window.removeEventListener(eventName, onClick, { capture: true })
window.removeEventListener(endEventName, onMouseUp, { capture: true })
})

return () => {
Expand Down