Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Adds a query prop #51

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions packages/docsearch-react/src/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface DocSearchProps
indexName: string;
placeholder?: string;
searchParameters?: any;
query?: string;
transformItems?(items: DocSearchHit[]): DocSearchHit[];
hitComponent?(props: {
hit: DocSearchHit;
Expand Down
23 changes: 13 additions & 10 deletions packages/docsearch-react/src/DocSearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function DocSearchModal({
resultsFooterComponent = () => null,
navigator,
initialScrollY = 0,
query,
}: DocSearchModalProps) {
const [state, setState] = React.useState<
AutocompleteState<InternalDocSearchHit>
Expand All @@ -51,11 +52,13 @@ export function DocSearchModal({
const searchBoxRef = React.useRef<HTMLDivElement | null>(null);
const dropdownRef = React.useRef<HTMLDivElement | null>(null);
const inputRef = React.useRef<HTMLInputElement | null>(null);
const snipetLength = React.useRef<number>(10);
const snippetLength = React.useRef<number>(10);

const defaultQueryText = query ? query : '';
const initialQuery = React.useRef(
typeof window !== 'undefined'
? window.getSelection()!.toString().slice(0, MAX_QUERY_SIZE)
: ''
: defaultQueryText
).current;

const searchClient = useSearchClient(appId, apiKey);
Expand Down Expand Up @@ -166,13 +169,13 @@ export function DocSearchModal({
'url',
],
attributesToSnippet: [
`hierarchy.lvl1:${snipetLength.current}`,
`hierarchy.lvl2:${snipetLength.current}`,
`hierarchy.lvl3:${snipetLength.current}`,
`hierarchy.lvl4:${snipetLength.current}`,
`hierarchy.lvl5:${snipetLength.current}`,
`hierarchy.lvl6:${snipetLength.current}`,
`content:${snipetLength.current}`,
`hierarchy.lvl1:${snippetLength.current}`,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

`hierarchy.lvl2:${snippetLength.current}`,
`hierarchy.lvl3:${snippetLength.current}`,
`hierarchy.lvl4:${snippetLength.current}`,
`hierarchy.lvl5:${snippetLength.current}`,
`hierarchy.lvl6:${snippetLength.current}`,
`content:${snippetLength.current}`,
],
snippetEllipsisText: '…',
highlightPreTag: '<mark>',
Expand Down Expand Up @@ -292,7 +295,7 @@ export function DocSearchModal({
const isMobileMediaQuery = window.matchMedia('(max-width: 750px)');

if (isMobileMediaQuery.matches) {
snipetLength.current = 5;
snippetLength.current = 5;
}
}, []);

Expand Down