diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index 011e60b8fa0d..64bc9a445383 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -902,6 +902,9 @@ function hideThemeButtonState() {
handleClick("help-button", function(ev) {
displayHelp(true, ev);
});
+ handleClick(MAIN_ID, function() {
+ hideSidebar();
+ });
onEachLazy(document.getElementsByTagName("a"), function(el) {
// For clicks on internal links ( tags with a hash property), we expand the section we're
@@ -910,6 +913,7 @@ function hideThemeButtonState() {
if (el.hash) {
el.addEventListener("click", function() {
expandSection(el.hash.slice(1));
+ hideSidebar();
});
}
});
diff --git a/src/test/rustdoc-gui/sidebar-mobile.goml b/src/test/rustdoc-gui/sidebar-mobile.goml
index 7138f9166757..eecd584bca9e 100644
--- a/src/test/rustdoc-gui/sidebar-mobile.goml
+++ b/src/test/rustdoc-gui/sidebar-mobile.goml
@@ -18,3 +18,11 @@ assert-css: (".sidebar-elems", {"display": "block", "left": "0px"})
// When we tab out of the sidebar, close it.
focus: ".search-input"
assert-css: (".sidebar-elems", {"display": "block", "left": "-246px"})
+
+// Open the sidebar menu.
+click: ".sidebar-menu"
+assert-css: (".sidebar-elems", {"left": "0px"})
+
+// Click elsewhere.
+click: "body"
+assert-css: (".sidebar-elems", {"left": "-246px"})