From 2f8d5f51189cacd13d1d74de2db15e43f367bca3 Mon Sep 17 00:00:00 2001 From: faysou Date: Mon, 23 Nov 2026 23:29:51 +0000 Subject: [PATCH] fix(ui): keep the arrow cursor during scrollbar drags (#5504) Dragging a scrollbar thumb across a split divider could show the resize cursor while the page was still scrolling. In a side-by-side Markdown editor, pressing the outside edge of the source thumb could start resizing the pane. Keep the arrow until the scrollbar drag ends and keep the source thumb clear of the Markdown divider. A transparent surface catches the pointer during the drag, so grabbing the thumb does not change inherited styles across the document. Co-authored-by: faysou <893862+faysou@users.noreply.github.com> --- e2e/editor.spec.ts | 39 +++++++++++++++++++ src/editor/core/cm/lanes.ts | 16 +++++++- .../viewers/markdown/Markdown.module.css | 7 ++++ src/styles/global.css | 7 ++++ src/ui/Scrollbar.test.tsx | 16 +++++++- src/ui/Scrollbar.tsx | 32 ++++++++++----- src/ui/dragCursor.ts | 25 +++++++++--- 7 files changed, 124 insertions(+), 18 deletions(-) diff --git a/e2e/editor.spec.ts b/e2e/editor.spec.ts index 395625a00..405289c68 100644 --- a/e2e/editor.spec.ts +++ b/e2e/editor.spec.ts @@ -817,6 +817,45 @@ test("dragging the vertical tab column's edge resizes it and the width is kept", await expect.poll(async () => Math.round((await strip.boundingBox())!.width)).toBe(Math.round(narrowed)); }); +test("dragging the source scrollbar across the Markdown divider keeps the arrow and pane width", async ({ + workbench: page, +}) => { + const editor = await openFromTree(page, "README.md"); + const content = editor.locator(".cm-content"); + await content.click(); + await page.keyboard.press("Meta+a"); + await page.keyboard.insertText("Top\n\n" + "Long paragraph.\n\n".repeat(180)); + const divider = page.getByRole("separator", { name: "Resize Markdown preview" }); + const scroller = editor.locator(".cm-scroller"); + await expect(page.getByRole("document", { name: "Markdown preview" })).toContainText("Long paragraph."); + const thumb = editor.locator(".cm-openidea-overviewThumb"); + const box = (await thumb.boundingBox())!; + const boundary = (await divider.boundingBox())!; + const x = boundary.x + boundary.width / 2; + const y = box.y + box.height / 2; + const dy = y > boundary.y + boundary.height / 2 ? -80 : 80; + const before = await scroller.evaluate((el) => el.scrollTop); + await page.mouse.move(box.x + box.width - 1, y); + await page.mouse.down(); + try { + await page.mouse.move(x, y + dy, { steps: 5 }); + expect( + await page.evaluate(({ x, y }) => getComputedStyle(document.elementFromPoint(x, y)!).cursor, { x, y: y + dy }), + ).toBe("default"); + await page.mouse.move(x + 20, y + dy, { steps: 3 }); + expect((await divider.boundingBox())!.x).toBe(boundary.x); + expect(Math.sign((await scroller.evaluate((el) => el.scrollTop)) - before)).toBe(Math.sign(dy)); + } finally { + await page.mouse.up(); + } + await page.mouse.move(x, y + dy); + await expect(divider).toHaveCSS("cursor", "col-resize"); + await page.mouse.down(); + await page.mouse.move(x + 50, y + dy, { steps: 4 }); + await page.mouse.up(); + expect((await divider.boundingBox())!.x).toBeGreaterThan(boundary.x + 40); +}); + test("dragging the Markdown split divider resizes and reflows editor and preview content, keeps the ratio, and resets on double-click", async ({ workbench: page, }) => { diff --git a/src/editor/core/cm/lanes.ts b/src/editor/core/cm/lanes.ts index 8a265f96b..faafddda6 100644 --- a/src/editor/core/cm/lanes.ts +++ b/src/editor/core/cm/lanes.ts @@ -6,6 +6,7 @@ import { EditorSelection, type EditorState, type Extension, type StateEffect } f import { foldedRanges, unfoldEffect } from "@codemirror/language"; import { EditorView, ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view"; import { gutterLaneMarkers, scrollbarMarks, type ScrollbarMark } from "@/editor/api"; +import { lockScrollbarCursor } from "@/ui/dragCursor"; export const changeLane: Extension = gutter({ class: "cm-openidea-lane", @@ -73,6 +74,7 @@ class OverviewLane { * press on this strip stays here instead of reaching the text layer as a text drag. */ private thumb: HTMLElement; + private endThumbDrag?: () => void; constructor(private view: EditorView) { this.scroller = view.scrollDOM; @@ -253,6 +255,8 @@ class OverviewLane { const s = this.scroller; const thumb = scrollbarThumb(s.scrollTop, s.scrollHeight, s.clientHeight); if (!thumb) return; + this.endThumbDrag?.(); + const unlock = lockScrollbarCursor(); const startY = e.clientY; const startTop = s.scrollTop; const travel = s.scrollHeight - s.clientHeight; @@ -261,11 +265,20 @@ class OverviewLane { const onMove = (ev: MouseEvent) => { s.scrollTop = startTop + ((ev.clientY - startY) * travel) / track; }; - const onUp = () => { + const onUp = (event?: MouseEvent | PointerEvent | FocusEvent) => { + if (event?.type === "mouseup" && (event as MouseEvent).button !== 0) return; + if (event?.type === "pointercancel" && (event as PointerEvent).pointerType !== "mouse") return; window.removeEventListener("mousemove", onMove); window.removeEventListener("mouseup", onUp); + window.removeEventListener("blur", onUp); + window.removeEventListener("pointercancel", onUp); + this.endThumbDrag = undefined; + unlock(); this.updateThumbPassThrough(); }; + this.endThumbDrag = onUp; + window.addEventListener("blur", onUp); + window.addEventListener("pointercancel", onUp); window.addEventListener("mousemove", onMove); window.addEventListener("mouseup", onUp); }; @@ -300,6 +313,7 @@ class OverviewLane { }; destroy() { + this.endThumbDrag?.(); if (this.markTimer !== undefined) window.clearTimeout(this.markTimer); this.dom.removeEventListener("mousedown", this.onMouseDown); this.thumb.removeEventListener("mousedown", this.onThumbMouseDown); diff --git a/src/features/viewers/markdown/Markdown.module.css b/src/features/viewers/markdown/Markdown.module.css index 34d060dd5..b22f70fb2 100644 --- a/src/features/viewers/markdown/Markdown.module.css +++ b/src/features/viewers/markdown/Markdown.module.css @@ -21,6 +21,13 @@ display: flex; flex-direction: column; } +/* Keep the thumb clear of the side-by-side divider's mouse hit area. */ +.mode-split + .group:has(> :global([data-separator][aria-orientation="vertical"])) + .editor + :global(.cm-openidea-overviewThumb) { + right: 5px; +} .mode-preview > :global([data-group]) > :global([data-panel]:first-child) { display: none !important; } diff --git a/src/styles/global.css b/src/styles/global.css index 3b5dc12db..da79ab4ed 100644 --- a/src/styles/global.css +++ b/src/styles/global.css @@ -45,6 +45,13 @@ body { cursor: default; } +[data-scrollbar-drag-surface] { + position: fixed; + inset: 0; + z-index: var(--z-tooltip); + cursor: default !important; +} + /* A resize drag owns the cursor until release, even over the editor or a button. */ html[data-drag-cursor], html[data-drag-cursor] *, diff --git a/src/ui/Scrollbar.test.tsx b/src/ui/Scrollbar.test.tsx index 83f665942..190399046 100644 --- a/src/ui/Scrollbar.test.tsx +++ b/src/ui/Scrollbar.test.tsx @@ -35,13 +35,25 @@ describe("Scrollbar", () => { const track = container.querySelector("[aria-hidden]")!; const thumb = track.firstElementChild as HTMLElement; track.setPointerCapture = () => {}; - thumb.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, button: 0, clientY: 10 })); - track.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, clientY: 46 })); + track.hasPointerCapture = () => false; + thumb.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerId: 1, button: 0, clientY: 10 })); + expect(document.querySelector("[data-scrollbar-drag-surface]")).not.toBeNull(); + document.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerId: 2 })); + expect(document.querySelector("[data-scrollbar-drag-surface]")).not.toBeNull(); + track.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, pointerId: 1, clientY: 46 })); // A 32px thumb travels 68px of the track for 900px of content: 36px ≈ 476px. expect(top).toBeCloseTo((36 / 68) * 900); expect(onScroll).toHaveBeenCalledTimes(1); expect(onScroll).toHaveBeenLastCalledWith(top); + track.dispatchEvent(new PointerEvent("lostpointercapture", { bubbles: true, pointerId: 1 })); + expect(document.querySelector("[data-scrollbar-drag-surface]")).toBeNull(); + + thumb.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerId: 1, button: 0, clientY: 10 })); + expect(thumb.dataset.dragging).toBe(""); + window.dispatchEvent(new Event("blur")); + expect(thumb.dataset.dragging).toBeUndefined(); + expect(document.querySelector("[data-scrollbar-drag-surface]")).toBeNull(); }); it.each([ diff --git a/src/ui/Scrollbar.tsx b/src/ui/Scrollbar.tsx index fbe2b075b..aa5fd3f4c 100644 --- a/src/ui/Scrollbar.tsx +++ b/src/ui/Scrollbar.tsx @@ -1,4 +1,5 @@ -import { useEffect, useLayoutEffect, useRef, type RefObject } from "react"; +import { useCallback, useEffect, useLayoutEffect, useRef, type RefObject } from "react"; +import { lockScrollbarCursor } from "./dragCursor"; import styles from "./Scrollbar.module.css"; const MIN_THUMB = 32; @@ -24,8 +25,17 @@ export function Scrollbar({ }) { const track = useRef(null); const thumb = useRef(null); - const drag = useRef<{ y: number; top: number } | null>(null); + const drag = useRef<{ pointerId: number; y: number; top: number; unlock: () => void } | null>(null); const geometry = useRef(null); + const endDrag = useCallback(() => { + const current = drag.current; + if (!current) return; + drag.current = null; + current.unlock(); + delete thumb.current?.dataset.dragging; + const el = track.current; + if (el?.hasPointerCapture(current.pointerId)) el.releasePointerCapture(current.pointerId); + }, []); useLayoutEffect(() => { geometry.current = layout(target.current, track.current, thumb.current); @@ -39,11 +49,14 @@ export function Scrollbar({ el.addEventListener("scroll", sync, { passive: true }); const observer = new ResizeObserver(sync); observer.observe(el); + window.addEventListener("blur", endDrag); return () => { + endDrag(); + window.removeEventListener("blur", endDrag); el.removeEventListener("scroll", sync); observer.disconnect(); }; - }, [target]); + }, [target, endDrag]); return (
{ const el = target.current; - if (!el || e.button !== 0) return; + if (!el || e.button !== 0 || drag.current) return; e.preventDefault(); // A click on the track pages towards it, as the native scrollbar does. if (e.target !== thumb.current) { @@ -61,19 +74,18 @@ export function Scrollbar({ else if (e.clientY > bottom) scrollNow(el, el.scrollTop + el.clientHeight); return; } - drag.current = { y: e.clientY, top: el.scrollTop }; - thumb.current!.dataset.dragging = ""; e.currentTarget.setPointerCapture(e.pointerId); + drag.current = { pointerId: e.pointerId, y: e.clientY, top: el.scrollTop, unlock: lockScrollbarCursor() }; + thumb.current!.dataset.dragging = ""; }} onPointerMove={(e) => { const el = target.current; - if (!el || !drag.current) return; + if (!el || drag.current?.pointerId !== e.pointerId) return; const g = geometry.current; if (g) scrollNow(el, drag.current.top + ((e.clientY - drag.current.y) / g.range) * g.max); }} - onLostPointerCapture={() => { - drag.current = null; - delete thumb.current?.dataset.dragging; + onLostPointerCapture={(e) => { + if (drag.current?.pointerId === e.pointerId) endDrag(); }} >
diff --git a/src/ui/dragCursor.ts b/src/ui/dragCursor.ts index 87db909de..af1408735 100644 --- a/src/ui/dragCursor.ts +++ b/src/ui/dragCursor.ts @@ -33,17 +33,32 @@ export function lockDragCursor(cursor: DragCursor): () => void { const wasLocked = html.hasAttribute("data-drag-cursor"); html.style.setProperty("--drag-cursor", cursor); html.setAttribute("data-drag-cursor", ""); + return untilDragEnds(() => { + if (wasLocked) html.style.setProperty("--drag-cursor", previous); + else { + html.removeAttribute("data-drag-cursor"); + html.style.removeProperty("--drag-cursor"); + } + }); +} + +export function lockScrollbarCursor(): () => void { + // Changing an inherited cursor on the document restyles every glyph in a large preview. + const surface = document.createElement("div"); + surface.setAttribute("data-scrollbar-drag-surface", ""); + surface.setAttribute("aria-hidden", "true"); + document.body.appendChild(surface); + return () => surface.remove(); +} + +function untilDragEnds(cleanup: () => void): () => void { listenForEnd(); let released = false; const release = () => { if (released) return; released = true; active.delete(release); - if (wasLocked) html.style.setProperty("--drag-cursor", previous); - else { - html.removeAttribute("data-drag-cursor"); - html.style.removeProperty("--drag-cursor"); - } + cleanup(); }; active.add(release); return release;