1- import { Component , createEffect , createSignal , splitProps , JSX , onMount , onCleanup } from 'solid-js' ;
1+ import { Component , createEffect , createSignal , onMount , onCleanup } from 'solid-js' ;
22import { useZoom } from '../hooks/useZoom' ;
33
44export const Preview : Component < Props > = ( props ) => {
55 const { zoomState } = useZoom ( ) ;
6- const [ internal , external ] = splitProps ( props , [ 'code' , 'isDark' , 'class' , 'reloadSignal' , 'devtools' ] ) ;
76
87 let iframe ! : HTMLIFrameElement ;
98
@@ -13,11 +12,10 @@ export const Preview: Component<Props> = (props) => {
1312 const CODE_UPDATE = 'CODE_UPDATE' ;
1413
1514 createEffect ( ( ) => {
16- const isEmpty = ! internal . code ;
15+ if ( ! props . code ) return ;
16+ if ( ! isIframeReady ( ) ) return ;
1717
18- if ( isEmpty || ! isIframeReady ( ) ) return ;
19-
20- latestCode = internal . code . replace ( 'render(' , 'window.dispose = render(' ) ;
18+ latestCode = props . code . replace ( 'render(' , 'window.dispose = render(' ) ;
2119
2220 const blob = new Blob ( [ latestCode ] , {
2321 type : 'text/javascript' ,
@@ -29,20 +27,17 @@ export const Preview: Component<Props> = (props) => {
2927 } ) ;
3028
3129 createEffect ( ( ) => {
32- if ( ! iframe ) return ;
33- iframe . contentWindow ! . postMessage ( { event : 'DEVTOOLS' , value : internal . devtools } , '*' ) ;
30+ iframe . contentWindow ! . postMessage ( { event : 'DEVTOOLS' , value : props . devtools } , '*' ) ;
3431 } ) ;
3532
3633 const setDarkMode = ( ) => {
37- const doc = iframe . contentDocument || iframe . contentWindow ?. document ;
38- doc ?. body ! . classList . toggle ( 'dark' , internal . isDark ) ;
39- iframe . contentWindow ! . postMessage ( { event : 'THEME' , value : internal . isDark } , '*' ) ;
34+ const doc = iframe . contentDocument ! . body ;
35+ doc . classList . toggle ( 'dark' , props . isDark ) ;
36+ iframe . contentWindow ! . postMessage ( { event : 'THEME' , value : props . isDark } , '*' ) ;
4037 } ;
4138
4239 createEffect ( ( ) => {
43- if ( iframe && isIframeReady ( ) ) {
44- setDarkMode ( ) ;
45- }
40+ if ( isIframeReady ( ) ) setDarkMode ( ) ;
4641 } ) ;
4742
4843 const html = `
@@ -112,7 +107,7 @@ export const Preview: Component<Props> = (props) => {
112107 tool: ["console", "network", "resources", "elements"],
113108 defaults: {
114109 displaySize: 40,
115- theme: "${ internal . isDark ? 'Dark' : 'Light' } "
110+ theme: "${ props . isDark ? 'Dark' : 'Light' } "
116111 }
117112 });
118113 eruda.add(erudaDom);
@@ -122,7 +117,7 @@ export const Preview: Component<Props> = (props) => {
122117 href: '${ location . origin } /eruda.css'
123118 });
124119 eruda._shadowRoot.appendChild(style);
125- if ( ${ internal . devtools } ) eruda.show();
120+ ${ props . devtools ? ' eruda.show();' : '' }
126121 </script>
127122 <script type="module" id="setup">
128123 window.addEventListener('message', async ({ data }) => {
@@ -175,7 +170,7 @@ export const Preview: Component<Props> = (props) => {
175170
176171 createEffect ( ( ) => {
177172 // Bail early on first mount or we are already reloading
178- if ( ! internal . reloadSignal ) return ;
173+ if ( ! props . reloadSignal ) return ;
179174
180175 // Otherwise, reload everytime we clicked the reload button
181176 setIframeReady ( false ) ;
@@ -191,28 +186,29 @@ export const Preview: Component<Props> = (props) => {
191186 } ;
192187
193188 onMount ( ( ) => {
194- iframe . addEventListener ( 'load' , ( ) => {
195- setIframeReady ( true ) ;
196-
197- setDarkMode ( ) ;
198- } ) ;
189+ iframe . addEventListener ( 'load' , ( ) => setIframeReady ( true ) ) ;
199190 } ) ;
200191
201192 return (
202- < iframe
203- title = "Solid REPL"
204- class = { `overflow-auto p-0 dark:bg-other block ${ internal . class } ` }
205- style = { styleScale ( ) }
206- ref = { iframe }
207- src = { src }
208- { ...external }
209- // @ts -ignore
210- sandbox = "allow-popups-to-escape-sandbox allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation allow-modals allow-same-origin"
211- > </ iframe >
193+ < div class = "h-full w-full relative" >
194+ < iframe
195+ title = "Solid REPL"
196+ class = "overflow-auto p-0 dark:bg-other block h-full w-full bg-white row-start-5"
197+ classList = { props . classList }
198+ style = { styleScale ( ) }
199+ ref = { iframe }
200+ src = { src }
201+ // @ts -ignore
202+ sandbox = "allow-popups-to-escape-sandbox allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation allow-modals allow-same-origin"
203+ > </ iframe >
204+ </ div >
212205 ) ;
213206} ;
214207
215- type Props = JSX . HTMLAttributes < HTMLIFrameElement > & {
208+ type Props = {
209+ classList ?: {
210+ [ k : string ] : boolean | undefined ;
211+ } ;
216212 code : string ;
217213 reloadSignal : boolean ;
218214 devtools : boolean ;
0 commit comments