Last active
April 21, 2021 05:31
-
-
Save VanTanev/956185f05d9decc29e2d2b01bdd779cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { XstateInspectLoader } from 'shared/util/XstateInspectLoader' | |
const App = () => { | |
<XstateInspectLoader> | |
// rest of app goes here | |
</XstateInspectLoader> | |
} |
This file contains hidden or 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
import React from 'react' | |
import { isXstateInspectEnabled } from 'shared/util/XstateInspectLoader' | |
const Component: React.FC = (): React.ReactElement => { | |
const [state, send] = useMachine( | |
() => createMachine({...}) | |
{ | |
devTools: isXstateInspectEnabled, | |
}, | |
) | |
// ... | |
} |
This file contains hidden or 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
import React from 'react' | |
import ReactDOM from 'react-dom' | |
const FLAG = 'xstate-inspect-enabled' | |
const xstateInspect = { | |
start: () => { | |
sessionStorage.setItem(FLAG, '1') | |
window.location.reload() | |
}, | |
stop: () => { | |
sessionStorage.removeItem(FLAG) | |
window.location.reload() | |
}, | |
} | |
;(window as any).xstateInspect = xstateInspect | |
const xstateInspectEnabled = (): boolean => { | |
try { | |
return sessionStorage.getItem(FLAG) === '1' | |
} catch { | |
return false | |
} | |
} | |
export const isXstateInspectEnabled = xstateInspectEnabled() | |
export const XstateInspectLoader: React.FC = ({ children }) => { | |
const [loading, setLoading] = React.useState(isXstateInspectEnabled) | |
React.useEffect(() => { | |
if (isXstateInspectEnabled) { | |
import('@xstate/inspect').then(({ inspect }) => { | |
const wrapper = document.createElement('div') | |
document.querySelector('#root')!.prepend(wrapper) | |
ReactDOM.render( | |
<div style={{ height: '600px', overflow: 'hidden', overflowY: 'scroll' }}> | |
<iframe | |
title="xstate-inspector" | |
style={{ width: '100%', height: '2000px' }} | |
data-xstate | |
></iframe> | |
</div>, | |
wrapper, | |
) | |
inspect() | |
setLoading(false) | |
}) | |
} | |
}, []) | |
return loading ? null : <>{children}</> | |
} | |
export default XstateInspectLoader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment