Open History to see the last commit
1import { TanStackRouterDevtoolsPanelCore } from '@tanstack/router-devtools-core'
2import { defineComponent, h, onMounted, onUnmounted, ref, watch } from 'vue'
3import { useRouter, useRouterState } from '@tanstack/vue-router'
4import type { AnyRouter } from '@tanstack/vue-router'
5β
6export interface TanStackRouterDevtoolsPanelOptions {
7 /**
8 * The standard style object used to style a component with inline styles
9 */
10 style?: any
11 /**
12 * The standard class property used to style a component with classes
13 */
14 className?: string
15 /**
16 * A boolean variable indicating whether the panel is open or closed
17 */
18 isOpen?: boolean
19 /**
20 * A function that toggles the open and close state of the panel
21 */
22 setIsOpen?: (isOpen: boolean) => void
23 /**
24 * Handles the opening and closing the devtools panel
25 */
26 handleDragStart?: (e: any) => void
27 /**
28 * The router instance to use for the devtools.
29 */
30 router?: AnyRouter
31 /**
32 * Use this to attach the devtool's styles to specific element in the DOM.
33 */
34 shadowDOMTarget?: ShadowRoot
35}
36β
37export const TanStackRouterDevtoolsPanel = /* #__PURE__ */ defineComponent({
38 name: 'TanStackRouterDevtoolsPanel',
39 props: [
40 'style',
41 'className',
42 'isOpen',
43 'setIsOpen',
44 'handleDragStart',
45 'router',
46 'shadowDOMTarget',
47 ] as unknown as undefined,
48 setup(props: TanStackRouterDevtoolsPanelOptions) {
49 const devToolRef = ref<HTMLDivElement | null>(null)
50 let isMounted = false
51β
52 const hookRouter = useRouter({ warn: false })
53 const activeRouter = props.router ?? hookRouter
54β
55 const activeRouterState = useRouterState({ router: activeRouter })
56β
57 const devtools = new TanStackRouterDevtoolsPanelCore({
58 style: props.style,
59 className: props.className,
60 isOpen: props.isOpen,
61 setIsOpen: props.setIsOpen,
62 handleDragStart: props.handleDragStart,
63 shadowDOMTarget: props.shadowDOMTarget,
64 router: activeRouter,
65 routerState: activeRouterState.value,
66 })
67β
68 // Update devtools when router changes
69 watch(
70 () => activeRouter,
71 (router) => {
72 devtools.setRouter(router)
73 },
74 )
75β
76 // Update devtools when router state changes
77 watch(activeRouterState, (routerState) => {
78 devtools.setRouterState(routerState)
79 })
80β
81 // Update devtools when options change
82 watch(
83 () => [props.className, props.style, props.shadowDOMTarget],
84 () => {
85 devtools.setOptions({
86 className: props.className,
87 style: props.style,
88 shadowDOMTarget: props.shadowDOMTarget,
89 })
90 },
91 )
92β
93 onMounted(() => {
94 if (devToolRef.value) {
95 devtools.mount(devToolRef.value)
96 isMounted = true
97 }
98 })
99β
100 onUnmounted(() => {
101 if (isMounted) {
102 devtools.unmount()
103 }
104 })
105β
106 return () => h('div', { ref: devToolRef })
107 },
108})