Skip to content

Commit 11906f9

Browse files
committed
Add search.js
1 parent 69d9c6a commit 11906f9

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/_layouts/default.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,6 @@
7676
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.3.0/anchor.min.js"></script>
7777
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"></script>
7878
<script src="/assets/js/app.js"></script>
79+
<script src="/assets/js/search.js"></script>
7980
</body>
8081
</html>

src/assets/js/search.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
$(() => {
2+
const searchForm = document.getElementById("search-form")
3+
4+
addEventListener("keydown", (e) => {
5+
switch (e.key) {
6+
case '/':
7+
if (document.activeElement === searchForm) break
8+
9+
e.preventDefault()
10+
searchForm.focus()
11+
break
12+
case 'Escape':
13+
if (document.activeElement === searchForm) {
14+
searchForm.blur()
15+
}
16+
break
17+
}
18+
})
19+
});

0 commit comments

Comments
 (0)