Skip to content
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

Fix: Prevent calling onClose() when shiftKey, ctrlKey or metaKey is pressed #1870

Merged
merged 6 commits into from Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/docsearch-react/src/DocSearchModal.tsx
Expand Up @@ -20,7 +20,13 @@ import type {
import { useSearchClient } from './useSearchClient';
import { useTouchEvents } from './useTouchEvents';
import { useTrapFocus } from './useTrapFocus';
import { groupBy, identity, noop, removeHighlightTags } from './utils';
import {
groupBy,
identity,
noop,
removeHighlightTags,
isModifierEvent,
} from './utils';

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

if (!event.shiftKey && !event.ctrlKey && !event.metaKey) {
if (!isModifierEvent(event)) {
onClose();
}
},
Expand All @@ -172,7 +178,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 +262,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 +437,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
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
5 changes: 4 additions & 1 deletion packages/docsearch-react/src/ScreenState.tsx
Expand Up @@ -32,7 +32,10 @@ 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
1 change: 1 addition & 0 deletions packages/docsearch-react/src/utils/index.ts
@@ -1,4 +1,5 @@
export * from './groupBy';
export * from './identity';
export * from './isModifierEvent';
export * from './noop';
export * from './removeHighlightTags';
17 changes: 17 additions & 0 deletions packages/docsearch-react/src/utils/isModifierEvent.ts
@@ -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 KeyboardEvent | MouseEvent>(
event: TEvent
): boolean {
const isMiddleClick = (event as MouseEvent).button === 1;

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