Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider document scrollable if not prevented #1081

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-taxis-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/core': patch
---

Avoid considering `document` as scrollable if `overflow: hidden` is present.
13 changes: 10 additions & 3 deletions packages/core/src/utilities/scroll/getScrollableAncestors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
} from '@dnd-kit/utilities';

import {isFixed} from './isFixed';
import {isScrollable} from './isScrollable';
import {isScrollingDisabled} from './isScrollingDisabled';
import {isScrollingEnabled} from './isScrollingEnabled';

export function getScrollableAncestors(
element: Node | null,
Expand All @@ -28,7 +29,13 @@ export function getScrollableAncestors(
node.scrollingElement != null &&
!scrollParents.includes(node.scrollingElement)
) {
scrollParents.push(node.scrollingElement);
const computedStyle = getWindow(element).getComputedStyle(
node.scrollingElement
);

if (!isScrollingDisabled(node.scrollingElement, computedStyle)) {
scrollParents.push(node.scrollingElement);
}

return scrollParents;
}
Expand All @@ -44,7 +51,7 @@ export function getScrollableAncestors(
const computedStyle = getWindow(element).getComputedStyle(node);

if (node !== element) {
if (isScrollable(node, computedStyle)) {
if (isScrollingEnabled(node, computedStyle)) {
scrollParents.push(node);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {getWindow} from '@dnd-kit/utilities';

export function isScrollable(
element: HTMLElement,
export function hasOverflowStyles(
element: Element,
Copy link
Author

@leo leo Mar 24, 2023

Choose a reason for hiding this comment

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

HTMLElement is a subset of Element, so Element should be fine to use here (it won't break the public API).

Broadening the type was needed to accept node.scrollableElement in getScrollableAncestors.ts, which is of type Element and not HTMLElement. However they both are processed by hasOverflowStyles just fine, because getComputedStyle accepts both.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: it's a breaking change if somebody uses this function's type in contra-variant position like const fn: (hasOverflowStyles: typeof hasOverflowStyles) => void and then fn(myCustomHasOverflowStyles) but it's a stretch.

computedStyle: CSSStyleDeclaration = getWindow(element).getComputedStyle(
element
)
),
overflowRegex: RegExp
): boolean {
const overflowRegex = /(auto|scroll|overlay)/;
const properties = ['overflow', 'overflowX', 'overflowY'];

return properties.some((property) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utilities/scroll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export {
} from './getScrollOffsets';
export {getScrollPosition} from './getScrollPosition';
export {isDocumentScrollingElement} from './documentScrollingElement';
export {isScrollable} from './isScrollable';
export {isScrollingEnabled as isScrollable} from './isScrollingEnabled';
export {scrollIntoViewIfNeeded} from './scrollIntoViewIfNeeded';
12 changes: 12 additions & 0 deletions packages/core/src/utilities/scroll/isScrollingDisabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {getWindow} from '@dnd-kit/utilities';

import {hasOverflowStyles} from './hasOverflowStyles';

export function isScrollingDisabled(
Copy link
Author

Choose a reason for hiding this comment

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

I created this dedicated function because using a single isScrollable function containing both checks doesn't work for the following reasons:

  • isScrollingEnabled needs to check if scrolling was explicitly enabled.
  • isScrollingDisabled needs to check if scrolling was explicitly disabled.

Combining both into Boolean(isScrollingEnabled && !isScrollingDisabled) (in a single isScrollable function) would imply the element won't be considered scrollable unless it explicitly had scrolling enabled, which is not something that is wanted when checking node.scrollableElement in getScrollableAncestors.ts because that element should always be considered scrollable, unless scrolling is explicitly disabled.

element: Element,
computedStyle: CSSStyleDeclaration = getWindow(element).getComputedStyle(
element
)
): boolean {
return hasOverflowStyles(element, computedStyle, /(hidden)/);
}
11 changes: 11 additions & 0 deletions packages/core/src/utilities/scroll/isScrollingEnabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {getWindow} from '@dnd-kit/utilities';
import {hasOverflowStyles} from './hasOverflowStyles';

export function isScrollingEnabled(
element: Element,
computedStyle: CSSStyleDeclaration = getWindow(element).getComputedStyle(
element
)
): boolean {
return hasOverflowStyles(element, computedStyle, /(auto|scroll|overlay)/);
}