import { Ref, inject, computed, onUnmounted } from 'vue'; export type Parent = null | { children: Ref; }; export function useParent(key: string, child: T = {} as T) { const parent = inject>(key, null); if (parent) { const children = parent.children.value; const index = computed(() => children.indexOf(child)); children.push(child); onUnmounted(() => { children.splice(index.value, 1); }); return { index, parent, }; } return {}; }