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

SvelteQueryDevtools.svelte

124 lines3.3 KB

1<script lang="ts">
2 import { onMount } from 'svelte'
3 import { BROWSER } 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 * This overrides the theme selected in the devtools Settings menu. When omitted, the
57 * selected theme is used, or `'system'` if none is selected.
58 */
59 theme?: Theme
60 }
61​
62 let {
63 initialIsOpen = false,
64 buttonPosition = 'bottom-right',
65 position = 'bottom',
66 client = useQueryClient(),
67 errorTypes = [],
68 styleNonce = undefined,
69 shadowDOMTarget = undefined,
70 hideDisabledQueries = false,
71 theme,
72 }: DevtoolsOptions = $props()
73​
74 let ref: HTMLDivElement
75 let devtools = $state<TanstackQueryDevtools | undefined>(undefined)
76​
77 if (BROWSER) {
78 onMount(() => {
79 import('@tanstack/query-devtools').then((m) => {
80 const QueryDevtools = m.TanstackQueryDevtools
81​
82 devtools = new QueryDevtools({
83 client,
84 queryFlavor: 'Svelte Query',
85 version: '5',
86 onlineManager,
87 buttonPosition,
88 position,
89 initialIsOpen,
90 errorTypes,
91 styleNonce,
92 shadowDOMTarget,
93 hideDisabledQueries,
94 theme,
95 })
96​
97 devtools.mount(ref)
98 })
99 return () => devtools?.unmount()
100 })
101​
102 $effect(() => {
103 devtools?.setButtonPosition(buttonPosition)
104 })
105​
106 $effect(() => {
107 devtools?.setPosition(position)
108 })
109​
110 $effect(() => {
111 devtools?.setInitialIsOpen(initialIsOpen)
112 })
113​
114 $effect(() => {
115 devtools?.setErrorTypes(errorTypes)
116 })
117​
118 $effect(() => {
119 devtools?.setTheme(theme)
120 })
121 }
122</script>
123​
124<div class="tsqd-parent-container" bind:this={ref}></div>