paricafe/packages/client/src/components/ui/popup.vue

240 lines
6.1 KiB
Vue
Raw Normal View History

2021-08-07 22:19:10 -05:00
<template>
2021-11-28 05:07:37 -06:00
<transition :name="$store.state.animation ? 'popup-menu' : ''" appear @after-leave="$emit('closed')" @enter="$emit('opening')">
2021-11-19 04:36:12 -06:00
<div v-show="manualShowing != null ? manualShowing : showing" ref="content" class="ccczpooj" :class="{ front, fixed, top: position === 'top' }" :style="{ pointerEvents: (manualShowing != null ? manualShowing : showing) ? 'auto' : 'none', '--transformOrigin': transformOrigin }">
2021-11-28 05:07:37 -06:00
<slot :max-height="maxHeight" :close="close"></slot>
2021-08-07 22:19:10 -05:00
</div>
</transition>
</template>
<script lang="ts">
2021-11-28 05:07:37 -06:00
import { defineComponent, nextTick, onMounted, onUnmounted, PropType, ref, watch } from 'vue';
2021-08-07 22:19:10 -05:00
2021-11-28 05:07:37 -06:00
function getFixedContainer(el: Element | null | undefined): Element | null {
2021-08-07 22:19:10 -05:00
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({
props: {
manualShowing: {
type: Boolean,
required: false,
default: null,
},
srcCenter: {
type: Boolean,
required: false
},
src: {
2021-08-07 22:45:44 -05:00
type: Object as PropType<HTMLElement>,
2021-08-07 22:19:10 -05:00
required: false,
},
position: {
required: false
},
front: {
type: Boolean,
required: false,
default: false,
2021-11-28 05:07:37 -06:00
},
noOverlap: {
type: Boolean,
required: false,
default: true,
},
2021-08-07 22:19:10 -05:00
},
emits: ['opening', 'click', 'esc', 'close', 'closed'],
2021-11-28 05:07:37 -06:00
setup(props, context) {
const maxHeight = ref<number>();
const fixed = ref(false);
const transformOrigin = ref('center');
const showing = ref(true);
const content = ref<HTMLElement>();
2021-08-07 22:19:10 -05:00
2021-11-28 05:07:37 -06:00
const close = () => {
// eslint-disable-next-line vue/no-mutating-props
if (props.src) props.src.style.pointerEvents = 'auto';
showing.value = false;
context.emit('close');
};
2021-08-07 22:19:10 -05:00
2021-11-28 05:07:37 -06:00
const MARGIN = 16;
2021-08-07 22:19:10 -05:00
2021-11-28 05:07:37 -06:00
const align = () => {
if (props.src == null) return;
2021-08-07 22:19:10 -05:00
2021-11-28 05:07:37 -06:00
const popover = content.value!;
2021-08-07 22:19:10 -05:00
if (popover == null) return;
2021-11-28 05:07:37 -06:00
const rect = props.src.getBoundingClientRect();
2021-08-07 22:19:10 -05:00
const width = popover.offsetWidth;
const height = popover.offsetHeight;
let left;
let top;
2021-11-28 05:07:37 -06: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-08-07 22:19:10 -05:00
left = (x - (width / 2));
top = (y - (height / 2));
} else {
2021-11-28 05:07:37 -06: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-08-07 22:19:10 -05:00
left = (x - (width / 2));
top = y;
}
2021-11-28 05:07:37 -06:00
if (fixed.value) {
// 画面から横にはみ出る場合
2021-08-07 22:19:10 -05:00
if (left + width > window.innerWidth) {
left = window.innerWidth - width;
}
2021-11-28 05:07:37 -06: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-08-07 22:19:10 -05:00
}
} else {
2021-11-28 05:07:37 -06:00
// 画面から横にはみ出る場合
2021-08-07 22:19:10 -05:00
if (left + width - window.pageXOffset > window.innerWidth) {
left = window.innerWidth - width + window.pageXOffset - 1;
}
2021-11-28 05:07:37 -06: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;
}
2021-08-07 22:19:10 -05:00
}
}
if (top < 0) {
2021-11-28 05:07:37 -06:00
top = MARGIN;
2021-08-07 22:19:10 -05:00
}
if (left < 0) {
left = 0;
}
2021-11-28 05:07:37 -06: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-08-07 22:19:10 -05:00
} else {
2021-11-28 05:07:37 -06:00
transformOrigin.value = 'center';
2021-08-07 22:19:10 -05:00
}
popover.style.left = left + 'px';
popover.style.top = top + 'px';
2021-11-28 05:07:37 -06:00
};
2021-08-07 22:19:10 -05:00
2021-11-28 05:07:37 -06:00
const onDocumentClick = (ev: MouseEvent) => {
const flyoutElement = content.value;
2021-08-07 22:19:10 -05:00
let targetElement = ev.target;
do {
if (targetElement === flyoutElement) {
return;
}
targetElement = targetElement.parentNode;
} while (targetElement);
2021-11-28 05:07:37 -06:00
close();
};
onMounted(() => {
watch(() => props.src, async () => {
if (props.src) {
// eslint-disable-next-line vue/no-mutating-props
props.src.style.pointerEvents = 'none';
}
fixed.value = getFixedContainer(props.src) != null;
await nextTick()
align();
}, { immediate: true, });
nextTick(() => {
const popover = content.value;
new ResizeObserver((entries, observer) => {
align();
}).observe(popover!);
});
document.addEventListener('mousedown', onDocumentClick, { passive: true });
onUnmounted(() => {
document.removeEventListener('mousedown', onDocumentClick);
});
});
return {
showing,
fixed,
content,
transformOrigin,
maxHeight,
close,
};
},
2021-08-07 22:19:10 -05:00
});
</script>
<style lang="scss" scoped>
.popup-menu-enter-active {
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;
}
.popup-menu-leave-active {
transform-origin: var(--transformOrigin);
transition: opacity 0.2s cubic-bezier(0.4, 0, 1, 1), transform 0.2s cubic-bezier(0.4, 0, 1, 1) !important;
}
.popup-menu-enter-from, .popup-menu-leave-to {
pointer-events: none;
opacity: 0;
transform: scale(0.9);
}
.ccczpooj {
position: absolute;
z-index: 10000;
&.fixed {
position: fixed;
}
&.front {
z-index: 20000;
}
}
</style>