Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/router-devtools-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
"goober": "^2.1.16"
},
"devDependencies": {
"@solidjs/web": "^2.0.0",
"@testing-library/jest-dom": "catalog:",
"solid-js": "catalog:",
"solid-js": "^2.0.0",
"vite": "*",
"vite-plugin-solid": "catalog:",
"@solidjs/vite-plugin": "^3.0.1",
"@tanstack/vite-config": "catalog:",
"vitest": "catalog:",
"jsdom": "catalog:",
Expand Down
103 changes: 55 additions & 48 deletions packages/router-devtools-core/src/BaseTanStackRouterDevtoolsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import type {
Route,
RouterState,
} from '@tanstack/router-core'
import type { Accessor, JSX } from 'solid-js'
import type { Accessor } from 'solid-js'
import type { JSX } from '@solidjs/web'

export interface BaseDevtoolsPanelOptions {
/**
Expand Down Expand Up @@ -290,61 +291,64 @@ export const BaseTanStackRouterDevtoolsPanel =
? matches
: []
}
let cache = router()._cache
let cache = untrack(() => router()._cache)
let cacheMatches = [...cache.values()]
const [cachedMatches, setCachedMatches] = createSignal(cacheMatches)

createEffect(() => {
const refreshCache = () => {
const next = router()._cache
let valuesChanged = next.size !== cacheMatches.length
if (!valuesChanged) {
let index = 0
for (const match of next.values()) {
if (match !== cacheMatches[index++]) {
valuesChanged = true
break
createEffect(
() => router(),
(currentRouter) => {
const refreshCache = () => {
const next = currentRouter._cache
let valuesChanged = next.size !== cacheMatches.length
if (!valuesChanged) {
let index = 0
for (const match of next.values()) {
if (match !== cacheMatches[index++]) {
valuesChanged = true
break
}
}
}
}

if (next !== cache || valuesChanged) {
cache = next
cacheMatches = [...next.values()]
setCachedMatches(cacheMatches)
if (next !== cache || valuesChanged) {
cache = next
cacheMatches = [...next.values()]
setCachedMatches(cacheMatches)
}
}
}
refreshCache()
const interval = setInterval(refreshCache, 500)
onCleanup(() => clearInterval(interval))
})
refreshCache()
const interval = setInterval(refreshCache, 500)
return () => clearInterval(interval)
},
)

createEffect(() => {
const matches = routerState().matches
const currentMatch = matches[matches.length - 1]
if (!currentMatch) {
return
}
// Read history WITHOUT tracking it to avoid infinite loops
const historyUntracked = untrack(() => history())
const lastMatch = historyUntracked[0]
const sameLocation =
lastMatch &&
lastMatch.pathname === currentMatch.pathname &&
JSON.stringify(lastMatch.search ?? {}) ===
JSON.stringify(currentMatch.search ?? {})
if (!lastMatch || !sameLocation) {
if (historyUntracked.length >= HISTORY_LIMIT) {
setHasHistoryOverflowed(true)
createEffect(
() => routerState().matches,
(matches) => {
const currentMatch = matches[matches.length - 1]
if (!currentMatch) {
return
}
setHistory((prev) => {
const newHistory = [currentMatch, ...prev]
// truncate to ensure we don't overflow too much the ui
newHistory.splice(HISTORY_LIMIT)
return newHistory
})
}
})
const lastMatch = history()[0]
const sameLocation =
lastMatch &&
lastMatch.pathname === currentMatch.pathname &&
JSON.stringify(lastMatch.search ?? {}) ===
JSON.stringify(currentMatch.search ?? {})
if (!lastMatch || !sameLocation) {
if (history().length >= HISTORY_LIMIT) {
setHasHistoryOverflowed(true)
}
setHistory((prev) => {
const newHistory = [currentMatch, ...prev]
// truncate to ensure we don't overflow too much the ui
newHistory.splice(HISTORY_LIMIT)
return newHistory
})
}
},
)

const activeMatch = createMemo(() => {
const matches = [
Expand Down Expand Up @@ -412,7 +416,10 @@ export const BaseTanStackRouterDevtoolsPanel =
{...otherPanelProps}
>
{handleDragStart ? (
<div class={styles().dragHandle} onMouseDown={handleDragStart}></div>
<div
class={styles().dragHandle}
ref={(el) => el.addEventListener('mousedown', handleDragStart)}
></div>
) : null}
<button
class={styles().panelCloseBtn}
Expand Down
5 changes: 3 additions & 2 deletions packages/router-devtools-core/src/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
getServerComponentType,
} from './utils'
import { ShadowDomTargetContext } from './context'
import type { Accessor, JSX } from 'solid-js'
import type { Accessor } from 'solid-js'
import type { JSX } from '@solidjs/web'

type ExpanderProps = {
expanded: boolean
Expand Down Expand Up @@ -338,7 +339,7 @@ export function Explorer({
)
}

const stylesFactory = (shadowDOMTarget?: ShadowRoot) => {
const stylesFactory = (shadowDOMTarget?: ShadowRoot | null) => {
const { colors, font, size, border } = tokens
const { fontFamily, lineHeight, size: fontSize } = font
const css = shadowDOMTarget
Expand Down
117 changes: 60 additions & 57 deletions packages/router-devtools-core/src/FloatingTanStackRouterDevtools.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { clsx as cx } from 'clsx'

import { createEffect, createMemo, createSignal } from 'solid-js'
import { Dynamic } from 'solid-js/web'
import { Dynamic } from '@solidjs/web'

import { DevtoolsOnCloseContext } from './context'
import { useIsMounted } from './utils'
import { BaseTanStackRouterDevtoolsPanel } from './BaseTanStackRouterDevtoolsPanel'
import useLocalStorage from './useLocalStorage'
import { TanStackLogo } from './logo'
import { useStyles } from './useStyles'
import type { Accessor, JSX } from 'solid-js'
import type { Accessor } from 'solid-js'
import type { JSX } from '@solidjs/web'
import type { AnyRouter } from '@tanstack/router-core'

export interface FloatingDevtoolsOptions {
Expand Down Expand Up @@ -69,9 +70,7 @@ export function FloatingTanStackRouterDevtools({
shadowDOMTarget,
}: FloatingDevtoolsOptions): JSX.Element | null {
const [rootEl, setRootEl] = createSignal<HTMLDivElement>()

// eslint-disable-next-line prefer-const
let panelRef: HTMLDivElement | undefined = undefined
const [panelRef, setPanelRef] = createSignal<HTMLDivElement>()

const [isOpen, setIsOpen] = useLocalStorage(
'tanstackRouterDevtoolsOpen',
Expand Down Expand Up @@ -105,6 +104,11 @@ export function FloatingTanStackRouterDevtools({
const delta = dragInfo.pageY - moveEvent.pageY
const newHeight = dragInfo.originalHeight + delta

// Directly manipulate DOM for immediate visual feedback during drag
if (panelElement) {
panelElement.style.height = `${newHeight}px`
}

setDevtoolsHeight(newHeight)

if (newHeight < 70) {
Expand All @@ -117,7 +121,7 @@ export function FloatingTanStackRouterDevtools({
const unsub = () => {
setIsResizing(false)
document.removeEventListener('mousemove', run)
document.removeEventListener('mouseUp', unsub)
document.removeEventListener('mouseup', unsub)
}

document.addEventListener('mousemove', run)
Expand All @@ -126,62 +130,61 @@ export function FloatingTanStackRouterDevtools({

const isButtonClosed = isOpen() ?? false

createEffect(() => {
setIsResolvedOpen(isOpen() ?? false)
})

createEffect(() => {
if (isResolvedOpen()) {
const previousValue = rootEl()?.parentElement?.style.paddingBottom
createEffect(
() => isOpen(),
(val) => {
setIsResolvedOpen(val ?? false)
},
)

const run = () => {
const containerHeight = panelRef!.getBoundingClientRect().height
if (rootEl()?.parentElement) {
setRootEl((prev) => {
if (prev?.parentElement) {
prev.parentElement.style.paddingBottom = `${containerHeight}px`
}
return prev
})
createEffect(
() => ({
open: isResolvedOpen(),
root: rootEl(),
panel: panelRef(),
}),
({ open, root, panel }) => {
if (open) {
const previousValue = root?.parentElement?.style.paddingBottom

const run = () => {
const containerHeight = panel?.getBoundingClientRect().height ?? 0
if (root?.parentElement) {
root.parentElement.style.paddingBottom = `${containerHeight}px`
}
}
}

run()
run()

if (typeof window !== 'undefined') {
window.addEventListener('resize', run)
if (typeof window !== 'undefined') {
window.addEventListener('resize', run)

return () => {
window.removeEventListener('resize', run)
if (rootEl()?.parentElement && typeof previousValue === 'string') {
setRootEl((prev) => {
prev!.parentElement!.style.paddingBottom = previousValue
return prev
})
return () => {
window.removeEventListener('resize', run)
if (root?.parentElement && typeof previousValue === 'string') {
root.parentElement.style.paddingBottom = previousValue
}
}
}
} else {
// Reset padding when devtools are closed
if (root?.parentElement) {
root.parentElement.removeAttribute('style')
}
}
} else {
// Reset padding when devtools are closed
if (rootEl()?.parentElement) {
setRootEl((prev) => {
if (prev?.parentElement) {
prev.parentElement.removeAttribute('style')
}
return prev
})
}
}
return
})
return
},
)

createEffect(() => {
if (rootEl()) {
const el = rootEl()
const fontSize = getComputedStyle(el!).fontSize
el?.style.setProperty('--tsrd-font-size', fontSize)
}
})
createEffect(
() => rootEl(),
(el) => {
if (el) {
const fontSize = getComputedStyle(el).fontSize
el.style.setProperty('--tsrd-font-size', fontSize)
}
},
)

const { style: panelStyle = {}, ...otherPanelProps } = panelProps as {
style?: Record<string, any>
Expand Down Expand Up @@ -239,28 +242,28 @@ export function FloatingTanStackRouterDevtools({
ref={setRootEl}
class="TanStackRouterDevtools"
>
<DevtoolsOnCloseContext.Provider
<DevtoolsOnCloseContext
value={{
onCloseClick: onCloseClick ?? (() => {}),
}}
>
{/* {router() ? ( */}
<BaseTanStackRouterDevtoolsPanel
ref={panelRef}
ref={setPanelRef}
{...otherPanelProps}
router={router}
routerState={routerState}
className={basePanelClass}
style={basePanelStyle}
isOpen={isResolvedOpen()}
setIsOpen={setIsOpen}
handleDragStart={(e) => handleDragStart(panelRef, e)}
handleDragStart={(e) => handleDragStart(panelRef(), e)}
shadowDOMTarget={shadowDOMTarget}
/>
{/* ) : (
<p>No router</p>
)} */}
</DevtoolsOnCloseContext.Provider>
</DevtoolsOnCloseContext>

<button
type="button"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSignal, lazy } from 'solid-js'
import { render } from 'solid-js/web'
import { render } from '@solidjs/web'
import { ShadowDomTargetContext } from './context'
import type { AnyRouter } from '@tanstack/router-core'
import type { Signal } from 'solid-js'
Expand Down Expand Up @@ -105,7 +105,7 @@ export class TanStackRouterDevtoolsCore {
}

return (
<ShadowDomTargetContext.Provider value={shadowDOMTarget}>
<ShadowDomTargetContext value={shadowDOMTarget ?? null}>
<Devtools
position={position}
initialIsOpen={initialIsOpen}
Expand All @@ -117,7 +117,7 @@ export class TanStackRouterDevtoolsCore {
toggleButtonProps={toggleButtonProps}
containerElement={containerElement}
/>
</ShadowDomTargetContext.Provider>
</ShadowDomTargetContext>
)
}, el)

Expand Down
Loading
Loading