Skip to content

Fix scroll position jumping when shift tabbing into non-virtualized tableview#2233

Closed
LFDanLu wants to merge 12 commits into
mainfrom
shift_tab_fix_issue_2156
Closed

Fix scroll position jumping when shift tabbing into non-virtualized tableview#2233
LFDanLu wants to merge 12 commits into
mainfrom
shift_tab_fix_issue_2156

Conversation

@LFDanLu
Copy link
Copy Markdown
Member

@LFDanLu LFDanLu commented Aug 19, 2021

Closes

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

allows non virtualized tables/grids created with useTable/useGrid to have automatic scrolling when moving focus via arrow keys
some components will have a ref that is not the same element as the scrollable region so add a prop for scrollRef
…ualized table

preserves the scroll position if item is in view, otherwise scrolls it into view
also refined the detection for when a element is out of view
@LFDanLu LFDanLu marked this pull request as draft August 20, 2021 00:31
@LFDanLu LFDanLu changed the title Fix scroll position jumping when shift tabbing into non-virtualized tableview (WIP) Fix scroll position jumping when shift tabbing into non-virtualized tableview Aug 20, 2021
@LFDanLu
Copy link
Copy Markdown
Member Author

LFDanLu commented Aug 20, 2021

TODO: fix the tests and fix the case where user shift tabs back into the collection and the previously focused item was 3/4ths of the way down the list. If it is close enough to the bottom it scrolls down too much. Perhaps I have to prevent the tab default behavior still...

Comment on lines +350 to +351
useEffect(() => {
let onFocus = (e) => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Problem with this approach: if the user focuses a cell -> scrolls so that it is out of view -> moves focus out of the table -> clicks on the table column header -> the table's scrollable region will scroll to bring the previous cell into view. This happens because this capturing listener triggers before useGridCell/useSelectableItem's onFocus can make the column header the new focusedKey, thus this block thinks it needs to scroll the old cell into view.

Not sure what to do here

Comment on lines +391 to +392
useEffect(() => {
let onKeyDown = (e) => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This block was added to block the scrollable region from scrolling since shift tabbing back into the table will focus the last checkbox in the table before it shifts focus back to the tableview's focused key -> causes a scroll to happen -> messes up the calculation of scrollIntoView.

Problem is that this doesn't quite work when tabbing from outside the window (e.g. browser bar)...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There also exists a bug for non-virtualized tables where tabbing from the browser bar to the table will focus the select all checkbox -> causes the focused key to update, overwriting the previously focused key if one existed.
https://github.com/adobe/react-spectrum/blob/main/packages/@react-aria/grid/src/useGridCell.ts#L200-L202 is supposed to stop cases like that but isFocusVisible doesn't return true when tabbing from the browser bar. This is because https://github.com/adobe/react-spectrum/blob/main/packages/@react-aria/interactions/src/useFocusVisible.ts#L132 won't capture the tab keydown from the browser bar to the table since it is outside the document

@adobe-bot
Copy link
Copy Markdown

Build successful! 🎉

@LFDanLu LFDanLu changed the title (WIP) Fix scroll position jumping when shift tabbing into non-virtualized tableview Fix scroll position jumping when shift tabbing into non-virtualized tableview Aug 21, 2021
@LFDanLu
Copy link
Copy Markdown
Member Author

LFDanLu commented Aug 21, 2021

This feels pretty hacky... gonna need some second opinions, might be missing a more obvious approach

@adobe-bot
Copy link
Copy Markdown

Build successful! 🎉

@LFDanLu LFDanLu marked this pull request as ready for review August 23, 2021 20:44
@devongovett
Copy link
Copy Markdown
Member

This seems like... a lot. Can you explain why the solution we use in Virtualizer didn't work here?

@LFDanLu
Copy link
Copy Markdown
Member Author

LFDanLu commented Aug 23, 2021

@devongovett Well, the changes in this PR were an attempt to mimic what we do in Virtualizer/useVirtualizer but for a non-virtualizer case. useVirtualizer basically scrolls the item back into view when we tab back into the collection using Virtualizer's scrollToItem logic to calculate where to "scroll" to.

Since this is the non-virtualizer case and I don't have access to Virtualizer's scrollToItem logic, I instead check if focus is entering the collection via a capturing onFocus listener -> check if the previously focused item is currently in view -> preserve the scroll position if the item is in view, else use the scrollIntoView logic (ported over previously from useSelectableList) to bring it into view. This sums up the onFocus block in the pull.

The onKeyDown capturing listener was added to prevent shift + tab from messing up the scrollIntoView calculations since the browser would focus the last checkbox in the table and mess up the scrollIntoView scroll calculations a bit by causing it to overshoot by a bit.

Base automatically changed from issue_2156 to main August 24, 2021 23:56
@adobe-bot
Copy link
Copy Markdown

Build successful! 🎉

@adobe-bot
Copy link
Copy Markdown

Build successful! 🎉

@LFDanLu
Copy link
Copy Markdown
Member Author

LFDanLu commented Sep 10, 2021

Closing in favor of #2314

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