@@ -12,6 +12,14 @@ type PluginRender =
1212 | JSX . Element
1313 | ( ( el : HTMLElement , theme : 'dark' | 'light' ) => JSX . Element )
1414
15+ type TriggerProps = {
16+ theme : 'dark' | 'light'
17+ }
18+
19+ type TriggerRender =
20+ | JSX . Element
21+ | ( ( el : HTMLElement , props : TriggerProps ) => JSX . Element )
22+
1523export type TanStackDevtoolsReactPlugin = Omit <
1624 TanStackDevtoolsPlugin ,
1725 'render' | 'name'
@@ -57,6 +65,24 @@ export type TanStackDevtoolsReactPlugin = Omit<
5765 name : string | PluginRender
5866}
5967
68+ type TanStackDevtoolsReactConfig = Omit <
69+ Partial < TanStackDevtoolsConfig > ,
70+ 'customTrigger'
71+ > & {
72+ /**
73+ * Optional custom trigger component for the devtools.
74+ * It can be a React element or a function that renders one.
75+ *
76+ * Example:
77+ * ```jsx
78+ * {
79+ * customTrigger: <CustomTriggerComponent />,
80+ * }
81+ * ```
82+ */
83+ customTrigger ?: TriggerRender
84+ }
85+
6086export interface TanStackDevtoolsReactInit {
6187 /**
6288 * Array of plugins to be used in the devtools.
@@ -81,7 +107,7 @@ export interface TanStackDevtoolsReactInit {
81107 * initial state of the devtools when it is started for the first time. Afterwards,
82108 * the settings are persisted in local storage and changed through the settings panel.
83109 */
84- config ?: Partial < TanStackDevtoolsConfig >
110+ config ?: TanStackDevtoolsReactConfig
85111 /**
86112 * Configuration for the TanStack Devtools client event bus.
87113 */
@@ -105,6 +131,17 @@ const convertRender = (
105131 } ) )
106132}
107133
134+ const convertTrigger = (
135+ Component : TriggerRender ,
136+ setComponent : React . Dispatch < React . SetStateAction < JSX . Element | null > > ,
137+ e : HTMLElement ,
138+ props : TriggerProps ,
139+ ) => {
140+ const element =
141+ typeof Component === 'function' ? Component ( e , props ) : Component
142+ setComponent ( element )
143+ }
144+
108145export const TanStackDevtools = ( {
109146 plugins,
110147 config,
@@ -118,13 +155,19 @@ export const TanStackDevtools = ({
118155 const [ titleContainers , setTitleContainers ] = useState <
119156 Record < string , HTMLElement >
120157 > ( { } )
158+ const [ triggerContainer , setTriggerContainer ] = useState < HTMLElement | null > (
159+ null ,
160+ )
121161
122162 const [ PluginComponents , setPluginComponents ] = useState <
123163 Record < string , JSX . Element >
124164 > ( { } )
125165 const [ TitleComponents , setTitleComponents ] = useState <
126166 Record < string , JSX . Element >
127167 > ( { } )
168+ const [ TriggerComponent , setTriggerComponent ] = useState < JSX . Element | null > (
169+ null ,
170+ )
128171
129172 const pluginsMap : Array < TanStackDevtoolsPlugin > = useMemo (
130173 ( ) =>
@@ -170,14 +213,22 @@ export const TanStackDevtools = ({
170213 [ plugins ] ,
171214 )
172215
173- const [ devtools ] = useState (
174- ( ) =>
175- new TanStackDevtoolsCore ( {
176- config,
177- eventBusConfig,
178- plugins : pluginsMap ,
179- } ) ,
180- )
216+ const [ devtools ] = useState ( ( ) => {
217+ const { customTrigger, ...coreConfig } = config || { }
218+ return new TanStackDevtoolsCore ( {
219+ config : {
220+ ...coreConfig ,
221+ customTrigger : customTrigger
222+ ? ( el , props ) => {
223+ setTriggerContainer ( el )
224+ convertTrigger ( customTrigger , setTriggerComponent , el , props )
225+ }
226+ : undefined ,
227+ } ,
228+ eventBusConfig,
229+ plugins : pluginsMap ,
230+ } )
231+ } )
181232
182233 useEffect ( ( ) => {
183234 devtools . setConfig ( {
@@ -215,6 +266,10 @@ export const TanStackDevtools = ({
215266 createPortal ( < > { TitleComponents [ key ] } </ > , titleContainer ) ,
216267 )
217268 : null }
269+
270+ { triggerContainer && TriggerComponent
271+ ? createPortal ( < > { TriggerComponent } </ > , triggerContainer )
272+ : null }
218273 </ >
219274 )
220275}
0 commit comments