Skip to content

Commit

Permalink
feature(search component): add buttonTriggers parameter for adding
Browse files Browse the repository at this point in the history
custom event trigger
  • Loading branch information
cdxker authored and skeptrunedev committed Dec 13, 2024
1 parent b36e117 commit 5a8f9a7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions clients/search-component/example/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { TanStackRouterDevtools } from "@tanstack/router-devtools";
export const Route = createRootRoute({
component: () => (
<>
<div className="random-trigger-location" style={{ background: "red" }}>
Hi, I'm a random root div that is outside the Hiearchy
</div>
<Outlet />
<TanStackRouterDevtools />
</>
Expand Down
6 changes: 6 additions & 0 deletions clients/search-component/example/src/routes/index.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export default function Home() {
),
},
]}
buttonTriggers={[
{
selector: ".random-trigger-location",
mode: "chat",
}
]}
useGroupSearch={useGroupSearch}
defaultAiQuestions={[
"What is Trieve?",
Expand Down
2 changes: 1 addition & 1 deletion clients/search-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"import": "./dist/vanilla/index.js"
}
},
"version": "0.2.15",
"version": "0.2.19",
"license": "MIT",
"homepage": "https://github.com/devflowinc/trieve/tree/main/clients/search-component",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions clients/search-component/src/TrieveModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { ModeSwitch } from "./ModeSwitch";
import { OpenModalButton } from "./OpenModalButton";
import { ChatProvider } from "../utils/hooks/chat-context";
import r2wc from "@r2wc/react-to-web-component";
import { setClickTriggers } from "../utils/hooks/setClickTriggers";

const Modal = () => {
useKeyboardNavigation();
setClickTriggers();
const { mode, open, setOpen, setMode, props } = useModalState();

useEffect(() => {
Expand Down
4 changes: 4 additions & 0 deletions clients/search-component/src/utils/hooks/modal-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export type ModalProps = {
openLinksInNewTab?: boolean;
onOpenChange?: (open: boolean) => void;
debounceMs?: number;
buttonTriggers?: {
selector: string;
mode: SearchModes;
}[];
};

const defaultProps = {
Expand Down
18 changes: 18 additions & 0 deletions clients/search-component/src/utils/hooks/setClickTriggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { startTransition } from "react";
import { useModalState } from "./modal-context";

export const setClickTriggers = () => {
const { setOpen, setMode, props } = useModalState();

props.buttonTriggers?.forEach((trigger) => {
const element = document.querySelector(trigger.selector);
if (element) {
element.addEventListener("click", () => {
startTransition(() => {
setMode(trigger.mode);
setOpen(true);
})
});
}
})
}

0 comments on commit 5a8f9a7

Please sign in to comment.