Skip to content

Commit

Permalink
Add cmd-k/ctrl-k shortcut to focus searchbar
Browse files Browse the repository at this point in the history
  • Loading branch information
sodapopcan committed Feb 21, 2024
1 parent 5962468 commit c2d1e27
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 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
16 changes: 11 additions & 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 All @@ -17,6 +17,7 @@ const SEARCH_CLOSE_BUTTON_SELECTOR = 'form.search-bar .search-close-button'
*/
export function initialize () {
addEventListeners()
setInputPlaceholder()
}

/**
Expand Down Expand Up @@ -102,6 +103,15 @@ function addEventListeners () {
})
}

function setInputPlaceholder () {
const searchInput = qs(SEARCH_INPUT_SELECTOR)
if (isMacOS()) {
searchInput.setAttribute('placeholder', 'Search - ⌘K')
} else {
searchInput.setAttribute('placeholder', 'Search - ctrl K')
}
}

function handleAutocompleteFormSubmission (event) {
const searchInput = qs(SEARCH_INPUT_SELECTOR)
const newWindowKeyDown = (event.shiftKey || event.ctrlKey)
Expand Down Expand Up @@ -138,10 +148,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
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/html/templates/sidebar_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<form class="search-bar" action="search.html">
<label class="search-label">
<span class="sr-only">Search documentation of <%= config.project %></span>
<input name="q" type="text" class="search-input" placeholder="Press / to search" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
<input name="q" type="text" class="search-input" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
</label>
<button type="submit" class="search-button" aria-label="Submit Search">
<i class="ri-search-2-line ri-lg" aria-hidden="true" title="Submit search"></i>
Expand Down

0 comments on commit c2d1e27

Please sign in to comment.