Open History to see the last commit
1import {
2 createContext,
3 createEffect,
4 createMemo,
5 createSignal,
6 onCleanup,
7 useContext,
8} from 'solid-js'
9import { clearDelegatedEvents, delegateEvents } from 'solid-js/web'
10import { PIP_DEFAULT_HEIGHT } from '../constants'
11import { useQueryDevtoolsContext } from './QueryDevtoolsContext'
12import type { Accessor, JSX } from 'solid-js'
13import type { StorageObject, StorageSetter } from '@solid-primitives/storage'
14β
15interface PiPProviderProps {
16 children: JSX.Element
17 localStore: StorageObject<string>
18 setLocalStore: StorageSetter<string, unknown>
19 disabled?: boolean
20}
21β
22type PiPContextType = {
23 pipWindow: Window | null
24 requestPipWindow: (width: number, height: number) => void
25 closePipWindow: () => void
26 disabled: boolean
27}
28β
29const PiPContext = createContext<Accessor<PiPContextType> | undefined>(
30 undefined,
31)
32β
33export const PiPProvider = (props: PiPProviderProps) => {
34 // Expose pipWindow that is currently active
35 const [pipWindow, setPipWindow] = createSignal<Window | null>(null)
36β
37 // Close pipWindow programmatically
38 const closePipWindow = () => {
39 const w = pipWindow()
40 if (w != null) {
41 w.close()
42 setPipWindow(null)
43 }
44 }
45β
46 // Open new pipWindow
47 const requestPipWindow = (width: number, height: number) => {
48 // We don't want to allow multiple requests.
49 if (pipWindow() != null) {
50 return
51 }
52β
53 const pip = window.open(
54 '',
55 'TSQD-Devtools-Panel',
56 `width=${width},height=${height},popup`,
57 )
58β
59 if (!pip) {
60 throw new Error(
61 'Failed to open popup. Please allow popups for this site to view the devtools in picture-in-picture mode.',
62 )
63 }
64β
65 // Remove existing styles
66 pip.document.head.innerHTML = ''
67 // Remove existing body
68 pip.document.body.innerHTML = ''
69 // Clear Delegated Events
70 clearDelegatedEvents(pip.document)
71β
72 pip.document.title = 'TanStack Query Devtools'
73 pip.document.body.style.margin = '0'
74β
75 // Detect when window is closed by user
76 pip.addEventListener('pagehide', () => {
77 props.setLocalStore('pip_open', 'false')
78 setPipWindow(null)
79 })
80β
81 // It is important to copy all parent window styles. Otherwise, there would be no CSS available at all
82 // https://developer.chrome.com/docs/web-platform/document-picture-in-picture/#copy-style-sheets-to-the-picture-in-picture-window
83 ;[
84 ...(useQueryDevtoolsContext().shadowDOMTarget || document).styleSheets,
85 ].forEach((styleSheet) => {
86 try {
87 const cssRules = [...styleSheet.cssRules]
88 .map((rule) => rule.cssText)
89 .join('')
90 const style = document.createElement('style')
91 const style_node = styleSheet.ownerNode
92 let style_id = ''
93β
94 if (style_node && 'id' in style_node) {
95 style_id = style_node.id
96 }
97β
98 if (style_id) {
99 style.setAttribute('id', style_id)
100 }
101 style.textContent = cssRules
102 pip.document.head.appendChild(style)
103 } catch (e) {
104 const link = document.createElement('link')
105 if (styleSheet.href == null) {
106 return
107 }
108β
109 link.rel = 'stylesheet'
110 link.type = styleSheet.type
111 link.media = styleSheet.media.toString()
112 link.href = styleSheet.href
113 pip.document.head.appendChild(link)
114 }
115 })
116 delegateEvents(
117 [
118 'focusin',
119 'focusout',
120 'pointermove',
121 'keydown',
122 'pointerdown',
123 'pointerup',
124 'click',
125 'mousedown',
126 'input',
127 ],
128 pip.document,
129 )
130 props.setLocalStore('pip_open', 'true')
131 setPipWindow(pip)
132 }
133β
134 createEffect(() => {
135 const pip_open = (props.localStore.pip_open ?? 'false') as 'true' | 'false'
136 if (pip_open === 'true' && !props.disabled) {
137 requestPipWindow(
138 Number(window.innerWidth),
139 Number(props.localStore.height || PIP_DEFAULT_HEIGHT),
140 )
141 }
142 })
143β
144 createEffect(() => {
145 // Setup mutation observer for goober styles with id `_goober
146 const gooberStyles = (
147 useQueryDevtoolsContext().shadowDOMTarget || document
148 ).querySelector('#_goober')
149 const w = pipWindow()
150 if (gooberStyles && w) {
151 const observer = new MutationObserver(() => {
152 const pip_style = (
153 useQueryDevtoolsContext().shadowDOMTarget || w.document
154 ).querySelector('#_goober')
155 if (pip_style) {
156 pip_style.textContent = gooberStyles.textContent
157 }
158 })
159 observer.observe(gooberStyles, {
160 childList: true, // observe direct children
161 subtree: true, // and lower descendants too
162 characterDataOldValue: true, // pass old data to callback
163 })
164 onCleanup(() => {
165 observer.disconnect()
166 })
167 }
168 })
169β
170 const value = createMemo(() => ({
171 pipWindow: pipWindow(),
172 requestPipWindow,
173 closePipWindow,
174 disabled: props.disabled ?? false,
175 }))
176β
177 return (
178 <PiPContext.Provider value={value}>{props.children}</PiPContext.Provider>
179 )
180}
181β
182export const usePiPWindow = () => {
183 const context = createMemo(() => {
184 const ctx = useContext(PiPContext)
185 if (!ctx) {
186 throw new Error('usePiPWindow must be used within a PiPProvider')
187 }
188 return ctx()
189 })
190 return context
191}