One solution to this problem is to create a separate reactive variable for each topic component that tracks its open/closed state. This way, the openTree array can be used to set the initial state of the components, but the user can still interact with them and change their state.
To implement this solution, you can modify the TopicBar component to use a reactive variable to track its open/closed state instead of relying on the openTree array. Here's an example:
<script setup>
import { computed, ref, inject } from 'vue';
import ChevronDown from '@/Shared/SVG/ChevronDown.vue';
import TopicBlock from './TopicBlock.vue';
import TopicBarButtons from '@/Shared/Topic/TopicBarButtons.vue';
const props = defineProps({
topic: Object
});
const open = ref(inject('openTree').includes(props.topic.id));
function toggleOpen() {
open.value = !open.value;
}
</script>
<template>
<div class="rounded p-2 flex items-center relative">
<ChevronDown
v-if="topic.children && topic.children.length > 0"
width="20"
height="20"
@click="toggleOpen"
:class="{'-rotate-90' : !open}"
class="fill-white absolute -left-5"
/>
<div class="truncate w-32 rounded px-2 py-1 text-center" :class="topic.background">
{{ topic.name }}
</div>
<div class="text-base text-left hidden lg:inline ml-3 truncate w-auto">
{{ topic.description }}
</div>
<TopicBarButtons :topic="topic" />
</div>
<div v-if="open" class="relative ml-2 lg:ml-10">
<div>
<TopicBlock :topics="topic.children" />
</div>
</div>
</template>
In this modified TopicBar component, we use the inject function to get the openTree array from the parent component, and then use the ref function to create a reactive variable called open that tracks the open/closed state of the component. We set the initial value of open to true if the openTree array includes the current topic's ID, and false otherwise.
We also define a toggleOpen function that toggles the value of open when the user clicks on the chevron icon.
Finally, we update the v-if directive on the TopicBlock component to use the open variable instead of the showChildren variable.
With these changes, the openTree array can still be used to set the initial state of the topic components, but the user can also interact with them and change their state.