Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

TanStackRouterDevtoolsPanel.tsx

87 lines2.2 KB

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