Skip to content

Commit

Permalink
Use ⌘ to trigger search bar for Mac users
Browse files Browse the repository at this point in the history
The standard way to trigger a search bar on websites for Mac users has become the command key (see for example tailwind.com or github.com).

This PR allows focusing the search bar with either the `ctrlKey` or the `metakey`, so both platforms are supported. It also updates the placeholder text for MacOS users to show ⌘+K instead of Ctrl+K.
  • Loading branch information
wrgoldstein committed Nov 28, 2023
1 parent 13b9746 commit 80e98f5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/theme/SearchBar/DocSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ class DocSearch {
DocSearch.bindSearchBoxEvent();
}


// Ctrl + K should focus the search bar, emulating the Algolia search UI
// Ctrl/Cmd + K should focus the search bar, emulating the Algolia search UI
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key == 'k') {
if ((e.ctrlKey || e.metaKey) && e.key == 'k') {
this.input.focus();

// By default, using Ctrl + K in Chrome will open the location bar, so disable this
Expand Down
5 changes: 4 additions & 1 deletion src/theme/SearchBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ const Search = props => {
[props.isSearchBarExpanded]
);

let placeholder
if (isBrowser) {
loadAlgolia();
placeholder = window.navigator.platform.startsWith("Mac") ?
'Search ⌘+K' : 'Search Ctrl+K'
}

return (
Expand All @@ -115,7 +118,7 @@ const Search = props => {
<input
id="search_input_react"
type="search"
placeholder={indexReady ? 'Search Ctrl+K' : 'Loading...'}
placeholder={indexReady ? placeholder : 'Loading...'}
aria-label="Search"
className={clsx(
"navbar__search-input",
Expand Down

0 comments on commit 80e98f5

Please sign in to comment.