@@ -3,7 +3,6 @@ import * as React from 'react';
33import { IconChevron } from 'components/Icon/IconChevron' ;
44
55import { SandpackCodeViewer , useSandpack } from '@codesandbox/sandpack-react' ;
6- import type { SandpackMessage } from '@codesandbox/sandpack-client' ;
76
87const getType = ( message : Methods ) : 'info' | 'warning' | 'error' => {
98 if ( message === 'log' || message === 'info' ) {
@@ -62,70 +61,85 @@ export const SandpackConsole: React.FC<{clientId?: string}> = ({clientId}) => {
6261 return unsubscribe ;
6362 } , [ listen ] ) ;
6463
64+ const [ showConsole , toggleConsole ] = React . useState ( false ) ;
65+
6566 React . useEffect ( ( ) => {
6667 if ( wrapperRef . current ) {
6768 wrapperRef . current . scrollTop = wrapperRef . current . scrollHeight ;
6869 }
6970 } , [ logs ] ) ;
7071
7172 return (
72- < div className = "w-full h-full border-y dark:border-gray-700 dark:bg-gray-95 dark:text-white" >
73- { ! ! logs . length && (
74- < div className = "flex justify-between" >
75- < p className = "p-2 text-md" > console</ p >
76- < button className = "p-2" onClick = { ( ) => setLogs ( [ ] ) } >
77- < svg
78- viewBox = "0 0 24 24"
79- width = "18"
80- height = "18"
81- stroke = "currentColor"
82- strokeWidth = "2"
83- fill = "none"
84- strokeLinecap = "round"
85- strokeLinejoin = "round"
86- className = "css-i6dzq1" >
87- < circle cx = "12" cy = "12" r = "10" > </ circle >
88- < line x1 = "4.93" y1 = "4.93" x2 = "19.07" y2 = "19.07" > </ line >
89- </ svg >
90- </ button >
73+ < div
74+ className = { cn (
75+ 'absolute dark:border-gray-700 dark:bg-gray-95 border-t bottom-0 w-full' ,
76+ ! ! ! logs . length && 'cursor-not-allowed'
77+ ) } >
78+ < div className = "flex justify-between h-8 items-center" >
79+ < div onClick = { ( ) => ! ! logs . length && toggleConsole ( ! showConsole ) } >
80+ < IconChevron displayDirection = { showConsole ? 'down' : 'right' } />
9181 </ div >
92- ) }
93- < div className = { cn ( 'console-scroll' ) } ref = { wrapperRef } >
94- { logs . map ( ( { data, id, method} ) => {
95- return (
96- < p
97- key = { id }
98- className = { cn (
99- 'border-y border dark:border-gray-700 text-md p-1 pl-2' ,
100- `console-${ getType ( method ) } `
101- ) } >
102- < span className = { cn ( 'console-message' ) } >
103- { data . map ( ( msg , index ) => {
104- if ( typeof msg === 'string' ) {
105- return < span key = { `${ msg } -${ index } ` } > { msg } </ span > ;
106- }
107-
108- console . log ( 'console' , console ) ;
109-
110- const children = JSON . stringify ( msg ) ;
111-
112- return (
113- < span
114- className = { cn ( 'console-span' ) }
115- key = { `${ msg } -${ index } ` } >
116- < SandpackCodeViewer
117- initMode = "user-visible"
118- // fileType="js"
119- code = { children }
120- />
121- </ span >
122- ) ;
123- } ) }
124- </ span >
125- </ p >
126- ) ;
127- } ) }
82+ < p className = "p-1 text-md" > console ({ logs . length } )</ p >
83+ < button
84+ className = { cn ( 'p-1' , ! ! ! logs . length && 'cursor-not-allowed' ) }
85+ onClick = { ( ) => {
86+ setLogs ( [ ] ) ;
87+ toggleConsole ( false ) ;
88+ } } >
89+ < svg
90+ viewBox = "0 0 24 24"
91+ width = "18"
92+ height = "18"
93+ stroke = "currentColor"
94+ strokeWidth = "2"
95+ fill = "none"
96+ strokeLinecap = "round"
97+ strokeLinejoin = "round" >
98+ < circle cx = "12" cy = "12" r = "10" > </ circle >
99+ < line x1 = "4.93" y1 = "4.93" x2 = "19.07" y2 = "19.07" > </ line >
100+ </ svg >
101+ </ button >
128102 </ div >
103+ { showConsole && (
104+ < div className = "w-full h-full border-y dark:border-gray-700 dark:bg-gray-95 dark:text-white" >
105+ < div className = { cn ( 'console-scroll' ) } ref = { wrapperRef } >
106+ { logs . map ( ( { data, id, method} ) => {
107+ return (
108+ < p
109+ key = { id }
110+ className = { cn (
111+ 'border-y border dark:border-gray-700 text-md p-1 pl-2' ,
112+ `console-${ getType ( method ) } `
113+ ) } >
114+ < span className = { cn ( 'console-message' ) } >
115+ { data . map ( ( msg , index ) => {
116+ if ( typeof msg === 'string' ) {
117+ return < span key = { `${ msg } -${ index } ` } > { msg } </ span > ;
118+ }
119+
120+ console . log ( 'console' , console ) ;
121+
122+ const children = JSON . stringify ( msg ) ;
123+
124+ return (
125+ < span
126+ className = { cn ( 'console-span' ) }
127+ key = { `${ msg } -${ index } ` } >
128+ < SandpackCodeViewer
129+ initMode = "user-visible"
130+ // fileType="js"
131+ code = { children }
132+ />
133+ </ span >
134+ ) ;
135+ } ) }
136+ </ span >
137+ </ p >
138+ ) ;
139+ } ) }
140+ </ div >
141+ </ div >
142+ ) }
129143 </ div >
130144 ) ;
131145} ;
0 commit comments