Skip to content

Commit c4067c5

Browse files
committed
Restore scroll position on page if giving active element focus changes it
1 parent 4b3c5bc commit c4067c5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

javascript/progressbar.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,26 @@ function check_gallery(id_gallery){
9292
if (prevSelectedIndex !== -1 && galleryButtons.length>prevSelectedIndex && !galleryBtnSelected) {
9393
// automatically re-open previously selected index (if exists)
9494
activeElement = gradioApp().activeElement;
95+
let scrollX = window.scrollX;
96+
let scrollY = window.scrollY;
9597

9698
galleryButtons[prevSelectedIndex].click();
9799
showGalleryImage();
98100

101+
// When the gallery button is clicked, it gains focus and scrolls itself into view
102+
// We need to scroll back to the previous position
103+
setTimeout(function (){
104+
window.scrollTo(scrollX, scrollY);
105+
}, 50);
106+
99107
if(activeElement){
100108
// i fought this for about an hour; i don't know why the focus is lost or why this helps recover it
101-
// if somenoe has a better solution please by all means
102-
setTimeout(function() { activeElement.focus() }, 1);
109+
// if someone has a better solution please by all means
110+
setTimeout(function (){
111+
activeElement.focus({
112+
preventScroll: true // Refocus the element that was focused before the gallery was opened without scrolling to it
113+
})
114+
}, 1);
103115
}
104116
}
105117
})

0 commit comments

Comments
 (0)