doc: allow pressing / to focus search bar (#10522)

* type / to focus search; rename unused variables

* use ev.preventDefault() instead of setTimeout()

* delete doc.js_
pull/10525/head
shadowninja55 2021-06-19 22:16:50 -04:00 committed by GitHub
parent 21fe91e508
commit 984551227b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -20,7 +20,7 @@ function setupScrollSpy() {
sectionPositions.push(section.offsetTop); sectionPositions.push(section.offsetTop);
}); });
var scrollPos = 0; var scrollPos = 0;
window.addEventListener('scroll', function (e) { window.addEventListener('scroll', function (_) {
// Reset classes // Reset classes
document.querySelectorAll('.doc-toc a[class="active"]').forEach(function (link) { document.querySelectorAll('.doc-toc a[class="active"]').forEach(function (link) {
link.classList.remove('active'); link.classList.remove('active');
@ -52,7 +52,7 @@ function setupScrollSpy() {
function setupMobileToggle() { function setupMobileToggle() {
var toggle = document.getElementById('toggle-menu'); var toggle = document.getElementById('toggle-menu');
toggle.addEventListener('click', function (ev) { toggle.addEventListener('click', function (_) {
var docNav = document.querySelector('.doc-nav'); var docNav = document.querySelector('.doc-nav');
var isHidden = docNav.classList.contains('hidden'); var isHidden = docNav.classList.contains('hidden');
docNav.classList.toggle('hidden'); docNav.classList.toggle('hidden');
@ -225,3 +225,11 @@ function debounce(func, timeout) {
timer = setTimeout(next, timeout > 0 ? timeout : 300); timer = setTimeout(next, timeout > 0 ? timeout : 300);
} }
} }
document.addEventListener('keypress', (ev) => {
if (ev.key == '/') {
let search = document.getElementById('search');
ev.preventDefault();
search.focus();
}
});