Open History to see the last commit
1import { useRouter, useRouterState } from '@tanstack/react-router'
2import { TanStackRouterDevtoolsPanelCore } from '@tanstack/router-devtools-core'
3import React, { useEffect, useRef, useState } from 'react'
4import type { AnyRouter } from '@tanstack/react-router'
5β
6export interface TanStackRouterDevtoolsPanelOptions {
7 /**
8 * The standard React style object used to style a component with inline styles
9 */
10 style?: any
11 /**
12 * The standard React 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 * A boolean variable indicating if the "lite" version of the library is being used
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: React.FC<
38 TanStackRouterDevtoolsPanelOptions
39> = (props): React.ReactElement | null => {
40 const { router: propsRouter, ...rest } = props
41 const hookRouter = useRouter({ warn: false })
42 const activeRouter = propsRouter ?? hookRouter
43 const activeRouterState = useRouterState({ router: activeRouter })
44β
45 const devToolRef = useRef<HTMLDivElement>(null)
46 const [devtools] = useState(
47 () =>
48 new TanStackRouterDevtoolsPanelCore({
49 ...rest,
50 router: activeRouter,
51 routerState: activeRouterState,
52 }),
53 )
54β
55 // Update devtools when props change
56 useEffect(() => {
57 devtools.setRouter(activeRouter)
58 }, [devtools, activeRouter])
59β
60 useEffect(() => {
61 devtools.setRouterState(activeRouterState)
62 }, [devtools, activeRouterState])
63β
64 useEffect(() => {
65 devtools.setOptions({
66 className: props.className,
67 style: props.style,
68 shadowDOMTarget: props.shadowDOMTarget,
69 })
70 }, [devtools, props.className, props.style, props.shadowDOMTarget])
71β
72 useEffect(() => {
73 if (devToolRef.current) {
74 devtools.mount(devToolRef.current)
75 }
76β
77 return () => {
78 devtools.unmount()
79 }
80 }, [devtools])
81β
82 return (
83 <>
84 <div ref={devToolRef} />
85 </>
86 )
87}