Skip to content

Commit fe2fe63

Browse files
authored
Fix search dropdown behaviour (#1512)
After this change: | Scenario | Behavior | |----------|----------| | Type search, click result | Navigate without `?q=`, clean URL without dropdown | | Type search, press Enter | Navigate without `?q=`, clean URL without dropdown | | Land on URL with `?q=` param | Show dropdown with pre-filled query | | Click outside dropdown | Hide dropdown, keep input value | | Focus input with existing query | Show dropdown again |
1 parent 1e58ee2 commit fe2fe63

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

lib/rdoc/generator/template/aliki/js/aliki.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,7 @@ function createSearchInstance(input, result) {
6767
}
6868

6969
search.select = function(result) {
70-
let href = result.firstChild.firstChild.href;
71-
const query = this.input.value;
72-
if (query) {
73-
const url = new URL(href, window.location.origin);
74-
url.searchParams.set('q', query);
75-
url.searchParams.set('nav', '0');
76-
href = url.toString();
77-
}
78-
window.location.href = href;
70+
window.location.href = result.firstChild.firstChild.href;
7971
}
8072

8173
search.scrollIntoView = search.scrollInWindow;
@@ -97,15 +89,27 @@ function hookSearch() {
9789
const search = createSearchInstance(input, result);
9890
if (!search) return;
9991

92+
// Hide search results when clicking outside the search area
93+
document.addEventListener('click', (e) => {
94+
if (!e.target.closest('.navbar-search-desktop')) {
95+
search.hide();
96+
}
97+
});
98+
99+
// Show search results when focusing on input (if there's a query)
100+
input.addEventListener('focus', () => {
101+
if (input.value.trim()) {
102+
search.show();
103+
}
104+
});
105+
100106
// Check for ?q= URL parameter and trigger search automatically
101107
if (typeof URLSearchParams !== 'undefined') {
102108
const urlParams = new URLSearchParams(window.location.search);
103109
const queryParam = urlParams.get('q');
104110
if (queryParam) {
105-
const navParam = urlParams.get('nav');
106-
const autoSelect = navParam !== '0';
107111
input.value = queryParam;
108-
search.search(queryParam, autoSelect);
112+
search.search(queryParam, false);
109113
}
110114
}
111115
}
@@ -383,9 +387,7 @@ function hookSearchModal() {
383387
if (queryParam && isSmallViewport) {
384388
openSearchModal();
385389
searchInput.value = queryParam;
386-
const navParam = urlParams.get('nav');
387-
const autoSelect = navParam !== '0';
388-
mobileSearch.search(queryParam, autoSelect);
390+
mobileSearch.search(queryParam, false);
389391
}
390392
}
391393
}

lib/rdoc/generator/template/aliki/js/search_controller.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,14 @@ SearchController.prototype = Object.assign({}, SearchNavigation, new function()
116116
});
117117
}
118118

119+
this.hide = function() {
120+
this.result.setAttribute('aria-expanded', 'false');
121+
this.setNavigationActive(false);
122+
}
123+
124+
this.show = function() {
125+
this.result.setAttribute('aria-expanded', 'true');
126+
this.setNavigationActive(true);
127+
}
119128
});
120129

lib/rdoc/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module RDoc
55
##
66
# RDoc version you are using
77

8-
VERSION = '7.0.1'
8+
VERSION = '7.0.2'
99

1010
end

0 commit comments

Comments
 (0)