Skip to content

fix: Keep sortable List auto-scroll inside non-overflowing scroll containers - #5301

Open
ernst-dev wants to merge 7 commits into
mainfrom
fix/sortable-autoscroll-boundary-non-overflowing
Open

ernst-dev wants to merge 7 commits into
mainfrom
fix/sortable-autoscroll-boundary-non-overflowing

Conversation

@ernst-dev

@ernst-dev ernst-dev commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Description

Follow-up to #5228.

Problem: #5228 limited dnd-kit auto-scroll to the sortable list's nearest scroll container. That fix still breaks when the container has overflow-y: auto but its content does not overflow yet, for example a short list of 4 items in a fixed-height panel or a sticky drawer. A pointer drag near the container's top or bottom edge then scrolls the page behind it.

Root cause: the boundary came from getFirstScrollableParent, which only matches an ancestor whose content currently overflows (scrollHeight > clientHeight). A non-overflowing overflow-y: auto container is skipped. The boundary then becomes an outer scroller (the page or drawer scroller), and dnd-kit auto-scrolls that instead.

Fix:

  • Add getNearestScrollContainer(element) in internal/utils/scrollable-containers. It returns the nearest ancestor (excluding the element itself) whose computed overflow-y is auto, scroll or overlay, whether or not it currently overflows.
  • SortableArea's useScopedAutoScroll now uses it as the boundary. A non-overflowing container has nothing to scroll, so a drag scrolls nothing.
  • getFirstScrollableParent and its other callers (popover positioning, prompt-input token mode) are unchanged.
  • Containers that scroll only horizontally (overflow-y: hidden or visible) are not treated as a boundary.
  • When the list has no such ancestor, behaviour is unchanged: no boundary is set, so every container, including the window, stays eligible for auto-scroll.
  • Keyboard reordering is unaffected.
  • If the nearest such ancestor is body and the root element's overflow is visible in both axes, the body's overflow propagates to the viewport (CSS Overflow 3, standards and quirks mode alike). The body then doesn't scroll itself, so the boundary becomes document.scrollingElement (falling back to html in standards mode, body in quirks mode). Without this, canScroll rejected the document scroller and pointer-drag page auto-scroll stopped.
  • A body that is an independent scroller (root overflow not visible) stays the boundary.

dnd-kit context: clauderic/dnd-kit#1893

How has this been tested?

Real-browser pointer drags on the dev page list/sortable-sticky-drawer-autoscroll, extended with layout and itemCount URL params. I used Playwright 1.65 (Chromium and WebKit, mcr.microsoft.com/playwright:v1.65.0-noble) against the dev server. Each run:

  1. mousedown on the drag handle;
  2. 25-step move to 4px inside the container's bottom edge, hold 1.5s;
  3. the same towards the top edge;
  4. mouseup.

Values are the scrollTop change (px) of every scrollable ancestor during the hold; {} means nothing scrolled.

Scenario Engine Before: bottom / top After: bottom / top
Sticky drawer, 4 items, list container not overflowing Chromium page scroller +1800 / −1800 ❌ nothing / nothing ✅
WebKit page scroller +1788 / −1278 ❌ nothing / nothing ✅
Sticky drawer, 30 items, list container overflowing Chromium list container +278 / −279 same ✅
WebKit list container +278 / −279 same ✅
Non-overflowing container nested inside a scrolling drawer, 4 items Chromium drawer scroller +840 / −280 ❌ nothing / nothing ✅
WebKit drawer scroller +840 / −280 ❌ nothing / nothing ✅
Same nested container, 30 items both inner container +404 / −405 same ✅
Fixed-height overflow-y: auto container in a scrolling page, 4 items both window +1696 / −810 ❌ nothing / nothing ✅
Same container, 30 items both inner container +404 / −405 same ✅
overflow-x-only container (no vertical scroll container) both window +1696 / −810 same (unchanged, no y-boundary)
No scrollable ancestor: drawer list without its own scroller Chromium / WebKit top: outer −1800 / −1343 top: outer −1800 / −1331 (unchanged)
List directly in a scrolling page, 30 items both window +327 / −328 same ✅
Keyboard reorder (Space, ↓↓, Space) both reorders correctly same ✅
html overflow visible, body { overflow-y: auto }, 30 items, no other scroller (viewport edges) both document (html) +1466 / −1466 same ✅ (lost in the first revision, restored by the second commit)
html { overflow: hidden }, body an independent 100%-height scroller, 30 items both body +1466 / −1466 same ✅

All rows were re-measured on the final head in both engines and match the first revision. The page-flow rows shift a little because of the merge from main (page-30 window +346 / −346, overflow-x-only top −847, WebKit no-scroller top −1338), and they are identical before and after the second commit. The two body rows were also measured before #5228 and with #5228 only: both scroll the same as after this PR.

Unit tests:

  • New getNearestScrollContainer tests in scrollable-containers.test.tsx:
    • matches auto, scroll and overlay;
    • returns the container even when it does not overflow, where getFirstScrollableParent returns the outer one;
    • skips the element itself and overflow-x-only or clip ancestors;
    • returns undefined when nothing matches.
  • getNearestScrollContainer body tests:
    • a viewport-propagated body maps to document.scrollingElement, with fallbacks to html (standards) and body (quirks);
    • a body stays the boundary when the root overflow is hidden, auto, clip, or non-visible in one axis;
    • a non-body scroll container is unaffected.
  • list-sortable.test.tsx auto-scroll tests:
    • the boundary stays on a non-overflowing container;
    • overflow-x-only ancestors are ignored;
    • with no scroll container ancestor, every container stays eligible;
    • with a viewport-propagated body, the document scroller stays eligible;
    • with an independent body scroller, auto-scroll is limited to the body.
  • scrollable-containers, list, sortable-area and collection-preferences (the other SortableArea consumer): 18 suites, 221 tests, all passing.
  • ESLint and Prettier are clean on the changed files.
  • tsc -p tsconfig.json passes.

Review checklist

  • Changes are backward-compatible, with no change to the shared getFirstScrollableParent.
  • Tests added for the new helper and the hook behaviour.

…tainers

The auto-scroll boundary introduced in #5228 used getFirstScrollableParent,
which only matches ancestors whose content currently overflows. A short
sortable list inside an overflow-y: auto container therefore fell through
to the page scroller, and dragging scrolled the page behind the container.

Use the nearest ancestor with a scrolling overflow-y (auto, scroll, overlay)
as the boundary, whether or not it overflows. getFirstScrollableParent and
its other callers are unchanged.
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.87%. Comparing base (ddb08ab) to head (42500cc).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5301      +/-   ##
==========================================
- Coverage   97.87%   97.87%   -0.01%     
==========================================
  Files        1016     1016              
  Lines       32887    32912      +25     
  Branches    12199    12208       +9     
==========================================
+ Hits        32187    32211      +24     
- Misses        660      695      +35     
+ Partials       40        6      -34     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new boundary lookup can mistake viewport-propagated body overflow for an independent scroller, disabling pointer-drag page auto-scroll in standards mode.

1 open issue
What changed in this PR

This follow-up to #5228 keeps sortable-list pointer auto-scroll within the nearest vertical scroll container, even when its content does not overflow.

Changes:

  • Adds an overflow-independent container lookup and uses it in SortableArea, leaving getFirstScrollableParent unchanged.
  • Adds regression tests for non-overflowing containers, horizontal-only ancestors, and unrestricted page scrolling.
  • Expands the demo with additional layouts and URL-configurable item counts.
File Description
src/​list/​__tests__/​list-sortable.test.tsx Tests auto-scroll boundary selection and reset behavior.
src/​internal/​utils/​scrollable-containers.ts Adds getNearestScrollContainer.
src/​internal/​utils/​__tests__/​scrollable-containers.test.tsx Tests container lookup across overflow configurations.
src/​internal/​components/​sortable-area/​index.tsx Uses the new lookup for auto-scroll boundaries.
pages/​list/​sortable-sticky-drawer-autoscroll.page.tsx Adds nested and fixed-height layouts plus item-count controls.

Review effort: Balanced

…iewport

With html overflow visible and body { overflow-y: auto }, the body's overflow
applies to the viewport, so the body does not scroll itself. The sortable
auto-scroll boundary picked the body and rejected the document scroller,
disabling pointer-drag page auto-scroll. Map such a body to
document.scrollingElement; keep a body that is an independent scroller.
@ernst-dev
ernst-dev removed the request for review from vvaliyev November 19, 2026 10:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The scroll-container logic ignores CSS containment on html or body, potentially selecting the document scroller and auto-scrolling the outer page during pointer drags instead of the independently scrolling body.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment on lines +164 to +165
const rootStyle = getComputedStyle(root);
return rootStyle.overflowX === 'visible' && rootStyle.overflowY === 'visible';
* When that ancestor is a `body` whose overflow is propagated to the viewport, the document's scrolling element
* is returned instead. Returns `undefined` when no such ancestor exists.
*/
export function getNearestScrollContainer(element: HTMLElement): HTMLElement | undefined {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNearestScrollableContainer does conceptually the same as getFirstScrollableParent, only with a small behavior change that their names tell nothing about.

If instead we apply the change to getFirstScrollableParent (which is used by popovers and prompt input), would it break them? Or by the contrary, would it potentially improve those consumers as well?

If you determine that it really would break them, could we reuse the same function with a flag instead of having two?

This branch was successfully deployed

3 active deployments
dev-pages-react18 — 42500cc1 Deployed Nov 19, 2026 by ernst-dev via deploy (React 18) / deploy #3416
dev-pages-react16 — 42500cc1 Deployed Nov 19, 2026 by ernst-dev via deploy (React 16) / deploy #3416
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants