Skip to content

Commit 954af5c

Browse files
committed
feat: utils and props passing
1 parent e93746e commit 954af5c

File tree

8 files changed

+144
-22
lines changed

8 files changed

+144
-22
lines changed

examples/vue/basic/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
"@tanstack/devtools": "^0.6.22",
1212
"@tanstack/vue-devtools": "^0.1.0",
1313
"@tanstack/vue-query": "^5.90.5",
14-
"@tanstack/vue-query-devtools": "^5.91.0",
14+
"@tanstack/vue-query-devtools": "^6.1.0",
1515
"vue": "^3.5.22"
1616
},
1717
"devDependencies": {
1818
"@vitejs/plugin-vue": "^6.0.1",
1919
"typescript": "~5.9.2",
2020
"vite": "^7.1.7"
2121
}
22-
}
22+
}

packages/devtools-utils/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"peerDependencies": {
5454
"@types/react": ">=19.0.0",
5555
"react": ">=19.0.0",
56-
"solid-js": ">=1.9.7"
56+
"solid-js": ">=1.9.7",
57+
"vue": ">=3.2.0"
5758
},
5859
"peerDependenciesMeta": {
5960
"react": {
@@ -64,6 +65,9 @@
6465
},
6566
"solid-js": {
6667
"optional": true
68+
},
69+
"vue": {
70+
"optional": true
6771
}
6872
},
6973
"files": [
@@ -85,4 +89,4 @@
8589
"tsup-preset-solid": "^2.2.0",
8690
"vite-plugin-solid": "^2.11.8"
8791
}
88-
}
92+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './panel'
2+
export * from './plugin'
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { defineComponent, h, onMounted, onUnmounted, ref } from 'vue'
2+
import type { DefineComponent } from 'vue'
3+
4+
export interface DevtoolsPanelProps {
5+
theme?: 'dark' | 'light' | 'system'
6+
}
7+
8+
export function createVuePanel<
9+
TComponentProps extends DevtoolsPanelProps,
10+
TCoreDevtoolsClass extends {
11+
mount: (el: HTMLElement, theme?: DevtoolsPanelProps['theme']) => void
12+
unmount: () => void
13+
},
14+
>(CoreClass: new (props: TComponentProps) => TCoreDevtoolsClass) {
15+
const props = {
16+
theme: {
17+
type: String as () => DevtoolsPanelProps['theme'],
18+
},
19+
devtoolsProps: {
20+
type: Object as () => TComponentProps,
21+
},
22+
}
23+
24+
const Panel = defineComponent({
25+
props,
26+
setup(config) {
27+
const devToolRef = ref<HTMLElement | null>(null)
28+
const devtools = ref<TCoreDevtoolsClass | null>(null)
29+
30+
onMounted(() => {
31+
const instance = new CoreClass(config.devtoolsProps as TComponentProps)
32+
devtools.value = instance
33+
34+
if (devToolRef.value) {
35+
instance.mount(devToolRef.value, config.theme)
36+
}
37+
})
38+
39+
onUnmounted(() => {
40+
if (devToolRef.value && devtools.value) {
41+
devtools.value.unmount()
42+
}
43+
})
44+
45+
return () => {
46+
return h('div', {
47+
style: { height: '100%' },
48+
ref: devToolRef,
49+
})
50+
}
51+
},
52+
})
53+
54+
const NoOpPanel = defineComponent({
55+
props,
56+
setup() {
57+
return () => null
58+
},
59+
})
60+
61+
return [Panel, NoOpPanel] as unknown as [
62+
DefineComponent<{
63+
theme?: DevtoolsPanelProps['theme']
64+
devtoolsProps: TComponentProps
65+
}>,
66+
DefineComponent<{
67+
theme?: DevtoolsPanelProps['theme']
68+
devtoolsProps: TComponentProps
69+
}>,
70+
]
71+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Fragment } from 'vue'
2+
import type { DefineComponent } from 'vue'
3+
4+
export function createVuePlugin<TComponentProps extends Record<string, any>>(
5+
name: string,
6+
component: DefineComponent<TComponentProps, {}, unknown>,
7+
props: TComponentProps,
8+
) {
9+
function Plugin() {
10+
return {
11+
name,
12+
component,
13+
props,
14+
}
15+
}
16+
function NoOpPlugin(props: Record<string, any>) {
17+
return {
18+
name,
19+
component: Fragment,
20+
props,
21+
}
22+
}
23+
return [Plugin, NoOpPlugin] as const
24+
}

packages/vue-devtools/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ export interface TanStackDevtoolsVueInit {
4545
*/
4646
eventBusConfig?: ClientEventBusConfig
4747
}
48+
49+
export type RenderArray = Array<{
50+
id: string
51+
component: Component
52+
props: Record<string, unknown>
53+
}>

packages/vue-devtools/src/vue-devtools.vue

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
<script setup lang="ts">
22
import { onMounted, onScopeDispose, ref, shallowRef, watchEffect } from 'vue'
33
import { PLUGIN_CONTAINER_ID, TanStackDevtoolsCore } from '@tanstack/devtools'
4+
import type { DefineComponent } from 'vue'
45
import type { TanStackDevtoolsPlugin } from '@tanstack/devtools'
56
import type {
7+
RenderArray,
68
TanStackDevtoolsVueInit,
79
TanStackDevtoolsVuePlugin,
810
} from './types'
911
1012
const props = defineProps<TanStackDevtoolsVueInit>()
1113
12-
const titlesToRender = shallowRef<Array<{ id: string; component: any }>>([])
13-
const pluginsToRender = shallowRef<
14-
Array<{ id: string; component: any; props: any }>
15-
>([])
14+
const titlesToRender = shallowRef<RenderArray>([])
15+
const pluginsToRender = shallowRef<RenderArray>([])
1616
const div = ref<HTMLElement>()
1717
1818
function getPlugin(plugin: TanStackDevtoolsVuePlugin): TanStackDevtoolsPlugin {
1919
return {
20-
...plugin,
20+
id: plugin.id,
2121
name:
2222
typeof plugin.name === 'string'
2323
? plugin.name
24-
: (e, _theme) => {
24+
: (e, theme) => {
2525
const id = e.getAttribute('id')!
2626
titlesToRender.value = [
2727
...titlesToRender.value,
2828
{
2929
id,
30-
component: plugin.name,
30+
component: plugin.name as DefineComponent<any>,
31+
props: {
32+
theme,
33+
},
3134
},
3235
]
3336
},
34-
render: (e, _theme) => {
37+
render: (e, theme) => {
3538
const id = e.getAttribute('id')!
3639
pluginsToRender.value = [
3740
...pluginsToRender.value,
38-
{ id, component: plugin.component, props: plugin.props },
41+
{ id, component: plugin.component, props: { theme, ...plugin.props } },
3942
]
4043
},
4144
destroy: (pluginId) => {
@@ -78,13 +81,17 @@ onScopeDispose(() => {
7881
:key="title.id"
7982
:to="'#' + title.id"
8083
>
81-
<component :is="title.component" />
84+
<component :is="title.component" v-bind="title.props" />
8285
</Teleport>
8386
<Teleport
8487
v-for="plugin in pluginsToRender"
8588
:key="plugin.id"
8689
:to="'#' + plugin.id"
8790
>
88-
<component :is="plugin.component" v-bind="plugin.props" />
91+
<component
92+
:is="plugin.component"
93+
:devtools-props="plugin.props"
94+
v-bind="plugin.props"
95+
/>
8996
</Teleport>
9097
</template>

pnpm-lock.yaml

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)