MCPcopy Create free account
hub / github.com/TanStack/devtools / PiPProvider

Function PiPProvider

packages/devtools/src/context/pip-context.tsx:30–165  ·  view source on GitHub ↗
(props: PiPProviderProps)

Source from the content-addressed store, hash-verified

28
29export const PiPProvider = (props: PiPProviderProps) => {
30 // Expose pipWindow that is currently active
31 const [pipWindow, setPipWindow] = createSignal<Window | null>(null)
32
33 // Close pipWindow programmatically
34 const closePipWindow = () => {
35 const w = pipWindow()
36 if (w != null) {
37 w.close()
38 setPipWindow(null)
39 }
40 }
41
42 // Open new pipWindow
43 const requestPipWindow = (settings: string) => {
44 // We don't want to allow multiple requests.
45 if (pipWindow() != null) {
46 return
47 }
48
49 const pip = window.open('', 'TSDT-Devtools-Panel', `${settings},popup`)
50
51 if (!pip) {
52 throw new Error(
53 'Failed to open popup. Please allow popups for this site to view the devtools in picture-in-picture mode.',
54 )
55 }
56
57 if (import.meta.hot && typeof import.meta.hot.on === 'function') {
58 import.meta.hot.on('vite:beforeUpdate', () => {
59 localStorage.setItem('pip_open', 'false')
60 closePipWindow()
61 })
62 }
63 window.addEventListener('beforeunload', () => {
64 localStorage.setItem('pip_open', 'false')
65 closePipWindow()
66 })
67 // Remove existing styles
68 pip.document.head.innerHTML = ''
69 // Remove existing body
70 pip.document.body.innerHTML = ''
71
72 pip.document.title = 'TanStack Devtools'
73 pip.document.body.style.margin = '0'
74
75 // Detect when window is closed by user
76 pip.addEventListener('pagehide', () => {
77 localStorage.setItem('pip_open', 'false')
78 closePipWindow()
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 ;[...document.styleSheets].forEach((styleSheet) => {
84 try {
85 const cssRules = [...styleSheet.cssRules]
86 .map((rule) => rule.cssText)
87 .join('')

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected