-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c5b54f
commit 6d3ba12
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { stringifyState } from '@zag-js/stringify-state' | ||
import { createEffect } from 'solid-js' | ||
|
||
interface Service { | ||
initialContext: { id: string } | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
state: Record<string, any> | ||
subscribe: (callback: () => void) => () => void | ||
} | ||
|
||
export const useDebugger = (service: Service) => { | ||
createEffect(() => { | ||
const syncPre = () => { | ||
let group = document.getElementById('__zag-debug') | ||
|
||
if (!group) { | ||
group = document.createElement('div') | ||
group.id = '__zag-debug' | ||
Object.assign(group.style, { | ||
position: 'fixed', | ||
top: '0', | ||
right: '0', | ||
zIndex: '9999', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '4px', | ||
boxSizing: 'border-box', | ||
}) | ||
|
||
document.body.appendChild(group) | ||
} | ||
|
||
const id = service.initialContext.id | ||
let pre = document.getElementById(id) | ||
|
||
if (!pre) { | ||
pre = document.createElement('pre') | ||
pre.id = id | ||
Object.assign(pre.style, { | ||
padding: '4px', | ||
background: 'white', | ||
border: '1px solid black', | ||
borderRadius: '4px', | ||
maxHeight: '300px', | ||
overflow: 'auto', | ||
}) | ||
group.appendChild(pre) | ||
} | ||
|
||
pre.textContent = stringifyState(service.state) | ||
|
||
return () => { | ||
pre.remove() | ||
} | ||
} | ||
|
||
const removePre = syncPre() | ||
const unsubscribe = service.subscribe(syncPre) | ||
return () => { | ||
removePre() | ||
unsubscribe() | ||
} | ||
}) | ||
} |