Skip to content

Commit

Permalink
fix: escape key interop with other radix utilities (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jun 26, 2024
1 parent 433fd40 commit b267991
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@radix-ui/react-primitive": "^2.0.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-use-callback-ref": "^1.1.0",
"@radix-ui/react-use-escape-keydown": "^1.1.0",
"nanoid": "^5.0.7",
"react-textarea-autosize": "^8.5.3",
"zod": "^3.23.8",
Expand Down
15 changes: 9 additions & 6 deletions packages/react/src/primitives/composer/ComposerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TextareaAutosize, {
import { useComposerContext } from "../../context/react/ComposerContext";
import { useThreadContext } from "../../context/react/ThreadContext";
import { useOnScrollToBottom } from "../../utils/hooks/useOnScrollToBottom";
import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";

type ComposerInputProps = TextareaAutosizeProps & {
asChild?: boolean;
Expand All @@ -42,15 +43,17 @@ export const ComposerInput = forwardRef<
const textareaRef = useRef<HTMLTextAreaElement>(null);
const ref = useComposedRefs(forwardedRef, textareaRef);

useEscapeKeydown((e) => {
const composer = useComposer.getState();
if (composer.cancel()) {
e.preventDefault();
}
});

const handleKeyPress = (e: KeyboardEvent) => {
if (disabled) return;

if (e.key === "Escape") {
const composer = useComposer.getState();
if (composer.cancel()) {
e.preventDefault();
}
} else if (e.key === "Enter" && e.shiftKey === false) {
if (e.key === "Enter" && e.shiftKey === false) {
const isRunning = useThread.getState().isRunning;
if (!isRunning) {
e.preventDefault();
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit b267991

Please sign in to comment.