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 3 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
13 changes: 8 additions & 5 deletions packages/docsearch-react/src/DocSearchModal.tsx
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';
shortcuts marked this conversation as resolved.
Show resolved Hide resolved

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
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
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
@@ -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
);
}