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

Devtools.svelte

108 lines2.9 KB

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 } from '@tanstack/query-devtools'
12​
13 interface DevtoolsOptions {
14 /**
15 * Set this true if you want the dev tools to default to being open
16 */
17 initialIsOpen?: boolean
18 /**
19 * The position of the TanStack Query logo to open and close the devtools panel.
20 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
21 * Defaults to 'bottom-right'.
22 */
23 buttonPosition?: DevtoolsButtonPosition
24 /**
25 * The position of the TanStack Query devtools panel.
26 * 'top' | 'bottom' | 'left' | 'right'
27 * Defaults to 'bottom'.
28 */
29 position?: DevtoolsPosition
30 /**
31 * Custom instance of QueryClient
32 */
33 client?: QueryClient
34 /**
35 * Use this so you can define custom errors that can be shown in the devtools.
36 */
37 errorTypes?: Array<DevtoolsErrorType>
38 /**
39 * 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.
40 */
41 styleNonce?: string
42 /**
43 * Use this so you can attach the devtool's styles to specific element in the DOM.
44 */
45 shadowDOMTarget?: ShadowRoot
46 /**
47 * Set this to true to hide disabled queries from the devtools panel.
48 */
49 hideDisabledQueries?: boolean
50 }
51​
52 let {
53 initialIsOpen = false,
54 buttonPosition = 'bottom-right',
55 position = 'bottom',
56 client = useQueryClient(),
57 errorTypes = [],
58 styleNonce = undefined,
59 shadowDOMTarget = undefined,
60 hideDisabledQueries = false,
61 }: DevtoolsOptions = $props()
62​
63 let ref: HTMLDivElement
64 let devtools: TanstackQueryDevtools | undefined
65​
66 if (DEV && BROWSER) {
67 onMount(() => {
68 import('@tanstack/query-devtools').then((m) => {
69 const QueryDevtools = m.TanstackQueryDevtools
70​
71 devtools = new QueryDevtools({
72 client,
73 queryFlavor: 'Svelte Query',
74 version: '5',
75 onlineManager,
76 buttonPosition,
77 position,
78 initialIsOpen,
79 errorTypes,
80 styleNonce,
81 shadowDOMTarget,
82 hideDisabledQueries,
83 })
84​
85 devtools.mount(ref)
86 })
87 return () => devtools?.unmount()
88 })
89​
90 $effect(() => {
91 devtools?.setButtonPosition(buttonPosition)
92 })
93​
94 $effect(() => {
95 devtools?.setPosition(position)
96 })
97​
98 $effect(() => {
99 devtools?.setInitialIsOpen(initialIsOpen)
100 })
101​
102 $effect(() => {
103 devtools?.setErrorTypes(errorTypes)
104 })
105 }
106</script>
107​
108<div class="tsqd-parent-container" bind:this={ref}></div>