Open History to see the last commit
1<script setup lang="ts">
2import { onMounted, onScopeDispose, ref, shallowRef, watchEffect } from 'vue'
3import { PLUGIN_CONTAINER_ID, TanStackDevtoolsCore } from '@tanstack/devtools'
4import type { DefineComponent } from 'vue'
5import type { TanStackDevtoolsPlugin } from '@tanstack/devtools'
6import type {
7 RenderArray,
8 TanStackDevtoolsVueInit,
9 TanStackDevtoolsVuePlugin,
10} from './types'
11β
12const props = defineProps<TanStackDevtoolsVueInit>()
13β
14const titlesToRender = shallowRef<RenderArray>([])
15const pluginsToRender = shallowRef<RenderArray>([])
16const div = ref<HTMLElement>()
17β
18function getPlugin(plugin: TanStackDevtoolsVuePlugin): TanStackDevtoolsPlugin {
19 return {
20 id: plugin.id,
21 name:
22 typeof plugin.name === 'string'
23 ? plugin.name
24 : (e, theme) => {
25 const id = e.getAttribute('id')!
26 titlesToRender.value = [
27 ...titlesToRender.value,
28 {
29 id,
30 component: plugin.name as DefineComponent<any>,
31 props: {
32 theme,
33 },
34 },
35 ]
36 },
37 render: (e, theme) => {
38 const id = e.getAttribute('id')!
39 pluginsToRender.value = [
40 ...pluginsToRender.value,
41 { id, component: plugin.component, props: { theme, ...plugin.props } },
42 ]
43 },
44 destroy: (pluginId) => {
45 pluginsToRender.value = pluginsToRender.value.filter(
46 (plugin) => plugin.id !== `${PLUGIN_CONTAINER_ID}-${pluginId}`,
47 )
48 },
49 }
50}
51β
52const devtools = new TanStackDevtoolsCore({
53 config: props.config,
54 eventBusConfig: props.eventBusConfig,
55 plugins: props.plugins?.map(getPlugin),
56})
57β
58watchEffect(() => {
59 devtools.setConfig({
60 config: props.config,
61 eventBusConfig: props.eventBusConfig,
62 plugins: props.plugins?.map(getPlugin),
63 })
64})
65β
66onMounted(() => {
67 if (div.value) {
68 devtools.mount(div.value)
69 }
70})
71β
72onScopeDispose(() => {
73 devtools.unmount()
74})
75</script>
76β
77<template>
78 <div ref="div" />
79 <Teleport
80 v-for="title in titlesToRender"
81 :key="title.id"
82 :to="'#' + title.id"
83 >
84 <component :is="title.component" v-bind="title.props" />
85 </Teleport>
86 <Teleport
87 v-for="plugin in pluginsToRender"
88 :key="plugin.id"
89 :to="'#' + plugin.id"
90 >
91 <component
92 :is="plugin.component"
93 :devtools-props="plugin.props"
94 v-bind="plugin.props"
95 />
96 </Teleport>
97</template>