@tanstack/preact-devtools 0.9.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 TanStack
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @tanstack/preact-devtools
2
+
3
+ This package is still under active development and might have breaking changes in the future. Please use it with caution.
4
+
5
+ ## Usage
6
+
7
+ ```tsx
8
+ import { TanStackDevtools } from '@tanstack/preact-devtools'
9
+ import { PreactQueryDevtoolsPanel } from '@tanstack/preact-query-devtools'
10
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/preact-router-devtools'
11
+ import { QueryClient, QueryClientProvider } from '@tanstack/preact-query'
12
+ const queryClient = new QueryClient()
13
+ function App() {
14
+ return (
15
+ <QueryClientProvider client={queryClient}>
16
+ <h1>My App</h1>
17
+ <TanStackDevtools
18
+ plugins={[
19
+ {
20
+ name: 'TanStack Query',
21
+ render: <PreactQueryDevtoolsPanel />,
22
+ },
23
+ {
24
+ name: 'TanStack Router',
25
+ render: <TanStackRouterDevtoolsPanel router={router} />,
26
+ },
27
+ ]}
28
+ />
29
+ </QueryClientProvider>
30
+ )
31
+ }
32
+ ```
33
+
34
+ ## Creating plugins
35
+
36
+ In order to create a plugin for TanStack Devtools, you can use the `plugins` prop of the `TanStackDevtools` component. Here's an example of how to create a simple plugin:
37
+
38
+ ```tsx
39
+ import { TanStackDevtools } from '@tanstack/preact-devtools'
40
+
41
+ function App() {
42
+ return (
43
+ <div>
44
+ <h1>My App</h1>
45
+ <TanStackDevtools
46
+ plugins={[
47
+ {
48
+ id: 'your-plugin-id',
49
+ name: 'Your Plugin',
50
+ render: <CustomPluginComponent />,
51
+ },
52
+ ]}
53
+ />
54
+ </div>
55
+ )
56
+ }
57
+ ```
@@ -0,0 +1,94 @@
1
+ import { JSX } from 'preact';
2
+ import { ClientEventBusConfig, TanStackDevtoolsConfig, TanStackDevtoolsPlugin } from '@tanstack/devtools';
3
+ type PluginRender = JSX.Element | ((el: HTMLElement, theme: 'dark' | 'light') => JSX.Element);
4
+ type TriggerProps = {
5
+ theme: 'dark' | 'light';
6
+ };
7
+ type TriggerRender = JSX.Element | ((el: HTMLElement, props: TriggerProps) => JSX.Element);
8
+ export type TanStackDevtoolsPreactPlugin = Omit<TanStackDevtoolsPlugin, 'render' | 'name'> & {
9
+ /**
10
+ * The render function can be a Preact element or a function that returns a Preact element.
11
+ * If it's a function, it will be called to render the plugin, otherwise it will be rendered directly.
12
+ *
13
+ * Example:
14
+ * ```jsx
15
+ * {
16
+ * render: () => <CustomPluginComponent />,
17
+ * }
18
+ * ```
19
+ * or
20
+ * ```jsx
21
+ * {
22
+ * render: <CustomPluginComponent />,
23
+ * }
24
+ * ```
25
+ */
26
+ render: PluginRender;
27
+ /**
28
+ * Name to be displayed in the devtools UI.
29
+ * If a string, it will be used as the plugin name.
30
+ * If a function, it will be called with the mount element.
31
+ *
32
+ * Example:
33
+ * ```jsx
34
+ * {
35
+ * name: "Your Plugin",
36
+ * render: () => <CustomPluginComponent />,
37
+ * }
38
+ * ```
39
+ * or
40
+ * ```jsx
41
+ * {
42
+ * name: <h1>Your Plugin title</h1>,
43
+ * render: () => <CustomPluginComponent />,
44
+ * }
45
+ * ```
46
+ */
47
+ name: string | PluginRender;
48
+ };
49
+ type TanStackDevtoolsPreactConfig = Omit<Partial<TanStackDevtoolsConfig>, 'customTrigger'> & {
50
+ /**
51
+ * Optional custom trigger component for the devtools.
52
+ * It can be a Preact element or a function that renders one.
53
+ *
54
+ * Example:
55
+ * ```jsx
56
+ * {
57
+ * customTrigger: <CustomTriggerComponent />,
58
+ * }
59
+ * ```
60
+ */
61
+ customTrigger?: TriggerRender;
62
+ };
63
+ export interface TanStackDevtoolsPreactInit {
64
+ /**
65
+ * Array of plugins to be used in the devtools.
66
+ * Each plugin should have a `render` function that returns a Preact element or a function
67
+ *
68
+ * Example:
69
+ * ```jsx
70
+ * <TanStackDevtools
71
+ * plugins={[
72
+ * {
73
+ * id: "your-plugin-id",
74
+ * name: "Your Plugin",
75
+ * render: <CustomPluginComponent />,
76
+ * }
77
+ * ]}
78
+ * />
79
+ * ```
80
+ */
81
+ plugins?: Array<TanStackDevtoolsPreactPlugin>;
82
+ /**
83
+ * Configuration for the devtools shell. These configuration options are used to set the
84
+ * initial state of the devtools when it is started for the first time. Afterwards,
85
+ * the settings are persisted in local storage and changed through the settings panel.
86
+ */
87
+ config?: TanStackDevtoolsPreactConfig;
88
+ /**
89
+ * Configuration for the TanStack Devtools client event bus.
90
+ */
91
+ eventBusConfig?: ClientEventBusConfig;
92
+ }
93
+ export declare const TanStackDevtools: ({ plugins, config, eventBusConfig, }: TanStackDevtoolsPreactInit) => JSX.Element | null;
94
+ export {};
@@ -0,0 +1,122 @@
1
+ import { jsxs, Fragment, jsx } from "preact/jsx-runtime";
2
+ import { useRef, useState, useMemo, useEffect } from "preact/hooks";
3
+ import { render } from "preact";
4
+ import { TanStackDevtoolsCore } from "@tanstack/devtools";
5
+ function Portal({
6
+ children,
7
+ container
8
+ }) {
9
+ useEffect(() => {
10
+ render(children, container);
11
+ return () => {
12
+ render(null, container);
13
+ };
14
+ }, [children, container]);
15
+ return null;
16
+ }
17
+ const convertRender = (Component, setComponents, e, theme) => {
18
+ const element = typeof Component === "function" ? Component(e, theme) : Component;
19
+ setComponents((prev) => ({
20
+ ...prev,
21
+ [e.getAttribute("id")]: element
22
+ }));
23
+ };
24
+ const convertTrigger = (Component, setComponent, e, props) => {
25
+ const element = typeof Component === "function" ? Component(e, props) : Component;
26
+ setComponent(element);
27
+ };
28
+ const TanStackDevtools = ({
29
+ plugins,
30
+ config,
31
+ eventBusConfig
32
+ }) => {
33
+ const devToolRef = useRef(null);
34
+ const [pluginContainers, setPluginContainers] = useState({});
35
+ const [titleContainers, setTitleContainers] = useState({});
36
+ const [triggerContainer, setTriggerContainer] = useState(
37
+ null
38
+ );
39
+ const [PluginComponents, setPluginComponents] = useState({});
40
+ const [TitleComponents, setTitleComponents] = useState({});
41
+ const [TriggerComponent, setTriggerComponent] = useState(
42
+ null
43
+ );
44
+ const pluginsMap = useMemo(
45
+ () => plugins?.map((plugin) => {
46
+ return {
47
+ ...plugin,
48
+ name: typeof plugin.name === "string" ? plugin.name : (e, theme) => {
49
+ const id = e.getAttribute("id");
50
+ const target = e.ownerDocument.getElementById(id);
51
+ if (target) {
52
+ setTitleContainers((prev) => ({
53
+ ...prev,
54
+ [id]: e
55
+ }));
56
+ }
57
+ convertRender(
58
+ plugin.name,
59
+ setTitleComponents,
60
+ e,
61
+ theme
62
+ );
63
+ },
64
+ render: (e, theme) => {
65
+ const id = e.getAttribute("id");
66
+ const target = e.ownerDocument.getElementById(id);
67
+ if (target) {
68
+ setPluginContainers((prev) => ({
69
+ ...prev,
70
+ [id]: e
71
+ }));
72
+ }
73
+ convertRender(plugin.render, setPluginComponents, e, theme);
74
+ }
75
+ };
76
+ }) ?? [],
77
+ [plugins]
78
+ );
79
+ const [devtools] = useState(() => {
80
+ const { customTrigger, ...coreConfig } = config || {};
81
+ return new TanStackDevtoolsCore({
82
+ config: {
83
+ ...coreConfig,
84
+ customTrigger: customTrigger ? (el, props) => {
85
+ setTriggerContainer(el);
86
+ convertTrigger(customTrigger, setTriggerComponent, el, props);
87
+ } : void 0
88
+ },
89
+ eventBusConfig,
90
+ plugins: pluginsMap
91
+ });
92
+ });
93
+ useEffect(() => {
94
+ devtools.setConfig({
95
+ plugins: pluginsMap
96
+ });
97
+ }, [devtools, pluginsMap]);
98
+ useEffect(() => {
99
+ if (devToolRef.current) {
100
+ devtools.mount(devToolRef.current);
101
+ }
102
+ return () => devtools.unmount();
103
+ }, [devtools]);
104
+ const hasPlugins = Object.values(pluginContainers).length > 0 && Object.values(PluginComponents).length > 0;
105
+ const hasTitles = Object.values(titleContainers).length > 0 && Object.values(TitleComponents).length > 0;
106
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
107
+ /* @__PURE__ */ jsx("div", { style: { position: "absolute" }, ref: devToolRef }),
108
+ hasPlugins ? Object.entries(pluginContainers).map(([key, pluginContainer]) => {
109
+ const component = PluginComponents[key];
110
+ return component ? /* @__PURE__ */ jsx(Portal, { container: pluginContainer, children: component }, key) : null;
111
+ }) : null,
112
+ hasTitles ? Object.entries(titleContainers).map(([key, titleContainer]) => {
113
+ const component = TitleComponents[key];
114
+ return component ? /* @__PURE__ */ jsx(Portal, { container: titleContainer, children: component }, key) : null;
115
+ }) : null,
116
+ triggerContainer && TriggerComponent ? /* @__PURE__ */ jsx(Portal, { container: triggerContainer, children: TriggerComponent }) : null
117
+ ] });
118
+ };
119
+ export {
120
+ TanStackDevtools
121
+ };
122
+ //# sourceMappingURL=devtools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devtools.js","sources":["../../src/devtools.tsx"],"sourcesContent":["import { useEffect, useMemo, useRef, useState } from 'preact/hooks'\nimport { render } from 'preact'\nimport { TanStackDevtoolsCore } from '@tanstack/devtools'\nimport type { JSX } from 'preact'\nimport type {\n ClientEventBusConfig,\n TanStackDevtoolsConfig,\n TanStackDevtoolsPlugin,\n} from '@tanstack/devtools'\n\ntype PluginRender =\n | JSX.Element\n | ((el: HTMLElement, theme: 'dark' | 'light') => JSX.Element)\n\ntype TriggerProps = {\n theme: 'dark' | 'light'\n}\n\ntype TriggerRender =\n | JSX.Element\n | ((el: HTMLElement, props: TriggerProps) => JSX.Element)\n\nexport type TanStackDevtoolsPreactPlugin = Omit<\n TanStackDevtoolsPlugin,\n 'render' | 'name'\n> & {\n /**\n * The render function can be a Preact element or a function that returns a Preact element.\n * If it's a function, it will be called to render the plugin, otherwise it will be rendered directly.\n *\n * Example:\n * ```jsx\n * {\n * render: () => <CustomPluginComponent />,\n * }\n * ```\n * or\n * ```jsx\n * {\n * render: <CustomPluginComponent />,\n * }\n * ```\n */\n render: PluginRender\n /**\n * Name to be displayed in the devtools UI.\n * If a string, it will be used as the plugin name.\n * If a function, it will be called with the mount element.\n *\n * Example:\n * ```jsx\n * {\n * name: \"Your Plugin\",\n * render: () => <CustomPluginComponent />,\n * }\n * ```\n * or\n * ```jsx\n * {\n * name: <h1>Your Plugin title</h1>,\n * render: () => <CustomPluginComponent />,\n * }\n * ```\n */\n name: string | PluginRender\n}\n\ntype TanStackDevtoolsPreactConfig = Omit<\n Partial<TanStackDevtoolsConfig>,\n 'customTrigger'\n> & {\n /**\n * Optional custom trigger component for the devtools.\n * It can be a Preact element or a function that renders one.\n *\n * Example:\n * ```jsx\n * {\n * customTrigger: <CustomTriggerComponent />,\n * }\n * ```\n */\n customTrigger?: TriggerRender\n}\n\nexport interface TanStackDevtoolsPreactInit {\n /**\n * Array of plugins to be used in the devtools.\n * Each plugin should have a `render` function that returns a Preact element or a function\n *\n * Example:\n * ```jsx\n * <TanStackDevtools\n * plugins={[\n * {\n * id: \"your-plugin-id\",\n * name: \"Your Plugin\",\n * render: <CustomPluginComponent />,\n * }\n * ]}\n * />\n * ```\n */\n plugins?: Array<TanStackDevtoolsPreactPlugin>\n /**\n * Configuration for the devtools shell. These configuration options are used to set the\n * initial state of the devtools when it is started for the first time. Afterwards,\n * the settings are persisted in local storage and changed through the settings panel.\n */\n config?: TanStackDevtoolsPreactConfig\n /**\n * Configuration for the TanStack Devtools client event bus.\n */\n eventBusConfig?: ClientEventBusConfig\n}\n\n// Simple portal component for Preact\nfunction Portal({\n children,\n container,\n}: {\n children: JSX.Element\n container: HTMLElement\n}) {\n useEffect(() => {\n render(children, container)\n return () => {\n render(null, container)\n }\n }, [children, container])\n\n return null\n}\n\nconst convertRender = (\n Component: PluginRender,\n setComponents: (\n value:\n | Record<string, JSX.Element>\n | ((prev: Record<string, JSX.Element>) => Record<string, JSX.Element>),\n ) => void,\n e: HTMLElement,\n theme: 'dark' | 'light',\n) => {\n const element =\n typeof Component === 'function' ? Component(e, theme) : Component\n\n setComponents((prev) => ({\n ...prev,\n [e.getAttribute('id') as string]: element,\n }))\n}\n\nconst convertTrigger = (\n Component: TriggerRender,\n setComponent: (component: JSX.Element | null) => void,\n e: HTMLElement,\n props: TriggerProps,\n) => {\n const element =\n typeof Component === 'function' ? Component(e, props) : Component\n setComponent(element)\n}\n\nexport const TanStackDevtools = ({\n plugins,\n config,\n eventBusConfig,\n}: TanStackDevtoolsPreactInit): JSX.Element | null => {\n const devToolRef = useRef<HTMLDivElement>(null)\n\n const [pluginContainers, setPluginContainers] = useState<\n Record<string, HTMLElement>\n >({})\n const [titleContainers, setTitleContainers] = useState<\n Record<string, HTMLElement>\n >({})\n const [triggerContainer, setTriggerContainer] = useState<HTMLElement | null>(\n null,\n )\n\n const [PluginComponents, setPluginComponents] = useState<\n Record<string, JSX.Element>\n >({})\n const [TitleComponents, setTitleComponents] = useState<\n Record<string, JSX.Element>\n >({})\n const [TriggerComponent, setTriggerComponent] = useState<JSX.Element | null>(\n null,\n )\n\n const pluginsMap: Array<TanStackDevtoolsPlugin> = useMemo(\n () =>\n plugins?.map((plugin) => {\n return {\n ...plugin,\n name:\n typeof plugin.name === 'string'\n ? plugin.name\n : (e, theme) => {\n const id = e.getAttribute('id')!\n const target = e.ownerDocument.getElementById(id)\n\n if (target) {\n setTitleContainers((prev) => ({\n ...prev,\n [id]: e,\n }))\n }\n\n convertRender(\n plugin.name as PluginRender,\n setTitleComponents,\n e,\n theme,\n )\n },\n render: (e, theme) => {\n const id = e.getAttribute('id')!\n const target = e.ownerDocument.getElementById(id)\n\n if (target) {\n setPluginContainers((prev) => ({\n ...prev,\n [id]: e,\n }))\n }\n\n convertRender(plugin.render, setPluginComponents, e, theme)\n },\n }\n }) ?? [],\n [plugins],\n )\n\n const [devtools] = useState(() => {\n const { customTrigger, ...coreConfig } = config || {}\n return new TanStackDevtoolsCore({\n config: {\n ...coreConfig,\n customTrigger: customTrigger\n ? (el, props) => {\n setTriggerContainer(el)\n convertTrigger(customTrigger, setTriggerComponent, el, props)\n }\n : undefined,\n },\n eventBusConfig,\n plugins: pluginsMap,\n })\n })\n\n useEffect(() => {\n devtools.setConfig({\n plugins: pluginsMap,\n })\n }, [devtools, pluginsMap])\n\n useEffect(() => {\n if (devToolRef.current) {\n devtools.mount(devToolRef.current)\n }\n\n return () => devtools.unmount()\n }, [devtools])\n\n const hasPlugins =\n Object.values(pluginContainers).length > 0 &&\n Object.values(PluginComponents).length > 0\n const hasTitles =\n Object.values(titleContainers).length > 0 &&\n Object.values(TitleComponents).length > 0\n\n return (\n <>\n <div style={{ position: 'absolute' }} ref={devToolRef} />\n\n {hasPlugins\n ? Object.entries(pluginContainers).map(([key, pluginContainer]) => {\n const component = PluginComponents[key]\n return component ? (\n <Portal key={key} container={pluginContainer}>\n {component}\n </Portal>\n ) : null\n })\n : null}\n\n {hasTitles\n ? Object.entries(titleContainers).map(([key, titleContainer]) => {\n const component = TitleComponents[key]\n return component ? (\n <Portal key={key} container={titleContainer}>\n {component}\n </Portal>\n ) : null\n })\n : null}\n\n {triggerContainer && TriggerComponent ? (\n <Portal container={triggerContainer}>{TriggerComponent}</Portal>\n ) : null}\n </>\n )\n}\n"],"names":[],"mappings":";;;;AAqHA,SAAS,OAAO;AAAA,EACd;AAAA,EACA;AACF,GAGG;AACD,YAAU,MAAM;AACd,WAAO,UAAU,SAAS;AAC1B,WAAO,MAAM;AACX,aAAO,MAAM,SAAS;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,UAAU,SAAS,CAAC;AAExB,SAAO;AACT;AAEA,MAAM,gBAAgB,CACpB,WACA,eAKA,GACA,UACG;AACH,QAAM,UACJ,OAAO,cAAc,aAAa,UAAU,GAAG,KAAK,IAAI;AAE1D,gBAAc,CAAC,UAAU;AAAA,IACvB,GAAG;AAAA,IACH,CAAC,EAAE,aAAa,IAAI,CAAW,GAAG;AAAA,EAAA,EAClC;AACJ;AAEA,MAAM,iBAAiB,CACrB,WACA,cACA,GACA,UACG;AACH,QAAM,UACJ,OAAO,cAAc,aAAa,UAAU,GAAG,KAAK,IAAI;AAC1D,eAAa,OAAO;AACtB;AAEO,MAAM,mBAAmB,CAAC;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AACF,MAAsD;AACpD,QAAM,aAAa,OAAuB,IAAI;AAE9C,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,SAE9C,CAAA,CAAE;AACJ,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SAE5C,CAAA,CAAE;AACJ,QAAM,CAAC,kBAAkB,mBAAmB,IAAI;AAAA,IAC9C;AAAA,EAAA;AAGF,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,SAE9C,CAAA,CAAE;AACJ,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SAE5C,CAAA,CAAE;AACJ,QAAM,CAAC,kBAAkB,mBAAmB,IAAI;AAAA,IAC9C;AAAA,EAAA;AAGF,QAAM,aAA4C;AAAA,IAChD,MACE,SAAS,IAAI,CAAC,WAAW;AACvB,aAAO;AAAA,QACL,GAAG;AAAA,QACH,MACE,OAAO,OAAO,SAAS,WACnB,OAAO,OACP,CAAC,GAAG,UAAU;AACZ,gBAAM,KAAK,EAAE,aAAa,IAAI;AAC9B,gBAAM,SAAS,EAAE,cAAc,eAAe,EAAE;AAEhD,cAAI,QAAQ;AACV,+BAAmB,CAAC,UAAU;AAAA,cAC5B,GAAG;AAAA,cACH,CAAC,EAAE,GAAG;AAAA,YAAA,EACN;AAAA,UACJ;AAEA;AAAA,YACE,OAAO;AAAA,YACP;AAAA,YACA;AAAA,YACA;AAAA,UAAA;AAAA,QAEJ;AAAA,QACN,QAAQ,CAAC,GAAG,UAAU;AACpB,gBAAM,KAAK,EAAE,aAAa,IAAI;AAC9B,gBAAM,SAAS,EAAE,cAAc,eAAe,EAAE;AAEhD,cAAI,QAAQ;AACV,gCAAoB,CAAC,UAAU;AAAA,cAC7B,GAAG;AAAA,cACH,CAAC,EAAE,GAAG;AAAA,YAAA,EACN;AAAA,UACJ;AAEA,wBAAc,OAAO,QAAQ,qBAAqB,GAAG,KAAK;AAAA,QAC5D;AAAA,MAAA;AAAA,IAEJ,CAAC,KAAK,CAAA;AAAA,IACR,CAAC,OAAO;AAAA,EAAA;AAGV,QAAM,CAAC,QAAQ,IAAI,SAAS,MAAM;AAChC,UAAM,EAAE,eAAe,GAAG,WAAA,IAAe,UAAU,CAAA;AACnD,WAAO,IAAI,qBAAqB;AAAA,MAC9B,QAAQ;AAAA,QACN,GAAG;AAAA,QACH,eAAe,gBACX,CAAC,IAAI,UAAU;AACb,8BAAoB,EAAE;AACtB,yBAAe,eAAe,qBAAqB,IAAI,KAAK;AAAA,QAC9D,IACA;AAAA,MAAA;AAAA,MAEN;AAAA,MACA,SAAS;AAAA,IAAA,CACV;AAAA,EACH,CAAC;AAED,YAAU,MAAM;AACd,aAAS,UAAU;AAAA,MACjB,SAAS;AAAA,IAAA,CACV;AAAA,EACH,GAAG,CAAC,UAAU,UAAU,CAAC;AAEzB,YAAU,MAAM;AACd,QAAI,WAAW,SAAS;AACtB,eAAS,MAAM,WAAW,OAAO;AAAA,IACnC;AAEA,WAAO,MAAM,SAAS,QAAA;AAAA,EACxB,GAAG,CAAC,QAAQ,CAAC;AAEb,QAAM,aACJ,OAAO,OAAO,gBAAgB,EAAE,SAAS,KACzC,OAAO,OAAO,gBAAgB,EAAE,SAAS;AAC3C,QAAM,YACJ,OAAO,OAAO,eAAe,EAAE,SAAS,KACxC,OAAO,OAAO,eAAe,EAAE,SAAS;AAE1C,SACE,qBAAA,UAAA,EACE,UAAA;AAAA,IAAA,oBAAC,SAAI,OAAO,EAAE,UAAU,WAAA,GAAc,KAAK,YAAY;AAAA,IAEtD,aACG,OAAO,QAAQ,gBAAgB,EAAE,IAAI,CAAC,CAAC,KAAK,eAAe,MAAM;AAC/D,YAAM,YAAY,iBAAiB,GAAG;AACtC,aAAO,YACL,oBAAC,QAAA,EAAiB,WAAW,iBAC1B,UAAA,UAAA,GADU,GAEb,IACE;AAAA,IACN,CAAC,IACD;AAAA,IAEH,YACG,OAAO,QAAQ,eAAe,EAAE,IAAI,CAAC,CAAC,KAAK,cAAc,MAAM;AAC7D,YAAM,YAAY,gBAAgB,GAAG;AACrC,aAAO,YACL,oBAAC,QAAA,EAAiB,WAAW,gBAC1B,UAAA,UAAA,GADU,GAEb,IACE;AAAA,IACN,CAAC,IACD;AAAA,IAEH,oBAAoB,mBACnB,oBAAC,UAAO,WAAW,kBAAmB,4BAAiB,IACrD;AAAA,EAAA,GACN;AAEJ;"}
@@ -0,0 +1,3 @@
1
+ import * as Devtools from './devtools.js';
2
+ export declare const TanStackDevtools: ({ plugins, config, eventBusConfig, }: Devtools.TanStackDevtoolsPreactInit) => import("preact").JSX.Element | null;
3
+ export type { TanStackDevtoolsPreactPlugin, TanStackDevtoolsPreactInit, } from './devtools.js';
@@ -0,0 +1,7 @@
1
+ "use client";
2
+ import { TanStackDevtools as TanStackDevtools$1 } from "./devtools.js";
3
+ const TanStackDevtools = TanStackDevtools$1;
4
+ export {
5
+ TanStackDevtools
6
+ };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["'use client'\n\nimport * as Devtools from './devtools'\n\nexport const TanStackDevtools = Devtools.TanStackDevtools\n\nexport type {\n TanStackDevtoolsPreactPlugin,\n TanStackDevtoolsPreactInit,\n} from './devtools'\n"],"names":[],"mappings":";;AAIO;;;;"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@tanstack/preact-devtools",
3
+ "version": "0.9.0",
4
+ "description": "TanStack Devtools is a set of tools for building advanced devtools for your Preact application.",
5
+ "author": "Tanner Linsley",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/TanStack/devtools.git",
10
+ "directory": "packages/preact-devtools"
11
+ },
12
+ "homepage": "https://tanstack.com/devtools",
13
+ "funding": {
14
+ "type": "github",
15
+ "url": "https://github.com/sponsors/tannerlinsley"
16
+ },
17
+ "keywords": [
18
+ "preact",
19
+ "devtools"
20
+ ],
21
+ "type": "module",
22
+ "types": "dist/esm/index.d.ts",
23
+ "module": "dist/esm/index.js",
24
+ "exports": {
25
+ ".": {
26
+ "import": {
27
+ "types": "./dist/esm/index.d.ts",
28
+ "default": "./dist/esm/index.js"
29
+ }
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "sideEffects": false,
34
+ "engines": {
35
+ "node": ">=18"
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "src"
40
+ ],
41
+ "dependencies": {
42
+ "@tanstack/devtools": "0.8.2"
43
+ },
44
+ "devDependencies": {
45
+ "@preact/preset-vite": "^2.10.2",
46
+ "eslint-plugin-react-hooks": "^7.0.1",
47
+ "preact": "^10.28.0"
48
+ },
49
+ "peerDependencies": {
50
+ "preact": ">=10.0.0"
51
+ },
52
+ "scripts": {
53
+ "clean": "premove ./build ./dist",
54
+ "test:eslint": "eslint ./src",
55
+ "test:lib": "vitest --passWithNoTests",
56
+ "test:lib:dev": "pnpm test:lib --watch",
57
+ "test:types": "tsc",
58
+ "test:build": "publint --strict",
59
+ "build": "vite build"
60
+ }
61
+ }
@@ -0,0 +1,305 @@
1
+ import { useEffect, useMemo, useRef, useState } from 'preact/hooks'
2
+ import { render } from 'preact'
3
+ import { TanStackDevtoolsCore } from '@tanstack/devtools'
4
+ import type { JSX } from 'preact'
5
+ import type {
6
+ ClientEventBusConfig,
7
+ TanStackDevtoolsConfig,
8
+ TanStackDevtoolsPlugin,
9
+ } from '@tanstack/devtools'
10
+
11
+ type PluginRender =
12
+ | JSX.Element
13
+ | ((el: HTMLElement, theme: 'dark' | 'light') => JSX.Element)
14
+
15
+ type TriggerProps = {
16
+ theme: 'dark' | 'light'
17
+ }
18
+
19
+ type TriggerRender =
20
+ | JSX.Element
21
+ | ((el: HTMLElement, props: TriggerProps) => JSX.Element)
22
+
23
+ export type TanStackDevtoolsPreactPlugin = Omit<
24
+ TanStackDevtoolsPlugin,
25
+ 'render' | 'name'
26
+ > & {
27
+ /**
28
+ * The render function can be a Preact element or a function that returns a Preact element.
29
+ * If it's a function, it will be called to render the plugin, otherwise it will be rendered directly.
30
+ *
31
+ * Example:
32
+ * ```jsx
33
+ * {
34
+ * render: () => <CustomPluginComponent />,
35
+ * }
36
+ * ```
37
+ * or
38
+ * ```jsx
39
+ * {
40
+ * render: <CustomPluginComponent />,
41
+ * }
42
+ * ```
43
+ */
44
+ render: PluginRender
45
+ /**
46
+ * Name to be displayed in the devtools UI.
47
+ * If a string, it will be used as the plugin name.
48
+ * If a function, it will be called with the mount element.
49
+ *
50
+ * Example:
51
+ * ```jsx
52
+ * {
53
+ * name: "Your Plugin",
54
+ * render: () => <CustomPluginComponent />,
55
+ * }
56
+ * ```
57
+ * or
58
+ * ```jsx
59
+ * {
60
+ * name: <h1>Your Plugin title</h1>,
61
+ * render: () => <CustomPluginComponent />,
62
+ * }
63
+ * ```
64
+ */
65
+ name: string | PluginRender
66
+ }
67
+
68
+ type TanStackDevtoolsPreactConfig = Omit<
69
+ Partial<TanStackDevtoolsConfig>,
70
+ 'customTrigger'
71
+ > & {
72
+ /**
73
+ * Optional custom trigger component for the devtools.
74
+ * It can be a Preact element or a function that renders one.
75
+ *
76
+ * Example:
77
+ * ```jsx
78
+ * {
79
+ * customTrigger: <CustomTriggerComponent />,
80
+ * }
81
+ * ```
82
+ */
83
+ customTrigger?: TriggerRender
84
+ }
85
+
86
+ export interface TanStackDevtoolsPreactInit {
87
+ /**
88
+ * Array of plugins to be used in the devtools.
89
+ * Each plugin should have a `render` function that returns a Preact element or a function
90
+ *
91
+ * Example:
92
+ * ```jsx
93
+ * <TanStackDevtools
94
+ * plugins={[
95
+ * {
96
+ * id: "your-plugin-id",
97
+ * name: "Your Plugin",
98
+ * render: <CustomPluginComponent />,
99
+ * }
100
+ * ]}
101
+ * />
102
+ * ```
103
+ */
104
+ plugins?: Array<TanStackDevtoolsPreactPlugin>
105
+ /**
106
+ * Configuration for the devtools shell. These configuration options are used to set the
107
+ * initial state of the devtools when it is started for the first time. Afterwards,
108
+ * the settings are persisted in local storage and changed through the settings panel.
109
+ */
110
+ config?: TanStackDevtoolsPreactConfig
111
+ /**
112
+ * Configuration for the TanStack Devtools client event bus.
113
+ */
114
+ eventBusConfig?: ClientEventBusConfig
115
+ }
116
+
117
+ // Simple portal component for Preact
118
+ function Portal({
119
+ children,
120
+ container,
121
+ }: {
122
+ children: JSX.Element
123
+ container: HTMLElement
124
+ }) {
125
+ useEffect(() => {
126
+ render(children, container)
127
+ return () => {
128
+ render(null, container)
129
+ }
130
+ }, [children, container])
131
+
132
+ return null
133
+ }
134
+
135
+ const convertRender = (
136
+ Component: PluginRender,
137
+ setComponents: (
138
+ value:
139
+ | Record<string, JSX.Element>
140
+ | ((prev: Record<string, JSX.Element>) => Record<string, JSX.Element>),
141
+ ) => void,
142
+ e: HTMLElement,
143
+ theme: 'dark' | 'light',
144
+ ) => {
145
+ const element =
146
+ typeof Component === 'function' ? Component(e, theme) : Component
147
+
148
+ setComponents((prev) => ({
149
+ ...prev,
150
+ [e.getAttribute('id') as string]: element,
151
+ }))
152
+ }
153
+
154
+ const convertTrigger = (
155
+ Component: TriggerRender,
156
+ setComponent: (component: JSX.Element | null) => void,
157
+ e: HTMLElement,
158
+ props: TriggerProps,
159
+ ) => {
160
+ const element =
161
+ typeof Component === 'function' ? Component(e, props) : Component
162
+ setComponent(element)
163
+ }
164
+
165
+ export const TanStackDevtools = ({
166
+ plugins,
167
+ config,
168
+ eventBusConfig,
169
+ }: TanStackDevtoolsPreactInit): JSX.Element | null => {
170
+ const devToolRef = useRef<HTMLDivElement>(null)
171
+
172
+ const [pluginContainers, setPluginContainers] = useState<
173
+ Record<string, HTMLElement>
174
+ >({})
175
+ const [titleContainers, setTitleContainers] = useState<
176
+ Record<string, HTMLElement>
177
+ >({})
178
+ const [triggerContainer, setTriggerContainer] = useState<HTMLElement | null>(
179
+ null,
180
+ )
181
+
182
+ const [PluginComponents, setPluginComponents] = useState<
183
+ Record<string, JSX.Element>
184
+ >({})
185
+ const [TitleComponents, setTitleComponents] = useState<
186
+ Record<string, JSX.Element>
187
+ >({})
188
+ const [TriggerComponent, setTriggerComponent] = useState<JSX.Element | null>(
189
+ null,
190
+ )
191
+
192
+ const pluginsMap: Array<TanStackDevtoolsPlugin> = useMemo(
193
+ () =>
194
+ plugins?.map((plugin) => {
195
+ return {
196
+ ...plugin,
197
+ name:
198
+ typeof plugin.name === 'string'
199
+ ? plugin.name
200
+ : (e, theme) => {
201
+ const id = e.getAttribute('id')!
202
+ const target = e.ownerDocument.getElementById(id)
203
+
204
+ if (target) {
205
+ setTitleContainers((prev) => ({
206
+ ...prev,
207
+ [id]: e,
208
+ }))
209
+ }
210
+
211
+ convertRender(
212
+ plugin.name as PluginRender,
213
+ setTitleComponents,
214
+ e,
215
+ theme,
216
+ )
217
+ },
218
+ render: (e, theme) => {
219
+ const id = e.getAttribute('id')!
220
+ const target = e.ownerDocument.getElementById(id)
221
+
222
+ if (target) {
223
+ setPluginContainers((prev) => ({
224
+ ...prev,
225
+ [id]: e,
226
+ }))
227
+ }
228
+
229
+ convertRender(plugin.render, setPluginComponents, e, theme)
230
+ },
231
+ }
232
+ }) ?? [],
233
+ [plugins],
234
+ )
235
+
236
+ const [devtools] = useState(() => {
237
+ const { customTrigger, ...coreConfig } = config || {}
238
+ return new TanStackDevtoolsCore({
239
+ config: {
240
+ ...coreConfig,
241
+ customTrigger: customTrigger
242
+ ? (el, props) => {
243
+ setTriggerContainer(el)
244
+ convertTrigger(customTrigger, setTriggerComponent, el, props)
245
+ }
246
+ : undefined,
247
+ },
248
+ eventBusConfig,
249
+ plugins: pluginsMap,
250
+ })
251
+ })
252
+
253
+ useEffect(() => {
254
+ devtools.setConfig({
255
+ plugins: pluginsMap,
256
+ })
257
+ }, [devtools, pluginsMap])
258
+
259
+ useEffect(() => {
260
+ if (devToolRef.current) {
261
+ devtools.mount(devToolRef.current)
262
+ }
263
+
264
+ return () => devtools.unmount()
265
+ }, [devtools])
266
+
267
+ const hasPlugins =
268
+ Object.values(pluginContainers).length > 0 &&
269
+ Object.values(PluginComponents).length > 0
270
+ const hasTitles =
271
+ Object.values(titleContainers).length > 0 &&
272
+ Object.values(TitleComponents).length > 0
273
+
274
+ return (
275
+ <>
276
+ <div style={{ position: 'absolute' }} ref={devToolRef} />
277
+
278
+ {hasPlugins
279
+ ? Object.entries(pluginContainers).map(([key, pluginContainer]) => {
280
+ const component = PluginComponents[key]
281
+ return component ? (
282
+ <Portal key={key} container={pluginContainer}>
283
+ {component}
284
+ </Portal>
285
+ ) : null
286
+ })
287
+ : null}
288
+
289
+ {hasTitles
290
+ ? Object.entries(titleContainers).map(([key, titleContainer]) => {
291
+ const component = TitleComponents[key]
292
+ return component ? (
293
+ <Portal key={key} container={titleContainer}>
294
+ {component}
295
+ </Portal>
296
+ ) : null
297
+ })
298
+ : null}
299
+
300
+ {triggerContainer && TriggerComponent ? (
301
+ <Portal container={triggerContainer}>{TriggerComponent}</Portal>
302
+ ) : null}
303
+ </>
304
+ )
305
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+
3
+ import * as Devtools from './devtools'
4
+
5
+ export const TanStackDevtools = Devtools.TanStackDevtools
6
+
7
+ export type {
8
+ TanStackDevtoolsPreactPlugin,
9
+ TanStackDevtoolsPreactInit,
10
+ } from './devtools'