mizzkey/src/client/components/modal.vue

164 lines
3.7 KiB
Vue
Raw Normal View History

<template>
2020-09-05 14:52:36 +02:00
<div class="mk-modal" v-hotkey.global="keymap" :style="{ pointerEvents: showing ? 'auto' : 'none' }">
2020-02-06 11:11:14 +01:00
<transition :name="$store.state.device.animation ? 'bg-fade' : ''" appear>
2020-09-05 15:06:33 +02:00
<div class="bg _modalBg" v-if="showing" @click="$emit('click')"></div>
</transition>
2020-09-06 03:04:26 +02:00
<div class="content" :class="{ popup, fixed }" @click.self="$emit('click')" ref="content">
2020-09-05 15:06:33 +02:00
<transition :name="$store.state.device.animation ? 'modal' : ''" appear @after-leave="$emit('closed')">
<slot v-if="showing"></slot>
</transition>
</div>
</div>
</template>
<script lang="ts">
2020-07-23 20:15:32 +02:00
import { defineComponent } from 'vue';
2020-09-05 17:20:06 +02:00
// memo: popup.vueのfixedプロパティに相当するものはsource要素の祖先を辿るなどして自動で判定できるのでは
2020-07-23 20:15:32 +02:00
export default defineComponent({
2020-09-05 15:06:33 +02:00
emits: ['click', 'esc', 'closed'],
props: {
2020-09-05 14:52:36 +02:00
showing: {
type: Boolean,
required: true,
},
canClose: {
type: Boolean,
required: false,
default: true,
},
2020-09-06 03:04:26 +02:00
// TODO: 要る?
noCenter: {
type: Boolean,
required: false
},
source: {
2020-09-05 17:20:06 +02:00
required: false,
}
},
2020-09-06 03:04:26 +02:00
data() {
return {
fixed: false,
};
},
2020-02-16 14:46:18 +01:00
computed: {
keymap(): any {
return {
2020-09-05 14:52:36 +02:00
'esc': () => this.$emit('esc'),
2020-02-16 14:46:18 +01:00
};
},
2020-09-06 03:04:26 +02:00
popup(): boolean {
return this.source != null;
}
2020-02-16 14:46:18 +01:00
},
2020-09-05 17:20:06 +02:00
mounted() {
this.$nextTick(() => {
const popover = this.$refs.content as any;
const rect = this.source.getBoundingClientRect();
const width = popover.offsetWidth;
const height = popover.offsetHeight;
let left;
let top;
if (this.$root.isMobile && !this.noCenter) {
const x = rect.left + (this.fixed ? 0 : window.pageXOffset) + (this.source.offsetWidth / 2);
const y = rect.top + (this.fixed ? 0 : window.pageYOffset) + (this.source.offsetHeight / 2);
left = (x - (width / 2));
top = (y - (height / 2));
popover.style.transformOrigin = 'center';
} else {
const x = rect.left + (this.fixed ? 0 : window.pageXOffset) + (this.source.offsetWidth / 2);
const y = rect.top + (this.fixed ? 0 : window.pageYOffset) + this.source.offsetHeight;
left = (x - (width / 2));
top = y;
}
if (this.fixed) {
if (left + width > window.innerWidth) {
left = window.innerWidth - width;
popover.style.transformOrigin = 'center';
}
if (top + height > window.innerHeight) {
top = window.innerHeight - height;
popover.style.transformOrigin = 'center';
}
} else {
if (left + width - window.pageXOffset > window.innerWidth) {
left = window.innerWidth - width + window.pageXOffset;
popover.style.transformOrigin = 'center';
}
if (top + height - window.pageYOffset > window.innerHeight) {
top = window.innerHeight - height + window.pageYOffset;
popover.style.transformOrigin = 'center';
}
}
if (top < 0) {
top = 0;
}
if (left < 0) {
left = 0;
}
popover.style.left = left + 'px';
popover.style.top = top + 'px';
});
},
});
</script>
<style lang="scss" scoped>
.modal-enter-active, .modal-leave-active {
transition: opacity 0.3s, transform 0.3s !important;
}
2020-09-05 14:52:36 +02:00
.modal-enter-from, .modal-leave-to {
pointer-events: none;
opacity: 0;
transform: scale(0.9);
}
.bg-fade-enter-active, .bg-fade-leave-active {
transition: opacity 0.3s !important;
}
2020-09-05 14:52:36 +02:00
.bg-fade-enter-from, .bg-fade-leave-to {
opacity: 0;
}
.mk-modal {
> .bg {
z-index: 10000;
}
2020-09-06 03:04:26 +02:00
> .content:not(.popup) {
position: fixed;
z-index: 10000;
top: 0;
bottom: 0;
left: 0;
right: 0;
2020-09-05 15:13:34 +02:00
margin: auto;
max-width: calc(100% - 16px);
max-height: calc(100% - 16px);
overflow: auto;
2020-09-05 14:52:36 +02:00
display: flex;
justify-content: center;
align-items: center;
}
2020-09-06 03:04:26 +02:00
> .content.popup {
position: absolute;
z-index: 10000;
&.fixed {
position: fixed;
}
}
}
</style>