@tanstack/devtools-utils 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/intent.js +20 -0
- package/dist/preact/esm/index.js +1 -5
- package/dist/preact/esm/panel.d.ts +4 -5
- package/dist/preact/esm/panel.js +48 -28
- package/dist/preact/esm/panel.js.map +1 -1
- package/dist/preact/esm/plugin.d.ts +3 -2
- package/dist/preact/esm/plugin.js +20 -22
- package/dist/preact/esm/plugin.js.map +1 -1
- package/dist/react/esm/index.js +1 -5
- package/dist/react/esm/panel.d.ts +4 -4
- package/dist/react/esm/panel.js +47 -28
- package/dist/react/esm/panel.js.map +1 -1
- package/dist/react/esm/plugin.d.ts +3 -2
- package/dist/react/esm/plugin.js +20 -22
- package/dist/react/esm/plugin.js.map +1 -1
- package/dist/solid/esm/chunk/{ZXPCWXRH.js → 7TSS377A.js} +2 -10
- package/dist/solid/esm/chunk/{UX5ZZ4MG.js → WUD4VD3N.js} +2 -10
- package/dist/solid/esm/class-mount-impl/5TF6RAHJ.js +1 -0
- package/dist/solid/esm/class-mount-impl/VN5QTPB3.js +1 -0
- package/dist/solid/esm/dev.js +9 -11
- package/dist/solid/esm/index.d.ts +10 -10
- package/dist/solid/esm/index.js +9 -11
- package/dist/solid/esm/server.js +8 -10
- package/dist/solid-class/esm/class-mount-impl.d.ts +3 -2
- package/dist/solid-class/esm/class-mount-impl.js +17 -23
- package/dist/solid-class/esm/class-mount-impl.js.map +1 -1
- package/dist/solid-class/esm/class.d.ts +4 -3
- package/dist/solid-class/esm/class.js +57 -54
- package/dist/solid-class/esm/class.js.map +1 -1
- package/dist/solid-class/esm/panel.d.ts +3 -3
- package/dist/solid-class/esm/plugin.d.ts +3 -2
- package/dist/vue/esm/index.js +1 -5
- package/dist/vue/esm/panel.d.ts +3 -3
- package/dist/vue/esm/panel.js +36 -45
- package/dist/vue/esm/panel.js.map +1 -1
- package/dist/vue/esm/plugin.js +20 -19
- package/dist/vue/esm/plugin.js.map +1 -1
- package/package.json +12 -9
- package/skills/devtools-framework-adapters/SKILL.md +263 -0
- package/skills/devtools-framework-adapters/references/preact.md +262 -0
- package/skills/devtools-framework-adapters/references/react.md +241 -0
- package/skills/devtools-framework-adapters/references/solid.md +274 -0
- package/skills/devtools-framework-adapters/references/vue.md +270 -0
- package/src/preact/panel.tsx +6 -7
- package/src/preact/plugin.tsx +4 -3
- package/src/react/panel.tsx +6 -7
- package/src/react/plugin.tsx +4 -3
- package/src/solid/class-mount-impl.tsx +7 -10
- package/src/solid/class.test.tsx +43 -12
- package/src/solid/class.ts +15 -4
- package/src/solid/panel.tsx +6 -7
- package/src/solid/plugin.tsx +4 -3
- package/src/vue/panel.ts +6 -7
- package/dist/preact/esm/index.js.map +0 -1
- package/dist/react/esm/index.js.map +0 -1
- package/dist/solid/esm/class-mount-impl/LK7V47IP.js +0 -1
- package/dist/solid/esm/class-mount-impl/ZAIAXV4M.js +0 -1
- package/dist/vue/esm/index.js.map +0 -1
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: devtools-framework-adapters
|
|
3
|
+
description: >
|
|
4
|
+
Use devtools-utils factory functions to create per-framework plugin adapters.
|
|
5
|
+
createReactPlugin/createSolidPlugin/createVuePlugin/createPreactPlugin,
|
|
6
|
+
createReactPanel/createSolidPanel/createVuePanel/createPreactPanel.
|
|
7
|
+
[Plugin, NoOpPlugin] tuple for tree-shaking. DevtoolsPanelProps (theme).
|
|
8
|
+
Vue uses (name, component) not options object. Solid render must be function.
|
|
9
|
+
type: framework
|
|
10
|
+
library: tanstack-devtools
|
|
11
|
+
library_version: '0.10.12'
|
|
12
|
+
requires:
|
|
13
|
+
- tanstack-devtools/plugin-panel
|
|
14
|
+
sources:
|
|
15
|
+
- 'TanStack/devtools:docs/devtools-utils.md'
|
|
16
|
+
- 'TanStack/devtools:packages/devtools-utils/src/react/plugin.tsx'
|
|
17
|
+
- 'TanStack/devtools:packages/devtools-utils/src/vue/plugin.ts'
|
|
18
|
+
- 'TanStack/devtools:packages/devtools-utils/src/solid/plugin.tsx'
|
|
19
|
+
- 'TanStack/devtools:packages/devtools-utils/src/preact/plugin.tsx'
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
Use `@tanstack/devtools-utils` factory functions to create per-framework devtools plugin adapters. Each framework has a subpath export (`/react`, `/vue`, `/solid`, `/preact`) with two factories:
|
|
23
|
+
|
|
24
|
+
1. **`createXPlugin`** -- wraps a component into a `[Plugin, NoOpPlugin]` tuple for tree-shaking.
|
|
25
|
+
2. **`createXPanel`** -- wraps a class-based devtools core (`mount`/`unmount`) into a `[Panel, NoOpPanel]` component tuple.
|
|
26
|
+
|
|
27
|
+
## Key Source Files
|
|
28
|
+
|
|
29
|
+
- `packages/devtools-utils/src/react/plugin.tsx` -- createReactPlugin
|
|
30
|
+
- `packages/devtools-utils/src/react/panel.tsx` -- createReactPanel, DevtoolsPanelProps
|
|
31
|
+
- `packages/devtools-utils/src/vue/plugin.ts` -- createVuePlugin (different API)
|
|
32
|
+
- `packages/devtools-utils/src/vue/panel.ts` -- createVuePanel, DevtoolsPanelProps (includes 'system' theme)
|
|
33
|
+
- `packages/devtools-utils/src/solid/plugin.tsx` -- createSolidPlugin
|
|
34
|
+
- `packages/devtools-utils/src/solid/panel.tsx` -- createSolidPanel
|
|
35
|
+
- `packages/devtools-utils/src/solid/class.ts` -- constructCoreClass (Solid-specific)
|
|
36
|
+
- `packages/devtools-utils/src/preact/plugin.tsx` -- createPreactPlugin
|
|
37
|
+
- `packages/devtools-utils/src/preact/panel.tsx` -- createPreactPanel
|
|
38
|
+
|
|
39
|
+
## Shared Pattern
|
|
40
|
+
|
|
41
|
+
All four frameworks follow the same two-factory pattern:
|
|
42
|
+
|
|
43
|
+
### Plugin Factory
|
|
44
|
+
|
|
45
|
+
Every `createXPlugin` returns `readonly [Plugin, NoOpPlugin]`:
|
|
46
|
+
|
|
47
|
+
- **Plugin** -- returns a plugin object with metadata and a `render` function that renders your component.
|
|
48
|
+
- **NoOpPlugin** -- returns a plugin object with the same metadata but renders an empty fragment.
|
|
49
|
+
|
|
50
|
+
### Panel Factory
|
|
51
|
+
|
|
52
|
+
Every `createXPanel` returns `readonly [Panel, NoOpPanel]`:
|
|
53
|
+
|
|
54
|
+
- **Panel** -- a framework component that creates a `<div style="height:100%">`, instantiates the core class, calls `core.mount(el, theme)` on mount, and `core.unmount()` on cleanup.
|
|
55
|
+
- **NoOpPanel** -- renders an empty fragment.
|
|
56
|
+
|
|
57
|
+
### DevtoolsPanelProps
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// React, Solid, Preact
|
|
61
|
+
interface DevtoolsPanelProps {
|
|
62
|
+
theme?: 'light' | 'dark'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Vue (note: includes 'system')
|
|
66
|
+
interface DevtoolsPanelProps {
|
|
67
|
+
theme?: 'dark' | 'light' | 'system'
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Import from the framework subpath:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import type { DevtoolsPanelProps } from '@tanstack/devtools-utils/react'
|
|
75
|
+
import type { DevtoolsPanelProps } from '@tanstack/devtools-utils/vue'
|
|
76
|
+
import type { DevtoolsPanelProps } from '@tanstack/devtools-utils/solid'
|
|
77
|
+
import type { DevtoolsPanelProps } from '@tanstack/devtools-utils/preact'
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Primary Example (React)
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import { createReactPlugin } from '@tanstack/devtools-utils/react'
|
|
84
|
+
|
|
85
|
+
function MyStorePanel({ theme }: { theme?: 'light' | 'dark' }) {
|
|
86
|
+
return <div className={theme}>My Store Devtools</div>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const [MyPlugin, NoOpPlugin] = createReactPlugin({
|
|
90
|
+
name: 'My Store',
|
|
91
|
+
id: 'my-store',
|
|
92
|
+
defaultOpen: false,
|
|
93
|
+
Component: MyStorePanel,
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// Tree-shaking: use NoOp in production
|
|
97
|
+
const ActivePlugin =
|
|
98
|
+
process.env.NODE_ENV === 'development' ? MyPlugin : NoOpPlugin
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### With Class-Based Panel
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
import {
|
|
105
|
+
createReactPanel,
|
|
106
|
+
createReactPlugin,
|
|
107
|
+
} from '@tanstack/devtools-utils/react'
|
|
108
|
+
|
|
109
|
+
class MyDevtoolsCore {
|
|
110
|
+
mount(el: HTMLElement, theme: 'light' | 'dark') {
|
|
111
|
+
/* render into el */
|
|
112
|
+
}
|
|
113
|
+
unmount() {
|
|
114
|
+
/* cleanup */
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const [MyPanel, NoOpPanel] = createReactPanel(MyDevtoolsCore)
|
|
119
|
+
|
|
120
|
+
const [MyPlugin, NoOpPlugin] = createReactPlugin({
|
|
121
|
+
name: 'My Store',
|
|
122
|
+
Component: MyPanel,
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Framework API Differences
|
|
127
|
+
|
|
128
|
+
### React & Preact -- Options Object
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
createReactPlugin({ name, id?, defaultOpen?, Component }) // => [Plugin, NoOpPlugin]
|
|
132
|
+
createPreactPlugin({ name, id?, defaultOpen?, Component }) // => [Plugin, NoOpPlugin]
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- `Component` receives `DevtoolsPanelProps` (with `theme`).
|
|
136
|
+
- `Plugin()` returns `{ name, id?, defaultOpen?, render(el, theme) }`.
|
|
137
|
+
- Preact is identical to React but uses Preact JSX types and `preact/hooks`.
|
|
138
|
+
|
|
139
|
+
### Vue -- Positional Arguments, NOT Options Object
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
createVuePlugin(name: string, component: DefineComponent) // => [Plugin, NoOpPlugin]
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
- Takes `(name, component)` as separate arguments, NOT an options object.
|
|
146
|
+
- `Plugin(props)` returns `{ name, component, props }` -- it passes props through.
|
|
147
|
+
- `NoOpPlugin(props)` returns `{ name, component: Fragment, props }`.
|
|
148
|
+
- Vue's `DevtoolsPanelProps.theme` also accepts `'system'`.
|
|
149
|
+
|
|
150
|
+
### Solid -- Same API as React, Different Internals
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
createSolidPlugin({ name, id?, defaultOpen?, Component }) // => [Plugin, NoOpPlugin]
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
- Same options-object API as React.
|
|
157
|
+
- `Component` must be a Solid component function `(props: DevtoolsPanelProps) => JSX.Element`.
|
|
158
|
+
- The render function internally returns `<Component theme={theme} />` -- Solid handles reactivity.
|
|
159
|
+
- Solid also exports `constructCoreClass` from `@tanstack/devtools-utils/solid/class` for building lazy-loaded devtools cores.
|
|
160
|
+
|
|
161
|
+
## Common Mistakes
|
|
162
|
+
|
|
163
|
+
### CRITICAL: Using React JSX Pattern in Vue Adapter
|
|
164
|
+
|
|
165
|
+
Vue uses positional `(name, component)` arguments, NOT an options object.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
// WRONG -- will fail at compile time or produce garbage at runtime
|
|
169
|
+
const [MyPlugin, NoOpPlugin] = createVuePlugin({
|
|
170
|
+
name: 'My Plugin',
|
|
171
|
+
Component: MyPanel,
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
// CORRECT
|
|
175
|
+
const [MyPlugin, NoOpPlugin] = createVuePlugin('My Plugin', MyPanel)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Vue plugins also work differently at call time -- you pass props:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
// WRONG -- calling Plugin() with no args (React pattern)
|
|
182
|
+
const plugin = MyPlugin()
|
|
183
|
+
|
|
184
|
+
// CORRECT -- Vue Plugin takes props
|
|
185
|
+
const plugin = MyPlugin({ theme: 'dark' })
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### CRITICAL: Solid Render Prop Not Wrapped in Function
|
|
189
|
+
|
|
190
|
+
When using Solid components, `Component` must be a function reference, not raw JSX.
|
|
191
|
+
|
|
192
|
+
```tsx
|
|
193
|
+
// WRONG -- evaluates immediately, breaks Solid reactivity
|
|
194
|
+
createSolidPlugin({
|
|
195
|
+
name: 'My Store',
|
|
196
|
+
Component: <MyPanel />, // This is JSX.Element, not a component function
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
// CORRECT -- pass the component function itself
|
|
200
|
+
createSolidPlugin({
|
|
201
|
+
name: 'My Store',
|
|
202
|
+
Component: (props) => <MyPanel theme={props.theme} />,
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
// ALSO CORRECT -- pass the component reference directly
|
|
206
|
+
createSolidPlugin({
|
|
207
|
+
name: 'My Store',
|
|
208
|
+
Component: MyPanel,
|
|
209
|
+
})
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### HIGH: Ignoring NoOp Variant for Production
|
|
213
|
+
|
|
214
|
+
The factory returns `[Plugin, NoOpPlugin]`. Both must be destructured and used for proper tree-shaking.
|
|
215
|
+
|
|
216
|
+
```tsx
|
|
217
|
+
// WRONG -- NoOp variant discarded, devtools code ships to production
|
|
218
|
+
const [MyPlugin] = createReactPlugin({ name: 'Store', Component: MyPanel })
|
|
219
|
+
|
|
220
|
+
// CORRECT -- conditionally use NoOp in production
|
|
221
|
+
const [MyPlugin, NoOpPlugin] = createReactPlugin({
|
|
222
|
+
name: 'Store',
|
|
223
|
+
Component: MyPanel,
|
|
224
|
+
})
|
|
225
|
+
const ActivePlugin =
|
|
226
|
+
process.env.NODE_ENV === 'development' ? MyPlugin : NoOpPlugin
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### MEDIUM: Not Passing Theme Prop to Panel Component
|
|
230
|
+
|
|
231
|
+
`DevtoolsPanelProps` includes `theme`. The devtools shell passes it so panels can match light/dark mode. If your component ignores it, the panel will not adapt to theme changes.
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
// WRONG -- theme is ignored
|
|
235
|
+
const Component = () => <div>My Panel</div>
|
|
236
|
+
|
|
237
|
+
// CORRECT -- use theme for styling
|
|
238
|
+
const Component = ({ theme }: DevtoolsPanelProps) => (
|
|
239
|
+
<div className={theme === 'dark' ? 'dark-mode' : 'light-mode'}>My Panel</div>
|
|
240
|
+
)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Design Tension
|
|
244
|
+
|
|
245
|
+
The core architecture is framework-agnostic, but each framework has different idioms:
|
|
246
|
+
|
|
247
|
+
- React/Preact use an options object with `Component` as a JSX function component.
|
|
248
|
+
- Vue uses positional arguments with a `DefineComponent` and passes props through.
|
|
249
|
+
- Solid uses the same options API as React but with Solid's JSX and reactivity model.
|
|
250
|
+
|
|
251
|
+
Agents trained on React patterns will get Vue wrong. Always check the import path to determine which factory API to use.
|
|
252
|
+
|
|
253
|
+
## Cross-References
|
|
254
|
+
|
|
255
|
+
- **devtools-plugin-panel** -- Build your panel component first, then wrap it with the appropriate framework adapter.
|
|
256
|
+
- **devtools-production** -- NoOp variants are the primary mechanism for stripping devtools from production bundles.
|
|
257
|
+
|
|
258
|
+
## Reference Files
|
|
259
|
+
|
|
260
|
+
- `references/react.md` -- Full React factory API and examples
|
|
261
|
+
- `references/vue.md` -- Full Vue factory API and examples (different from React)
|
|
262
|
+
- `references/solid.md` -- Full Solid factory API and examples
|
|
263
|
+
- `references/preact.md` -- Full Preact factory API and examples
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Preact Framework Adapter Reference
|
|
2
|
+
|
|
3
|
+
## Import
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import {
|
|
7
|
+
createPreactPlugin,
|
|
8
|
+
createPreactPanel,
|
|
9
|
+
} from '@tanstack/devtools-utils/preact'
|
|
10
|
+
import type { DevtoolsPanelProps } from '@tanstack/devtools-utils/preact'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## DevtoolsPanelProps
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
interface DevtoolsPanelProps {
|
|
17
|
+
theme?: 'light' | 'dark'
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## createPreactPlugin
|
|
22
|
+
|
|
23
|
+
Creates a `[Plugin, NoOpPlugin]` tuple from a Preact component and plugin metadata. Identical API to `createReactPlugin`.
|
|
24
|
+
|
|
25
|
+
### Signature
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
function createPreactPlugin(options: {
|
|
29
|
+
name: string
|
|
30
|
+
id?: string
|
|
31
|
+
defaultOpen?: boolean
|
|
32
|
+
Component: (props: DevtoolsPanelProps) => JSX.Element
|
|
33
|
+
}): readonly [Plugin, NoOpPlugin]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Parameters
|
|
37
|
+
|
|
38
|
+
| Parameter | Type | Required | Description |
|
|
39
|
+
| ------------- | -------------------------------------------- | -------- | --------------------------------------- |
|
|
40
|
+
| `name` | `string` | Yes | Display name shown in the devtools tab |
|
|
41
|
+
| `id` | `string` | No | Unique identifier for the plugin |
|
|
42
|
+
| `defaultOpen` | `boolean` | No | Whether the plugin panel starts open |
|
|
43
|
+
| `Component` | `(props: DevtoolsPanelProps) => JSX.Element` | Yes | Preact component to render in the panel |
|
|
44
|
+
|
|
45
|
+
### Return Value
|
|
46
|
+
|
|
47
|
+
A `readonly [Plugin, NoOpPlugin]` tuple:
|
|
48
|
+
|
|
49
|
+
- **`Plugin()`** -- returns `{ name, id?, defaultOpen?, render(el, theme) }`. The `render` function renders `<Component theme={theme} />`.
|
|
50
|
+
- **`NoOpPlugin()`** -- returns the same shape but `render` returns `<></>`.
|
|
51
|
+
|
|
52
|
+
### Source
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
// packages/devtools-utils/src/preact/plugin.tsx
|
|
56
|
+
/** @jsxImportSource preact */
|
|
57
|
+
import type { JSX } from 'preact'
|
|
58
|
+
import type { DevtoolsPanelProps } from './panel'
|
|
59
|
+
|
|
60
|
+
export function createPreactPlugin({
|
|
61
|
+
Component,
|
|
62
|
+
...config
|
|
63
|
+
}: {
|
|
64
|
+
name: string
|
|
65
|
+
id?: string
|
|
66
|
+
defaultOpen?: boolean
|
|
67
|
+
Component: (props: DevtoolsPanelProps) => JSX.Element
|
|
68
|
+
}) {
|
|
69
|
+
function Plugin() {
|
|
70
|
+
return {
|
|
71
|
+
...config,
|
|
72
|
+
render: (_el: HTMLElement, theme: 'light' | 'dark') => (
|
|
73
|
+
<Component theme={theme} />
|
|
74
|
+
),
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function NoOpPlugin() {
|
|
78
|
+
return {
|
|
79
|
+
...config,
|
|
80
|
+
render: (_el: HTMLElement, _theme: 'light' | 'dark') => <></>,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return [Plugin, NoOpPlugin] as const
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Usage
|
|
88
|
+
|
|
89
|
+
#### Basic
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import { createPreactPlugin } from '@tanstack/devtools-utils/preact'
|
|
93
|
+
|
|
94
|
+
function MyStorePanel({ theme }: { theme?: 'light' | 'dark' }) {
|
|
95
|
+
return (
|
|
96
|
+
<div class={theme === 'dark' ? 'dark' : 'light'}>
|
|
97
|
+
<h2>My Store Devtools</h2>
|
|
98
|
+
</div>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const [MyPlugin, NoOpPlugin] = createPreactPlugin({
|
|
103
|
+
name: 'My Store',
|
|
104
|
+
id: 'my-store',
|
|
105
|
+
defaultOpen: false,
|
|
106
|
+
Component: MyStorePanel,
|
|
107
|
+
})
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Inline Component
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
const [MyPlugin, NoOpPlugin] = createPreactPlugin({
|
|
114
|
+
name: 'My Store',
|
|
115
|
+
Component: ({ theme }) => <MyStorePanel theme={theme} />,
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### Production Tree-Shaking
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
const [MyPlugin, NoOpPlugin] = createPreactPlugin({
|
|
123
|
+
name: 'My Store',
|
|
124
|
+
Component: MyStorePanel,
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
const ActivePlugin =
|
|
128
|
+
process.env.NODE_ENV === 'development' ? MyPlugin : NoOpPlugin
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## createPreactPanel
|
|
132
|
+
|
|
133
|
+
Wraps a class-based devtools core in a Preact component. Identical behavior to `createReactPanel` but uses `preact/hooks`.
|
|
134
|
+
|
|
135
|
+
### Signature
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
function createPreactPanel<
|
|
139
|
+
TComponentProps extends DevtoolsPanelProps | undefined,
|
|
140
|
+
TCoreDevtoolsClass extends {
|
|
141
|
+
mount: (el: HTMLElement, theme: 'light' | 'dark') => void
|
|
142
|
+
unmount: () => void
|
|
143
|
+
},
|
|
144
|
+
>(CoreClass: new () => TCoreDevtoolsClass): readonly [Panel, NoOpPanel]
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Parameters
|
|
148
|
+
|
|
149
|
+
| Parameter | Type | Required | Description |
|
|
150
|
+
| ----------- | ------------------------------------------------------- | -------- | --------------------------------------- |
|
|
151
|
+
| `CoreClass` | `new () => { mount(el, theme): void; unmount(): void }` | Yes | Class constructor for the devtools core |
|
|
152
|
+
|
|
153
|
+
### Return Value
|
|
154
|
+
|
|
155
|
+
- **`Panel`** -- A Preact component that:
|
|
156
|
+
- Creates a `<div style={{ height: '100%' }}>` with a ref.
|
|
157
|
+
- Instantiates `CoreClass` on mount via `useEffect` (from `preact/hooks`).
|
|
158
|
+
- Calls `core.mount(el, props.theme ?? 'dark')`.
|
|
159
|
+
- Calls `core.unmount()` on cleanup.
|
|
160
|
+
- Re-runs the effect when `theme` prop changes.
|
|
161
|
+
- **`NoOpPanel`** -- Renders `<></>`.
|
|
162
|
+
|
|
163
|
+
### Source
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
// packages/devtools-utils/src/preact/panel.tsx
|
|
167
|
+
/** @jsxImportSource preact */
|
|
168
|
+
import { useEffect, useRef } from 'preact/hooks'
|
|
169
|
+
|
|
170
|
+
export function createPreactPanel<
|
|
171
|
+
TComponentProps extends DevtoolsPanelProps | undefined,
|
|
172
|
+
TCoreDevtoolsClass extends {
|
|
173
|
+
mount: (el: HTMLElement, theme: 'light' | 'dark') => void
|
|
174
|
+
unmount: () => void
|
|
175
|
+
},
|
|
176
|
+
>(CoreClass: new () => TCoreDevtoolsClass) {
|
|
177
|
+
function Panel(props: TComponentProps) {
|
|
178
|
+
const devToolRef = useRef<HTMLDivElement>(null)
|
|
179
|
+
const devtools = useRef<TCoreDevtoolsClass | null>(null)
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
if (devtools.current) return
|
|
182
|
+
devtools.current = new CoreClass()
|
|
183
|
+
if (devToolRef.current) {
|
|
184
|
+
devtools.current.mount(devToolRef.current, props?.theme ?? 'dark')
|
|
185
|
+
}
|
|
186
|
+
return () => {
|
|
187
|
+
if (devToolRef.current) {
|
|
188
|
+
devtools.current?.unmount()
|
|
189
|
+
devtools.current = null
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}, [props?.theme])
|
|
193
|
+
return <div style={{ height: '100%' }} ref={devToolRef} />
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function NoOpPanel(_props: TComponentProps) {
|
|
197
|
+
return <></>
|
|
198
|
+
}
|
|
199
|
+
return [Panel, NoOpPanel] as const
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Usage
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
import {
|
|
207
|
+
createPreactPanel,
|
|
208
|
+
createPreactPlugin,
|
|
209
|
+
} from '@tanstack/devtools-utils/preact'
|
|
210
|
+
|
|
211
|
+
class MyDevtoolsCore {
|
|
212
|
+
mount(el: HTMLElement, theme: 'light' | 'dark') {
|
|
213
|
+
// Use DOM APIs to render your devtools UI into the provided element
|
|
214
|
+
const container = document.createElement('div')
|
|
215
|
+
container.className = theme
|
|
216
|
+
container.textContent = 'Devtools loaded'
|
|
217
|
+
el.appendChild(container)
|
|
218
|
+
}
|
|
219
|
+
unmount() {
|
|
220
|
+
// cleanup
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Step 1: Create panel from class
|
|
225
|
+
const [MyPanel, NoOpPanel] = createPreactPanel(MyDevtoolsCore)
|
|
226
|
+
|
|
227
|
+
// Step 2: Create plugin from panel
|
|
228
|
+
const [MyPlugin, NoOpPlugin] = createPreactPlugin({
|
|
229
|
+
name: 'My Store',
|
|
230
|
+
Component: MyPanel,
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
// Step 3: Conditional for production
|
|
234
|
+
const ActivePlugin =
|
|
235
|
+
process.env.NODE_ENV === 'development' ? MyPlugin : NoOpPlugin
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Preact-Specific Notes
|
|
239
|
+
|
|
240
|
+
1. **Identical to React API.** `createPreactPlugin` and `createPreactPanel` have the exact same API signatures as their React counterparts. The only difference is the JSX runtime (`preact` vs `react`) and hooks import (`preact/hooks` vs `react`).
|
|
241
|
+
|
|
242
|
+
2. **Use `class` not `className`.** Preact supports both, but idiomatic Preact uses `class` in JSX.
|
|
243
|
+
|
|
244
|
+
3. **No Strict Mode double-mount by default.** Preact does not have React's Strict Mode double-invocation behavior, but the ref guard (`if (devtools.current) return`) is still present and harmless.
|
|
245
|
+
|
|
246
|
+
4. **Default theme is `'dark'`.** If `props.theme` is undefined, the panel defaults to `'dark'`.
|
|
247
|
+
|
|
248
|
+
5. **Same hooks behavior.** The `useEffect` dependency on `[props?.theme]` means the panel re-mounts when theme changes, same as React.
|
|
249
|
+
|
|
250
|
+
## Comparison with React
|
|
251
|
+
|
|
252
|
+
| Aspect | React | Preact |
|
|
253
|
+
| ------------------------- | -------------------------------- | --------------------------------- |
|
|
254
|
+
| Import path | `@tanstack/devtools-utils/react` | `@tanstack/devtools-utils/preact` |
|
|
255
|
+
| JSX types | `react` JSX | `preact` JSX |
|
|
256
|
+
| Hooks import | `react` | `preact/hooks` |
|
|
257
|
+
| API shape | Identical | Identical |
|
|
258
|
+
| `createXPlugin` signature | Same | Same |
|
|
259
|
+
| `createXPanel` signature | Same | Same |
|
|
260
|
+
| `DevtoolsPanelProps` | Same | Same |
|
|
261
|
+
|
|
262
|
+
If you have working React adapter code, converting to Preact is a matter of changing the import paths.
|