-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2284dfa
commit a120ccc
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
|
||
const CodeSandbox = () => { | ||
const [htmlCode, setHtmlCode] = useState(` | ||
<style> | ||
p { | ||
color: blue; | ||
} | ||
</style> | ||
<p>Hello World</p> | ||
<script> | ||
const p = document.querySelector('p'); | ||
setInterval(() => { | ||
p.innerHTML += '! '; | ||
}, 1000) | ||
</script> | ||
`); | ||
const iframeRef = useRef(null); | ||
|
||
const handleCodeChange = (e) => { | ||
setHtmlCode(e.target.value); | ||
}; | ||
|
||
|
||
// Update the iframe content whenever htmlCode changes | ||
useEffect(() => { | ||
const iframe = iframeRef.current; | ||
const iframeDocument = iframe.contentDocument || iframe.contentWindow.document; | ||
|
||
// Write the user-provided HTML/JS/CSS into the iframe | ||
iframeDocument.open(); | ||
iframeDocument.write(htmlCode); | ||
iframeDocument.close(); | ||
}, [htmlCode]); | ||
|
||
return ( | ||
<div> | ||
<textarea | ||
rows="10" | ||
cols="50" | ||
value={htmlCode} | ||
onChange={handleCodeChange} | ||
placeholder="Write HTML code here..." | ||
/> | ||
<div> | ||
<h3>Output:</h3> | ||
<iframe title="Code Output" ref={iframeRef} style={{ width: '100%', height: '300px', border: '1px dotted gray' }} /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CodeSandbox; |