iceshrimp-legacy/packages/client/src/components/MkMenu.child.vue

79 lines
1.4 KiB
Vue
Raw Normal View History

2022-07-17 14:06:33 +02:00
<template>
2023-04-29 04:24:40 +02:00
<div ref="el" class="sfhdhdhr" tabindex="-1">
2023-04-30 22:27:27 +02:00
<MkMenu
ref="menu"
:items="items"
:align="align"
:width="width"
:as-drawer="false"
@close="onChildClosed"
/>
</div>
2022-07-17 14:06:33 +02:00
</template>
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { on } from "events";
import {
nextTick,
onBeforeUnmount,
onMounted,
onUnmounted,
ref,
watch,
} from "vue";
import MkMenu from "./MkMenu.vue";
import { MenuItem } from "@/types/menu";
2022-07-17 14:06:33 +02:00
const props = defineProps<{
items: MenuItem[];
targetElement: HTMLElement;
2022-07-17 16:18:05 +02:00
rootElement: HTMLElement;
2022-07-17 14:06:33 +02:00
width?: number;
viaKeyboard?: boolean;
}>();
const emit = defineEmits<{
2023-04-08 02:01:42 +02:00
(ev: "closed"): void;
(ev: "actioned"): void;
2022-07-17 14:06:33 +02:00
}>();
const el = ref<HTMLElement>();
2023-04-08 02:01:42 +02:00
const align = "left";
2022-07-17 14:06:33 +02:00
function setPosition() {
2022-07-17 16:18:05 +02:00
const rootRect = props.rootElement.getBoundingClientRect();
2022-07-17 14:06:33 +02:00
const rect = props.targetElement.getBoundingClientRect();
2022-07-17 16:18:05 +02:00
const left = props.targetElement.offsetWidth;
2023-04-08 02:01:42 +02:00
const top = rect.top - rootRect.top - 8;
el.value.style.left = left + "px";
el.value.style.top = top + "px";
2022-07-17 14:06:33 +02:00
}
function onChildClosed(actioned?: boolean) {
if (actioned) {
2023-04-08 02:01:42 +02:00
emit("actioned");
2022-07-17 14:06:33 +02:00
} else {
2023-04-08 02:01:42 +02:00
emit("closed");
2022-07-17 14:06:33 +02:00
}
}
onMounted(() => {
setPosition();
nextTick(() => {
setPosition();
});
});
defineExpose({
checkHit: (ev: MouseEvent) => {
2023-04-08 02:01:42 +02:00
return ev.target === el.value || el.value.contains(ev.target);
2022-07-17 14:06:33 +02:00
},
});
</script>
<style lang="scss" scoped>
.sfhdhdhr {
2022-07-17 16:18:05 +02:00
position: absolute;
2022-07-17 14:06:33 +02:00
}
</style>