Open History to see the last commit
1import { TanStackDevtoolsCore } from '@tanstack/devtools'
2import {
3 createEffect,
4 createMemo,
5 createSignal,
6 onCleanup,
7 onMount,
8} from 'solid-js'
9import { Portal } from 'solid-js/web'
10import type { JSX } from 'solid-js'
11import type {
12 ClientEventBusConfig,
13 TanStackDevtoolsConfig,
14 TanStackDevtoolsPlugin,
15 TanStackDevtoolsPluginProps,
16 TanStackDevtoolsTheme,
17} from '@tanstack/devtools'
18β
19type SolidPluginRender =
20 | JSX.Element
21 | ((
22 el: HTMLDivElement | HTMLHeadingElement,
23 props: TanStackDevtoolsPluginProps,
24 ) => JSX.Element)
25const convertRender = (
26 el: HTMLDivElement | HTMLHeadingElement,
27 Component: SolidPluginRender,
28 props: TanStackDevtoolsPluginProps,
29) => (
30 <Portal mount={el}>
31 {typeof Component === 'function' ? Component(el, props) : Component}
32 </Portal>
33)
34β
35export type TanStackDevtoolsSolidPlugin = Omit<
36 TanStackDevtoolsPlugin,
37 'render' | 'name'
38> & {
39 /**
40 * The render function can be a SolidJS element or a function that returns a SolidJS element.
41 * If it's a function, it will be called to render the plugin, otherwise it will be rendered directly.
42 *
43 * Example:
44 * ```ts
45 * {
46 * render: () => <CustomPluginComponent />,
47 * }
48 * ```
49 * or
50 * ```ts
51 * {
52 * render: <CustomPluginComponent />,
53 * }
54 * ```
55 */
56 render: SolidPluginRender
57 /**
58 * Name to be displayed in the devtools UI.
59 * If a string, it will be used as the plugin name.
60 * If a function, it will be called with the mount element.
61 *
62 * Example:
63 * ```ts
64 * {
65 * name: "Your Plugin",
66 * render: () => <CustomPluginComponent />,
67 * }
68 * ```
69 * or
70 * ```ts
71 * {
72 * name: <h1>Your Plugin title</h1>,
73 * render: () => <CustomPluginComponent />,
74 * }
75 * ```
76 */
77 name: string | SolidPluginRender
78}
79interface TriggerProps {
80 theme: TanStackDevtoolsTheme
81}
82export interface TanStackDevtoolsInit {
83 /**
84 * Array of plugins to be used in the devtools.
85 * Each plugin should have a `render` function that returns a React element or a function
86 *
87 * Example:
88 * ```jsx
89 * <TanStackDevtools
90 * plugins={[
91 * {
92 * id: "your-plugin-id",
93 * name: "Your Plugin",
94 * render: <CustomPluginComponent />,
95 * }
96 * ]}
97 * />
98 * ```
99 */
100 plugins?: Array<TanStackDevtoolsSolidPlugin>
101 /**
102 * Configuration for the devtools shell. These configuration options are used to set the
103 * initial state of the devtools when it is started for the first time. Afterwards,
104 * the settings are persisted in local storage and changed through the settings panel.
105 */
106 config?: Omit<Partial<TanStackDevtoolsConfig>, 'customTrigger'> & {
107 /**
108 * An optional custom function to render the dev tools trigger component.
109 */
110 customTrigger?:
111 | ((el: HTMLElement, props: TriggerProps) => JSX.Element)
112 | JSX.Element
113 }
114 /**
115 * Configuration for the TanStack Devtools client event bus.
116 */
117 eventBusConfig?: ClientEventBusConfig
118}
119β
120export default function SolidDevtoolsCore({
121 config,
122 plugins,
123 eventBusConfig,
124}: TanStackDevtoolsInit) {
125 // Convert plugins to the format expected by the core
126 const pluginsMap = createMemo<Array<TanStackDevtoolsPlugin> | undefined>(() =>
127 plugins?.map((plugin) => ({
128 ...plugin,
129 name:
130 typeof plugin.name === 'string'
131 ? plugin.name
132 : // The check above confirms that `plugin.name` is of Render type
133 (el, props) =>
134 convertRender(el, plugin.name as SolidPluginRender, props),
135 render: (el: HTMLDivElement, props: TanStackDevtoolsPluginProps) =>
136 convertRender(el, plugin.render, props),
137 })),
138 )
139β
140 const convertTrigger = (el: HTMLElement, props: TriggerProps) => {
141 const Trigger = config?.customTrigger
142β
143 return (
144 <Portal mount={el}>
145 {typeof Trigger === 'function' ? Trigger(el, props) : Trigger}
146 </Portal>
147 )
148 }
149 const [devtools] = createSignal(
150 new TanStackDevtoolsCore({
151 config: {
152 ...config,
153 customTrigger: config?.customTrigger
154 ? (el, props) => convertTrigger(el, props)
155 : undefined,
156 },
157 eventBusConfig,
158 plugins: pluginsMap(),
159 }),
160 )
161 let devToolRef: HTMLDivElement | undefined
162β
163 createEffect(() => {
164 devtools().setConfig({
165 config: {
166 ...config,
167 customTrigger: config?.customTrigger
168 ? (el, props) => convertTrigger(el, props)
169 : undefined,
170 },
171 })
172 })
173β
174 // Update plugins when they change
175 createEffect(() => {
176 const currentPlugins = pluginsMap()
177 if (currentPlugins) {
178 devtools().setConfig({ plugins: currentPlugins })
179 }
180 })
181β
182 onMount(() => {
183 if (devToolRef) {
184 devtools().mount(devToolRef)
185β
186 onCleanup(() => {
187 devtools().unmount()
188 })
189 }
190 })
191 return <div style={{ position: 'absolute' }} ref={devToolRef} />
192}