Skip to content

Commit

Permalink
feat(query-devtools): Add shadowDOMTarget property to react-query-dev…
Browse files Browse the repository at this point in the history
…tool (#7005)

* feat(devtools): add shadowDOMTarget prop to vue, svelte, angular and solid
  • Loading branch information
michelleoj authored Mar 12, 2024
1 parent 97f274e commit d4def6b
Show file tree
Hide file tree
Showing 27 changed files with 658 additions and 409 deletions.
4 changes: 4 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@
{
"label": "Algolia",
"to": "framework/react/examples/algolia"
},
{
"label": "Shadow DOM",
"to": "framework/react/examples/shadow-dom"
}
]
},
Expand Down
3 changes: 3 additions & 0 deletions docs/framework/angular/devtools.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ import { Component } from '@angular/core';
- 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 instad of within the head tag in the light DOM.
3 changes: 3 additions & 0 deletions docs/framework/react/devtools.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ function App() {
- 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 instad of within the head tag in the light DOM.

## Devtools in production

Expand Down
3 changes: 3 additions & 0 deletions docs/framework/solid/devtools.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ function App() {
- 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 instad of within the head tag in the light DOM.
3 changes: 3 additions & 0 deletions docs/framework/vue/devtools.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ import { VueQueryDevtools } from '@tanstack/vue-query-devtools'
- 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 instad of within the head tag in the light DOM.
18 changes: 18 additions & 0 deletions examples/react/shadow-dom/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
24 changes: 24 additions & 0 deletions examples/react/shadow-dom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
6 changes: 6 additions & 0 deletions examples/react/shadow-dom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Example

To run this example:

- `pnpm install`
- `pnpm run dev`
13 changes: 13 additions & 0 deletions examples/react/shadow-dom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
30 changes: 30 additions & 0 deletions examples/react/shadow-dom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "shadow-dom",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "workspace:^",
"@tanstack/react-query-devtools": "workspace:^",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.56.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "5.2.2",
"vite": "^5.1.1"
}
}
68 changes: 68 additions & 0 deletions examples/react/shadow-dom/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
34 changes: 34 additions & 0 deletions examples/react/shadow-dom/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

const appRoot = document.getElementById('root')

if (appRoot) {
const queryClient = new QueryClient()
const shadowRoot = appRoot.attachShadow({ mode: 'open' })
const root = ReactDOM.createRoot(shadowRoot)

root.render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<div
style={{
display: 'flex',
justifyContent: 'center',
width: '100vw',
}}
>
I'm just an app rendered in a shadow dom...
</div>
<ReactQueryDevtools
initialIsOpen={false}
buttonPosition="bottom-left"
shadowDOMTarget={appRoot.shadowRoot!}
/>
</QueryClientProvider>
</React.StrictMode>,
)
}
1 change: 1 addition & 0 deletions examples/react/shadow-dom/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
25 changes: 25 additions & 0 deletions examples/react/shadow-dom/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
11 changes: 11 additions & 0 deletions examples/react/shadow-dom/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
}
7 changes: 7 additions & 0 deletions examples/react/shadow-dom/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class AngularQueryDevtools
*/
@Input() styleNonce?: string

/**
* Use this so you can attach the devtool's styles to specific element in the DOM.
*/
@Input() shadowDOMTarget?: ShadowRoot

/**
* Use this so you can define custom errors that can be shown in the devtools.
*/
Expand Down Expand Up @@ -129,6 +134,7 @@ export class AngularQueryDevtools
initialIsOpen: this.initialIsOpen,
errorTypes: this.errorTypes,
styleNonce: this.styleNonce,
shadowDOMTarget: this.shadowDOMTarget,
})
devtools.mount(this.ref.nativeElement)
this.#devtools = devtools
Expand Down
2 changes: 2 additions & 0 deletions packages/query-devtools/src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export interface QueryDevtoolsProps {
position?: DevtoolsPosition
initialIsOpen?: boolean
errorTypes?: Array<DevToolsErrorType>
shadowDOMTarget?: ShadowRoot
}

export const QueryDevtoolsContext = createContext<QueryDevtoolsProps>({
client: undefined as unknown as QueryClient,
onlineManager: undefined as unknown as typeof onlineManager,
queryFlavor: '',
version: '',
shadowDOMTarget: undefined,
})

export function useQueryDevtoolsContext() {
Expand Down
Loading

0 comments on commit d4def6b

Please sign in to comment.