Skip to content

Commit 634b6da

Browse files
feat(vue-query): component based vue devtools - Vue3 only (#6255)
1 parent 9b19eaa commit 634b6da

File tree

16 files changed

+385
-4
lines changed

16 files changed

+385
-4
lines changed

docs/vue/devtools.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,52 @@ The only thing you need to do is to install the official **[Vue Devtools](https:
1111

1212
Vue Query will seamlessly integrate with the official devtools, adding custom inspector and timeline events.
1313
Devtool code will be treeshaken from production bundles by default.
14+
15+
## Component based Devtools (Vue 3)
16+
17+
You can embeed devtools component into your page by using dedicated package.
18+
Component based devtools are using the same framework-agnostic implementation, have more features and are updated more frequently.
19+
20+
The devtools component is a separate package that you need to install:
21+
22+
```bash
23+
$ npm i @tanstack/vue-query-devtools
24+
# or
25+
$ pnpm add @tanstack/vue-query-devtools
26+
# or
27+
$ yarn add @tanstack/vue-query-devtools
28+
```
29+
30+
By default, Vue Query Devtools are only included in bundles when `process.env.NODE_ENV === 'development'`, so you don't need to worry about excluding them during a production build.
31+
32+
Devtools will be mounted as a fixed, floating element in your app and provide a toggle in the corner of the screen to show and hide the devtools. This toggle state will be stored and remembered in localStorage across reloads.
33+
34+
Place the following code as high in your Vue app as you can. The closer it is to the root of the page, the better it will work!
35+
36+
```vue
37+
<script setup>
38+
import { VueQueryDevtools } from '@tanstack/vue-query-devtools'
39+
</script>
40+
41+
<template>
42+
<h1>The app!</h1>
43+
<VueQueryDevtools />
44+
</template>
45+
```
46+
47+
### Options
48+
49+
- `initialIsOpen: Boolean`
50+
- Set this `true` if you want the dev tools to default to being open
51+
- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right"`
52+
- Defaults to `bottom-left`
53+
- The position of the React Query logo to open and close the devtools panel
54+
- `position?: "top" | "bottom" | "left" | "right"`
55+
- Defaults to `bottom`
56+
- The position of the React Query devtools panel
57+
- `client?: QueryClient`,
58+
- Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
59+
- `errorTypes?: { name: string; initializer: (query: Query) => TError}`
60+
- Use this to predefine some errors that can be triggered on your queries. Initializer will be called (with the specific query) when that error is toggled on from the UI. It must return an Error.
61+
- `styleNonce?: string`
62+
- 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.

examples/vue/basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"@tanstack/vue-query": "^5.0.0",
12+
"@tanstack/vue-query-devtools": "^5.0.0",
1213
"vue": "^3.3.0"
1314
},
1415
"devDependencies": {

examples/vue/basic/src/App.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<script lang="ts">
22
import { defineComponent, ref } from 'vue'
3+
import { VueQueryDevtools } from '@tanstack/vue-query-devtools'
34
45
import Posts from './Posts.vue'
56
import Post from './Post.vue'
67
78
export default defineComponent({
89
name: 'App',
9-
components: { Posts, Post },
10+
components: { Posts, Post, VueQueryDevtools },
1011
setup() {
1112
const visitedPosts = ref(new Set())
1213
const isVisited = (id: number) => visitedPosts.value.has(id)
@@ -40,4 +41,5 @@ export default defineComponent({
4041
</p>
4142
<Post v-if="postId > -1" :postId="postId" @setPostId="setPostId" />
4243
<Posts v-else :isVisited="isVisited" @setPostId="setPostId" />
44+
<VueQueryDevtools />
4345
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @ts-check
2+
3+
/** @type {import('eslint').Linter.Config} */
4+
const config = {
5+
parserOptions: {
6+
tsconfigRootDir: __dirname,
7+
project: './tsconfig.json',
8+
extraFileExtensions: ['.vue'],
9+
},
10+
}
11+
12+
module.exports = config
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "@tanstack/vue-query-devtools",
3+
"version": "5.1.0",
4+
"description": "Developer tools to interact with and visualize the TanStack/vue-query cache",
5+
"author": "tannerlinsley",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/TanStack/query.git",
10+
"directory": "packages/vue-query-devtools"
11+
},
12+
"homepage": "https://tanstack.com/query",
13+
"funding": {
14+
"type": "github",
15+
"url": "https://github.com/sponsors/tannerlinsley"
16+
},
17+
"type": "module",
18+
"types": "dist/index.d.ts",
19+
"module": "dist/index.js",
20+
"main": "dist/index.js",
21+
"exports": {
22+
".": {
23+
"types": "./dist/index.d.ts",
24+
"default": "./dist/index.js"
25+
},
26+
"./production": {
27+
"types": "./dist/production.d.ts",
28+
"default": "./dist/production.js"
29+
},
30+
"./dist/production.js": {
31+
"types": "./dist/production.d.ts",
32+
"default": "./dist/production.js"
33+
},
34+
"./package.json": "./package.json"
35+
},
36+
"sideEffects": false,
37+
"files": [
38+
"dist",
39+
"src"
40+
],
41+
"scripts": {
42+
"clean": "rimraf ./build && rimraf ./coverage",
43+
"test:eslint": "eslint --ext .ts,.tsx ./src",
44+
"test:types": "tsc",
45+
"test:build": "publint --strict",
46+
"build": "vite build && vue-tsc"
47+
},
48+
"dependencies": {
49+
"@tanstack/query-devtools": "workspace:*"
50+
},
51+
"devDependencies": {
52+
"@tanstack/vue-query": "workspace:*",
53+
"@vitejs/plugin-vue": "^4.4.0",
54+
"vue": "^3.3.0",
55+
"vue-tsc": "^1.8.21",
56+
"vite": "^4.4.4"
57+
},
58+
"peerDependencies": {
59+
"vue": "^3.3.0"
60+
}
61+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script setup lang="ts">
2+
import { onMounted, onScopeDispose, watchEffect, ref } from 'vue'
3+
import { onlineManager, useQueryClient } from '@tanstack/vue-query'
4+
import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5+
import type { DevtoolsOptions } from './types'
6+
7+
const props = defineProps<DevtoolsOptions>()
8+
9+
const div = ref<HTMLElement>()
10+
const client = props.client || useQueryClient()
11+
const devtools = new TanstackQueryDevtools({
12+
client,
13+
queryFlavor: 'Vue Query',
14+
version: '5',
15+
onlineManager,
16+
buttonPosition: props.buttonPosition,
17+
position: props.position,
18+
initialIsOpen: props.initialIsOpen,
19+
errorTypes: props.errorTypes,
20+
styleNonce: props.styleNonce,
21+
})
22+
23+
watchEffect(() => {
24+
devtools.setButtonPosition(props.buttonPosition || 'bottom-left')
25+
devtools.setPosition(props.position || 'bottom')
26+
devtools.setInitialIsOpen(props.initialIsOpen)
27+
devtools.setErrorTypes(props.errorTypes || [])
28+
})
29+
30+
onMounted(() => {
31+
devtools.mount(div.value as HTMLElement)
32+
})
33+
34+
onScopeDispose(() => {
35+
devtools.unmount()
36+
})
37+
</script>
38+
39+
<template>
40+
<div className="tsqd-parent-container" ref="div"></div>
41+
</template>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import devtools from './devtools.vue'
2+
import type { DefineComponent } from 'vue'
3+
import type { DevtoolsOptions } from './types'
4+
5+
export const VueQueryDevtools = (
6+
process.env.NODE_ENV !== 'development'
7+
? function () {
8+
return null
9+
}
10+
: devtools
11+
) as DefineComponent<DevtoolsOptions, {}, unknown>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import devtools from './devtools.vue'
2+
3+
export default devtools
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module '*.vue' {
2+
import type { DefineComponent } from 'vue'
3+
4+
const component: DefineComponent<{}, {}, any>
5+
export default component
6+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {
2+
DevToolsErrorType,
3+
DevtoolsButtonPosition,
4+
DevtoolsPosition,
5+
} from '@tanstack/query-devtools'
6+
import type { QueryClient } from '@tanstack/vue-query'
7+
8+
export interface DevtoolsOptions {
9+
/**
10+
* Set this true if you want the dev tools to default to being open
11+
*/
12+
initialIsOpen?: boolean
13+
/**
14+
* The position of the React Query logo to open and close the devtools panel.
15+
* 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
16+
* Defaults to 'bottom-left'.
17+
*/
18+
buttonPosition?: DevtoolsButtonPosition
19+
/**
20+
* The position of the React Query devtools panel.
21+
* 'top' | 'bottom' | 'left' | 'right'
22+
* Defaults to 'bottom'.
23+
*/
24+
position?: DevtoolsPosition
25+
/**
26+
* Custom instance of QueryClient
27+
*/
28+
client?: QueryClient
29+
/**
30+
* Use this so you can define custom errors that can be shown in the devtools.
31+
*/
32+
errorTypes?: Array<DevToolsErrorType>
33+
/**
34+
* 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.
35+
*/
36+
styleNonce?: string
37+
}

0 commit comments

Comments
 (0)