@@ -25,11 +25,20 @@ import {
25
25
} from '@theme-ui/editor'
26
26
import { Theme } from '@theme-ui/css'
27
27
28
- const runScript = ( script : any ) =>
28
+ interface DevToolsExceptionInfo {
29
+ isError : boolean
30
+ code : string
31
+ description : string
32
+ details : any [ ]
33
+ isException : boolean
34
+ value : string
35
+ }
36
+
37
+ const runScript = ( script : string ) =>
29
38
new Promise ( ( resolve , reject ) => {
30
39
debounce ( window . chrome . devtools . inspectedWindow . eval , 100 ) (
31
40
script ,
32
- ( result : any , err : Error ) => {
41
+ ( result : object , err : DevToolsExceptionInfo ) => {
33
42
if ( err ) {
34
43
console . error ( err )
35
44
reject ( err )
@@ -39,7 +48,9 @@ const runScript = (script: any) =>
39
48
)
40
49
} )
41
50
42
- const mergeState = ( state : any , next : any ) => merge ( { } , state , next )
51
+ type StateWithColorMode = { colorMode ?: string ; theme ?: Theme }
52
+ const mergeState = ( state : StateWithColorMode , next : StateWithColorMode ) =>
53
+ merge ( { } , state , next )
43
54
44
55
const CopyTheme = ( { theme } : { theme : Theme } ) => {
45
56
const [ copied , setCopied ] = useState ( false )
@@ -62,19 +73,18 @@ const Spacer = () => <div sx={{ my: 2 }} />
62
73
const App : FunctionComponent = ( ) => {
63
74
const dark = window . chrome . devtools . panels . themeName === 'dark'
64
75
65
- const [ state , setState ] = useReducer ( mergeState , {
66
- theme : null ,
67
- colorMode : null ,
68
- } )
76
+ const [ state , setState ] = useReducer ( mergeState , { } )
69
77
70
78
const getTheme = ( ) => {
71
- runScript ( `window.__THEME_UI__.theme` ) . then ( theme => {
79
+ runScript ( `window.__THEME_UI__.theme` ) . then ( resolvedTheme => {
80
+ const theme = resolvedTheme as StateWithColorMode [ 'theme' ]
72
81
setState ( { theme } )
73
82
} )
74
83
}
75
84
76
85
const getColorMode = ( ) => {
77
- runScript ( `window.__THEME_UI__.colorMode` ) . then ( colorMode => {
86
+ runScript ( `window.__THEME_UI__.colorMode` ) . then ( resolvedColorMode => {
87
+ const colorMode = resolvedColorMode as StateWithColorMode [ 'colorMode' ]
78
88
setState ( { colorMode } )
79
89
} )
80
90
}
@@ -143,7 +153,7 @@ const App: FunctionComponent = () => {
143
153
< Space />
144
154
</ Row >
145
155
< Spacer />
146
- < CopyTheme theme = { state . theme } />
156
+ < CopyTheme theme = { context . theme } />
147
157
</ Editor >
148
158
)
149
159
}
0 commit comments