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

83 lines
1.5 KiB
Vue
Raw Normal View History

2022-07-17 21:06:33 +09:00
<template>
2023-02-13 11:56:01 +09:00
<FocusTrap v-bind:active="isActive">
<div ref="el" class="sfhdhdhr">
<MkMenu
ref="menu"
:items="items"
:align="align"
:width="width"
:as-drawer="false"
@close="onChildClosed"
/>
</div>
2023-02-13 11:56:01 +09:00
</FocusTrap>
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";
2023-02-13 11:56:01 +09:00
import { FocusTrap } from 'focus-trap-vue';
2023-04-08 09:01:42 +09:00
import * as os from "@/os";
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>