Skip to content

Commit 57c528f

Browse files
authored
Merge pull request #69 from stackabletech/feat-keyboard-shortcut-open-search
feat: Open search with Ctrl+K
2 parents 1f316ee + bae4573 commit 57c528f

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/js/07-search.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
; (function () {
22
'use strict'
33

4-
var searchButton = document.getElementById('search-button')
5-
if (!searchButton) return
6-
searchButton.addEventListener('click', toggleNavbarMenu.bind(searchButton))
7-
8-
function toggleNavbarMenu (e) {
9-
e.stopPropagation() // trap event
4+
function openSearchPopover () {
105
document.getElementById('search-background').style.display = 'block'
116
document.getElementById('search').style.display = 'block'
127
var searchInput = document.querySelector('.pagefind-modular-input')
138
if (!searchInput) return
149
searchInput.focus()
1510
}
16-
})()
1711

18-
; (function () {
19-
'use strict'
12+
// open the popover when clicking the magnifying glass search icon
13+
var searchButton = document.getElementById('search-button')
14+
if (!searchButton) return
15+
searchButton.addEventListener('click', function (event) {
16+
event.stopPropagation()
17+
openSearchPopover()
18+
})
2019

20+
// open the popover with ctrl+k
21+
document.addEventListener('keydown', function (event) {
22+
if (event.ctrlKey && event.key === 'k') {
23+
event.preventDefault() // prevent focussing the URL bar in the browser
24+
openSearchPopover()
25+
}
26+
})
27+
28+
// close functionality when clicking the background
2129
var searchBackground = document.getElementById('search-background')
2230
if (!searchBackground) return
23-
searchBackground.addEventListener('click', toggleNavbarMenu.bind(searchBackground))
24-
25-
function toggleNavbarMenu (e) {
31+
searchBackground.addEventListener('click', function (e) {
2632
e.stopPropagation() // trap event
2733
document.getElementById('search-background').style.display = 'none'
2834
document.getElementById('search').style.display = 'none'
29-
}
35+
})
3036
})()

0 commit comments

Comments
 (0)