Open History to see the last commit
1import { createSignal, lazy } from 'solid-js'
2import { render } from 'solid-js/web'
3import { ShadowDomTargetContext } from './context'
4import type { AnyRouter } from '@tanstack/router-core'
5import type { Signal } from 'solid-js'
6β
7export interface TanStackRouterDevtoolsCoreOptions {
8 /**
9 * Set this true if you want the dev tools to default to being open
10 */
11 initialIsOpen?: boolean
12 /**
13 * Use this to add props to the panel. For example, you can add class, style (merge and override default style), etc.
14 */
15 panelProps?: any & {
16 ref?: any
17 }
18 /**
19 * Use this to add props to the close button. For example, you can add class, style (merge and override default style), onClick (extend default handler), etc.
20 */
21 closeButtonProps?: any & {
22 ref?: any
23 }
24 /**
25 * Use this to add props to the toggle button. For example, you can add class, style (merge and override default style), onClick (extend default handler), etc.
26 */
27 toggleButtonProps?: any & {
28 ref?: any
29 }
30 /**
31 * The position of the TanStack Router logo to open and close the devtools panel.
32 * Defaults to 'bottom-left'.
33 */
34 position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
35 /**
36 * Use this to render the devtools inside a different type of container element for a11y purposes.
37 * Any string which corresponds to a valid intrinsic JSX element is allowed.
38 * Defaults to 'footer'.
39 */
40 containerElement?: string | any
41 /**
42 * A boolean variable indicating if the "lite" version of the library is being used
43 */
44 router: AnyRouter
45 routerState: any
46 /**
47 * Use this to attach the devtool's styles to specific element in the DOM.
48 */
49 shadowDOMTarget?: ShadowRoot
50}
51β
52export class TanStackRouterDevtoolsCore {
53 #router: Signal<AnyRouter>
54 #routerState: Signal<any>
55 #position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
56 #initialIsOpen: boolean
57 #shadowDOMTarget?: ShadowRoot
58β
59 #panelProps: any
60 #closeButtonProps: any
61 #toggleButtonProps: any
62 #containerElement?: string | any
63β
64 #isMounted = false
65 #Component: any
66 #dispose?: () => void
67β
68 constructor(config: TanStackRouterDevtoolsCoreOptions) {
69 this.#router = createSignal(config.router)
70 this.#routerState = createSignal(config.routerState)
71 this.#position = config.position ?? 'bottom-left'
72 this.#initialIsOpen = config.initialIsOpen ?? false
73 this.#shadowDOMTarget = config.shadowDOMTarget
74β
75 this.#panelProps = config.panelProps
76 this.#closeButtonProps = config.closeButtonProps
77 this.#toggleButtonProps = config.toggleButtonProps
78 this.#containerElement = config.containerElement
79 }
80β
81 mount<T extends HTMLElement>(el: T) {
82 if (this.#isMounted) {
83 throw new Error('Devtools is already mounted')
84 }
85β
86 const dispose = render(() => {
87 const [router] = this.#router
88 const [routerState] = this.#routerState
89 const position = this.#position
90 const initialIsOpen = this.#initialIsOpen
91 const shadowDOMTarget = this.#shadowDOMTarget
92β
93 const panelProps = this.#panelProps
94 const closeButtonProps = this.#closeButtonProps
95 const toggleButtonProps = this.#toggleButtonProps
96 const containerElement = this.#containerElement
97β
98 let Devtools
99β
100 if (this.#Component) {
101 Devtools = this.#Component
102 } else {
103 Devtools = lazy(() => import('./FloatingTanStackRouterDevtools'))
104 this.#Component = Devtools
105 }
106β
107 return (
108 <ShadowDomTargetContext.Provider value={shadowDOMTarget}>
109 <Devtools
110 position={position}
111 initialIsOpen={initialIsOpen}
112 router={router}
113 routerState={routerState}
114 shadowDOMTarget={shadowDOMTarget}
115 panelProps={panelProps}
116 closeButtonProps={closeButtonProps}
117 toggleButtonProps={toggleButtonProps}
118 containerElement={containerElement}
119 />
120 </ShadowDomTargetContext.Provider>
121 )
122 }, el)
123β
124 this.#isMounted = true
125 this.#dispose = dispose
126 }
127β
128 unmount() {
129 if (!this.#isMounted) {
130 throw new Error('Devtools is not mounted')
131 }
132 this.#dispose?.()
133 this.#isMounted = false
134 }
135β
136 setRouter(router: AnyRouter) {
137 this.#router[1](router)
138 }
139β
140 setRouterState(routerState: any) {
141 this.#routerState[1](routerState)
142 }
143β
144 setOptions(options: Partial<TanStackRouterDevtoolsCoreOptions>) {
145 if (options.position !== undefined) {
146 this.#position = options.position
147 }
148β
149 if (options.initialIsOpen !== undefined) {
150 this.#initialIsOpen = options.initialIsOpen
151 }
152β
153 if (options.shadowDOMTarget !== undefined) {
154 this.#shadowDOMTarget = options.shadowDOMTarget
155 }
156β
157 if (options.containerElement !== undefined) {
158 this.#containerElement = options.containerElement
159 }
160 }
161}