Skip to content

Commit

Permalink
Add cmd-k/ctrl-k shortcut to focus searchbar (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodapopcan authored Feb 22, 2024
1 parent d16e576 commit a001531
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
9 changes: 9 additions & 0 deletions assets/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ export function debounce (fn, milliseconds) {
export function getProjectNameAndVersion () {
return document.head.querySelector('meta[name=project][content]').content
}

/**
* Return `true` if the client's OS is MacOS
*
* @return {Boolean}
*/
export function isMacOS () {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
}
22 changes: 19 additions & 3 deletions assets/js/keyboard-shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { qs } from './helpers'
import { isMacOS, qs } from './helpers'
import { toggleSidebar } from './sidebar/sidebar-drawer'
import { focusSearchInput } from './search-bar'
import { cycleTheme } from './theme'
Expand Down Expand Up @@ -29,6 +29,11 @@ export const keyboardShortcuts = [
key: '/',
action: searchKeyAction
},
{
key: 'k',
hasModifier: true,
action: searchKeyAction
},
{
key: 'g',
description: 'Search HexDocs package',
Expand Down Expand Up @@ -64,9 +69,20 @@ function addEventListeners () {
function handleKeyDown (event) {
if (state.shortcutBeingPressed) { return }
if (event.target.matches('input, textarea')) { return }
if (event.ctrlKey || event.metaKey || event.altKey) { return }

const matchingShortcut = keyboardShortcuts.find(shortcut => shortcut.key === event.key)
const matchingShortcut = keyboardShortcuts.find(shortcut => {
if (shortcut.hasModifier) {
if (isMacOS() && event.metaKey) { return shortcut.key === event.key }
if (event.ctrlKey) { return shortcut.key === event.key }

return false
} else {
if (event.ctrlKey || event.metaKey || event.altKey) { return false }

return shortcut.key === event.key
}
})

if (!matchingShortcut) { return }
state.shortcutBeingPressed = matchingShortcut

Expand Down
6 changes: 1 addition & 5 deletions assets/js/search-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AUTOCOMPLETE_CONTAINER_SELECTOR,
AUTOCOMPLETE_SUGGESTION_SELECTOR
} from './autocomplete/autocomplete-list'
import { qs } from './helpers'
import { isMacOS, qs } from './helpers'

const SEARCH_INPUT_SELECTOR = 'form.search-bar input'
const SEARCH_CLOSE_BUTTON_SELECTOR = 'form.search-bar .search-close-button'
Expand Down Expand Up @@ -138,10 +138,6 @@ function hideAutocomplete () {
hideAutocompleteList()
}

function isMacOS () {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
}

let lastScrollTop = window.scrollY
const topSearch = document.querySelector('.top-search')
const sidebarMenu = document.getElementById('sidebar-menu')
Expand Down

0 comments on commit a001531

Please sign in to comment.