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
6 changes: 5 additions & 1 deletion src/components/search/docsearch/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {ScreenStateTranslations} from './ScreenState';
import {ScreenState} from './ScreenState';
import type {SearchBoxTranslations} from './SearchBox';
import {SearchBox} from './SearchBox';
import {MAX_QUERY_SIZE} from './constants';
import {MAX_QUERY_SIZE, MIN_QUERY_SIZE} from './constants';
import {createStoredSearches} from './stored-searches';
import type {
DocSearchHit,
Expand Down Expand Up @@ -231,6 +231,10 @@ export function DocSearchModal({
];
}

if (query.trim().length < MIN_QUERY_SIZE) {
return [];
}

const insightsActive = Boolean(insights);
const product = sourcesState.context.activeProduct as
| string
Expand Down
34 changes: 25 additions & 9 deletions src/components/search/docsearch/ScreenState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
import React from 'react';

import type {DocSearchProps} from './DocSearch';
import {MIN_QUERY_SIZE} from './constants';
import type {ErrorScreenTranslations} from './ErrorScreen';
import {ErrorScreen} from './ErrorScreen';
import type {NoResultsScreenTranslations} from './NoResultsScreen';
Expand All @@ -22,13 +23,14 @@ export type ScreenStateTranslations = Partial<{
noResultsScreen: NoResultsScreenTranslations;
}>;

export interface ScreenStateProps<TItem extends BaseItem>
extends AutocompleteApi<
TItem,
React.FormEvent,
React.MouseEvent,
React.KeyboardEvent
> {
export interface ScreenStateProps<
TItem extends BaseItem
> extends AutocompleteApi<
TItem,
React.FormEvent,
React.MouseEvent,
React.KeyboardEvent
> {
state: AutocompleteState<TItem>;
recentSearches: StoredSearchPlugin<StoredDocSearchHit>;
favoriteSearches: StoredSearchPlugin<StoredDocSearchHit>;
Expand All @@ -47,6 +49,19 @@ export interface ScreenStateProps<TItem extends BaseItem>

export const ScreenState = React.memo(
({translations = {}, ...props}: ScreenStateProps<InternalDocSearchHit>) => {
if (
props.state.query &&
props.state.query.trim().length < MIN_QUERY_SIZE
) {
return (
<div className="DocSearch-StartScreen">
<p className="DocSearch-Help" role="status">
Type at least {MIN_QUERY_SIZE} characters to search.
</p>
</div>
);
}

if (props.state.status === 'error') {
return <ErrorScreen translations={translations?.errorScreen} />;
}
Expand Down Expand Up @@ -82,8 +97,9 @@ export const ScreenState = React.memo(
// - Empty screen → Results screen
// - NoResults screen → NoResults screen with another query
return (
nextProps.state.status === 'loading' ||
nextProps.state.status === 'stalled'
nextProps.state.query.trim().length >= MIN_QUERY_SIZE &&
(nextProps.state.status === 'loading' ||
nextProps.state.status === 'stalled')
);
}
);
Expand Down
1 change: 1 addition & 0 deletions src/components/search/docsearch/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const MIN_QUERY_SIZE = 3;
export const MAX_QUERY_SIZE = 64;
Loading