2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
2024-02-13 15:50:11 +00:00
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 05:31:52 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2022-07-17 12:06:33 +00:00
|
|
|
<template>
|
2023-02-25 04:44:37 +00:00
|
|
|
<div ref="el" :class="$style.root">
|
2023-05-19 04:58:09 +00:00
|
|
|
<MkMenu :items="items" :align="align" :width="width" :asDrawer="false" @close="onChildClosed"/>
|
2022-07-17 12:06:33 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-02-25 04:44:37 +00:00
|
|
|
import { nextTick, onMounted, onUnmounted, shallowRef, watch } from 'vue';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkMenu from './MkMenu.vue';
|
2023-09-23 11:34:45 +00:00
|
|
|
import { MenuItem } from '@/types/menu.js';
|
2022-07-17 12:06:33 +00:00
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
items: MenuItem[];
|
|
|
|
targetElement: HTMLElement;
|
2022-07-17 14:18:05 +00:00
|
|
|
rootElement: HTMLElement;
|
2022-07-17 12:06:33 +00:00
|
|
|
width?: number;
|
|
|
|
viaKeyboard?: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'closed'): void;
|
|
|
|
(ev: 'actioned'): void;
|
|
|
|
}>();
|
|
|
|
|
2023-01-03 01:12:37 +00:00
|
|
|
const el = shallowRef<HTMLElement>();
|
2022-07-17 12:06:33 +00:00
|
|
|
const align = 'left';
|
|
|
|
|
2023-02-25 04:44:37 +00:00
|
|
|
const SCROLLBAR_THICKNESS = 16;
|
|
|
|
|
2022-07-17 12:06:33 +00:00
|
|
|
function setPosition() {
|
2024-01-30 10:53:53 +00:00
|
|
|
if (el.value == null) return;
|
2022-07-17 14:18:05 +00:00
|
|
|
const rootRect = props.rootElement.getBoundingClientRect();
|
2023-02-25 04:44:37 +00:00
|
|
|
const parentRect = props.targetElement.getBoundingClientRect();
|
|
|
|
const myRect = el.value.getBoundingClientRect();
|
|
|
|
|
|
|
|
let left = props.targetElement.offsetWidth;
|
|
|
|
let top = (parentRect.top - rootRect.top) - 8;
|
|
|
|
if (rootRect.left + left + myRect.width >= (window.innerWidth - SCROLLBAR_THICKNESS)) {
|
|
|
|
left = -myRect.width;
|
|
|
|
}
|
|
|
|
if (rootRect.top + top + myRect.height >= (window.innerHeight - SCROLLBAR_THICKNESS)) {
|
|
|
|
top = top - ((rootRect.top + top + myRect.height) - (window.innerHeight - SCROLLBAR_THICKNESS));
|
|
|
|
}
|
2022-07-17 12:06:33 +00:00
|
|
|
el.value.style.left = left + 'px';
|
|
|
|
el.value.style.top = top + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
function onChildClosed(actioned?: boolean) {
|
|
|
|
if (actioned) {
|
|
|
|
emit('actioned');
|
|
|
|
} else {
|
|
|
|
emit('closed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 02:32:45 +00:00
|
|
|
watch(() => props.targetElement, () => {
|
|
|
|
setPosition();
|
|
|
|
});
|
|
|
|
|
2023-02-25 04:44:37 +00:00
|
|
|
const ro = new ResizeObserver((entries, observer) => {
|
|
|
|
setPosition();
|
|
|
|
});
|
|
|
|
|
2022-07-17 12:06:33 +00:00
|
|
|
onMounted(() => {
|
2024-01-30 10:53:53 +00:00
|
|
|
if (el.value) ro.observe(el.value);
|
2022-07-17 12:06:33 +00:00
|
|
|
setPosition();
|
|
|
|
nextTick(() => {
|
|
|
|
setPosition();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-25 04:44:37 +00:00
|
|
|
onUnmounted(() => {
|
|
|
|
ro.disconnect();
|
|
|
|
});
|
|
|
|
|
2022-07-17 12:06:33 +00:00
|
|
|
defineExpose({
|
|
|
|
checkHit: (ev: MouseEvent) => {
|
2024-01-30 10:53:53 +00:00
|
|
|
return (ev.target === el.value || el.value?.contains(ev.target as Node));
|
2022-07-17 12:06:33 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-02-25 04:44:37 +00:00
|
|
|
<style lang="scss" module>
|
|
|
|
.root {
|
2022-07-17 14:18:05 +00:00
|
|
|
position: absolute;
|
2022-07-17 12:06:33 +00:00
|
|
|
}
|
|
|
|
</style>
|