@@ -31,15 +31,15 @@ interface TerminalMessage {
3131}
3232
3333class Preview extends React . Component < PreviewProps , PreviewState > {
34- ref : React . RefObject < HTMLDivElement > ;
34+ terminalContainer : React . RefObject < HTMLDivElement > ;
3535 constructor ( props : PreviewProps ) {
3636 super ( props ) ;
3737 this . state = {
3838 diagnostics : props . diagnostics ,
3939 messages : [ ] ,
4040 } ;
4141
42- this . ref = createRef < HTMLDivElement > ( ) ;
42+ this . terminalContainer = createRef < HTMLDivElement > ( ) ;
4343 }
4444
4545 println ( text : string ) {
@@ -66,9 +66,20 @@ class Preview extends React.Component<PreviewProps, PreviewState> {
6666 render ( ) {
6767 // if the code doesn't contain any errors and the diagnostics aren't warnings
6868 if ( this . state . diagnostics == null || this . state . diagnostics . filter ( ( i ) => i . severity === 1 ) . length == 0 ) {
69+
70+ // auto scroll to bottom
71+ const terminal = this . terminalContainer . current ;
72+ const newLine = terminal ?. lastElementChild ;
73+ if ( newLine && terminal ) {
74+ const rect = newLine . getBoundingClientRect ( ) ;
75+ if ( rect . bottom <= terminal . getBoundingClientRect ( ) . bottom ) {
76+ newLine . scrollIntoView ( ) ;
77+ }
78+ }
79+
6980 return (
7081 < div >
71- < div className = "text-sm flex flex-col p-4 overflow-hidden overflow-y-scroll" ref = { this . ref } >
82+ < div className = "text-sm flex flex-col p-4 overflow-hidden overflow-y-scroll" ref = { this . terminalContainer } >
7283 { this . state . messages . map ( ( message , index ) =>
7384 < p key = { index } className = { message . type == "error" ? "text-base text-accentRed" : "text-white" } > { message . type == "error" ? "An error occurred: " : "" } { message . content } </ p >
7485 ) }
0 commit comments