Skip to content

fix(scrollIntoView): respect scroll padding #7484

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

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
40 changes: 28 additions & 12 deletions packages/@react-aria/utils/src/scrollIntoView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,40 @@ export function scrollIntoView(scrollView: HTMLElement, element: HTMLElement) {
let x = scrollView.scrollLeft;
let y = scrollView.scrollTop;

// Account for top/left border offsetting the scroll top/Left
let {borderTopWidth, borderLeftWidth} = getComputedStyle(scrollView);
let borderAdjustedX = scrollView.scrollLeft + parseInt(borderLeftWidth, 10);
let borderAdjustedY = scrollView.scrollTop + parseInt(borderTopWidth, 10);
// Account for top/left border offsetting the scroll top/Left + scroll padding
let {
borderTopWidth,
borderLeftWidth,
scrollPaddingTop,
scrollPaddingRight,
scrollPaddingBottom,
scrollPaddingLeft
} = getComputedStyle(scrollView);

let borderAdjustedX = x + parseInt(borderLeftWidth, 10);
let borderAdjustedY = y + parseInt(borderTopWidth, 10);
// Ignore end/bottom border via clientHeight/Width instead of offsetHeight/Width
let maxX = borderAdjustedX + scrollView.clientWidth;
let maxY = borderAdjustedY + scrollView.clientHeight;

if (offsetX <= x) {
x = offsetX - parseInt(borderLeftWidth, 10);
} else if (offsetX + width > maxX) {
x += offsetX + width - maxX;
// Get scroll padding values as pixels - defaults to 0 if no scroll padding
// is used.
let scrollPaddingTopNumber = parseInt(scrollPaddingTop, 10) || 0;
let scrollPaddingBottomNumber = parseInt(scrollPaddingBottom, 10) || 0;
let scrollPaddingRightNumber = parseInt(scrollPaddingRight, 10) || 0;
let scrollPaddingLeftNumber = parseInt(scrollPaddingLeft, 10) || 0;

if (offsetX <= x + scrollPaddingLeftNumber) {
Copy link
Member

Choose a reason for hiding this comment

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

why isn't this one if (offsetX <= borderAdjustedX + scrollPaddingLeftNumber) {? or why isn't line 61 just y instead of borderAdjustedY?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't tell you - the original expression used x instead of borderAdjustedX, so I kept it the same. Do you want me to change it?

Copy link
Member

Choose a reason for hiding this comment

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

no worries, it's not a change from existing behaviour as you pointed out, I just don't recall why one was changed but not the other and hoped you might have some insight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me it looks like it could have been an oversight in the original implementation. I'm happy to make the change if we all agree?

x = offsetX - parseInt(borderLeftWidth, 10) - scrollPaddingLeftNumber;
} else if (offsetX + width > maxX - scrollPaddingRightNumber) {
x += offsetX + width - maxX + scrollPaddingRightNumber;
}
if (offsetY <= borderAdjustedY) {
y = offsetY - parseInt(borderTopWidth, 10);
} else if (offsetY + height > maxY) {
y += offsetY + height - maxY;
if (offsetY <= borderAdjustedY + scrollPaddingTopNumber) {
y = offsetY - parseInt(borderTopWidth, 10) - scrollPaddingTopNumber;
} else if (offsetY + height > maxY - scrollPaddingBottomNumber) {
y += offsetY + height - maxY + scrollPaddingBottomNumber;
}

scrollView.scrollLeft = x;
scrollView.scrollTop = y;
}
Expand Down
48 changes: 48 additions & 0 deletions packages/react-aria-components/stories/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,54 @@ export const MenuComplex = () => (
</MenuTrigger>
);

export const MenuScrollPaddingExample = () => (
<MenuTrigger>
<Button aria-label="Menu">☰</Button>
<Popover>
<Menu
className={styles.menu}
onAction={action('onAction')}
style={{
maxHeight: 200,
position: 'relative',
scrollPaddingTop: '25px',
scrollPaddingBottom: '25px',
paddingBottom: '25px' // needed due to absolute-positioned footer
}}>
<Header
style={{
fontSize: '1.2em',
position: 'sticky',
top: 0,
height: '25px',
background: 'lightgray',
borderBottom: '1px solid gray'
}}>
Section 1
</Header>
{Array.from({length: 30}).map((_, i) => (
<MyMenuItem key={i}>Item {i + 1}</MyMenuItem>
))}
</Menu>
{/* Menu doesn't have a footer, so have to put one outside to
and position it absolutely to demo scroll padding bottom support. */}
<div
style={{
fontSize: '1.2em',
position: 'absolute',
bottom: 0,
left: 0,
width: '100%',
height: '24px', // with the border it'll be 25px
borderTop: '1px solid gray',
background: 'lightgray'
}}>
A footer
</div>
</Popover>
</MenuTrigger>
);

export const SubmenuExample = (args) => (
<MenuTrigger>
<Button aria-label="Menu">☰</Button>
Expand Down
Loading