Skip to content

Commit

Permalink
feat: prefill the search if the button is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Feb 7, 2023
1 parent 771aaff commit 3a8742a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/prefill-click.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"meilisearch-docsearch": "patch"
---

Prefill the search with highlighted text when opening the modal by clicking the search button.
7 changes: 6 additions & 1 deletion src/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const DocSearch: Component<DocSearchProps> = (props) => {
const onOpen = () => setIsOpen(true);
const onClose = () => setIsOpen(false);
const onInput = (query: string) => setInitialQuery(query);
const onClick = () => {
const selectedText = window.getSelection();
if (selectedText) setInitialQuery(selectedText.toString());
setIsOpen(true);
};

useHotKeys({
isOpen,
Expand All @@ -41,7 +46,7 @@ export const DocSearch: Component<DocSearchProps> = (props) => {
<>
<DocSearchButton
translations={props?.translations?.button}
onClick={() => setIsOpen(true)}
onClick={onClick}
/>
{isOpen() && (
<Portal mount={environment.document.body}>
Expand Down

0 comments on commit 3a8742a

Please sign in to comment.