Skip to content

Commit

Permalink
add CodeSandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
jedpimentel committed Sep 11, 2024
1 parent 2284dfa commit a120ccc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from "react";
import ImageForm from "./components/ImageForm";
import CodeSandbox from "./components/CodeSandbox";
import useImageUploader from "./hooks/useImageUploader";
import "./App.css";
import { AIService } from "./services/aiService";
Expand Down Expand Up @@ -83,6 +84,7 @@ function App() {
Get Code
</button>
<div>{layoutResponse}</div>
<CodeSandbox/>
</>
);
}
Expand Down
53 changes: 53 additions & 0 deletions frontend/src/components/CodeSandbox.jsx
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;

0 comments on commit a120ccc

Please sign in to comment.