1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-16 16:45:30 +02:00
Files
misskey/src/client/components/modal.vue
syuilo 4d68baca0d wip
2020-09-06 00:20:06 +09:00

142 lines
3.4 KiB
Vue

<template>
<div class="mk-modal" v-hotkey.global="keymap" :style="{ pointerEvents: showing ? 'auto' : 'none' }">
<transition :name="$store.state.device.animation ? 'bg-fade' : ''" appear>
<div class="bg _modalBg" v-if="showing" @click="$emit('click')"></div>
</transition>
<div class="content" @click.self="$emit('click')">
<transition :name="$store.state.device.animation ? 'modal' : ''" appear @after-leave="$emit('closed')">
<slot v-if="showing"></slot>
</transition>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
// memo: popup.vueのfixedプロパティに相当するものはsource要素の祖先を辿るなどして自動で判定できるのでは
export default defineComponent({
emits: ['click', 'esc', 'closed'],
props: {
showing: {
type: Boolean,
required: true,
},
canClose: {
type: Boolean,
required: false,
default: true,
},
sourceEl: {
required: false,
}
},
computed: {
keymap(): any {
return {
'esc': () => this.$emit('esc'),
};
},
},
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;
}
.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;
}
.bg-fade-enter-from, .bg-fade-leave-to {
opacity: 0;
}
.mk-modal {
> .bg {
z-index: 10000;
}
> .content {
position: fixed;
z-index: 10000;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
max-width: calc(100% - 16px);
max-height: calc(100% - 16px);
overflow: auto;
display: flex;
justify-content: center;
align-items: center;
}
}
</style>