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