Skip to content

Commit 859c7f3

Browse files
authored
Allow users of web terminal to disable auto focus (#52992)
The terminal used to automatically took focus, which prevented any modals rendered at the same time as the terminal from maintaining focus. This adds a new `disableAutoFocus` prop to allow callers to take manual control of when the terminal should be focused. Fixes #52908.
1 parent 4558d50 commit 859c7f3

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

web/packages/teleport/src/Console/DocumentKubeExec/DocumentKubeExec.test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('DocumentKubeExec', () => {
9898
setup('waiting-for-exec-data');
9999

100100
expect(screen.getByText('Exec into a pod')).toBeInTheDocument();
101+
expect(screen.getByLabelText('Namespace')).toHaveFocus();
101102
});
102103

103104
test('does not render data dialog when status is initialized', () => {

web/packages/teleport/src/Console/DocumentKubeExec/DocumentKubeExec.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ export default function DocumentKubeExec({ doc, visible }: Props) {
4040
useKubeExecSession(doc);
4141
const mfa = useMfaEmitter(tty);
4242
useEffect(() => {
43-
// when switching tabs or closing tabs, focus on visible terminal
44-
terminalRef.current?.focus();
45-
}, [visible, mfa.challenge]);
43+
// when switching tabs, closing tabs, or
44+
// when the pod information modal is dismissed
45+
// focus the terminal
46+
if (status === 'initialized') {
47+
terminalRef.current?.focus();
48+
}
49+
}, [visible, mfa.challenge, status]);
4650
const theme = useTheme();
4751

4852
const terminal = (
@@ -52,6 +56,7 @@ export default function DocumentKubeExec({ doc, visible }: Props) {
5256
fontFamily={theme.fonts.mono}
5357
theme={theme.colors.terminal}
5458
convertEol
59+
disableAutoFocus={true}
5560
/>
5661
);
5762

web/packages/teleport/src/Console/DocumentSsh/Terminal/Terminal.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface TerminalProps {
4444
// terminalAddons is used to pass the tty to the parent component to enable any optional components like search or filetransfers.
4545
terminalAddons?: (terminalRef: XTermCtrl) => React.JSX.Element;
4646
disableCtrlC?: boolean;
47+
disableAutoFocus?: boolean;
4748
}
4849

4950
export const Terminal = forwardRef<TerminalRef, TerminalProps>((props, ref) => {
@@ -98,6 +99,12 @@ export const Terminal = forwardRef<TerminalRef, TerminalProps>((props, ref) => {
9899
termCtrlRef.current?.updateTheme(props.theme);
99100
}, [props.theme]);
100101

102+
useEffect(() => {
103+
if (!props.disableAutoFocus) {
104+
termCtrlRef.current?.focus();
105+
}
106+
}, []);
107+
101108
return (
102109
<Flex
103110
flexDirection="column"

web/packages/teleport/src/lib/term/terminal.ts

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ export default class TtyTerminal implements TerminalSearcher {
130130

131131
this.term.open(this._el);
132132
this._fitAddon.fit();
133-
this.focus();
134133
this.term.onData(data => {
135134
this.tty.send(data);
136135
});

0 commit comments

Comments
 (0)