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

FloatingTanStackRouterDevtools.tsx

290 lines8.0 KB

1import { clsx as cx } from 'clsx'
2​
3import { createEffect, createMemo, createSignal } from 'solid-js'
4import { Dynamic } from 'solid-js/web'
5​
6import { DevtoolsOnCloseContext } from './context'
7import { useIsMounted } from './utils'
8import { BaseTanStackRouterDevtoolsPanel } from './BaseTanStackRouterDevtoolsPanel'
9import useLocalStorage from './useLocalStorage'
10import { TanStackLogo } from './logo'
11import { useStyles } from './useStyles'
12import type { Accessor, JSX } from 'solid-js'
13import type { AnyRouter } from '@tanstack/router-core'
14​
15export interface FloatingDevtoolsOptions {
16 /**
17 * Set this true if you want the dev tools to default to being open
18 */
19 initialIsOpen?: boolean
20 /**
21 * Use this to add props to the panel. For example, you can add class, style (merge and override default style), etc.
22 */
23 panelProps?: any & {
24 ref?: any
25 }
26 /**
27 * 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.
28 */
29 closeButtonProps?: any & {
30 ref?: any
31 }
32 /**
33 * 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.
34 */
35 toggleButtonProps?: any & {
36 ref?: any
37 }
38 /**
39 * The position of the TanStack Router logo to open and close the devtools panel.
40 * Defaults to 'bottom-left'.
41 */
42 position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
43 /**
44 * Use this to render the devtools inside a different type of container element for a11y purposes.
45 * Any string which corresponds to a valid intrinsic JSX element is allowed.
46 * Defaults to 'footer'.
47 */
48 containerElement?: string | any
49 /**
50 * A boolean variable indicating if the "lite" version of the library is being used
51 */
52 router: Accessor<AnyRouter>
53 routerState: Accessor<any>
54 /**
55 * Use this to attach the devtool's styles to specific element in the DOM.
56 */
57 shadowDOMTarget?: ShadowRoot
59​
60export function FloatingTanStackRouterDevtools({
61 initialIsOpen,
62 panelProps = {},
63 closeButtonProps = {},
64 toggleButtonProps = {},
65 position = 'bottom-left',
66 containerElement: Container = 'footer',
67 router,
68 routerState,
69 shadowDOMTarget,
70}: FloatingDevtoolsOptions): JSX.Element | null {
71 const [rootEl, setRootEl] = createSignal<HTMLDivElement>()
72​
73 // eslint-disable-next-line prefer-const
74 let panelRef: HTMLDivElement | undefined = undefined
75​
76 const [isOpen, setIsOpen] = useLocalStorage(
77 'tanstackRouterDevtoolsOpen',
78 initialIsOpen,
79 )
80​
81 const [devtoolsHeight, setDevtoolsHeight] = useLocalStorage<number | null>(
82 'tanstackRouterDevtoolsHeight',
83 null,
84 )
85​
86 const [isResolvedOpen, setIsResolvedOpen] = createSignal(false)
87 const [isResizing, setIsResizing] = createSignal(false)
88 const isMounted = useIsMounted()
89 const styles = useStyles()
90​
91 const handleDragStart = (
92 panelElement: HTMLDivElement | undefined,
93 startEvent: any,
94 ) => {
95 if (startEvent.button !== 0) return // Only allow left click for drag
96​
97 setIsResizing(true)
98​
99 const dragInfo = {
100 originalHeight: panelElement?.getBoundingClientRect().height ?? 0,
101 pageY: startEvent.pageY,
102 }
103​
104 const run = (moveEvent: MouseEvent) => {
105 const delta = dragInfo.pageY - moveEvent.pageY
106 const newHeight = dragInfo.originalHeight + delta
107​
108 setDevtoolsHeight(newHeight)
109​
110 if (newHeight < 70) {
111 setIsOpen(false)
112 } else {
113 setIsOpen(true)
114 }
115 }
116​
117 const unsub = () => {
118 setIsResizing(false)
119 document.removeEventListener('mousemove', run)
120 document.removeEventListener('mouseUp', unsub)
121 }
122​
123 document.addEventListener('mousemove', run)
124 document.addEventListener('mouseup', unsub)
125 }
126​
127 const isButtonClosed = isOpen() ?? false
128​
129 createEffect(() => {
130 setIsResolvedOpen(isOpen() ?? false)
131 })
132​
133 createEffect(() => {
134 if (isResolvedOpen()) {
135 const previousValue = rootEl()?.parentElement?.style.paddingBottom
136​
137 const run = () => {
138 const containerHeight = panelRef!.getBoundingClientRect().height
139 if (rootEl()?.parentElement) {
140 setRootEl((prev) => {
141 if (prev?.parentElement) {
142 prev.parentElement.style.paddingBottom = `${containerHeight}px`
143 }
144 return prev
145 })
146 }
147 }
148​
149 run()
150​
151 if (typeof window !== 'undefined') {
152 window.addEventListener('resize', run)
153​
154 return () => {
155 window.removeEventListener('resize', run)
156 if (rootEl()?.parentElement && typeof previousValue === 'string') {
157 setRootEl((prev) => {
158 prev!.parentElement!.style.paddingBottom = previousValue
159 return prev
160 })
161 }
162 }
163 }
164 } else {
165 // Reset padding when devtools are closed
166 if (rootEl()?.parentElement) {
167 setRootEl((prev) => {
168 if (prev?.parentElement) {
169 prev.parentElement.removeAttribute('style')
170 }
171 return prev
172 })
173 }
174 }
175 return
176 })
177​
178 createEffect(() => {
179 if (rootEl()) {
180 const el = rootEl()
181 const fontSize = getComputedStyle(el!).fontSize
182 el?.style.setProperty('--tsrd-font-size', fontSize)
183 }
184 })
185​
186 const { style: panelStyle = {}, ...otherPanelProps } = panelProps as {
187 style?: Record<string, any>
188 }
189​
190 const {
191 style: closeButtonStyle = {},
192 onClick: onCloseClick,
193 ...otherCloseButtonProps
194 } = closeButtonProps
195​
196 const {
197 onClick: onToggleClick,
198 class: toggleButtonClassName,
199 ...otherToggleButtonProps
200 } = toggleButtonProps
201​
202 // Do not render on the server
203 if (!isMounted()) return null
204​
205 const resolvedHeight = createMemo(() => devtoolsHeight() ?? 500)
206​
207 const basePanelClass = createMemo(() => {
208 return cx(
209 styles().devtoolsPanelContainer,
210 styles().devtoolsPanelContainerVisibility(!!isOpen()),
211 styles().devtoolsPanelContainerResizing(isResizing),
212 styles().devtoolsPanelContainerAnimation(
213 isResolvedOpen(),
214 resolvedHeight() + 16,
215 ),
216 )
217 })
218​
219 const basePanelStyle = createMemo(() => {
220 return {
221 height: `${resolvedHeight()}px`,
222 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
223 ...(panelStyle || {}),
224 }
225 })
226​
227 const buttonStyle = createMemo(() => {
228 return cx(
229 styles().mainCloseBtn,
230 styles().mainCloseBtnPosition(position),
231 styles().mainCloseBtnAnimation(!!isOpen()),
232 toggleButtonClassName,
233 )
234 })
235​
236 return (
237 <Dynamic
238 component={Container}
239 ref={setRootEl}
240 class="TanStackRouterDevtools"
241 >
242 <DevtoolsOnCloseContext.Provider
243 value={{
244 onCloseClick: onCloseClick ?? (() => {}),
245 }}
246 >
247 {/* {router() ? ( */}
248 <BaseTanStackRouterDevtoolsPanel
249 ref={panelRef}
250 {...otherPanelProps}
251 router={router}
252 routerState={routerState}
253 className={basePanelClass}
254 style={basePanelStyle}
255 isOpen={isResolvedOpen()}
256 setIsOpen={setIsOpen}
257 handleDragStart={(e) => handleDragStart(panelRef, e)}
258 shadowDOMTarget={shadowDOMTarget}
259 />
260 {/* ) : (
261 <p>No router</p>
262 )} */}
263 </DevtoolsOnCloseContext.Provider>
264​
265 <button
266 type="button"
267 {...otherToggleButtonProps}
268 aria-label="Open TanStack Router Devtools"
269 onClick={(e) => {
270 setIsOpen(true)
271 onToggleClick && onToggleClick(e)
272 }}
273 class={buttonStyle()}
274 >
275 <div class={styles().mainCloseBtnIconContainer}>
276 <div class={styles().mainCloseBtnIconOuter}>
277 <TanStackLogo />
278 </div>
279 <div class={styles().mainCloseBtnIconInner}>
280 <TanStackLogo />
281 </div>
282 </div>
283 <div class={styles().mainCloseBtnDivider}>-</div>
284 <div class={styles().routerLogoCloseButton}>TanStack Router</div>
285 </button>
286 </Dynamic>
287 )
289​
290export default FloatingTanStackRouterDevtools