diff --git a/ui/src/components/PropertiesPanel.test.tsx b/ui/src/components/PropertiesPanel.test.tsx new file mode 100644 index 00000000000..caab50d195e --- /dev/null +++ b/ui/src/components/PropertiesPanel.test.tsx @@ -0,0 +1,279 @@ +// @vitest-environment jsdom + +import { act, type ReactNode } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { TooltipProvider } from "@/components/ui/tooltip"; +import { PropertiesPanel } from "./PropertiesPanel"; + +vi.mock("@/hooks/useCoordinatorWorkspaceEnabled", () => ({ + useCoordinatorWorkspaceEnabled: () => ({ enabled: false, loaded: true }), +})); + +const mockSandbox = vi.hoisted(() => ({ enabled: false })); +vi.mock("@/context/DesignSandboxContext", () => ({ useDesignSandbox: () => mockSandbox.enabled })); + +const mockInstanceSettingsApi = vi.hoisted(() => ({ + getExperimental: vi.fn(), +})); +vi.mock("@/api/instanceSettings", () => ({ + instanceSettingsApi: mockInstanceSettingsApi, +})); + +const mockPanelState = vi.hoisted(() => ({ + panelContent: null as ReactNode, + panelContentMode: "padded" as const, + panelVisible: true, + panelMaximizeRequested: false, +})); +const mockSetPanelVisible = vi.hoisted(() => vi.fn()); +const mockClearPanelMaximizeRequest = vi.hoisted(() => vi.fn()); + +vi.mock("../context/PanelContext", () => ({ + usePanel: () => ({ + panelContent: mockPanelState.panelContent, + panelContentMode: mockPanelState.panelContentMode, + panelVisible: mockPanelState.panelVisible, + openPanel: vi.fn(), + closePanel: vi.fn(), + setPanelVisible: mockSetPanelVisible, + togglePanelVisible: vi.fn(), + panelMaximizeRequested: mockPanelState.panelMaximizeRequested, + requestPanelMaximize: vi.fn(), + clearPanelMaximizeRequest: mockClearPanelMaximizeRequest, + }), +})); + +async function flushReact() { + await act(async () => { + for (let index = 0; index < 4; index += 1) { + await Promise.resolve(); + } + }); +} + +describe("PropertiesPanel layout row fitting", () => { + let container: HTMLDivElement; + let root: Root | null = null; + let originalInnerWidth: number; + let originalResizeObserver: typeof ResizeObserver | undefined; + let queryClient: QueryClient; + + async function renderPanel({ + panelVisible = true, + taskDetailLayout = true, + content =
content
, + inLayoutRow = true, + }: { + panelVisible?: boolean; + taskDetailLayout?: boolean; + content?: ReactNode; + inLayoutRow?: boolean; + } = {}) { + mockPanelState.panelContent = content; + mockPanelState.panelVisible = panelVisible; + root ??= createRoot(container); + const panel = ; + await act(async () => { + root!.render( + + + {inLayoutRow ? ( +
+
+
+
+ {panel} +
+ ) : panel} +
+
, + ); + }); + await flushReact(); + } + + beforeEach(() => { + originalInnerWidth = window.innerWidth; + originalResizeObserver = globalThis.ResizeObserver; + Object.defineProperty(window, "innerWidth", { configurable: true, value: 1280 }); + container = document.createElement("div"); + document.body.appendChild(container); + window.localStorage.clear(); + mockSetPanelVisible.mockClear(); + mockClearPanelMaximizeRequest.mockClear(); + mockPanelState.panelMaximizeRequested = false; + mockSandbox.enabled = false; + mockInstanceSettingsApi.getExperimental.mockResolvedValue({ + enableStreamlinedUi: true, + enableClassicTaskInterface: false, + }); + queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + }); + + afterEach(async () => { + await act(async () => { + root?.unmount(); + }); + root = null; + container.remove(); + Object.defineProperty(window, "innerWidth", { configurable: true, value: originalInnerWidth }); + if (originalResizeObserver === undefined) { + delete (globalThis as { ResizeObserver?: typeof ResizeObserver }).ResizeObserver; + } else { + globalThis.ResizeObserver = originalResizeObserver; + } + vi.clearAllMocks(); + vi.restoreAllMocks(); + document.documentElement.style.removeProperty("--sz-420px"); + }); + + it("fits the pane to its layout row so it is never clipped at the right edge", async () => { + window.localStorage.setItem("taskChatRedesign.taskDetailPropertiesPaneWidth", "624"); + const geometry = { rowRight: 1280, centerLeft: 700 }; + vi.spyOn(Element.prototype, "getBoundingClientRect").mockImplementation(function (this: Element) { + if (this.id === "main-content") return new DOMRect(geometry.centerLeft, 0, 0, 800); + if (this.getAttribute("data-testid") === "layout-row") return new DOMRect(0, 0, geometry.rowRight, 800); + return new DOMRect(); + }); + + await renderPanel({ taskDetailLayout: true, inLayoutRow: true }); + const aside = container.querySelector("aside")!; + const inner = aside.querySelector(":scope > div:not([role]) > div")!; + expect(aside.style.width).toBe("260px"); + expect(inner.style.width).toBe("260px"); + + geometry.centerLeft = 488; + await act(async () => { + window.dispatchEvent(new Event("resize")); + }); + expect(aside.style.width).toBe("372px"); + expect(inner.style.width).toBe("372px"); + expect(window.localStorage.getItem("taskChatRedesign.taskDetailPropertiesPaneWidth")).toBe("624"); + + // Respects the shared --sz-420px minimum center width token when customized. + document.documentElement.style.setProperty("--sz-420px", "460px"); + await act(async () => { + window.dispatchEvent(new Event("resize")); + }); + expect(aside.style.width).toBe("332px"); + document.documentElement.style.removeProperty("--sz-420px"); + + geometry.rowRight = 1440; + await act(async () => { + window.dispatchEvent(new Event("resize")); + }); + expect(aside.style.width).toBe("532px"); + + // Narrow desktop where space right of center start is below the 260px minimum. + geometry.rowRight = 900; + geometry.centerLeft = 700; + await act(async () => { + window.dispatchEvent(new Event("resize")); + }); + expect(aside.style.width).toBe("200px"); + expect(inner.style.width).toBe("200px"); + }); + + it("re-measures when #main-content shifts inside the row without the row resizing", async () => { + window.localStorage.setItem("taskChatRedesign.taskDetailPropertiesPaneWidth", "624"); + const geometry = { rowRight: 1280, centerLeft: 700 }; + vi.spyOn(Element.prototype, "getBoundingClientRect").mockImplementation(function (this: Element) { + if (this.id === "main-content") return new DOMRect(geometry.centerLeft, 0, 0, 800); + if (this.getAttribute("data-testid") === "layout-row") return new DOMRect(0, 0, geometry.rowRight, 800); + return new DOMRect(); + }); + + const observers = new Map(); + class FakeResizeObserver { + private readonly callback: ResizeObserverCallback; + private readonly targets = new Set(); + constructor(callback: ResizeObserverCallback) { + this.callback = callback; + } + observe(target: Element) { + this.targets.add(target); + const list = observers.get(target) ?? []; + list.push(this.callback); + observers.set(target, list); + } + unobserve(target: Element) { + this.targets.delete(target); + } + disconnect() { + for (const target of this.targets) { + const list = observers.get(target); + if (list) { + const next = list.filter((cb) => cb !== this.callback); + if (next.length === 0) observers.delete(target); + else observers.set(target, next); + } + } + this.targets.clear(); + } + } + globalThis.ResizeObserver = FakeResizeObserver as unknown as typeof ResizeObserver; + + await renderPanel({ taskDetailLayout: true, inLayoutRow: true }); + const aside = container.querySelector("aside")!; + const main = container.querySelector("#main-content")!; + expect(aside.style.width).toBe("260px"); + + // Simulate chat list collapsing inside chatAreaRef: #main-content shifts left, + // while the outer layout row keeps the exact same width. + geometry.centerLeft = 420; + await act(async () => { + for (const cb of observers.get(main) ?? []) { + cb([], {} as ResizeObserver); + } + }); + expect(aside.style.width).toBe("440px"); + + // Simulate chat list reappearing on a tight row: #main-content shifts right again. + geometry.rowRight = 900; + geometry.centerLeft = 700; + await act(async () => { + for (const cb of observers.get(main) ?? []) { + cb([], {} as ResizeObserver); + } + }); + expect(aside.style.width).toBe("200px"); + }); + + it("starts dragging from the fitted width and clamps drags to the layout row", async () => { + window.localStorage.setItem("taskChatRedesign.taskDetailPropertiesPaneWidth", "624"); + vi.spyOn(Element.prototype, "getBoundingClientRect").mockImplementation(function (this: Element) { + if (this.id === "main-content") return new DOMRect(488, 0, 0, 800); + if (this.getAttribute("data-testid") === "layout-row") return new DOMRect(0, 0, 1280, 800); + return new DOMRect(); + }); + + await renderPanel({ taskDetailLayout: true, inLayoutRow: true }); + const aside = container.querySelector("aside")!; + const grip = aside.querySelector('[role="separator"][aria-label="Resize panel"]')!; + grip.setPointerCapture = vi.fn(); + expect(aside.style.width).toBe("372px"); + + await act(async () => { + grip.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerId: 1, button: 0, pointerType: "mouse", clientX: 908 })); + grip.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, pointerId: 1, clientX: 708 })); + }); + expect(aside.style.width).toBe("372px"); + + await act(async () => { + grip.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, pointerId: 1, clientX: 958 })); + grip.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerId: 1, clientX: 958 })); + }); + expect(aside.style.width).toBe("322px"); + expect(window.localStorage.getItem("taskChatRedesign.taskDetailPropertiesPaneWidth")).toBe("322"); + }); + + it("falls back to window-based clamping when mounted without #main-content", async () => { + Object.defineProperty(window, "innerWidth", { configurable: true, value: 1000 }); + window.localStorage.setItem("taskChatRedesign.taskDetailPropertiesPaneWidth", "700"); + await renderPanel({ taskDetailLayout: true, inLayoutRow: false }); + const aside = container.querySelector("aside")!; + expect(aside.style.width).toBe("344px"); + }); +}); diff --git a/ui/src/components/PropertiesPanel.tsx b/ui/src/components/PropertiesPanel.tsx index cedd1ab7452..687cb6368b9 100644 --- a/ui/src/components/PropertiesPanel.tsx +++ b/ui/src/components/PropertiesPanel.tsx @@ -94,6 +94,9 @@ const TASK_DETAIL_DEFAULT_PANE_WIDTH = 434; const MIN_PANE_WIDTH = 260; /** ~236px sidebar + ~420px minimum center column stay usable while resizing. */ const RESERVED_LAYOUT_WIDTH = 656; +/** Fallback when `--sz-420px` is unavailable (e.g. non-browser test environments). */ +const MIN_CENTER_WIDTH_TOKEN = "--sz-420px"; +const MIN_CENTER_WIDTH_FALLBACK = 420; /** Content cap while maximized so text doesn't span the full viewport. */ const MAXIMIZED_CONTENT_MAX_WIDTH = 840; /** @@ -103,20 +106,37 @@ const MAXIMIZED_CONTENT_MAX_WIDTH = 840; */ const RESTORE_FALLBACK_DELAY = 400; -function readProposalPaneWidth(fallback: number): number { +function readCssPxToken(tokenName: string, fallback: number): number { if (typeof window === "undefined") return fallback; - const tokenWidth = Number.parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--design-page-aside-width")); + const tokenWidth = Number.parseFloat(getComputedStyle(document.documentElement).getPropertyValue(tokenName)); return Number.isFinite(tokenWidth) && tokenWidth > 0 ? tokenWidth : fallback; } -function clampPaneWidth(width: number): number { +function readProposalPaneWidth(fallback: number): number { + return readCssPxToken("--design-page-aside-width", fallback); +} + +function clampPaneWidth(width: number, fittedMax: number | null = null): number { const max = - typeof window === "undefined" + fittedMax ?? + (typeof window === "undefined" ? Number.POSITIVE_INFINITY - : Math.max(MIN_PANE_WIDTH, window.innerWidth - RESERVED_LAYOUT_WIDTH); + : Math.max(MIN_PANE_WIDTH, window.innerWidth - RESERVED_LAYOUT_WIDTH)); return Math.min(Math.max(Math.round(width), MIN_PANE_WIDTH), max); } +function measureFittedPaneMax(aside: HTMLElement | null): number | null { + const row = aside?.parentElement; + const main = row?.querySelector("#main-content"); + if (!row || !main) return null; + const rowRight = row.getBoundingClientRect().right; + const centerLeft = main.getBoundingClientRect().left; + if (rowRight <= centerLeft) return null; + const available = Math.floor(rowRight - centerLeft); + const minCenterWidth = readCssPxToken(MIN_CENTER_WIDTH_TOKEN, MIN_CENTER_WIDTH_FALLBACK); + return Math.max(Math.min(MIN_PANE_WIDTH, available), available - minCenterWidth); +} + function readStoredPaneWidth(storageKey: string, defaultWidth: number): number { if (typeof window === "undefined") return defaultWidth; try { @@ -207,8 +227,30 @@ function ResizablePropertiesPanel({ }, [panelVisible]); const asideRef = useRef(null); + const [fittedMax, setFittedMax] = useState(null); + const fittedMaxRef = useRef(fittedMax); + fittedMaxRef.current = fittedMax; + const displayWidth = fittedMax === null ? width : Math.min(width, fittedMax); const widthRef = useRef(width); widthRef.current = width; + const displayWidthRef = useRef(displayWidth); + displayWidthRef.current = displayWidth; + + useLayoutEffect(() => { + if (!panelVisible) return; + const row = asideRef.current?.parentElement; + const main = row?.querySelector("#main-content"); + const update = () => setFittedMax(measureFittedPaneMax(asideRef.current)); + update(); + window.addEventListener("resize", update); + const observer = (row || main) && typeof ResizeObserver !== "undefined" ? new ResizeObserver(update) : null; + if (row) observer?.observe(row); + if (main) observer?.observe(main); + return () => { + window.removeEventListener("resize", update); + observer?.disconnect(); + }; + }, [panelVisible]); const dragStateRef = useRef<{ pointerId: number; startX: number; startWidth: number } | null>( null, ); @@ -262,7 +304,7 @@ function ResizablePropertiesPanel({ dragStateRef.current = { pointerId: event.pointerId, startX: event.clientX, - startWidth: widthRef.current, + startWidth: displayWidthRef.current, }; previousBodyUserSelectRef.current = document.body.style.userSelect; document.body.style.userSelect = "none"; @@ -273,7 +315,9 @@ function ResizablePropertiesPanel({ const drag = dragStateRef.current; if (drag === null || drag.pointerId !== event.pointerId) return; // The grip sits on the panel's LEFT border: moving left widens the panel. - setWidth(clampPaneWidth(drag.startWidth + (drag.startX - event.clientX))); + const next = clampPaneWidth(drag.startWidth + (drag.startX - event.clientX), fittedMaxRef.current); + widthRef.current = next; + setWidth(next); }, []); const handleGripPointerUp = useCallback( @@ -335,7 +379,7 @@ function ResizablePropertiesPanel({ const rowRight = row ? row.getBoundingClientRect().right : window.innerWidth - pane.rightInset; - return { ...pane, left: rowRight - widthRef.current }; + return { ...pane, left: rowRight - displayWidthRef.current }; }); clearRestoreTimer(); restoreTimerRef.current = window.setTimeout(finishRestore, RESTORE_FALLBACK_DELAY); @@ -372,7 +416,7 @@ function ResizablePropertiesPanel({
) : null}