Skip to content

Commit 2f86ae1

Browse files
Copilotalexr00
andcommitted
Restore focus to comment textarea when description page regains focus
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 7293e1c commit 2f86ae1

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/github/pullRequestOverview.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
186186
if (this._panel.visible && this._item) {
187187
PullRequestOverviewPanel._onVisible.fire(this._item);
188188
}
189+
190+
// If the panel becomes active, send a message to restore focus to the comment textarea if needed
191+
if (this._panel.active) {
192+
this._postMessage({ command: 'pr.restoreFocus' });
193+
}
189194
}
190195

191196
private setVisibilityContext() {

webviews/common/context.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,17 @@ export class PRContext {
410410
pendingReview.focus();
411411
}
412412
return;
413+
case 'pr.restoreFocus':
414+
// Restore focus to the comment textarea if it had focus before the webview lost focus
415+
const commentTextarea = document.getElementById('comment-textarea');
416+
if (commentTextarea instanceof HTMLTextAreaElement) {
417+
// Check if there's a stored focus state
418+
const shouldRestoreFocus = sessionStorage.getItem('comment-textarea-had-focus') === 'true';
419+
if (shouldRestoreFocus) {
420+
commentTextarea.focus();
421+
}
422+
}
423+
return;
413424
case 'pr.submitting-review':
414425
return this.updatePR({ busy: true, lastReviewType: message.lastReviewType });
415426
case 'pr.append-review':

webviews/editorWebview/app.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@ export function Root({ children }) {
2121
ctx.onchange = setPR;
2222
setPR(ctx.pr);
2323
}, []);
24+
25+
// Track focus on the comment textarea to restore it when the webview regains focus
26+
useEffect(() => {
27+
const handleFocusIn = (e: FocusEvent) => {
28+
if (e.target instanceof HTMLTextAreaElement && e.target.id === 'comment-textarea') {
29+
sessionStorage.setItem('comment-textarea-had-focus', 'true');
30+
}
31+
};
32+
33+
const handleFocusOut = (e: FocusEvent) => {
34+
if (e.target instanceof HTMLTextAreaElement && e.target.id === 'comment-textarea') {
35+
// Only clear the flag if we're switching to a non-textarea element
36+
// This prevents clearing when the webview loses focus
37+
if (e.relatedTarget instanceof HTMLElement) {
38+
sessionStorage.setItem('comment-textarea-had-focus', 'false');
39+
}
40+
}
41+
};
42+
43+
document.addEventListener('focusin', handleFocusIn);
44+
document.addEventListener('focusout', handleFocusOut);
45+
46+
return () => {
47+
document.removeEventListener('focusin', handleFocusIn);
48+
document.removeEventListener('focusout', handleFocusOut);
49+
};
50+
}, []);
51+
2452
window.onscroll = debounce(() => {
2553
ctx.postMessage({
2654
command: 'scroll',

0 commit comments

Comments
 (0)