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

TanStackRouterDevtoolsPanelCore.tsx

160 lines4.1 KB

1import { render } from 'solid-js/web'
2import { createSignal, lazy } from 'solid-js'
3import { DevtoolsOnCloseContext, ShadowDomTargetContext } from './context'
4import type { JSX } from 'solid-js'
5import type { AnyRouter } from '@tanstack/router-core'
6​
7export interface TanStackRouterDevtoolsPanelCoreOptions {
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 routerState: any
34 /**
35 * Use this to attach the devtool's styles to specific element in the DOM.
36 */
37 shadowDOMTarget?: ShadowRoot
39​
40export class TanStackRouterDevtoolsPanelCore {
41 #router: any
42 #routerState: any
43 #style: any
44 #className: any
45 #shadowDOMTarget?: ShadowRoot
46 #isMounted = false
47 #setIsOpen?: (isOpen: boolean) => void
48 #dispose?: () => void
49 #Component: any
50​
51 constructor(config: TanStackRouterDevtoolsPanelCoreOptions) {
52 const {
53 router,
54 routerState,
55 shadowDOMTarget,
56 setIsOpen,
57 style,
58 className,
59 } = config
60​
61 this.#router = createSignal(router)
62 this.#routerState = createSignal(routerState)
63 this.#style = createSignal(style)
64 this.#className = createSignal(className)
65 this.#shadowDOMTarget = shadowDOMTarget
66 this.#setIsOpen = setIsOpen
67 }
68​
69 mount<T extends HTMLElement>(el: T) {
70 if (this.#isMounted) {
71 throw new Error('Devtools is already mounted')
72 }
73​
74 const dispose = render(() => {
75 const [router] = this.#router
76 const [routerState] = this.#routerState
77 const [style] = this.#style
78 const [className] = this.#className
79 const shadowDOMTarget = this.#shadowDOMTarget
80 const setIsOpen = this.#setIsOpen
81​
82 let BaseTanStackRouterDevtoolsPanel
83​
84 if (this.#Component) {
85 BaseTanStackRouterDevtoolsPanel = this.#Component
86 } else {
87 BaseTanStackRouterDevtoolsPanel = lazy(
88 () => import('./BaseTanStackRouterDevtoolsPanel'),
89 )
90 this.#Component = BaseTanStackRouterDevtoolsPanel
91 }
92​
93 return (
94 <ShadowDomTargetContext.Provider value={shadowDOMTarget}>
95 <DevtoolsOnCloseContext.Provider
96 value={{
97 onCloseClick: () => {},
98 }}
99 >
100 <BaseTanStackRouterDevtoolsPanel
101 router={router}
102 routerState={routerState}
103 shadowDOMTarget={shadowDOMTarget}
104 setIsOpen={setIsOpen}
105 style={style}
106 className={className}
107 />
108 </DevtoolsOnCloseContext.Provider>
109 </ShadowDomTargetContext.Provider>
110 )
111 }, el)
112​
113 this.#isMounted = true
114 this.#dispose = dispose
115 }
116​
117 unmount() {
118 if (!this.#isMounted) {
119 throw new Error('Devtools is not mounted')
120 }
121 this.#dispose?.()
122 this.#isMounted = false
123 }
124​
125 setRouter(router: AnyRouter) {
126 this.#router[1](router)
127 }
128​
129 setRouterState(routerState: any) {
130 this.#routerState[1](routerState)
131 }
132​
133 setStyle(style: any) {
134 this.#style[1](style)
135 }
136​
137 setClassName(className: any) {
138 this.#className[1](className)
139 }
140​
141 setOptions(options: Partial<TanStackRouterDevtoolsPanelCoreOptions>) {
142 if (options.shadowDOMTarget !== undefined) {
143 this.#shadowDOMTarget = options.shadowDOMTarget
144 }
145 if (options.router !== undefined) {
146 this.setRouter(options.router)
147 }
148 if (options.routerState !== undefined) {
149 this.setRouterState(options.routerState)
150 }
151​
152 if (options.style !== undefined) {
153 this.setStyle(options.style)
154 }
155​
156 if (options.className !== undefined) {
157 this.setClassName(options.className)
158 }
159 }