Open History to see the last commit
1import { defineComponent, h, onMounted, onUnmounted, ref } from 'vue'
2import type { TanStackDevtoolsPluginProps } from '@tanstack/devtools'
3import type { DefineComponent } from 'vue'
4β
5export interface DevtoolsPanelProps extends TanStackDevtoolsPluginProps {}
6β
7export function createVuePanel<
8 TComponentProps extends DevtoolsPanelProps,
9 TCoreDevtoolsClass extends {
10 mount: (el: HTMLElement, props?: TanStackDevtoolsPluginProps) => void
11 unmount: () => void
12 },
13>(CoreClass: new (props: TComponentProps) => TCoreDevtoolsClass) {
14 const props = {
15 props: {
16 type: Object as () => DevtoolsPanelProps,
17 },
18 devtoolsProps: {
19 type: Object as () => TComponentProps,
20 },
21 }
22β
23 const Panel = defineComponent({
24 props,
25 setup(config) {
26 const devToolRef = ref<HTMLElement | null>(null)
27 const devtools = ref<TCoreDevtoolsClass | null>(null)
28β
29 onMounted(() => {
30 const instance = new CoreClass(config.devtoolsProps as TComponentProps)
31 devtools.value = instance
32β
33 if (devToolRef.value) {
34 instance.mount(devToolRef.value, config.props)
35 }
36 })
37β
38 onUnmounted(() => {
39 if (devToolRef.value && devtools.value) {
40 devtools.value.unmount()
41 }
42 })
43β
44 return () => {
45 return h('div', {
46 style: { height: '100%' },
47 ref: devToolRef,
48 })
49 }
50 },
51 })
52β
53 const NoOpPanel = defineComponent({
54 props,
55 setup() {
56 return () => null
57 },
58 })
59β
60 return [Panel, NoOpPanel] as unknown as [
61 DefineComponent<{
62 theme?: DevtoolsPanelProps['theme']
63 devtoolsProps: TComponentProps
64 }>,
65 DefineComponent<{
66 theme?: DevtoolsPanelProps['theme']
67 devtoolsProps: TComponentProps
68 }>,
69 ]
70}