Skip to content

Commit

Permalink
implement focus trap with focus-trap-react
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Nov 20, 2024
1 parent 4b3bc21 commit 4f16595
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"eslint-plugin-testing-library": "^5.11.0",
"eslint-webpack-plugin": "^4.0.1",
"file-loader": "^6.2.0",
"focus-trap-react": "^10.3.1",
"html-webpack-plugin": "^5.5.1",
"identity-obj-proxy": "^3.0.0",
"iframe-phone": "^1.3.1",
Expand Down
3 changes: 3 additions & 0 deletions src/components/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
color: rgb(110,109,95);
text-align: center;
padding: 10px;
&.isActive {
background-color: #fee9ff;
}

h1 {
font-size: 12px;
Expand Down
45 changes: 31 additions & 14 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from "react";
import FocusTrap from "focus-trap-react";
import { initializePlugin } from "@concord-consortium/codap-plugin-api";
import { ReadAloudMenu } from "./readaloud-menu";
import { ChatInputComponent } from "./chat-input";
Expand Down Expand Up @@ -29,9 +30,11 @@ export const App = () => {
const [chatTranscript, setChatTranscript] = useState<ChatTranscript>({messages: [{speaker: "DAVAI", content: greeting, timestamp: timeStamp()}]});
const [readAloudEnabled, setReadAloudEnabled] = useState(false);
const [playbackSpeed, setPlaybackSpeed] = useState(1);
const [activeTrap, setActiveTrap] = useState(false);

useEffect(() => {
initializePlugin({pluginName: kPluginName, version: kVersion, dimensions: kInitialDimensions});
setActiveTrap(true);
}, []);

const handleSetReadAloudEnabled = () => {
Expand All @@ -55,19 +58,33 @@ export const App = () => {
};

return (
<div className="App">
<h1>
DAVAI
<span>(Data Analysis through Voice and Artificial Intelligence)</span>
</h1>
<ChatTranscriptComponent chatTranscript={chatTranscript} />
<ChatInputComponent onSubmit={handleChatInputSubmit} />
<ReadAloudMenu
enabled={readAloudEnabled}
onToggle={handleSetReadAloudEnabled}
playbackSpeed={playbackSpeed}
onPlaybackSpeedSelect={handleSetPlaybackSpeed}
/>
</div>
<FocusTrap
active={activeTrap}
focusTrapOptions={{
allowOutsideClick: true,
escapeDeactivates: true,
onDeactivate: () => setActiveTrap(false),
onActivate: () => setActiveTrap(true)
}}
>
<div
onFocus={() => setActiveTrap(true)}
role="main"
className={`App ${activeTrap && "isActive"}`}
>
<h1>
DAVAI
<span>(Data Analysis through Voice and Artificial Intelligence)</span>
</h1>
<ChatTranscriptComponent chatTranscript={chatTranscript} />
<ChatInputComponent onSubmit={handleChatInputSubmit} />
<ReadAloudMenu
enabled={readAloudEnabled}
onToggle={handleSetReadAloudEnabled}
playbackSpeed={playbackSpeed}
onPlaybackSpeedSelect={handleSetPlaybackSpeed}
/>
</div>
</FocusTrap>
);
};
4 changes: 0 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ const container = document.getElementById("app");
if (container) {
const root = createRoot(container);

window.addEventListener("load", () => {
window.focus();
});

root.render(<App />);
}

0 comments on commit 4f16595

Please sign in to comment.