Skip to content

Commit b183267

Browse files
committed
REPL: Focus on editor when changing tabs
1 parent d38d6f0 commit b183267

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/repl/components/Editor.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ export function FileTabs({
5050
files,
5151
activeFileIdx,
5252
setActiveFileIdx,
53-
closeFile
53+
focusEditor,
54+
closeFile,
5455
}: {
5556
files: EditorItem[];
5657
activeFileIdx: number;
5758
setActiveFileIdx: React.Dispatch<React.SetStateAction<number>>;
59+
focusEditor: () => void;
5860
closeFile: (e: React.SyntheticEvent, index: number) => void;
5961
}) {
6062
return (
@@ -74,7 +76,10 @@ export function FileTabs({
7476
<button
7577
className="editor-switch"
7678
aria-label={`Switch to ${f.name}`}
77-
onClick={() => setActiveFileIdx(index)}
79+
onClick={() => {
80+
setActiveFileIdx(index);
81+
setTimeout(focusEditor, 0);
82+
}}
7883
>
7984
</button>
8085
<div
@@ -342,6 +347,12 @@ export function Editor({
342347
};
343348
}, [saveFile, editorView, activeFile]);
344349

350+
const focusEditor = React.useCallback(() => {
351+
if (editorView) {
352+
editorView.focus();
353+
}
354+
}, [editorView]);
355+
345356
React.useEffect(() => {
346357
if (!editorRef.current) {
347358
return;
@@ -514,6 +525,7 @@ export function Editor({
514525
activeFileIdx={activeFileIdx}
515526
setActiveFileIdx={setActiveFileIdx}
516527
closeFile={closeFile}
528+
focusEditor={focusEditor}
517529
/>
518530
</div>
519531
<div

0 commit comments

Comments
 (0)