Skip to content

Commit 5fc4444

Browse files
committed
Refit terminal when screen is resized; closes pterodactyl#2365
1 parent 2d56cac commit 5fc4444

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

resources/scripts/components/server/Console.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
1+
import React, { useEffect, useMemo, useRef } from 'react';
22
import { ITerminalOptions, Terminal } from 'xterm';
33
import * as TerminalFit from 'xterm/lib/addons/fit/fit';
44
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
@@ -7,6 +7,8 @@ import styled from 'styled-components/macro';
77
import { usePermissions } from '@/plugins/usePermissions';
88
import tw from 'twin.macro';
99
import 'xterm/dist/xterm.css';
10+
import useEventListener from '@/plugins/useEventListener';
11+
import { debounce } from 'debounce';
1012

1113
const theme = {
1214
background: 'transparent',
@@ -51,9 +53,7 @@ const TerminalDiv = styled.div`
5153

5254
export default () => {
5355
const TERMINAL_PRELUDE = '\u001b[1m\u001b[33mcontainer@pterodactyl~ \u001b[0m';
54-
const [ terminalElement, setTerminalElement ] = useState<HTMLDivElement | null>(null);
55-
56-
const useRef = useCallback(node => setTerminalElement(node), []);
56+
const ref = useRef<HTMLDivElement>(null);
5757
const terminal = useMemo(() => new Terminal({ ...terminalProps }), []);
5858
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
5959
const [ canSendCommands ] = usePermissions([ 'control.console' ]);
@@ -80,8 +80,8 @@ export default () => {
8080
};
8181

8282
useEffect(() => {
83-
if (connected && terminalElement && !terminal.element) {
84-
terminal.open(terminalElement);
83+
if (connected && ref.current && !terminal.element) {
84+
terminal.open(ref.current);
8585

8686
// @see https://github.com/xtermjs/xterm.js/issues/2265
8787
// @see https://github.com/xtermjs/xterm.js/issues/2230
@@ -98,7 +98,13 @@ export default () => {
9898
return true;
9999
});
100100
}
101-
}, [ terminal, connected, terminalElement ]);
101+
}, [ terminal, connected ]);
102+
103+
const fit = debounce(() => {
104+
TerminalFit.fit(terminal);
105+
}, 100);
106+
107+
useEventListener('resize', () => fit());
102108

103109
useEffect(() => {
104110
if (connected && instance) {
@@ -135,7 +141,7 @@ export default () => {
135141
maxHeight: '32rem',
136142
}}
137143
>
138-
<TerminalDiv id={'terminal'} ref={useRef}/>
144+
<TerminalDiv id={'terminal'} ref={ref}/>
139145
</div>
140146
{canSendCommands &&
141147
<div css={tw`rounded-b bg-neutral-900 text-neutral-100 flex`}>

0 commit comments

Comments
 (0)