@@ -13,11 +13,12 @@ import { Modal, showToast } from "./ui-lib";
1313import { copyToClipboard , downloadAs } from "../utils" ;
1414import { Path , ApiPath , REPO_URL } from "@/app/constant" ;
1515import { Loading } from "./home" ;
16+ import styles from "./artifact.module.scss" ;
1617
1718export function HTMLPreview ( props : {
1819 code : string ;
1920 autoHeight ?: boolean ;
20- height ?: number ;
21+ height ?: number | string ;
2122 onLoad ?: ( title ?: string ) => void ;
2223} ) {
2324 const ref = useRef < HTMLIFrameElement > ( null ) ;
@@ -65,17 +66,22 @@ export function HTMLPreview(props: {
6566 return props . code + script ;
6667 } , [ props . code ] ) ;
6768
69+ const handleOnLoad = ( ) => {
70+ if ( props ?. onLoad ) {
71+ props . onLoad ( title ) ;
72+ }
73+ } ;
74+
6875 return (
6976 < iframe
77+ className = { styles [ "artifact-iframe" ] }
7078 id = { frameId . current }
7179 ref = { ref }
72- frameBorder = { 0 }
7380 sandbox = "allow-forms allow-modals allow-scripts"
74- style = { { width : "100%" , height } }
75- // src={`data:text/html,${encodeURIComponent(srcDoc)}`}
81+ style = { { height } }
7682 srcDoc = { srcDoc }
77- onLoad = { ( e ) => props ?. onLoad && props ?. onLoad ( title ) }
78- > </ iframe >
83+ onLoad = { handleOnLoad }
84+ / >
7985 ) ;
8086}
8187
@@ -179,7 +185,6 @@ export function Artifact() {
179185 const [ code , setCode ] = useState ( "" ) ;
180186 const [ loading , setLoading ] = useState ( true ) ;
181187 const [ fileName , setFileName ] = useState ( "" ) ;
182- const { height } = useWindowSize ( ) ;
183188
184189 useEffect ( ( ) => {
185190 if ( id ) {
@@ -199,40 +204,28 @@ export function Artifact() {
199204 } , [ id ] ) ;
200205
201206 return (
202- < div
203- style = { {
204- display : "block" ,
205- width : "100%" ,
206- height : "100%" ,
207- position : "relative" ,
208- } }
209- >
210- < div
211- style = { {
212- height : 36 ,
213- display : "flex" ,
214- alignItems : "center" ,
215- padding : 12 ,
216- } }
217- >
207+ < div className = { styles [ "artifact" ] } >
208+ < div className = { styles [ "artifact-header" ] } >
218209 < a href = { REPO_URL } target = "_blank" rel = "noopener noreferrer" >
219210 < IconButton bordered icon = { < GithubIcon /> } shadow />
220211 </ a >
221- < div style = { { flex : 1 , textAlign : "center" } } > NextChat Artifact</ div >
212+ < div className = { styles [ "artifact-title" ] } > NextChat Artifact</ div >
222213 < ArtifactShareButton id = { id } getCode = { ( ) => code } fileName = { fileName } />
223214 </ div >
224- { loading && < Loading /> }
225- { code && (
226- < HTMLPreview
227- code = { code }
228- autoHeight = { false }
229- height = { height - 36 }
230- onLoad = { ( title ) => {
231- setFileName ( title as string ) ;
232- setLoading ( false ) ;
233- } }
234- />
235- ) }
215+ < div className = { styles [ "artifact-content" ] } >
216+ { loading && < Loading /> }
217+ { code && (
218+ < HTMLPreview
219+ code = { code }
220+ autoHeight = { false }
221+ height = { "100%" }
222+ onLoad = { ( title ) => {
223+ setFileName ( title as string ) ;
224+ setLoading ( false ) ;
225+ } }
226+ />
227+ ) }
228+ </ div >
236229 </ div >
237230 ) ;
238231}
0 commit comments