Skip to content

Commit

Permalink
(EAI-543) [UI] Create HotkeyTrigger (#512)
Browse files Browse the repository at this point in the history
* (EAI-543) [UI] Create HotkeyTrigger

* add docs
  • Loading branch information
nlarew authored Sep 25, 2024
1 parent 269ba2b commit 11adc59
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/docs/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ The `<InputBarTrigger />` component opens a view component (like `<ModalView />`
| `suggestedPrompts` | `string[]?` | A list of suggested prompts that appear in the input bar dropdown menu. | If no prompts are specified, the dropdown is not shown. |
| `placeholder` | `string?` | The placeholder text shown when the input bar is empty. | If not specified, the input bar uses default placeholders. |

### `HotkeyTrigger`

The `<HotkeyTrigger />` component opens a view component (like `<ModalView />`) when the presses a specific key on their keyboard. It accepts the following props:

| Prop | Type | Description | Default |
| ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- |
| `onKey` | `string` | The key to listen for. This matches the value of [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key). | |

### `ModalView`

The `<ModalView />` component renders a chat message feed in a modal window. It accepts the following props:
Expand Down
7 changes: 5 additions & 2 deletions packages/mongodb-chatbot-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Toggle from "@leafygreen-ui/toggle";
import { Chatbot } from "./Chatbot";
import { DocsChatbot } from "./DocsChatbot";
import { DevCenterChatbot } from "./DevCenterChatbot";
import { HotkeyTrigger } from "./HotkeyTrigger";

const prefersDarkMode = () =>
window.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false;
Expand Down Expand Up @@ -59,7 +60,7 @@ function App() {
<div className={app_background(darkMode)}>
<div className={styles.main_content}>
<Chatbot
name="MongoDB AI"
name="MongoDB AI (Docs)"
shouldStream={shouldStream}
darkMode={darkMode}
fetchOptions={{ credentials: "include" }}
Expand All @@ -72,9 +73,10 @@ function App() {
maxInputCharacters={3000}
>
<DocsChatbot suggestedPrompts={SUGGESTED_PROMPTS} />
<HotkeyTrigger onKey="?" />
</Chatbot>
<Chatbot
name="MongoDB AI"
name="MongoDB AI (Dev Center)"
shouldStream={shouldStream}
darkMode={darkMode}
fetchOptions={{ credentials: "include" }}
Expand All @@ -89,6 +91,7 @@ function App() {
initialMessageSuggestedPrompts={SUGGESTED_PROMPTS}
initialMessageReferences={initialMessageReferences}
/>
<HotkeyTrigger onKey="/" />
</Chatbot>
</div>
<Controls>
Expand Down
29 changes: 29 additions & 0 deletions packages/mongodb-chatbot-ui/src/HotkeyTrigger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useEffect } from "react";
import { useChatbotContext } from "./useChatbotContext";

export type HotkeyTriggerProps = {
onKey: string;
};

export function HotkeyTrigger({ onKey }: HotkeyTriggerProps) {
const { openChat } = useChatbotContext();

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === onKey) {
openChat();
}
};

// Add event listener when component mounts
window.addEventListener("keydown", handleKeyDown);

// Remove event listener when component unmounts
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [onKey, openChat]);

// This component doesn't render anything
return null;
}
1 change: 1 addition & 0 deletions packages/mongodb-chatbot-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
InputBarTrigger,
type InputBarTriggerProps,
} from "./InputBarTrigger.tsx";
export { HotkeyTrigger, type HotkeyTriggerProps } from "./HotkeyTrigger.tsx";
export {
FloatingActionButtonTrigger,
type FloatingActionButtonTriggerProps,
Expand Down

0 comments on commit 11adc59

Please sign in to comment.