Skip to content

Commit a001531

Browse files
authored
Add cmd-k/ctrl-k shortcut to focus searchbar (#1870)
1 parent d16e576 commit a001531

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

assets/js/helpers.js

+9
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,12 @@ export function debounce (fn, milliseconds) {
159159
export function getProjectNameAndVersion () {
160160
return document.head.querySelector('meta[name=project][content]').content
161161
}
162+
163+
/**
164+
* Return `true` if the client's OS is MacOS
165+
*
166+
* @return {Boolean}
167+
*/
168+
export function isMacOS () {
169+
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
170+
}

assets/js/keyboard-shortcuts.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { qs } from './helpers'
1+
import { isMacOS, qs } from './helpers'
22
import { toggleSidebar } from './sidebar/sidebar-drawer'
33
import { focusSearchInput } from './search-bar'
44
import { cycleTheme } from './theme'
@@ -29,6 +29,11 @@ export const keyboardShortcuts = [
2929
key: '/',
3030
action: searchKeyAction
3131
},
32+
{
33+
key: 'k',
34+
hasModifier: true,
35+
action: searchKeyAction
36+
},
3237
{
3338
key: 'g',
3439
description: 'Search HexDocs package',
@@ -64,9 +69,20 @@ function addEventListeners () {
6469
function handleKeyDown (event) {
6570
if (state.shortcutBeingPressed) { return }
6671
if (event.target.matches('input, textarea')) { return }
67-
if (event.ctrlKey || event.metaKey || event.altKey) { return }
6872

69-
const matchingShortcut = keyboardShortcuts.find(shortcut => shortcut.key === event.key)
73+
const matchingShortcut = keyboardShortcuts.find(shortcut => {
74+
if (shortcut.hasModifier) {
75+
if (isMacOS() && event.metaKey) { return shortcut.key === event.key }
76+
if (event.ctrlKey) { return shortcut.key === event.key }
77+
78+
return false
79+
} else {
80+
if (event.ctrlKey || event.metaKey || event.altKey) { return false }
81+
82+
return shortcut.key === event.key
83+
}
84+
})
85+
7086
if (!matchingShortcut) { return }
7187
state.shortcutBeingPressed = matchingShortcut
7288

assets/js/search-bar.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
AUTOCOMPLETE_CONTAINER_SELECTOR,
88
AUTOCOMPLETE_SUGGESTION_SELECTOR
99
} from './autocomplete/autocomplete-list'
10-
import { qs } from './helpers'
10+
import { isMacOS, qs } from './helpers'
1111

1212
const SEARCH_INPUT_SELECTOR = 'form.search-bar input'
1313
const SEARCH_CLOSE_BUTTON_SELECTOR = 'form.search-bar .search-close-button'
@@ -138,10 +138,6 @@ function hideAutocomplete () {
138138
hideAutocompleteList()
139139
}
140140

141-
function isMacOS () {
142-
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
143-
}
144-
145141
let lastScrollTop = window.scrollY
146142
const topSearch = document.querySelector('.top-search')
147143
const sidebarMenu = document.getElementById('sidebar-menu')

0 commit comments

Comments
 (0)