Skip to content

Add cmd-k/ctrl-k shortcut to focus searchbar #1870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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