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

79 lines
1.4 KiB
Vue
Raw Normal View History

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