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

devtoolsPanel.tsx

106 lines3.1 KB

1import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
2import { onlineManager, useQueryClient } from '@tanstack/solid-query'
3import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
4import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
5import type { QueryClient } from '@tanstack/solid-query'
6import type { JSX } from 'solid-js'
7​
8/**
9 * The props of `SolidQueryDevtoolsPanel`, which renders the devtools panel inline.
10 */
11export interface DevtoolsPanelOptions {
12 /**
13 * Custom instance of QueryClient
14 */
15 client?: QueryClient
16 /**
17 * Use this so you can define custom errors that can be shown in the devtools.
18 */
19 errorTypes?: Array<DevtoolsErrorType>
20 /**
21 * Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
22 */
23 styleNonce?: string
24 /**
25 * Use this to pass a shadow DOM target to the devtools so that the styles will be applied
26 * within the shadow DOM instead of within the head tag in the light DOM.
27 */
28 shadowDOMTarget?: ShadowRoot
29​
30 /**
31 * Custom styles for the devtools panel
32 * @default { height: '500px' }
33 * @example { height: '100%' }
34 * @example { height: '100%', width: '100%' }
35 */
36 style?: JSX.CSSProperties
37​
38 /**
39 * Callback function that is called when the devtools panel is closed
40 */
41 onClose?: () => void
42 /**
43 * Set this to true to hide disabled queries from the devtools panel.
44 */
45 hideDisabledQueries?: boolean
46 /**
47 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
48 * This overrides the theme selected in the devtools Settings menu. When omitted, the
49 * selected theme is used, or `'system'` if none is selected.
50 */
51 theme?: Theme
53​
54/**
55 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
56 * @param props - The devtools panel options.
57 * @returns The element the devtools panel is mounted in.
58 */
59export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
60 const queryClient = useQueryClient(props.client)
61 const client = createMemo(() => queryClient)
62 let ref!: HTMLDivElement
63 const { errorTypes, styleNonce, shadowDOMTarget, hideDisabledQueries } = props
64 const devtools = new TanstackQueryDevtoolsPanel({
65 client: client(),
66 queryFlavor: 'Solid Query',
67 version: '5',
68 onlineManager,
69 buttonPosition: 'bottom-left',
70 position: 'bottom',
71 initialIsOpen: true,
72 errorTypes,
73 styleNonce,
74 shadowDOMTarget,
75 onClose: props.onClose,
76 hideDisabledQueries,
77 theme: props.theme,
78 })
79 createEffect(() => {
80 devtools.setClient(client())
81 })
82 createEffect(() => {
83 devtools.setOnClose(props.onClose ?? (() => {}))
84 })
85​
86 createEffect(() => {
87 devtools.setErrorTypes(props.errorTypes || [])
88 })
89​
90 createEffect(() => {
91 devtools.setTheme(props.theme)
92 })
93​
94 onMount(() => {
95 devtools.mount(ref)
96 onCleanup(() => devtools.unmount())
97 })
98​
99 return (
100 <div
101 style={{ height: '500px', ...props.style }}
102 class="tsqd-parent-container"
103 ref={ref}
104 />
105 )