@@ -13,11 +13,12 @@ import { Modal, showToast } from "./ui-lib";
13
13
import { copyToClipboard , downloadAs } from "../utils" ;
14
14
import { Path , ApiPath , REPO_URL } from "@/app/constant" ;
15
15
import { Loading } from "./home" ;
16
+ import styles from "./artifact.module.scss" ;
16
17
17
18
export function HTMLPreview ( props : {
18
19
code : string ;
19
20
autoHeight ?: boolean ;
20
- height ?: number ;
21
+ height ?: number | string ;
21
22
onLoad ?: ( title ?: string ) => void ;
22
23
} ) {
23
24
const ref = useRef < HTMLIFrameElement > ( null ) ;
@@ -65,17 +66,22 @@ export function HTMLPreview(props: {
65
66
return props . code + script ;
66
67
} , [ props . code ] ) ;
67
68
69
+ const handleOnLoad = ( ) => {
70
+ if ( props ?. onLoad ) {
71
+ props . onLoad ( title ) ;
72
+ }
73
+ } ;
74
+
68
75
return (
69
76
< iframe
77
+ className = { styles [ "artifact-iframe" ] }
70
78
id = { frameId . current }
71
79
ref = { ref }
72
- frameBorder = { 0 }
73
80
sandbox = "allow-forms allow-modals allow-scripts"
74
- style = { { width : "100%" , height } }
75
- // src={`data:text/html,${encodeURIComponent(srcDoc)}`}
81
+ style = { { height } }
76
82
srcDoc = { srcDoc }
77
- onLoad = { ( e ) => props ?. onLoad && props ?. onLoad ( title ) }
78
- > </ iframe >
83
+ onLoad = { handleOnLoad }
84
+ / >
79
85
) ;
80
86
}
81
87
@@ -179,7 +185,6 @@ export function Artifact() {
179
185
const [ code , setCode ] = useState ( "" ) ;
180
186
const [ loading , setLoading ] = useState ( true ) ;
181
187
const [ fileName , setFileName ] = useState ( "" ) ;
182
- const { height } = useWindowSize ( ) ;
183
188
184
189
useEffect ( ( ) => {
185
190
if ( id ) {
@@ -199,40 +204,28 @@ export function Artifact() {
199
204
} , [ id ] ) ;
200
205
201
206
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" ] } >
218
209
< a href = { REPO_URL } target = "_blank" rel = "noopener noreferrer" >
219
210
< IconButton bordered icon = { < GithubIcon /> } shadow />
220
211
</ a >
221
- < div style = { { flex : 1 , textAlign : "center" } } > NextChat Artifact</ div >
212
+ < div className = { styles [ "artifact-title" ] } > NextChat Artifact</ div >
222
213
< ArtifactShareButton id = { id } getCode = { ( ) => code } fileName = { fileName } />
223
214
</ 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 >
236
229
</ div >
237
230
) ;
238
231
}
0 commit comments