Skip to content

Commit ecd2291

Browse files
authored
Revert "fix: scrollIntoView should respect scroll-margin (#8715)" (#9078)
This reverts commit 591d1f8.
1 parent f40b575 commit ecd2291

File tree

2 files changed

+13
-87
lines changed

2 files changed

+13
-87
lines changed

packages/@react-aria/utils/src/scrollIntoView.ts

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212

1313
import {getScrollParents} from './getScrollParents';
14-
import {isChrome} from './platform';
1514

1615
interface ScrollIntoViewportOpts {
1716
/** The optional containing element of the target to be centered in the viewport. */
@@ -41,64 +40,32 @@ export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement): v
4140
scrollPaddingLeft
4241
} = getComputedStyle(scrollView);
4342

44-
// Account for scroll margin of the element
45-
let {
46-
scrollMarginTop,
47-
scrollMarginRight,
48-
scrollMarginBottom,
49-
scrollMarginLeft
50-
} = getComputedStyle(element);
51-
5243
let borderAdjustedX = x + parseInt(borderLeftWidth, 10);
5344
let borderAdjustedY = y + parseInt(borderTopWidth, 10);
5445
// Ignore end/bottom border via clientHeight/Width instead of offsetHeight/Width
5546
let maxX = borderAdjustedX + scrollView.clientWidth;
5647
let maxY = borderAdjustedY + scrollView.clientHeight;
5748

58-
// Get scroll padding / margin values as pixels - defaults to 0 if no scroll padding / margin
49+
// Get scroll padding values as pixels - defaults to 0 if no scroll padding
5950
// is used.
6051
let scrollPaddingTopNumber = parseInt(scrollPaddingTop, 10) || 0;
6152
let scrollPaddingBottomNumber = parseInt(scrollPaddingBottom, 10) || 0;
6253
let scrollPaddingRightNumber = parseInt(scrollPaddingRight, 10) || 0;
6354
let scrollPaddingLeftNumber = parseInt(scrollPaddingLeft, 10) || 0;
64-
let scrollMarginTopNumber = parseInt(scrollMarginTop, 10) || 0;
65-
let scrollMarginBottomNumber = parseInt(scrollMarginBottom, 10) || 0;
66-
let scrollMarginRightNumber = parseInt(scrollMarginRight, 10) || 0;
67-
let scrollMarginLeftNumber = parseInt(scrollMarginLeft, 10) || 0;
68-
69-
let targetLeft = offsetX - scrollMarginLeftNumber;
70-
let targetRight = offsetX + width + scrollMarginRightNumber;
71-
let targetTop = offsetY - scrollMarginTopNumber;
72-
let targetBottom = offsetY + height + scrollMarginBottomNumber;
7355

74-
let scrollPortLeft = x + parseInt(borderLeftWidth, 10) + scrollPaddingLeftNumber;
75-
let scrollPortRight = maxX - scrollPaddingRightNumber;
76-
let scrollPortTop = y + parseInt(borderTopWidth, 10) + scrollPaddingTopNumber;
77-
let scrollPortBottom = maxY - scrollPaddingBottomNumber;
78-
79-
if (targetLeft > scrollPortLeft || targetRight < scrollPortRight) {
80-
if (targetLeft <= x + scrollPaddingLeftNumber) {
81-
x = targetLeft - parseInt(borderLeftWidth, 10) - scrollPaddingLeftNumber;
82-
} else if (targetRight > maxX - scrollPaddingRightNumber) {
83-
x += targetRight - maxX + scrollPaddingRightNumber;
84-
}
56+
if (offsetX <= x + scrollPaddingLeftNumber) {
57+
x = offsetX - parseInt(borderLeftWidth, 10) - scrollPaddingLeftNumber;
58+
} else if (offsetX + width > maxX - scrollPaddingRightNumber) {
59+
x += offsetX + width - maxX + scrollPaddingRightNumber;
8560
}
86-
87-
if (targetTop > scrollPortTop || targetBottom < scrollPortBottom) {
88-
if (targetTop <= borderAdjustedY + scrollPaddingTopNumber) {
89-
y = targetTop - parseInt(borderTopWidth, 10) - scrollPaddingTopNumber;
90-
} else if (targetBottom > maxY - scrollPaddingBottomNumber) {
91-
y += targetBottom - maxY + scrollPaddingBottomNumber;
92-
}
93-
}
94-
95-
if (process.env.NODE_ENV === 'test') {
96-
scrollView.scrollLeft = x;
97-
scrollView.scrollTop = y;
98-
return;
61+
if (offsetY <= borderAdjustedY + scrollPaddingTopNumber) {
62+
y = offsetY - parseInt(borderTopWidth, 10) - scrollPaddingTopNumber;
63+
} else if (offsetY + height > maxY - scrollPaddingBottomNumber) {
64+
y += offsetY + height - maxY + scrollPaddingBottomNumber;
9965
}
10066

101-
scrollView.scrollTo({left: x, top: y});
67+
scrollView.scrollLeft = x;
68+
scrollView.scrollTop = y;
10269
}
10370

10471
/**
@@ -134,9 +101,8 @@ export function scrollIntoViewport(targetElement: Element | null, opts?: ScrollI
134101
if (targetElement && document.contains(targetElement)) {
135102
let root = document.scrollingElement || document.documentElement;
136103
let isScrollPrevented = window.getComputedStyle(root).overflow === 'hidden';
137-
// If scrolling is not currently prevented then we aren't in a overlay nor is a overlay open, just use element.scrollIntoView to bring the element into view
138-
// Also ignore in chrome because of this bug: https://issues.chromium.org/issues/40074749
139-
if (!isScrollPrevented && !isChrome()) {
104+
// If scrolling is not currently prevented then we aren’t in a overlay nor is a overlay open, just use element.scrollIntoView to bring the element into view
105+
if (!isScrollPrevented) {
140106
let {left: originalLeft, top: originalTop} = targetElement.getBoundingClientRect();
141107

142108
// use scrollIntoView({block: 'nearest'}) instead of .focus to check if the element is fully in view or not since .focus()

packages/react-aria-components/stories/ListBox.stories.tsx

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -743,46 +743,6 @@ export const AsyncListBoxVirtualized: StoryFn<typeof AsyncListBoxRender> = (args
743743
);
744744
};
745745

746-
export const ListBoxScrollMargin: ListBoxStory = (args) => {
747-
let items: {id: number, name: string, description: string}[] = [];
748-
for (let i = 0; i < 100; i++) {
749-
items.push({id: i, name: `Item ${i}`, description: `Description ${i}`});
750-
}
751-
return (
752-
<ListBox
753-
className={styles.menu}
754-
{...args}
755-
aria-label="test listbox"
756-
style={{height: 200, width: 100, overflow: 'scroll'}}
757-
items={items}>
758-
{item => (
759-
<MyListBoxItem style={{scrollMargin: 10, width: 150, display: 'flex', padding: '2px 20px', justifyContent: 'space-between'}}>
760-
<span>{item.name}</span>
761-
<span>{item.description}</span>
762-
</MyListBoxItem>
763-
)}
764-
</ListBox>
765-
);
766-
};
767-
768-
export const ListBoxSmoothScroll: ListBoxStory = (args) => {
769-
let items: {id: number, name: string}[] = [];
770-
for (let i = 0; i < 100; i++) {
771-
items.push({id: i, name: `Item ${i}`});
772-
}
773-
return (
774-
<ListBox
775-
className={styles.menu}
776-
{...args}
777-
aria-label="test listbox"
778-
style={{height: 200, width: 200, overflow: 'scroll', display: 'grid', gridTemplateColumns: 'repeat(4, 80px)', scrollBehavior: 'smooth'}}
779-
items={items}
780-
layout="grid">
781-
{item => <MyListBoxItem style={{minHeight: 32}}>{item.name}</MyListBoxItem>}
782-
</ListBox>
783-
);
784-
};
785-
786746
AsyncListBoxVirtualized.story = {
787747
args: {
788748
delay: 50

0 commit comments

Comments
 (0)