Skip to content

Fix: Prevent calling onClose() when shiftKey, ctrlKey or metaKey is pressed #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 6 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 8 additions & 5 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useSearchClient } from './useSearchClient';
import { useTouchEvents } from './useTouchEvents';
import { useTrapFocus } from './useTrapFocus';
import { groupBy, identity, noop, removeHighlightTags } from './utils';
import { isModifierEvent } from './utils/isModifierEvent';

export type ModalTranslations = Partial<{
searchBox: SearchBoxTranslations;
Expand Down Expand Up @@ -156,7 +157,7 @@ export function DocSearchModal({
onSelect({ item, event }) {
saveRecentSearch(item);

if (!event.shiftKey && !event.ctrlKey && !event.metaKey) {
if (!isModifierEvent(event)) {
onClose();
}
},
Expand All @@ -172,7 +173,7 @@ export function DocSearchModal({
onSelect({ item, event }) {
saveRecentSearch(item);

if (!event.shiftKey && !event.ctrlKey && !event.metaKey) {
if (!isModifierEvent(event)) {
onClose();
}
},
Expand Down Expand Up @@ -256,7 +257,7 @@ export function DocSearchModal({
onSelect({ item, event }) {
saveRecentSearch(item);

if (!event.shiftKey && !event.ctrlKey && !event.metaKey) {
if (!isModifierEvent(event)) {
onClose();
}
},
Expand Down Expand Up @@ -431,9 +432,11 @@ export function DocSearchModal({
inputRef={inputRef}
translations={screenStateTranslations}
getMissingResultsUrl={getMissingResultsUrl}
onItemClick={(item) => {
onItemClick={(item, event) => {
saveRecentSearch(item);
onClose();
if (!isModifierEvent(event)) {
onClose();
}
}}
/>
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/docsearch-react/src/Results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface ResultsProps<TItem extends BaseItem>
runDeleteTransition: (cb: () => void) => void;
runFavoriteTransition: (cb: () => void) => void;
}) => React.ReactNode;
onItemClick: (item: TItem) => void;
onItemClick: (item: TItem, event: KeyboardEvent | MouseEvent) => void;
hitComponent: DocSearchProps['hitComponent'];
}

Expand Down Expand Up @@ -104,8 +104,8 @@ function Result<TItem extends StoredDocSearchHit>({
{...getItemProps({
item,
source: collection.source,
onClick() {
onItemClick(item);
onClick(event) {
onItemClick(item, event);
},
})}
>
Expand Down
2 changes: 1 addition & 1 deletion packages/docsearch-react/src/ScreenState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ScreenStateProps<TItem extends BaseItem>
state: AutocompleteState<TItem>;
recentSearches: StoredSearchPlugin<StoredDocSearchHit>;
favoriteSearches: StoredSearchPlugin<StoredDocSearchHit>;
onItemClick: (item: InternalDocSearchHit) => void;
onItemClick: (item: InternalDocSearchHit, event: KeyboardEvent | MouseEvent) => void;
inputRef: React.MutableRefObject<HTMLInputElement | null>;
hitComponent: DocSearchProps['hitComponent'];
indexName: DocSearchProps['indexName'];
Expand Down
17 changes: 17 additions & 0 deletions packages/docsearch-react/src/utils/isModifierEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Detect when an event is modified with a special key to let the browser
* trigger its default behavior.
*/
export function isModifierEvent<TEvent extends MouseEvent | KeyboardEvent>(
event: TEvent
): boolean {
const isMiddleClick = (event as MouseEvent).button === 1;

return (
isMiddleClick ||
event.altKey ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey
);
}