π OpenHands: AI-Driven Development
fix: disable chat input drag-resize when not bottom-anchored (OHE-3062) (#16479)
Tim O'Farrelle8e6f45cc8f0
diff --git a/__tests__/hooks/use-drag-resize.test.tsx b/__tests__/hooks/use-drag-resize.test.tsxnew file mode 100644index 0000000000..885c85e953--- /dev/null+++ b/__tests__/hooks/use-drag-resize.test.tsx@@ -0,0 +1,184 @@+import { act, renderHook } from "@testing-library/react";+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";+import { useDragResize } from "#/hooks/use-drag-resize";++// Mock isMobileDevice to always return false so the hook uses the desktop+// event-listener path (document.addEventListener), which our test simulates.+vi.mock("#/utils/utils", async (importOriginal) => ({+ ...(await importOriginal<typeof import("#/utils/utils")>()),+ isMobileDevice: () => false,+}));++/**+ * Coverage for the top-grip resize direction fix (OHE-3062).+ *+ * The same chat-input component is used on the conversation page (where the+ * input is bottom-anchored) and the home page (where the input is centred).+ * On the conversation page, dragging the top grip makes the box grow upward β+ * correct. On the home page, the same drag would grow the box downward, which+ * is confusing, so the hook disables manual resizing entirely when the input+ * is not bottom-anchored.+ */+describe("useDragResize β disabled when not bottom-anchored", () => {+ let inputEl: HTMLDivElement;+ let wrapperEl: HTMLDivElement;+ let gripEl: HTMLDivElement;++ // Helper to simulate a drag from startY upward by `distance` px.+ const dragUp = (startY: number, distance: number) => {+ document.dispatchEvent(+ new MouseEvent("mousemove", { clientY: startY - distance }),+ );+ document.dispatchEvent(new MouseEvent("mouseup", { clientY: startY }));+ };++ beforeEach(() => {+ // Build the DOM structure that matches CustomChatInput:+ // <div class="relative w-full" id="wrapper"> β wrapper+ // <div id="resize-grip"> β grip+ // <div class="chat-input-container"> β container+ // <div id="inputEl"> β contentEditable (height target)+ wrapperEl = document.createElement("div");+ wrapperEl.id = "wrapper";++ gripEl = document.createElement("div");+ gripEl.id = "resize-grip";++ const containerEl = document.createElement("div");+ containerEl.className = "chat-input-container";++ inputEl = document.createElement("div");+ inputEl.style.height = "20px";++ containerEl.appendChild(inputEl);+ wrapperEl.appendChild(gripEl);+ wrapperEl.appendChild(containerEl);+ document.body.appendChild(wrapperEl);++ // jsdom doesn't implement offsetHeight; mock it to return the style height.+ Object.defineProperty(inputEl, "offsetHeight", {+ get() {+ return parseFloat(inputEl.style.height || "20");+ },+ configurable: true,+ });+ });++ afterEach(() => {+ wrapperEl.remove();+ vi.restoreAllMocks();+ });++ it("allows resizing when the input is bottom-anchored", () => {+ // Simulate a bottom-anchored layout: the wrapper's bottom is at the+ // viewport bottom.+ vi.spyOn(wrapperEl, "getBoundingClientRect").mockReturnValue({+ top: 668,+ bottom: 768, // == window.innerHeight+ left: 0,+ right: 0,+ width: 800,+ height: 100,+ x: 0,+ y: 668,+ toJSON: () => ({}),+ });+ vi.spyOn(window, "innerHeight", "get").mockReturnValue(768);++ const { result } = renderHook(() =>+ useDragResize({+ elementRef: { current: inputEl },+ minHeight: 20,+ maxHeight: 400,+ }),+ );++ act(() => {+ result.current.handleGripMouseDown(+ new MouseEvent("mousedown", {+ clientY: 100,+ }) as unknown as React.MouseEvent,+ );+ });++ // Drag up 30px β height should increase by 30+ act(() => dragUp(100, 30));++ expect(parseFloat(inputEl.style.height)).toBe(50);+ });++ it("does NOT resize when the input is not bottom-anchored (home page)", () => {+ // Simulate a centred layout: the wrapper's bottom is well above the+ // viewport bottom.+ vi.spyOn(wrapperEl, "getBoundingClientRect").mockReturnValue({+ top: 100,+ bottom: 200, // 200 << 768, not bottom-anchored+ left: 0,+ right: 0,+ width: 800,+ height: 100,+ x: 0,+ y: 100,+ toJSON: () => ({}),+ });+ vi.spyOn(window, "innerHeight", "get").mockReturnValue(768);++ const { result } = renderHook(() =>+ useDragResize({+ elementRef: { current: inputEl },+ minHeight: 20,+ maxHeight: 400,+ }),+ );++ act(() => {+ result.current.handleGripMouseDown(+ new MouseEvent("mousedown", {+ clientY: 100,+ }) as unknown as React.MouseEvent,+ );+ });++ // Attempt to drag up 30px β should be a no-op+ act(() => dragUp(100, 30));++ // Height should NOT have changed+ expect(parseFloat(inputEl.style.height)).toBe(20);+ });++ it("does NOT resize via touch when not bottom-anchored", () => {+ vi.spyOn(wrapperEl, "getBoundingClientRect").mockReturnValue({+ top: 100,+ bottom: 200,+ left: 0,+ right: 0,+ width: 800,+ height: 100,+ x: 0,+ y: 100,+ toJSON: () => ({}),+ });+ vi.spyOn(window, "innerHeight", "get").mockReturnValue(768);++ const { result } = renderHook(() =>+ useDragResize({+ elementRef: { current: inputEl },+ minHeight: 20,+ maxHeight: 400,+ }),+ );++ act(() => {+ result.current.handleGripTouchStart(+ new TouchEvent("touchstart", {+ touches: [{ clientY: 100 } as unknown as Touch],+ }) as unknown as React.TouchEvent,+ );+ });++ // Attempt to drag β should be a no-op+ act(() => dragUp(100, 30));++ expect(parseFloat(inputEl.style.height)).toBe(20);+ });+});diff --git a/src/components/features/chat/components/chat-input-grip.tsx b/src/components/features/chat/components/chat-input-grip.tsxindex a2f2325a54..686ca1770d 100644--- a/src/components/features/chat/components/chat-input-grip.tsx+++ b/src/components/features/chat/components/chat-input-grip.tsx@@ -5,6 +5,7 @@ interface ChatInputGripProps {gripRef: React.RefObject<HTMLDivElement | null>;isGripVisible: boolean;isGripDragging: boolean;+ canResize: boolean;handleTopEdgeClick: (e: React.MouseEvent) => void;handleGripMouseDown: (e: React.MouseEvent) => void;handleGripTouchStart: (e: React.TouchEvent) => void;@@ -14,30 +15,38 @@ export function ChatInputGrip({gripRef,isGripVisible,isGripDragging,+ canResize,handleTopEdgeClick,handleGripMouseDown,handleGripTouchStart,}: ChatInputGripProps) {return (<div- className="absolute top-0 left-0 w-full h-3 z-20 group"+ className={cn(+ "absolute top-0 left-0 w-full h-3 z-20 group",+ !canResize && "pointer-events-none",+ )}id="resize-grip"- onClick={handleTopEdgeClick}+ onClick={canResize ? handleTopEdgeClick : undefined}>{/* Resize hit target; 1px line is drawn at top-0 (flush with chat input box) */}- <div- className="absolute inset-0 z-[1] cursor-ns-resize select-none"- onMouseDown={handleGripMouseDown}- onTouchStart={handleGripTouchStart}- aria-hidden- />+ {canResize && (+ <div+ className="absolute inset-0 z-[1] cursor-ns-resize select-none"+ onMouseDown={handleGripMouseDown}+ onTouchStart={handleGripTouchStart}+ aria-hidden+ />+ )}<divref={gripRef}className={cn("pointer-events-none absolute top-0 left-0 w-full h-px bg-white z-[2] transition-opacity duration-200",- isGripVisible || isGripDragging- ? "opacity-100"- : "opacity-0 group-hover:opacity-100",+ !canResize && "opacity-0",+ canResize &&+ (isGripVisible || isGripDragging+ ? "opacity-100"+ : "opacity-0 group-hover:opacity-100"),)}/></div>diff --git a/src/components/features/chat/custom-chat-input.tsx b/src/components/features/chat/custom-chat-input.tsxindex 682daac488..ae879522a8 100644--- a/src/components/features/chat/custom-chat-input.tsx+++ b/src/components/features/chat/custom-chat-input.tsx@@ -107,6 +107,7 @@ export function CustomChatInput({gripRef,isGripVisible,isGripDragging,+ canResize,handleTopEdgeClick,smartResize,handleGripMouseDown,@@ -176,6 +177,7 @@ export function CustomChatInput({gripRef={gripRef}isGripVisible={isGripVisible}isGripDragging={isGripDragging}+ canResize={canResize}handleTopEdgeClick={handleTopEdgeClick}handleGripMouseDown={handleGripMouseDown}handleGripTouchStart={handleGripTouchStart}diff --git a/src/hooks/chat/use-grip-resize.ts b/src/hooks/chat/use-grip-resize.tsindex c03a98b6e3..21cff68d06 100644--- a/src/hooks/chat/use-grip-resize.ts+++ b/src/hooks/chat/use-grip-resize.ts@@ -1,6 +1,7 @@import type { RefObject, MouseEvent } from "react";-import { useRef, useState, useCallback } from "react";+import { useRef, useState, useCallback, useEffect } from "react";import { useAutoResize } from "#/hooks/use-auto-resize";+import { isBottomAnchored } from "#/hooks/use-drag-resize";import { CHAT_INPUT } from "#/utils/constants";import {IMessageToSend,@@ -16,6 +17,10 @@ export const useGripResize = () => {const [isGripVisible, setIsGripVisible] = useState(false);const [isGripDragging, setIsGripDragging] = useState(false);+ // Manual resizing only makes sense when the input is bottom-anchored (e.g.+ // the conversation page). On the home page the input is centred and dragging+ // the top grip would grow the box downward, so we hide the grip entirely.+ const [canResize, setCanResize] = useState(true);const { setShouldHideSuggestions, clearMessageToSend } =useConversationStore();@@ -24,6 +29,13 @@ export const useGripResize = (/** After a real resize drag, swallow the synthetic click so it doesn't toggle `isGripVisible`. */const suppressNextTopEdgeClickRef = useRef(false);+ useEffect(() => {+ const check = () => setCanResize(isBottomAnchored());+ check();+ window.addEventListener("resize", check);+ return () => window.removeEventListener("resize", check);+ }, []);+const handleGripDragStart = useCallback(() => {setIsGripDragging(true);}, []);@@ -34,16 +46,20 @@ export const useGripResize = (}, []);// Handle click on top edge area to toggle grip visibility- const handleTopEdgeClick = useCallback((e: MouseEvent) => {- if (suppressNextTopEdgeClickRef.current) {- suppressNextTopEdgeClickRef.current = false;- e.preventDefault();+ const handleTopEdgeClick = useCallback(+ (e: MouseEvent) => {+ if (!canResize) return;+ if (suppressNextTopEdgeClickRef.current) {+ suppressNextTopEdgeClickRef.current = false;+ e.preventDefault();+ e.stopPropagation();+ return;+ }e.stopPropagation();- return;- }- e.stopPropagation();- setIsGripVisible((prev) => !prev);- }, []);+ setIsGripVisible((prev) => !prev);+ },+ [canResize],+ );// Callback to handle height changes and manage suggestions visibilityconst handleHeightChange = useCallback(@@ -77,6 +93,7 @@ export const useGripResize = (gripRef,isGripVisible,isGripDragging,+ canResize,handleTopEdgeClick,smartResize,handleGripMouseDown,diff --git a/src/hooks/use-drag-resize.ts b/src/hooks/use-drag-resize.tsindex d9339c547f..733ac84d64 100644--- a/src/hooks/use-drag-resize.ts+++ b/src/hooks/use-drag-resize.ts@@ -9,6 +9,32 @@ import { isMobileDevice } from "#/utils/utils";/** Movement required before a pointer gesture is treated as a resize drag. */const DRAG_COMMIT_PX = 2;+/**+ * Distance (px) from the viewport bottom within which we consider the chat+ * input to be "bottom-anchored" by its parent's flexbox. When the input is+ * bottom-anchored (e.g. the conversation page), dragging the top grip makes+ * the box grow upward β the expected behaviour. When it is NOT+ * bottom-anchored (e.g. the home page where the input sits in the centre of+ * the screen), the same drag would make the box grow downward, which is+ * confusing, so we disable manual resizing entirely in that case.+ */+const BOTTOM_ANCHORED_THRESHOLD = 120;++/**+ * Determine whether the chat input is anchored to the bottom of the viewport+ * by its parent layout (e.g. the conversation page uses a flex column with a+ * growing scroll area above the input). When true, dragging the top grip+ * makes the box grow upward, which is the expected behaviour. When false,+ * the drag would grow the box downward, so we disable it.+ */+export const isBottomAnchored = (): boolean => {+ const grip = document.getElementById("resize-grip");+ const wrapper = grip?.parentElement ?? null;+ if (!wrapper) return true; // fall back to allowing the drag+ const rect = wrapper.getBoundingClientRect();+ return rect.bottom >= window.innerHeight - BOTTOM_ANCHORED_THRESHOLD;+};+// Drag handling hookinterface UseDragResizeOptions {elementRef: RefObject<HTMLElement | null>;@@ -150,12 +176,17 @@ export const useDragResize = ({// Handle mouse down on grip for manual resizingconst handleGripMouseDown = (e: ReactMouseEvent) => {e.preventDefault();+ // Only allow manual resizing when the input is bottom-anchored (e.g. the+ // conversation page). On the home page the input is centred and dragging+ // the top grip would grow the box downward, which is confusing.+ if (!isBottomAnchored()) return;startDrag(e.clientY);};// Handle touch start on grip for manual resizingconst handleGripTouchStart = (e: ReactTouchEvent) => {e.preventDefault();+ if (!isBottomAnchored()) return;startDrag(e.touches[0].clientY);};