2020-10-17 20:12:00 +09:00
|
|
|
<template>
|
2021-12-17 02:14:40 +09:00
|
|
|
<transition :name="$store.state.animation ? (type === 'drawer') ? 'modal-drawer' : (type === 'popup') ? 'modal-popup' : 'modal' : ''" :duration="$store.state.animation ? 200 : 0" appear @after-leave="$emit('closed')" @enter="$emit('opening')" @after-enter="childRendered">
|
|
|
|
<div v-show="manualShowing != null ? manualShowing : showing" v-hotkey.global="keymap" class="qzhlnise" :class="{ drawer: type === 'drawer', dialog: type === 'dialog' || type === 'dialog:top', popup: type === 'popup' }" :style="{ zIndex, pointerEvents: (manualShowing != null ? manualShowing : showing) ? 'auto' : 'none', '--transformOrigin': transformOrigin }">
|
2021-12-11 23:01:05 +09:00
|
|
|
<div class="bg _modalBg" :style="{ zIndex }" @click="onBgClick" @contextmenu.prevent.stop="() => {}"></div>
|
2021-12-17 02:14:40 +09:00
|
|
|
<div ref="content" class="content" :class="{ fixed, top: type === 'dialog:top' }" :style="{ zIndex }" @click.self="onBgClick">
|
|
|
|
<slot :max-height="maxHeight" :type="type"></slot>
|
2021-03-05 16:59:43 +09:00
|
|
|
</div>
|
2020-10-17 20:12:00 +09:00
|
|
|
</div>
|
2021-03-05 16:59:43 +09:00
|
|
|
</transition>
|
2020-10-17 20:12:00 +09:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-12-17 02:14:40 +09:00
|
|
|
import { defineComponent, nextTick, onMounted, computed, PropType, ref, watch } from 'vue';
|
2021-12-11 23:01:05 +09:00
|
|
|
import * as os from '@/os';
|
2021-12-17 02:14:40 +09:00
|
|
|
import { isTouchUsing } from '@/scripts/touch';
|
2020-10-17 20:12:00 +09:00
|
|
|
|
|
|
|
function getFixedContainer(el: Element | null): Element | null {
|
|
|
|
if (el == null || el.tagName === 'BODY') return null;
|
|
|
|
const position = window.getComputedStyle(el).getPropertyValue('position');
|
|
|
|
if (position === 'fixed') {
|
|
|
|
return el;
|
|
|
|
} else {
|
|
|
|
return getFixedContainer(el.parentElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
provide: {
|
|
|
|
modal: true
|
|
|
|
},
|
2021-12-17 02:14:40 +09:00
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
props: {
|
2021-02-28 01:09:59 +09:00
|
|
|
manualShowing: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: null,
|
|
|
|
},
|
2020-10-17 20:12:00 +09:00
|
|
|
srcCenter: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
src: {
|
2021-12-17 02:14:40 +09:00
|
|
|
type: Object as PropType<HTMLElement>,
|
2020-10-17 20:12:00 +09:00
|
|
|
required: false,
|
2021-12-17 02:14:40 +09:00
|
|
|
default: null,
|
2020-10-17 20:12:00 +09:00
|
|
|
},
|
2021-12-17 02:14:40 +09:00
|
|
|
preferType: {
|
|
|
|
required: false,
|
|
|
|
type: String,
|
|
|
|
default: 'auto',
|
2021-05-14 11:04:21 +09:00
|
|
|
},
|
|
|
|
front: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false,
|
2021-12-17 02:14:40 +09:00
|
|
|
},
|
|
|
|
noOverlap: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-10-17 20:12:00 +09:00
|
|
|
},
|
2021-12-17 02:14:40 +09:00
|
|
|
|
2021-02-28 01:09:59 +09:00
|
|
|
emits: ['opening', 'click', 'esc', 'close', 'closed'],
|
2021-12-17 02:14:40 +09:00
|
|
|
|
|
|
|
setup(props, context) {
|
|
|
|
const maxHeight = ref<number>();
|
|
|
|
const fixed = ref(false);
|
|
|
|
const transformOrigin = ref('center');
|
|
|
|
const showing = ref(true);
|
|
|
|
const content = ref<HTMLElement>();
|
|
|
|
const zIndex = os.claimZIndex(props.front);
|
|
|
|
const type = computed(() => {
|
|
|
|
if (props.preferType === 'auto') {
|
|
|
|
if (isTouchUsing && window.innerWidth < 500 && window.innerHeight < 1000) {
|
|
|
|
return 'drawer';
|
|
|
|
} else {
|
|
|
|
return props.src != null ? 'popup' : 'dialog';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return props.preferType;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let contentClicking = false;
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
// eslint-disable-next-line vue/no-mutating-props
|
|
|
|
if (props.src) props.src.style.pointerEvents = 'auto';
|
|
|
|
showing.value = false;
|
|
|
|
context.emit('close');
|
2020-10-17 20:12:00 +09:00
|
|
|
};
|
2021-12-17 02:14:40 +09:00
|
|
|
|
|
|
|
const onBgClick = () => {
|
|
|
|
if (contentClicking) return;
|
|
|
|
context.emit('click');
|
|
|
|
};
|
|
|
|
|
|
|
|
if (type.value === 'drawer') {
|
|
|
|
maxHeight.value = 300;
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
const keymap = {
|
|
|
|
'esc': () => context.emit('esc'),
|
|
|
|
};
|
|
|
|
|
|
|
|
const MARGIN = 16;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
const align = () => {
|
|
|
|
if (props.src == null) return;
|
|
|
|
if (type.value === 'drawer') return;
|
|
|
|
|
|
|
|
const popover = content.value!;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-03-05 16:59:43 +09:00
|
|
|
if (popover == null) return;
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
const rect = props.src.getBoundingClientRect();
|
2021-02-28 02:22:53 +09:00
|
|
|
|
|
|
|
const width = popover.offsetWidth;
|
|
|
|
const height = popover.offsetHeight;
|
|
|
|
|
|
|
|
let left;
|
|
|
|
let top;
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
if (props.srcCenter) {
|
|
|
|
const x = rect.left + (fixed.value ? 0 : window.pageXOffset) + (props.src.offsetWidth / 2);
|
|
|
|
const y = rect.top + (fixed.value ? 0 : window.pageYOffset) + (props.src.offsetHeight / 2);
|
2021-02-28 02:22:53 +09:00
|
|
|
left = (x - (width / 2));
|
|
|
|
top = (y - (height / 2));
|
|
|
|
} else {
|
2021-12-17 02:14:40 +09:00
|
|
|
const x = rect.left + (fixed.value ? 0 : window.pageXOffset) + (props.src.offsetWidth / 2);
|
|
|
|
const y = rect.top + (fixed.value ? 0 : window.pageYOffset) + props.src.offsetHeight;
|
2021-02-28 02:22:53 +09:00
|
|
|
left = (x - (width / 2));
|
|
|
|
top = y;
|
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
if (fixed.value) {
|
|
|
|
// 画面から横にはみ出る場合
|
2021-02-28 02:22:53 +09:00
|
|
|
if (left + width > window.innerWidth) {
|
|
|
|
left = window.innerWidth - width;
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
// 画面から縦にはみ出る場合
|
|
|
|
if (top + height > (window.innerHeight - MARGIN)) {
|
|
|
|
if (props.noOverlap) {
|
|
|
|
const underSpace = (window.innerHeight - MARGIN) - top;
|
|
|
|
const upperSpace = (rect.top - MARGIN);
|
|
|
|
if (underSpace >= (upperSpace / 3)) {
|
|
|
|
maxHeight.value = underSpace;
|
|
|
|
} else {
|
|
|
|
maxHeight.value = upperSpace;
|
|
|
|
top = (upperSpace + MARGIN) - height;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
top = (window.innerHeight - MARGIN) - height;
|
|
|
|
}
|
2021-02-28 02:22:53 +09:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-17 02:14:40 +09:00
|
|
|
// 画面から横にはみ出る場合
|
2021-02-28 02:22:53 +09:00
|
|
|
if (left + width - window.pageXOffset > window.innerWidth) {
|
|
|
|
left = window.innerWidth - width + window.pageXOffset - 1;
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
// 画面から縦にはみ出る場合
|
|
|
|
if (top + height - window.pageYOffset > (window.innerHeight - MARGIN)) {
|
|
|
|
if (props.noOverlap) {
|
|
|
|
const underSpace = (window.innerHeight - MARGIN) - (top - window.pageYOffset);
|
|
|
|
const upperSpace = (rect.top - MARGIN);
|
|
|
|
if (underSpace >= (upperSpace / 3)) {
|
|
|
|
maxHeight.value = underSpace;
|
|
|
|
} else {
|
|
|
|
maxHeight.value = upperSpace;
|
|
|
|
top = window.pageYOffset + ((upperSpace + MARGIN) - height);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
top = (window.innerHeight - MARGIN) - height + window.pageYOffset - 1;
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
2021-02-28 02:22:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (top < 0) {
|
2021-12-17 02:14:40 +09:00
|
|
|
top = MARGIN;
|
2021-02-28 02:22:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (left < 0) {
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
if (top > rect.top + (fixed.value ? 0 : window.pageYOffset)) {
|
|
|
|
transformOrigin.value = 'center top';
|
|
|
|
} else if ((top + height) <= rect.top + (fixed.value ? 0 : window.pageYOffset)) {
|
|
|
|
transformOrigin.value = 'center bottom';
|
2021-02-28 09:53:21 +09:00
|
|
|
} else {
|
2021-12-17 02:14:40 +09:00
|
|
|
transformOrigin.value = 'center';
|
2021-02-28 02:22:53 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
popover.style.left = left + 'px';
|
|
|
|
popover.style.top = top + 'px';
|
2021-12-17 02:14:40 +09:00
|
|
|
};
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
const childRendered = () => {
|
2020-10-17 20:12:00 +09:00
|
|
|
// モーダルコンテンツにマウスボタンが押され、コンテンツ外でマウスボタンが離されたときにモーダルバックグラウンドクリックと判定させないためにマウスイベントを監視しフラグ管理する
|
2021-12-17 02:14:40 +09:00
|
|
|
const el = content.value!.children[0];
|
|
|
|
el.addEventListener('mousedown', e => {
|
|
|
|
contentClicking = true;
|
2020-10-17 20:12:00 +09:00
|
|
|
window.addEventListener('mouseup', e => {
|
|
|
|
// click イベントより先に mouseup イベントが発生するかもしれないのでちょっと待つ
|
|
|
|
setTimeout(() => {
|
2021-12-17 02:14:40 +09:00
|
|
|
contentClicking = false;
|
2020-10-17 20:12:00 +09:00
|
|
|
}, 100);
|
|
|
|
}, { passive: true, once: true });
|
|
|
|
}, { passive: true });
|
2021-12-17 02:14:40 +09:00
|
|
|
};
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
onMounted(() => {
|
|
|
|
watch(() => props.src, async () => {
|
|
|
|
if (props.src) {
|
|
|
|
// eslint-disable-next-line vue/no-mutating-props
|
|
|
|
props.src.style.pointerEvents = 'none';
|
|
|
|
}
|
|
|
|
fixed.value = (type.value === 'drawer') || (getFixedContainer(props.src) != null);
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
align();
|
|
|
|
}, { immediate: true, });
|
|
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
const popover = content.value;
|
|
|
|
new ResizeObserver((entries, observer) => {
|
|
|
|
align();
|
|
|
|
}).observe(popover!);
|
|
|
|
});
|
|
|
|
});
|
2021-03-05 16:21:16 +09:00
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
return {
|
|
|
|
showing,
|
|
|
|
type,
|
|
|
|
fixed,
|
|
|
|
content,
|
|
|
|
transformOrigin,
|
|
|
|
maxHeight,
|
|
|
|
close,
|
|
|
|
zIndex,
|
|
|
|
keymap,
|
|
|
|
onBgClick,
|
|
|
|
childRendered,
|
|
|
|
};
|
|
|
|
},
|
2020-10-17 20:12:00 +09:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-03-05 16:59:43 +09:00
|
|
|
.modal-enter-active, .modal-leave-active {
|
|
|
|
> .bg {
|
2021-12-17 02:14:40 +09:00
|
|
|
transition: opacity 0.2s !important;
|
2021-03-05 16:59:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
> .content {
|
2021-12-17 02:14:40 +09:00
|
|
|
transform-origin: var(--transformOrigin);
|
|
|
|
transition: opacity 0.2s, transform 0.2s !important;
|
2021-03-05 16:59:43 +09:00
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
2021-03-05 16:59:43 +09:00
|
|
|
.modal-enter-from, .modal-leave-to {
|
|
|
|
> .bg {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .content {
|
|
|
|
pointer-events: none;
|
|
|
|
opacity: 0;
|
2021-12-17 02:14:40 +09:00
|
|
|
transform-origin: var(--transformOrigin);
|
2021-03-05 16:59:43 +09:00
|
|
|
transform: scale(0.9);
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:59:43 +09:00
|
|
|
.modal-popup-enter-active, .modal-popup-leave-active {
|
|
|
|
> .bg {
|
2021-12-17 02:14:40 +09:00
|
|
|
transition: opacity 0.2s !important;
|
2021-03-05 16:59:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
> .content {
|
2021-12-17 02:14:40 +09:00
|
|
|
transform-origin: var(--transformOrigin);
|
|
|
|
transition: opacity 0.2s cubic-bezier(0, 0, 0.2, 1), transform 0.2s cubic-bezier(0, 0, 0.2, 1) !important;
|
2021-03-05 16:59:43 +09:00
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
2021-03-05 16:59:43 +09:00
|
|
|
.modal-popup-enter-from, .modal-popup-leave-to {
|
|
|
|
> .bg {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .content {
|
|
|
|
pointer-events: none;
|
|
|
|
opacity: 0;
|
2021-12-17 02:14:40 +09:00
|
|
|
transform-origin: var(--transformOrigin);
|
2021-03-05 16:59:43 +09:00
|
|
|
transform: scale(0.9);
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
.modal-drawer-enter-active {
|
|
|
|
> .bg {
|
|
|
|
transition: opacity 0.2s !important;
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
> .content {
|
|
|
|
transition: transform 0.2s cubic-bezier(0,.5,0,1) !important;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.modal-drawer-leave-active {
|
|
|
|
> .bg {
|
|
|
|
transition: opacity 0.2s !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .content {
|
|
|
|
transition: transform 0.2s cubic-bezier(0,.5,0,1) !important;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.modal-drawer-enter-from, .modal-drawer-leave-to {
|
|
|
|
> .bg {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .content {
|
|
|
|
pointer-events: none;
|
|
|
|
transform: translateY(100%);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.qzhlnise {
|
|
|
|
&.dialog {
|
|
|
|
> .content {
|
|
|
|
position: fixed;
|
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
2020-10-17 20:12:00 +09:00
|
|
|
margin: auto;
|
2021-12-17 02:14:40 +09:00
|
|
|
padding: 32px;
|
|
|
|
// TODO: mask-imageはiOSだとやたら重い。なんとかしたい
|
|
|
|
-webkit-mask-image: linear-gradient(0deg, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 32px, rgba(0,0,0,1) calc(100% - 32px), rgba(0,0,0,0) 100%);
|
|
|
|
mask-image: linear-gradient(0deg, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 32px, rgba(0,0,0,1) calc(100% - 32px), rgba(0,0,0,0) 100%);
|
|
|
|
overflow: auto;
|
|
|
|
display: flex;
|
|
|
|
|
|
|
|
@media (max-width: 500px) {
|
|
|
|
padding: 16px;
|
|
|
|
-webkit-mask-image: linear-gradient(0deg, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 16px, rgba(0,0,0,1) calc(100% - 16px), rgba(0,0,0,0) 100%);
|
|
|
|
mask-image: linear-gradient(0deg, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 16px, rgba(0,0,0,1) calc(100% - 16px), rgba(0,0,0,0) 100%);
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-03-05 16:59:43 +09:00
|
|
|
> ::v-deep(*) {
|
2021-12-17 02:14:40 +09:00
|
|
|
margin: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.top {
|
|
|
|
> ::v-deep(*) {
|
|
|
|
margin-top: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.popup {
|
|
|
|
> .content {
|
|
|
|
position: absolute;
|
|
|
|
|
|
|
|
&.fixed {
|
|
|
|
position: fixed;
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
&.drawer {
|
|
|
|
position: fixed;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
overflow: clip;
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2021-12-17 02:14:40 +09:00
|
|
|
> .content {
|
2020-10-17 20:12:00 +09:00
|
|
|
position: fixed;
|
2021-12-17 02:14:40 +09:00
|
|
|
bottom: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
margin: auto;
|
|
|
|
|
|
|
|
> ::v-deep(*) {
|
|
|
|
margin: auto;
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
}
|
2021-12-17 02:14:40 +09:00
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
</style>
|