Skip to content

Commit

Permalink
feat(vue-query-devtools): Add VueQueryDevtoolsPanel support
Browse files Browse the repository at this point in the history
  • Loading branch information
toofff committed Sep 11, 2024
1 parent 9608f80 commit 4f97ea9
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 29 deletions.
4 changes: 4 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,10 @@
{
"label": "Persister",
"to": "framework/vue/examples/persister"
},
{
"label": "Devtools Embedded Panel",
"to": "framework/vue/examples/devtools-panel"
}
]
},
Expand Down
34 changes: 33 additions & 1 deletion docs/framework/vue/devtools.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ bun add @tanstack/vue-query-devtools

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.

## Floating Mode

@todo: blabla

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.

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!
Expand All @@ -57,9 +61,10 @@ import { VueQueryDevtools } from '@tanstack/vue-query-devtools'

- `initialIsOpen: Boolean`
- Set this `true` if you want the dev tools to default to being open.
- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right"`
- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
- Defaults to `bottom-right`.
- The position of the React Query logo to open and close the devtools panel.
- If `relative`, the button is placed in the location that you render the devtools.
- `position?: "top" | "bottom" | "left" | "right"`
- Defaults to `bottom`.
- The position of the React Query devtools panel.
Expand All @@ -73,6 +78,33 @@ import { VueQueryDevtools } from '@tanstack/vue-query-devtools'
- Default behavior will apply the devtool's styles to the head tag within the DOM.
- Use this to pass a shadow DOM target to the devtools so that the styles will be applied within the shadow DOM instead of within the head tag in the light DOM.

## Embedded Mode

@todo: blabla

### Options

- `style?: React.CSSProperties`
- Custom styles for the devtools panel
- Default: `{ height: '500px' }`
- Example: `{ height: '100%' }`
- Example: `{ height: '100%', width: '100%' }`
- `onClose?: () => unknown`
- Callback function that is called when the devtools panel is closed
- `client?: QueryClient`,
- Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
- 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.
- `styleNonce?: string`
- 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.
- `shadowDOMTarget?: ShadowRoot`
- Default behavior will apply the devtool's styles to the head tag within the DOM.
- Use this to pass a shadow DOM target to the devtools so that the styles will be applied within the shadow DOM instead of within the head tag in the light DOM.

## Devtools in production

@todo: blabla

## Traditional Devtools

Vue Query will seamlessly integrate with the [Official Vue devtools](https://github.com/vuejs/devtools-next), adding custom inspector and timeline events.
Expand Down
9 changes: 9 additions & 0 deletions examples/vue/devtools-panel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
.DS_Store
dist
dist-ssr
*.local

package-lock.json
yarn.lock
pnpm-lock.yaml
6 changes: 6 additions & 0 deletions examples/vue/devtools-panel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Basic example

To run this example:

- `npm install` or `yarn` or `pnpm i` or `bun i`
- `npm run dev` or `yarn dev` or `pnpm dev` or `bun dev`
12 changes: 12 additions & 0 deletions examples/vue/devtools-panel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TanStack Query Vue Devtools Panel Example App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/vue/devtools-panel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@tanstack/query-example-vue-devtools-panel",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@tanstack/vue-query": "^5.54.1",
"@tanstack/vue-query-devtools": "^5.54.1",
"vue": "^3.4.27",
"vue-demi": "^0.14.10"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.1.1",
"typescript": "5.3.3",
"vite": "^5.3.5"
}
}
45 changes: 45 additions & 0 deletions examples/vue/devtools-panel/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script lang="ts">
import { defineComponent } from 'vue'
import { useQuery } from '@tanstack/vue-query'
import { VueQueryDevtoolsPanel } from '@tanstack/vue-query-devtools'
export default defineComponent({
name: 'App',
components: { VueQueryDevtoolsPanel },
setup() {
const { data, error, isFetching, isPending } = useQuery({
queryKey: ['repoData'],
async queryFn() {
return await fetch('https://api.github.com/repos/Tanstack/query').then(
(response) => response.json(),
)
},
})
return {
data,
error,
isFetching,
isPending,
}
},
})
</script>

// @todo: add button to switch between open & close devtools panel

<template>
<template v-if="isPending"> Loading... </template>
<template v-else-if="error">
'An error has occurred: {{ error.message }}
</template>
<template v-else>
<h1>{{ data.name }}</h1>
<p>{{ data.description }}</p>
<strong>👀 {{ data.subscribers_count }}</strong>
<strong>✨ {{ data.stargazers_count }}</strong>
<strong>🍴 {{ data.forks_count }}</strong>
<div>{{ isFetching ? 'Updating...' : '' }}</div>
</template>
<VueQueryDevtoolsPanel />
</template>
6 changes: 6 additions & 0 deletions examples/vue/devtools-panel/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createApp } from 'vue'
import { VueQueryPlugin } from '@tanstack/vue-query'

import App from './App.vue'

createApp(App).use(VueQueryPlugin).mount('#app')
5 changes: 5 additions & 0 deletions examples/vue/devtools-panel/src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module '*.vue' {
import { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}
6 changes: 6 additions & 0 deletions examples/vue/devtools-panel/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Post {
userId: number
id: number
title: string
body: string
}
15 changes: 15 additions & 0 deletions examples/vue/devtools-panel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"lib": ["esnext", "dom"],
"types": ["vite/client"]
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.vue"]
}
9 changes: 9 additions & 0 deletions examples/vue/devtools-panel/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [vue()],
optimizeDeps: {
exclude: ['@tanstack/vue-query', 'vue-demi'],
},
})
10 changes: 5 additions & 5 deletions packages/react-query-devtools/src/ReactQueryDevtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import type { QueryClient } from '@tanstack/react-query'

export interface DevtoolsOptions {
/**
* Set this true if you want the dev tools to default to being open
* Set this true if you want the dev tools to default to being open.
*/
initialIsOpen?: boolean
/**
* The position of the React Query logo to open and close the devtools panel.
* 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
* Defaults to 'bottom-right'.
* 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative'
* @default 'bottom-right'
*/
buttonPosition?: DevtoolsButtonPosition
/**
* The position of the React Query devtools panel.
* 'top' | 'bottom' | 'left' | 'right'
* Defaults to 'bottom'.
* @default 'bottom'
*/
position?: DevtoolsPosition
/**
* Custom instance of QueryClient
* Custom instance of QueryClient.
*/
client?: QueryClient
/**
Expand Down
8 changes: 3 additions & 5 deletions packages/react-query-devtools/src/ReactQueryDevtoolsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { QueryClient } from '@tanstack/react-query'

export interface DevtoolsPanelOptions {
/**
* Custom instance of QueryClient
* Custom instance of QueryClient.
*/
client?: QueryClient
/**
Expand All @@ -22,17 +22,15 @@ export interface DevtoolsPanelOptions {
* Use this so you can attach the devtool's styles to specific element in the DOM.
*/
shadowDOMTarget?: ShadowRoot

/**
* Custom styles for the devtools panel
* Custom styles for the devtools panel.
* @default { height: '500px' }
* @example { height: '100%' }
* @example { height: '100%', width: '100%' }
*/
style?: React.CSSProperties

/**
* Callback function that is called when the devtools panel is closed
* Callback function that is called when the devtools panel is closed.
*/
onClose?: () => unknown
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vue-query-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@
"eslint-plugin-vue": "^9.27.0",
"vite": "^5.3.5",
"vue": "^3.4.27",
"vue-demi": "^0.14.10",
"vue-tsc": "^2.0.26"
},
"peerDependencies": {
"@tanstack/vue-query": "workspace:^",
"vue": "^3.3.0"
"vue": "^3.3.0",
"vue-demi": "^0.14.0"
}
}
47 changes: 47 additions & 0 deletions packages/vue-query-devtools/src/VueQueryDevtoolsPanel.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script setup lang="ts">
import { onlineManager, useQueryClient } from '@tanstack/vue-query'
import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
import { onMounted, onScopeDispose, ref, watchEffect } from 'vue'
import { reactive } from 'vue-demi'
import type { DevtoolsPanelOptions } from './types'
const props = defineProps<DevtoolsPanelOptions>()
const styleObject = reactive({
height: '500px',
...props.style,
})
const div = ref<HTMLElement>()
const client = props.client || useQueryClient()
const devtools = new TanstackQueryDevtoolsPanel({
client,
queryFlavor: 'Vue Query',
version: '5',
onlineManager,
buttonPosition: 'bottom-left',
position: 'bottom',
initialIsOpen: true,
errorTypes: props.errorTypes,
styleNonce: props.styleNonce,
shadowDOMTarget: props.shadowDOMTarget,
onClose: props.onClose,
})
watchEffect(() => {
devtools.setOnClose(props.onClose ?? (() => {}))
devtools.setErrorTypes(props.errorTypes || [])
})
onMounted(() => {
devtools.mount(div.value as HTMLElement)
})
onScopeDispose(() => {
devtools.unmount()
})
</script>

<template>
<div :style="styleObject" className="tsqd-parent-container" ref="div"></div>
</template>
15 changes: 12 additions & 3 deletions packages/vue-query-devtools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import devtools from './devtools.vue'
import Devtools from './VueQueryDevtools.vue'
import DevtoolsPanel from './VueQueryDevtoolsPanel.vue'
import type { DefineComponent } from 'vue'
import type { DevtoolsOptions } from './types'
import type { DevtoolsOptions, DevtoolsPanelOptions } from './types'

export const VueQueryDevtools = (
process.env.NODE_ENV !== 'development'
? function () {
return null
}
: devtools
: Devtools
) as DefineComponent<DevtoolsOptions, {}, unknown>

export const VueQueryDevtoolsPanel = (
process.env.NODE_ENV !== 'development'
? function () {
return null
}
: DevtoolsPanel
) as DefineComponent<DevtoolsPanelOptions, {}, unknown>
6 changes: 4 additions & 2 deletions packages/vue-query-devtools/src/production.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import devtools from './devtools.vue'
import Devtools from './VueQueryDevtools.vue'
import DevtoolsPanel from './VueQueryDevtoolsPanel.vue'

export default devtools
export const VueQueryDevtools = Devtools
export const VueQueryDevtoolsPanel = DevtoolsPanel
Loading

0 comments on commit 4f97ea9

Please sign in to comment.