Open History to see the last commit
1'use client'
2β
3import { useRouter, useRouterState } from '@tanstack/react-router'
4import { TanStackRouterDevtoolsPanelCore } from '@tanstack/router-devtools-core'
5import React, { useEffect, useRef, useState } from 'react'
6import type { AnyRouter } from '@tanstack/react-router'
7β
8export interface TanStackRouterDevtoolsPanelOptions {
9 /**
10 * The standard React style object used to style a component with inline styles
11 */
12 style?: any
13 /**
14 * The standard React class property used to style a component with classes
15 */
16 className?: string
17 /**
18 * A boolean variable indicating whether the panel is open or closed
19 */
20 isOpen?: boolean
21 /**
22 * A function that toggles the open and close state of the panel
23 */
24 setIsOpen?: (isOpen: boolean) => void
25 /**
26 * Handles the opening and closing the devtools panel
27 */
28 handleDragStart?: (e: any) => void
29 /**
30 * A boolean variable indicating if the "lite" version of the library is being used
31 */
32 router?: AnyRouter
33 /**
34 * Use this to attach the devtool's styles to specific element in the DOM.
35 */
36 shadowDOMTarget?: ShadowRoot
37}
38β
39export const TanStackRouterDevtoolsPanel: React.FC<
40 TanStackRouterDevtoolsPanelOptions
41> = (props): React.ReactElement | null => {
42 const { router: propsRouter, ...rest } = props
43 const hookRouter = useRouter({ warn: false })
44 const activeRouter = propsRouter ?? hookRouter
45 const activeRouterState = useRouterState({ router: activeRouter })
46β
47 const devToolRef = useRef<HTMLDivElement>(null)
48 const [devtools] = useState(
49 () =>
50 new TanStackRouterDevtoolsPanelCore({
51 ...rest,
52 router: activeRouter,
53 routerState: activeRouterState,
54 }),
55 )
56β
57 // Update devtools when props change
58 useEffect(() => {
59 devtools.setRouter(activeRouter)
60 }, [devtools, activeRouter])
61β
62 useEffect(() => {
63 devtools.setRouterState(activeRouterState)
64 }, [devtools, activeRouterState])
65β
66 useEffect(() => {
67 devtools.setOptions({
68 className: props.className,
69 style: props.style,
70 shadowDOMTarget: props.shadowDOMTarget,
71 })
72 }, [devtools, props.className, props.style, props.shadowDOMTarget])
73β
74 useEffect(() => {
75 if (devToolRef.current) {
76 devtools.mount(devToolRef.current)
77 }
78β
79 return () => {
80 devtools.unmount()
81 }
82 }, [devtools])
83β
84 return (
85 <>
86 <div ref={devToolRef} />
87 </>
88 )
89}