Open History to see the last commit
1import { TanStackRouterDevtoolsCore } 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 TanStackRouterDevtoolsOptions {
7 /**
8 * Set this true if you want the dev tools to default to being open
9 */
10 initialIsOpen?: boolean
11 /**
12 * Use this to add props to the panel. For example, you can add className, style (merge and override default style), etc.
13 */
14 panelProps?: Record<string, any>
15 /**
16 * Use this to add props to the close button. For example, you can add className, style (merge and override default style), onClick (extend default handler), etc.
17 */
18 closeButtonProps?: Record<string, any>
19 /**
20 * Use this to add props to the toggle button. For example, you can add className, style (merge and override default style), onClick (extend default handler), etc.
21 */
22 toggleButtonProps?: Record<string, any>
23 /**
24 * The position of the TanStack Router logo to open and close the devtools panel.
25 * Defaults to 'bottom-left'.
26 */
27 position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
28 /**
29 * Use this to render the devtools inside a different type of container element for a11y purposes.
30 * Any string which corresponds to a valid intrinsic JSX element is allowed.
31 * Defaults to 'footer'.
32 */
33 containerElement?: string | any
34 /**
35 * The router instance to use for the devtools.
36 */
37 router?: AnyRouter
38 /**
39 * Use this to attach the devtool's styles to specific element in the DOM.
40 */
41 shadowDOMTarget?: ShadowRoot
42}
43β
44export const TanStackRouterDevtools = /* #__PURE__ */ defineComponent({
45 name: 'TanStackRouterDevtools',
46 props: [
47 'initialIsOpen',
48 'panelProps',
49 'closeButtonProps',
50 'toggleButtonProps',
51 'position',
52 'containerElement',
53 'router',
54 'shadowDOMTarget',
55 ] as unknown as undefined,
56 setup(props: TanStackRouterDevtoolsOptions) {
57 const devToolRef = ref<HTMLDivElement | null>(null)
58 let isMounted = false
59β
60 const hookRouter = useRouter({ warn: false })
61 const activeRouter = props.router ?? hookRouter
62β
63 const activeRouterState = useRouterState({ router: activeRouter })
64β
65 const devtools = new TanStackRouterDevtoolsCore({
66 initialIsOpen: props.initialIsOpen,
67 panelProps: props.panelProps,
68 closeButtonProps: props.closeButtonProps,
69 toggleButtonProps: props.toggleButtonProps,
70 position: props.position,
71 containerElement: props.containerElement,
72 shadowDOMTarget: props.shadowDOMTarget,
73 router: activeRouter,
74 routerState: activeRouterState.value,
75 })
76β
77 // Update devtools when router changes
78 watch(
79 () => activeRouter,
80 (router) => {
81 devtools.setRouter(router)
82 },
83 )
84β
85 // Update devtools when router state changes
86 watch(activeRouterState, (routerState) => {
87 devtools.setRouterState(routerState)
88 })
89β
90 // Update devtools when options change
91 watch(
92 () => [
93 props.initialIsOpen,
94 props.panelProps,
95 props.closeButtonProps,
96 props.toggleButtonProps,
97 props.position,
98 props.containerElement,
99 props.shadowDOMTarget,
100 ],
101 () => {
102 devtools.setOptions({
103 initialIsOpen: props.initialIsOpen,
104 panelProps: props.panelProps,
105 closeButtonProps: props.closeButtonProps,
106 toggleButtonProps: props.toggleButtonProps,
107 position: props.position,
108 containerElement: props.containerElement,
109 shadowDOMTarget: props.shadowDOMTarget,
110 })
111 },
112 )
113β
114 onMounted(() => {
115 if (devToolRef.value) {
116 devtools.mount(devToolRef.value)
117 isMounted = true
118 }
119 })
120β
121 onUnmounted(() => {
122 if (isMounted) {
123 devtools.unmount()
124 }
125 })
126β
127 return () => h('div', { ref: devToolRef })
128 },
129})