Skip to content
Merged
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
8 changes: 8 additions & 0 deletions .changeset/olive-ducks-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@tanstack/react-query-devtools': minor
'@tanstack/solid-query-devtools': minor
'@tanstack/vue-query-devtools': minor
'@tanstack/query-devtools': minor
---

feat(devtools): allow passing a theme via prop
8 changes: 4 additions & 4 deletions packages/query-devtools/src/DevtoolsComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Devtools } from './Devtools'
import { getPreferredColorScheme } from './utils'
import { THEME_PREFERENCE } from './constants'
import { PiPProvider, QueryDevtoolsContext, ThemeContext } from './contexts'
import type { Theme } from './contexts'
import type { DevtoolsComponentType } from './Devtools'

const DevtoolsComponent: DevtoolsComponentType = (props) => {
Expand All @@ -14,10 +15,9 @@ const DevtoolsComponent: DevtoolsComponentType = (props) => {
const colorScheme = getPreferredColorScheme()

const theme = createMemo(() => {
const preference = (localStore.theme_preference || THEME_PREFERENCE) as
| 'system'
| 'dark'
| 'light'
const preference = (props.theme ||
localStore.theme_preference ||
THEME_PREFERENCE) as Theme
if (preference !== 'system') return preference
return colorScheme()
})
Expand Down
8 changes: 4 additions & 4 deletions packages/query-devtools/src/DevtoolsPanelComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ContentView, ParentPanel } from './Devtools'
import { getPreferredColorScheme } from './utils'
import { THEME_PREFERENCE } from './constants'
import { PiPProvider, QueryDevtoolsContext, ThemeContext } from './contexts'
import type { Theme } from './contexts'
import type { DevtoolsComponentType } from './Devtools'

const DevtoolsPanelComponent: DevtoolsComponentType = (props) => {
Expand All @@ -14,10 +15,9 @@ const DevtoolsPanelComponent: DevtoolsComponentType = (props) => {
const colorScheme = getPreferredColorScheme()

const theme = createMemo(() => {
const preference = (localStore.theme_preference || THEME_PREFERENCE) as
| 'system'
| 'dark'
| 'light'
const preference = (props.theme ||
localStore.theme_preference ||
THEME_PREFERENCE) as Theme
if (preference !== 'system') return preference
return colorScheme()
})
Expand Down
12 changes: 12 additions & 0 deletions packages/query-devtools/src/TanstackQueryDevtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
DevtoolsErrorType,
DevtoolsPosition,
QueryDevtoolsProps,
Theme,
} from './contexts'
import type { Signal } from 'solid-js'

Expand All @@ -33,6 +34,7 @@ class TanstackQueryDevtools {
#errorTypes: Signal<Array<DevtoolsErrorType> | undefined>
#hideDisabledQueries: Signal<boolean | undefined>
#Component: DevtoolsComponentType | undefined
#theme: Signal<Theme | undefined>
#dispose?: () => void

constructor(config: TanstackQueryDevtoolsConfig) {
Expand All @@ -48,6 +50,7 @@ class TanstackQueryDevtools {
styleNonce,
shadowDOMTarget,
hideDisabledQueries,
theme,
} = config
this.#client = createSignal(client)
this.#queryFlavor = queryFlavor
Expand All @@ -60,6 +63,7 @@ class TanstackQueryDevtools {
this.#initialIsOpen = createSignal(initialIsOpen)
this.#errorTypes = createSignal(errorTypes)
this.#hideDisabledQueries = createSignal(hideDisabledQueries)
this.#theme = createSignal(theme)
}

setButtonPosition(position: DevtoolsButtonPosition) {
Expand All @@ -82,6 +86,10 @@ class TanstackQueryDevtools {
this.#client[1](client)
}

setTheme(theme?: Theme) {
this.#theme[1](theme)
}

mount<T extends HTMLElement>(el: T) {
if (this.#isMounted) {
throw new Error('Devtools is already mounted')
Expand All @@ -93,6 +101,7 @@ class TanstackQueryDevtools {
const [errors] = this.#errorTypes
const [hideDisabledQueries] = this.#hideDisabledQueries
const [queryClient] = this.#client
const [theme] = this.#theme
let Devtools: DevtoolsComponentType

if (this.#Component) {
Expand Down Expand Up @@ -128,6 +137,9 @@ class TanstackQueryDevtools {
get hideDisabledQueries() {
return hideDisabledQueries()
},
get theme() {
return theme()
},
}}
/>
)
Expand Down
12 changes: 12 additions & 0 deletions packages/query-devtools/src/TanstackQueryDevtoolsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
DevtoolsErrorType,
DevtoolsPosition,
QueryDevtoolsProps,
Theme,
} from './contexts'
import type { Signal } from 'solid-js'

Expand All @@ -35,6 +36,7 @@ class TanstackQueryDevtoolsPanel {
#hideDisabledQueries: Signal<boolean | undefined>
#onClose: Signal<(() => unknown) | undefined>
#Component: DevtoolsComponentType | undefined
#theme: Signal<Theme | undefined>
#dispose?: () => void

constructor(config: TanstackQueryDevtoolsPanelConfig) {
Expand All @@ -51,6 +53,7 @@ class TanstackQueryDevtoolsPanel {
shadowDOMTarget,
onClose,
hideDisabledQueries,
theme,
} = config
this.#client = createSignal(client)
this.#queryFlavor = queryFlavor
Expand All @@ -64,6 +67,7 @@ class TanstackQueryDevtoolsPanel {
this.#errorTypes = createSignal(errorTypes)
this.#hideDisabledQueries = createSignal(hideDisabledQueries)
this.#onClose = createSignal(onClose)
this.#theme = createSignal(theme)
}

setButtonPosition(position: DevtoolsButtonPosition) {
Expand All @@ -90,6 +94,10 @@ class TanstackQueryDevtoolsPanel {
this.#onClose[1](() => onClose)
}

setTheme(theme?: Theme) {
this.#theme[1](theme)
}

mount<T extends HTMLElement>(el: T) {
if (this.#isMounted) {
throw new Error('Devtools is already mounted')
Expand All @@ -102,6 +110,7 @@ class TanstackQueryDevtoolsPanel {
const [hideDisabledQueries] = this.#hideDisabledQueries
const [queryClient] = this.#client
const [onClose] = this.#onClose
const [theme] = this.#theme
let Devtools: DevtoolsComponentType

if (this.#Component) {
Expand Down Expand Up @@ -140,6 +149,9 @@ class TanstackQueryDevtoolsPanel {
get onClose() {
return onClose()
},
get theme() {
return theme()
},
}}
/>
)
Expand Down
2 changes: 2 additions & 0 deletions packages/query-devtools/src/contexts/QueryDevtoolsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type XPosition = 'left' | 'right'
type YPosition = 'top' | 'bottom'
export type DevtoolsPosition = XPosition | YPosition
export type DevtoolsButtonPosition = `${YPosition}-${XPosition}` | 'relative'
export type Theme = 'dark' | 'light' | 'system'

export interface DevtoolsErrorType {
/**
Expand All @@ -30,6 +31,7 @@ export interface QueryDevtoolsProps {
shadowDOMTarget?: ShadowRoot
onClose?: () => unknown
hideDisabledQueries?: boolean
theme?: Theme
}

export const QueryDevtoolsContext = createContext<QueryDevtoolsProps>({
Expand Down
1 change: 1 addition & 0 deletions packages/query-devtools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type {
DevtoolsButtonPosition,
DevtoolsErrorType,
DevtoolsPosition,
Theme,
} from './contexts'
export {
TanstackQueryDevtools,
Expand Down
12 changes: 12 additions & 0 deletions packages/react-query-devtools/src/ReactQueryDevtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
DevtoolsButtonPosition,
DevtoolsErrorType,
DevtoolsPosition,
Theme,
} from '@tanstack/query-devtools'
import type { QueryClient } from '@tanstack/react-query'

Expand Down Expand Up @@ -46,6 +47,11 @@ export interface DevtoolsOptions {
* Set this to true to hide disabled queries from the devtools panel.
*/
hideDisabledQueries?: boolean
/**
* Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
* Defaults to 'system'.
*/
theme?: Theme
}

export function ReactQueryDevtools(
Expand All @@ -61,6 +67,7 @@ export function ReactQueryDevtools(
styleNonce,
shadowDOMTarget,
hideDisabledQueries,
theme,
} = props
const [devtools] = React.useState(
new TanstackQueryDevtools({
Expand All @@ -75,6 +82,7 @@ export function ReactQueryDevtools(
styleNonce,
shadowDOMTarget,
hideDisabledQueries,
theme,
}),
)

Expand Down Expand Up @@ -102,6 +110,10 @@ export function ReactQueryDevtools(
devtools.setErrorTypes(errorTypes || [])
}, [errorTypes, devtools])

React.useEffect(() => {
devtools.setTheme(theme)
}, [theme, devtools])

React.useEffect(() => {
if (ref.current) {
devtools.mount(ref.current)
Expand Down
20 changes: 18 additions & 2 deletions packages/react-query-devtools/src/ReactQueryDevtoolsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as React from 'react'
import { onlineManager, useQueryClient } from '@tanstack/react-query'
import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
import type { DevtoolsErrorType } from '@tanstack/query-devtools'
import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
import type { QueryClient } from '@tanstack/react-query'

export interface DevtoolsPanelOptions {
Expand Down Expand Up @@ -39,14 +39,25 @@ export interface DevtoolsPanelOptions {
* Set this to true to hide disabled queries from the devtools panel.
*/
hideDisabledQueries?: boolean
/**
* Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
* Defaults to 'system'.
*/
theme?: Theme
}

export function ReactQueryDevtoolsPanel(
props: DevtoolsPanelOptions,
): React.ReactElement | null {
const queryClient = useQueryClient(props.client)
const ref = React.useRef<HTMLDivElement>(null)
const { errorTypes, styleNonce, shadowDOMTarget, hideDisabledQueries } = props
const {
errorTypes,
styleNonce,
shadowDOMTarget,
hideDisabledQueries,
theme,
} = props
const [devtools] = React.useState(
new TanstackQueryDevtoolsPanel({
client: queryClient,
Expand All @@ -61,6 +72,7 @@ export function ReactQueryDevtoolsPanel(
shadowDOMTarget,
onClose: props.onClose,
hideDisabledQueries,
theme,
}),
)

Expand All @@ -76,6 +88,10 @@ export function ReactQueryDevtoolsPanel(
devtools.setErrorTypes(errorTypes || [])
}, [errorTypes, devtools])

React.useEffect(() => {
devtools.setTheme(theme)
}, [theme, devtools])

React.useEffect(() => {
if (ref.current) {
devtools.mount(ref.current)
Expand Down
11 changes: 11 additions & 0 deletions packages/solid-query-devtools/src/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
DevtoolsButtonPosition,
DevtoolsErrorType,
DevtoolsPosition,
Theme,
} from '@tanstack/query-devtools'
import type { QueryClient } from '@tanstack/solid-query'

Expand Down Expand Up @@ -45,6 +46,11 @@ interface DevtoolsOptions {
* Set this to true to hide disabled queries from the devtools panel.
*/
hideDisabledQueries?: boolean
/**
* Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
* Defaults to 'system'.
*/
theme?: Theme
}

export default function SolidQueryDevtools(props: DevtoolsOptions) {
Expand All @@ -63,6 +69,7 @@ export default function SolidQueryDevtools(props: DevtoolsOptions) {
styleNonce: props.styleNonce,
shadowDOMTarget: props.shadowDOMTarget,
hideDisabledQueries: props.hideDisabledQueries,
theme: props.theme,
})

createEffect(() => {
Expand Down Expand Up @@ -91,6 +98,10 @@ export default function SolidQueryDevtools(props: DevtoolsOptions) {
devtools.setErrorTypes(props.errorTypes || [])
})

createEffect(() => {
devtools.setTheme(props.theme || 'system')
})

onMount(() => {
devtools.mount(ref)
onCleanup(() => devtools.unmount())
Expand Down
12 changes: 11 additions & 1 deletion packages/solid-query-devtools/src/devtoolsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
import { onlineManager, useQueryClient } from '@tanstack/solid-query'
import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
import type { DevtoolsErrorType } from '@tanstack/query-devtools'
import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
import type { QueryClient } from '@tanstack/solid-query'
import type { JSX } from 'solid-js'

Expand Down Expand Up @@ -39,6 +39,11 @@ export interface DevtoolsPanelOptions {
* Set this to true to hide disabled queries from the devtools panel.
*/
hideDisabledQueries?: boolean
/**
* Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
* Defaults to 'system'.
*/
theme?: Theme
}

export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
Expand All @@ -59,6 +64,7 @@ export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
shadowDOMTarget,
onClose: props.onClose,
hideDisabledQueries,
theme: props.theme,
})
createEffect(() => {
devtools.setClient(client())
Expand All @@ -71,6 +77,10 @@ export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
devtools.setErrorTypes(props.errorTypes || [])
})

createEffect(() => {
devtools.setTheme(props.theme || 'system')
})

onMount(() => {
devtools.mount(ref)
onCleanup(() => devtools.unmount())
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-query-devtools/src/devtools.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ const devtools = new TanstackQueryDevtools({
styleNonce: props.styleNonce,
shadowDOMTarget: props.shadowDOMTarget,
hideDisabledQueries: props.hideDisabledQueries,
theme: props.theme,
})

watchEffect(() => {
devtools.setButtonPosition(props.buttonPosition || 'bottom-right')
devtools.setPosition(props.position || 'bottom')
devtools.setInitialIsOpen(props.initialIsOpen)
devtools.setErrorTypes(props.errorTypes || [])
devtools.setTheme(props.theme || 'system')
})

onMounted(() => {
Expand Down
Loading
Loading