2020-10-17 20:12:00 +09:00
|
|
|
<template>
|
2023-01-15 09:14:17 +09:00
|
|
|
<Transition
|
|
|
|
appear
|
2023-04-01 13:42:40 +09:00
|
|
|
:enter-active-class="defaultStore.state.animation ? $style.transition_fade_enterActive : ''"
|
|
|
|
:leave-active-class="defaultStore.state.animation ? $style.transition_fade_leaveActive : ''"
|
|
|
|
:enter-from-class="defaultStore.state.animation ? $style.transition_fade_enterFrom : ''"
|
|
|
|
:leave-to-class="defaultStore.state.animation ? $style.transition_fade_leaveTo : ''"
|
2023-01-15 09:14:17 +09:00
|
|
|
>
|
|
|
|
<div ref="rootEl" :class="$style.root" :style="{ zIndex }" @contextmenu.prevent.stop="() => {}">
|
2022-07-17 23:18:05 +09:00
|
|
|
<MkMenu :items="items" :align="'left'" @close="$emit('closed')"/>
|
2020-10-25 23:22:27 +09:00
|
|
|
</div>
|
2022-12-30 13:37:14 +09:00
|
|
|
</Transition>
|
2020-10-17 20:12:00 +09:00
|
|
|
</template>
|
|
|
|
|
2022-01-30 14:11:52 +09:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, onBeforeUnmount } from 'vue';
|
2022-09-06 18:21:49 +09:00
|
|
|
import MkMenu from './MkMenu.vue';
|
2022-01-30 14:11:52 +09:00
|
|
|
import { MenuItem } from './types/menu.vue';
|
2022-07-17 23:18:05 +09:00
|
|
|
import contains from '@/scripts/contains';
|
2023-04-01 13:42:40 +09:00
|
|
|
import { defaultStore } from '@/store';
|
2023-04-04 09:38:34 +09:00
|
|
|
import * as os from '@/os';
|
2020-10-17 20:12:00 +09:00
|
|
|
|
2022-01-30 14:11:52 +09:00
|
|
|
const props = defineProps<{
|
|
|
|
items: MenuItem[];
|
|
|
|
ev: MouseEvent;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-05-26 22:53:09 +09:00
|
|
|
(ev: 'closed'): void;
|
2022-01-30 14:11:52 +09:00
|
|
|
}>();
|
|
|
|
|
2023-01-03 10:12:37 +09:00
|
|
|
let rootEl = $shallowRef<HTMLDivElement>();
|
2022-01-30 14:11:52 +09:00
|
|
|
|
|
|
|
let zIndex = $ref<number>(os.claimZIndex('high'));
|
|
|
|
|
2023-02-25 13:44:37 +09:00
|
|
|
const SCROLLBAR_THICKNESS = 16;
|
|
|
|
|
2022-01-30 14:11:52 +09:00
|
|
|
onMounted(() => {
|
|
|
|
let left = props.ev.pageX + 1; // 間違って右ダブルクリックした場合に意図せずアイテムがクリックされるのを防ぐため + 1
|
|
|
|
let top = props.ev.pageY + 1; // 間違って右ダブルクリックした場合に意図せずアイテムがクリックされるのを防ぐため + 1
|
|
|
|
|
|
|
|
const width = rootEl.offsetWidth;
|
|
|
|
const height = rootEl.offsetHeight;
|
|
|
|
|
2023-02-25 13:44:37 +09:00
|
|
|
if (left + width - window.pageXOffset >= (window.innerWidth - SCROLLBAR_THICKNESS)) {
|
|
|
|
left = (window.innerWidth - SCROLLBAR_THICKNESS) - width + window.pageXOffset;
|
2022-01-30 14:11:52 +09:00
|
|
|
}
|
|
|
|
|
2023-02-25 13:44:37 +09:00
|
|
|
if (top + height - window.pageYOffset >= (window.innerHeight - SCROLLBAR_THICKNESS)) {
|
|
|
|
top = (window.innerHeight - SCROLLBAR_THICKNESS) - height + window.pageYOffset;
|
2022-01-30 14:11:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (top < 0) {
|
|
|
|
top = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left < 0) {
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rootEl.style.top = `${top}px`;
|
|
|
|
rootEl.style.left = `${left}px`;
|
|
|
|
|
|
|
|
for (const el of Array.from(document.querySelectorAll('body *'))) {
|
|
|
|
el.addEventListener('mousedown', onMousedown);
|
2020-10-17 20:12:00 +09:00
|
|
|
}
|
|
|
|
});
|
2022-01-30 14:11:52 +09:00
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
for (const el of Array.from(document.querySelectorAll('body *'))) {
|
|
|
|
el.removeEventListener('mousedown', onMousedown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-26 22:53:09 +09:00
|
|
|
function onMousedown(evt: Event) {
|
|
|
|
if (!contains(rootEl, evt.target) && (rootEl !== evt.target)) emit('closed');
|
2022-01-30 14:11:52 +09:00
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
</script>
|
|
|
|
|
2023-01-15 09:14:17 +09:00
|
|
|
<style lang="scss" module>
|
|
|
|
.transition_fade_enterActive,
|
|
|
|
.transition_fade_leaveActive {
|
2023-01-08 09:58:35 +09:00
|
|
|
transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1), transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
2020-10-25 23:22:27 +09:00
|
|
|
transform-origin: left top;
|
|
|
|
}
|
2023-01-15 09:14:17 +09:00
|
|
|
.transition_fade_enterFrom,
|
|
|
|
.transition_fade_leaveTo {
|
2020-10-25 23:22:27 +09:00
|
|
|
opacity: 0;
|
|
|
|
transform: scale(0.9);
|
|
|
|
}
|
2023-01-15 09:14:17 +09:00
|
|
|
|
|
|
|
.root {
|
|
|
|
position: absolute;
|
|
|
|
}
|
2020-10-17 20:12:00 +09:00
|
|
|
</style>
|