Open History to see the last commit
1<script lang="ts">
2 import { onMount } from 'svelte'
3 import { BROWSER, DEV } from 'esm-env'
4 import { onlineManager, useQueryClient } from '@tanstack/svelte-query'
5 import type { QueryClient } from '@tanstack/svelte-query'
6 import type {
7 DevtoolsButtonPosition,
8 DevtoolsErrorType,
9 DevtoolsPosition,
10 TanstackQueryDevtools,
11 Theme,
12 } from '@tanstack/query-devtools'
13β
14 interface DevtoolsOptions {
15 /**
16 * Set this true if you want the dev tools to default to being open
17 */
18 initialIsOpen?: boolean
19 /**
20 * The position of the TanStack logo to open and close the devtools panel.
21 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative'
22 *
23 * @defaultValue bottom-right
24 */
25 buttonPosition?: DevtoolsButtonPosition
26 /**
27 * The position of the Svelte Query devtools panel.
28 * 'top' | 'bottom' | 'left' | 'right'
29 *
30 * @defaultValue bottom
31 */
32 position?: DevtoolsPosition
33 /**
34 * Custom instance of QueryClient
35 */
36 client?: QueryClient
37 /**
38 * Use this so you can define custom errors that can be shown in the devtools.
39 */
40 errorTypes?: Array<DevtoolsErrorType>
41 /**
42 * 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.
43 */
44 styleNonce?: string
45 /**
46 * Use this to pass a shadow DOM target to the devtools so that the styles will be applied
47 * within the shadow DOM instead of within the head tag in the light DOM.
48 */
49 shadowDOMTarget?: ShadowRoot
50 /**
51 * Set this to true to hide disabled queries from the devtools panel.
52 */
53 hideDisabledQueries?: boolean
54 /**
55 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
56 * Defaults to 'system'.
57 */
58 theme?: Theme
59 }
60β
61 let {
62 initialIsOpen = false,
63 buttonPosition = 'bottom-right',
64 position = 'bottom',
65 client = useQueryClient(),
66 errorTypes = [],
67 styleNonce = undefined,
68 shadowDOMTarget = undefined,
69 hideDisabledQueries = false,
70 theme = 'system',
71 }: DevtoolsOptions = $props()
72β
73 let ref: HTMLDivElement
74 let devtools = $state<TanstackQueryDevtools | undefined>(undefined)
75β
76 if (DEV && BROWSER) {
77 onMount(() => {
78 import('@tanstack/query-devtools').then((m) => {
79 const QueryDevtools = m.TanstackQueryDevtools
80β
81 devtools = new QueryDevtools({
82 client,
83 queryFlavor: 'Svelte Query',
84 version: '5',
85 onlineManager,
86 buttonPosition,
87 position,
88 initialIsOpen,
89 errorTypes,
90 styleNonce,
91 shadowDOMTarget,
92 hideDisabledQueries,
93 theme,
94 })
95β
96 devtools.mount(ref)
97 })
98 return () => devtools?.unmount()
99 })
100β
101 $effect(() => {
102 devtools?.setButtonPosition(buttonPosition)
103 })
104β
105 $effect(() => {
106 devtools?.setPosition(position)
107 })
108β
109 $effect(() => {
110 devtools?.setInitialIsOpen(initialIsOpen)
111 })
112β
113 $effect(() => {
114 devtools?.setErrorTypes(errorTypes)
115 })
116β
117 $effect(() => {
118 devtools?.setTheme(theme)
119 })
120 }
121</script>
122β
123<div class="tsqd-parent-container" bind:this={ref}></div>