Sharkey/packages/frontend/src/components/MkMenu.child.vue

68 lines
1.4 KiB
Vue
Raw Normal View History

2022-07-17 12:06:33 +00:00
<template>
<div ref="el" class="sfhdhdhr">
<MkMenu ref="menu" :items="items" :align="align" :width="width" :as-drawer="false" @close="onChildClosed"/>
</div>
</template>
<script lang="ts" setup>
import { nextTick, onMounted, shallowRef, watch } from 'vue';
import MkMenu from './MkMenu.vue';
2022-07-17 12:06:33 +00:00
import { MenuItem } from '@/types/menu';
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;
}>();
const el = shallowRef<HTMLElement>();
2022-07-17 12:06:33 +00:00
const align = 'left';
function setPosition() {
2022-07-17 14:18:05 +00:00
const rootRect = props.rootElement.getBoundingClientRect();
2022-07-17 12:06:33 +00:00
const rect = props.targetElement.getBoundingClientRect();
2022-07-17 14:18:05 +00:00
const left = props.targetElement.offsetWidth;
const top = (rect.top - rootRect.top) - 8;
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');
}
}
watch(() => props.targetElement, () => {
setPosition();
});
2022-07-17 12:06:33 +00:00
onMounted(() => {
setPosition();
nextTick(() => {
setPosition();
});
});
defineExpose({
checkHit: (ev: MouseEvent) => {
return (ev.target === el.value || el.value.contains(ev.target));
},
});
</script>
<style lang="scss" scoped>
2022-07-17 12:06:33 +00:00
.sfhdhdhr {
2022-07-17 14:18:05 +00:00
position: absolute;
2022-07-17 12:06:33 +00:00
}
</style>