-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug-toggle.tsx
More file actions
51 lines (45 loc) · 1.38 KB
/
debug-toggle.tsx
File metadata and controls
51 lines (45 loc) · 1.38 KB
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
51
'use client';
import { Button, Tooltip } from '@radix-ui/themes';
import { Wrapper } from '@reactlit/core';
import { Bug, BugOff } from 'lucide-react';
import { createContext, useContext, useState } from 'react';
type DebugContextType = {
debug: boolean;
setDebug: (debug: boolean) => void;
};
const DebugContext = createContext<DebugContextType>({
debug: false,
setDebug: () => {},
});
export const DebugToggle = () => {
const { debug, setDebug } = useContext(DebugContext);
return (
<Tooltip content="Toggle debug">
<Button size="1" variant="ghost" onClick={() => setDebug(!debug)}>
{debug ? <BugOff /> : <Bug />}
</Button>
</Tooltip>
);
};
export const DebugProvider = ({ children }: { children: React.ReactNode }) => {
const [debug, setDebug] = useState(false);
return (
<DebugContext.Provider value={{ debug, setDebug }}>
{children}
</DebugContext.Provider>
);
};
export function useDebug() {
const { debug } = useContext(DebugContext);
return debug;
}
export const Debug: Wrapper = ({ children, stateKey }) => {
const debug = useDebug();
if (!debug) return children;
return (
<div className="grid grid-cols-[auto_1fr] gap-2 items-center border rounded-md mb-2 overflow-hidden">
<div className="min-w-16 p-2 h-full border-r text-xs">{stateKey}</div>
<div className="flex-auto p-2">{children}</div>
</div>
);
};