Skip to content

Commit

Permalink
fix(docsearch): don't open modal on / when editing text
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Aug 4, 2020
1 parent 78a0aa8 commit 45cf5c3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/useDocSearchKeyboardEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ interface UseDocSearchKeyboardEventsProps {
searchButtonRef?: React.RefObject<HTMLButtonElement>;
}

function isEditingContent(event: KeyboardEvent): boolean {
const element = event.target as HTMLElement;
const tagName = element.tagName;

return (
element.isContentEditable ||
tagName === 'INPUT' ||
tagName === 'SELECT' ||
tagName === 'TEXTAREA'
);
}

export function useDocSearchKeyboardEvents({
isOpen,
onOpen,
Expand All @@ -23,7 +35,7 @@ export function useDocSearchKeyboardEvents({
(event.key === 'k' && (event.metaKey || event.ctrlKey)) ||
// The `/` shortcut opens but doesn't close the modal because it's
// a character.
(event.key === '/' && !isOpen)
(!isEditingContent(event) && event.key === '/' && !isOpen)
) {
event.preventDefault();

Expand Down

0 comments on commit 45cf5c3

Please sign in to comment.