feat(webui): newline instead of send on touch keyboards
A plain Enter in the composer now inserts a newline on coarse-pointer devices; sending stays on the send button. The slash/mention pickers still select with Enter, and precision-pointer (desktop) behavior is unchanged. Adds enterKeyHint to label the soft keyboard action and enables interactive-widget=resizes-content in the viewport meta so Android keyboards resize the layout viewport.
This commit is contained in:
1 parent
96ad7b4dbf
commit
2750a19601
2 files changed
+110
-1
No files matched your search
@@ -971,6 +971,9 @@ export function ThreadComposer({
|
||||
const [recentSlashCommands, setRecentSlashCommands] = useState<string[]>(() => readSlashRecents());
|
||||
const [queuedPrompts, setQueuedPrompts] = useState<QueuedPrompt[]>([]);
|
||||
const hasTouchPrimaryPointer = useMediaQuery("(hover: none) and (pointer: coarse)");
|
||||
// Wider than hasTouchPrimaryPointer on purpose: tablets with a physical
|
||||
// keyboard still count as touch for the Enter key behavior below.
|
||||
const hasCoarsePointer = useMediaQuery("(pointer: coarse)");
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
const mentionOverlayRef = useRef<HTMLDivElement>(null);
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
@@ -2181,6 +2184,19 @@ export function ThreadComposer({
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Touch keyboards: a plain Enter inserts a newline; sending stays on the
|
||||
// send button. The select-on-Enter menu branches above take precedence.
|
||||
if (
|
||||
e.key === "Enter"
|
||||
&& !e.shiftKey
|
||||
&& !e.altKey
|
||||
&& !e.ctrlKey
|
||||
&& !e.metaKey
|
||||
&& !e.nativeEvent.isComposing
|
||||
&& hasCoarsePointer
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) {
|
||||
e.preventDefault();
|
||||
if (canQueueGuidance) {
|
||||
@@ -2533,6 +2549,7 @@ export function ThreadComposer({
|
||||
}}
|
||||
onPaste={onPaste}
|
||||
rows={1}
|
||||
enterKeyHint={hasCoarsePointer ? "enter" : undefined}
|
||||
placeholder={sessionDragPreview ? "" : resolvedPlaceholder}
|
||||
disabled={interactionDisabled}
|
||||
aria-label={inputAriaLabel ?? t("thread.composer.inputAria")}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import { act, createEvent, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
@@ -347,6 +347,17 @@ function renderPresetComposer(
|
||||
};
|
||||
}
|
||||
|
||||
function stubCoarsePointer(): void {
|
||||
vi.stubGlobal("matchMedia", vi.fn((query: string) => ({
|
||||
matches: query.includes("pointer: coarse"),
|
||||
media: query,
|
||||
onchange: null,
|
||||
addEventListener: vi.fn(),
|
||||
removeEventListener: vi.fn(),
|
||||
dispatchEvent: vi.fn(),
|
||||
})));
|
||||
}
|
||||
|
||||
describe("ThreadComposer", () => {
|
||||
it("locks an async send and keeps the draft when it is rejected", async () => {
|
||||
let resolveSend!: (accepted: boolean) => void;
|
||||
@@ -407,6 +418,87 @@ describe("ThreadComposer", () => {
|
||||
expect(input).not.toHaveFocus();
|
||||
});
|
||||
|
||||
it("inserts a newline instead of sending on Enter on coarse-pointer devices", () => {
|
||||
stubCoarsePointer();
|
||||
const onSend = vi.fn();
|
||||
render(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: "line one" } });
|
||||
const keyEvent = createEvent.keyDown(input, { key: "Enter" });
|
||||
fireEvent(input, keyEvent);
|
||||
|
||||
expect(keyEvent.defaultPrevented).toBe(false);
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps sending on Enter on precision-pointer devices", () => {
|
||||
const onSend = vi.fn();
|
||||
render(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: "desktop message" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
expect(onSend).toHaveBeenCalledWith("desktop message", undefined, undefined);
|
||||
});
|
||||
|
||||
it("still selects a slash command with Enter on coarse-pointer devices", () => {
|
||||
stubCoarsePointer();
|
||||
const onSend = vi.fn();
|
||||
render(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
placeholder="Type your message..."
|
||||
slashCommands={COMMANDS}
|
||||
/>,
|
||||
);
|
||||
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: "/" } });
|
||||
expect(screen.getByRole("option", { name: /\/history/i })).toHaveAttribute(
|
||||
"aria-selected",
|
||||
"true",
|
||||
);
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
expect(input).toHaveValue("/history ");
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("labels the keyboard action as enter on coarse-pointer devices", () => {
|
||||
stubCoarsePointer();
|
||||
render(
|
||||
<ThreadComposer
|
||||
onSend={vi.fn()}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByLabelText("Message input")).toHaveAttribute("enterkeyhint", "enter");
|
||||
});
|
||||
|
||||
it("does not set enterkeyhint on precision-pointer devices", () => {
|
||||
render(
|
||||
<ThreadComposer
|
||||
onSend={vi.fn()}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByLabelText("Message input")).not.toHaveAttribute("enterkeyhint");
|
||||
});
|
||||
|
||||
it("focuses and sends a removable quoted answer excerpt", async () => {
|
||||
const onSend = vi.fn();
|
||||
const onQuotedContextChange = vi.fn();
|
||||
|
||||
Reference in new issue
Block a user