Skip to content

Commit

Permalink
handle search keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaMoradi committed Dec 19, 2021
1 parent 7f46583 commit cfda260
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/components/util/AppbarSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import SearchIcon from '@mui/icons-material/Search';
import { IconButton, Input } from '@mui/material';
import CancelIcon from '@mui/icons-material/Cancel';
Expand All @@ -32,6 +32,22 @@ export default function AppbarSearch() {
if (inputRef && inputRef.current) inputRef.current.focus();
});
};

const handleSearchShortcut = (e: KeyboardEvent) => {
if ((e.code === 'F3') || (e.ctrlKey && e.code === 'KeyF')) {
e.preventDefault();
openSearch();
}
};

useEffect(() => {
window.addEventListener('keydown', handleSearchShortcut);

return () => {
window.removeEventListener('keydown', handleSearchShortcut);
};
}, [handleSearchShortcut]);

return (
<>
{searchOpen
Expand Down

0 comments on commit cfda260

Please sign in to comment.