@@ -29,13 +29,13 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
2929 onData,
3030 innerRef
3131} ) => {
32- let terminal : Terminal ;
32+ const terminalRef = React . useRef < Terminal > ( ) ;
3333 const ref = React . useRef < HTMLDivElement > ( ) ;
3434
3535 useImperativeHandle ( innerRef , ( ) => ( {
3636 focusTerminal ( ) {
37- if ( terminal ) {
38- terminal . focus ( ) ;
37+ if ( terminalRef . current ) {
38+ terminalRef . current . focus ( ) ;
3939 }
4040 } ,
4141 /**
@@ -44,8 +44,8 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
4444 * @param {string } data String content to be writen into the terminal
4545 */
4646 onDataReceived : ( data : string ) => {
47- if ( terminal ) {
48- terminal . write ( data ) ;
47+ if ( terminalRef . current ) {
48+ terminalRef . current . write ( data ) ;
4949 }
5050 } ,
5151 /**
@@ -54,16 +54,35 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
5454 * @param {string } reason String error to be written into the terminal
5555 */
5656 onConnectionClosed : ( reason : string ) => {
57- if ( terminal ) {
58- terminal . write ( `\x1b[31m${ reason || 'disconnected' } \x1b[m\r\n` ) ;
59- terminal . refresh ( terminal . rows , terminal . rows ) ; // start to end row
57+ if ( terminalRef . current ) {
58+ terminalRef . current . write ( `\x1b[31m${ reason || 'disconnected' } \x1b[m\r\n` ) ;
59+ terminalRef . current . refresh ( terminalRef . current . rows , terminalRef . current . rows ) ; // start to end row
6060 }
6161 }
6262 } ) ) ;
6363
64+ const onBeforeUnload = React . useCallback ( ( event : any ) => {
65+ // Firefox requires this when the page is in an iframe
66+ event . preventDefault ( ) ;
67+
68+ // see "an almost cross-browser solution" at
69+ // https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
70+ event . returnValue = '' ;
71+ return '' ;
72+ } , [ ] ) ;
73+
74+
75+ const onFocusIn = ( ) => {
76+ window . addEventListener ( 'beforeunload' , onBeforeUnload ) ;
77+ } ;
78+
79+ const onFocusOut = React . useCallback ( ( ) => {
80+ window . removeEventListener ( 'beforeunload' , onBeforeUnload ) ;
81+ } , [ onBeforeUnload ] ) ;
82+
6483 React . useEffect ( ( ) => {
6584 const fitAddon = new FitAddon ( ) ;
66- terminal = new Terminal ( {
85+ terminalRef . current = new Terminal ( {
6786 cols,
6887 rows,
6988 cursorBlink : true ,
@@ -75,21 +94,21 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
7594 const onWindowResize = ( ) => {
7695 const geometry = fitAddon . proposeDimensions ( ) ;
7796 if ( geometry ) {
78- terminal . resize ( geometry . rows , geometry . cols ) ;
97+ terminalRef . current . resize ( geometry . rows , geometry . cols ) ;
7998 }
8099 } ;
81100
82101 if ( onData ) {
83- terminal . onData ( onData ) ;
102+ terminalRef . current . onData ( onData ) ;
84103 }
85104
86105 if ( onTitleChanged ) {
87- terminal . onTitleChange ( onTitleChanged ) ;
106+ terminalRef . current . onTitleChange ( onTitleChanged ) ;
88107 }
89108
90- terminal . loadAddon ( fitAddon ) ;
109+ terminalRef . current . loadAddon ( fitAddon ) ;
91110
92- terminal . open ( ref . current ) ;
111+ terminalRef . current . open ( ref . current ) ;
93112
94113 const resizeListener = debounce ( onWindowResize , 100 ) ;
95114
@@ -99,34 +118,16 @@ export const XTerm: React.FunctionComponent<XTermProps> = ({
99118 }
100119 onWindowResize ( ) ;
101120 }
102- terminal . focus ( ) ;
121+ terminalRef . current . focus ( ) ;
103122
104123 return ( ) => {
105- terminal . dispose ( ) ;
124+ terminalRef . current . dispose ( ) ;
106125 if ( canUseDOM ) {
107126 window . removeEventListener ( 'resize' , resizeListener ) ;
108127 }
109128 onFocusOut ( ) ;
110129 } ;
111- } , [ ] ) ;
112-
113- const onBeforeUnload = ( event : any ) => {
114- // Firefox requires this when the page is in an iframe
115- event . preventDefault ( ) ;
116-
117- // see "an almost cross-browser solution" at
118- // https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
119- event . returnValue = '' ;
120- return '' ;
121- } ;
122-
123- const onFocusIn = ( ) => {
124- window . addEventListener ( 'beforeunload' , onBeforeUnload ) ;
125- } ;
126-
127- const onFocusOut = ( ) => {
128- window . removeEventListener ( 'beforeunload' , onBeforeUnload ) ;
129- } ;
130+ } , [ cols , fontFamily , fontSize , onData , onFocusOut , onTitleChanged , rows ] ) ;
130131
131132 // ensure react never reuses this div by keying it with the terminal widget
132133 // Workaround for xtermjs/xterm.js#3172
0 commit comments