Skip to content

Commit ebc3f17

Browse files
authored
Fix search input receiving keystrokes after escape dismiss in Safari (#2338)
1 parent 8e67694 commit ebc3f17

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,11 +836,25 @@ function setupMobileSidebarKeyboardHandlers() {
836836
event.preventDefault();
837837
event.stopPropagation();
838838

839+
// Save focus so we can restore it when the dialog closes
840+
const previouslyFocused = document.activeElement;
841+
839842
// When we open the dialog, we cut and paste the nodes and classes from
840843
// the widescreen sidebar into the dialog
841844
cutAndPasteNodesAndClasses(sidebar, dialog);
842845

843846
dialog.showModal();
847+
848+
// Restore focus when dialog closes
849+
dialog.addEventListener(
850+
"close",
851+
() => {
852+
if (previouslyFocused && previouslyFocused.focus) {
853+
previouslyFocused.focus();
854+
}
855+
},
856+
{ once: true },
857+
);
844858
});
845859

846860
// Listen for clicks on the backdrop in order to close the dialog

0 commit comments

Comments
 (0)