Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
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
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
5 changes: 4 additions & 1 deletion packages/docsearch-react/src/ScreenState.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
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 KeyboardEvent | MouseEvent>(
event: TEvent
): boolean {
const isMiddleClick = (event as MouseEvent).button === 1;

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