Profectus/src/components/Node.vue

42 lines
1.1 KiB
Vue
Raw Normal View History

2022-01-14 04:25:47 +00:00
<template>
<div class="node" ref="node"></div>
2022-01-14 04:25:47 +00:00
</template>
<script setup lang="ts">
import { RegisterNodeInjectionKey, UnregisterNodeInjectionKey } from "game/layers";
2022-08-22 05:27:36 +00:00
import { computed, inject, onUnmounted, shallowRef, toRefs, unref, watch } from "vue";
2022-01-14 04:25:47 +00:00
2022-01-25 04:25:34 +00:00
const _props = defineProps<{ id: string }>();
const props = toRefs(_props);
2022-01-14 04:25:47 +00:00
// eslint-disable-next-line @typescript-eslint/no-empty-function
const register = inject(RegisterNodeInjectionKey, () => {});
// eslint-disable-next-line @typescript-eslint/no-empty-function
const unregister = inject(UnregisterNodeInjectionKey, () => {});
2022-01-14 04:25:47 +00:00
2022-08-22 05:27:36 +00:00
const node = shallowRef<HTMLElement | null>(null);
2022-01-14 04:25:47 +00:00
const parentNode = computed(() => node.value && node.value.parentElement);
2022-07-09 23:42:16 +00:00
watch([parentNode, props.id], ([newNode, newID], [prevNode, prevID]) => {
if (prevNode) {
unregister(unref(prevID));
}
if (newNode) {
register(newID, newNode);
}
});
2022-07-09 23:42:16 +00:00
onUnmounted(() => unregister(unref(props.id)));
2022-01-14 04:25:47 +00:00
</script>
<style scoped>
.node {
2022-01-14 04:25:47 +00:00
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
</style>