-
Notifications
You must be signed in to change notification settings - Fork 0
/
introspector.ts
50 lines (44 loc) · 1.1 KB
/
introspector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {makeOnce} from "./once.js"
import {render} from "./purity.js"
import type {Rerender} from "./purity.js"
const makeState = <T>(rerender: Rerender) => {
let state: T
const get = () => state
const set = (callback: (s: T) => T) => {
state = callback(state)
rerender()
}
return {set, get}
}
const once = makeOnce()
export const makeIntrospector = (rerender: Rerender, isVisible = false) => {
if (!isVisible) return () => ""
const theState = makeState<boolean>(rerender)
return (data: Record<string, unknown>) => {
once("init-introspector", () => {
theState.set(() => true)
})
const size = theState.get() ? "calc(100% - 20px)" : "30px"
return render`
<pre id="the-introspector" ::click=${() => {
theState.set(s => !s)
}}>
${JSON.stringify(data, null, 2)}
</pre>
<style id="introspector-style">
#the-introspector {
position: fixed;
bottom: 10px;
right: 10px;
width: ${size};
height: ${size};
overflow: auto;
background: rgba(0, 0, 0, 0.75);
color: white;
z-index: 1000;
border: 1px solid red;
}
</style>
`
}
}