-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Refactor Virtualizer to improve performance, stability, and complexity #6451
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,6 +293,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions | |
}; | ||
|
||
// Store the scroll position so we can restore it later. | ||
/// TODO: should this happen all the time?? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't remember why we did this or what it fixed. Maybe it isn't needed anymore?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I thought it was for this flow: It looks like it's still working if I remove it though. I thought maybe it wasn't needed anymore because persistedKeys took care of it. This code was added before persistedKeys, however, it appeared to work before as well. Maybe @LFDanLu will remember, the only note was a comment from him referencing an issue he tried to solve previously #2233 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you can reproduce by getting rid of the Screen.Recording.2024-05-29.at.5.05.56.PM.movIf my memory + notes on the PR serves, this is due to how focus lands on the last checkbox in the table before being marshalled to the proper tracked focused key -> causes a scroll position change in the scrollable body -> messes up scrollIntoView calculations. Think the same issue happens if shift tabbing to a previously focused table column as well |
||
let scrollPos = useRef({top: 0, left: 0}); | ||
useEvent(scrollRef, 'scroll', isVirtualized ? null : () => { | ||
scrollPos.current = { | ||
|
@@ -342,7 +343,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions | |
scrollRef.current.scrollLeft = scrollPos.current.left; | ||
} | ||
|
||
if (!isVirtualized && manager.focusedKey != null) { | ||
if (manager.focusedKey != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was removing !isVirtualized intentional here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah we centralized the logic between both virtualized and non-virtualized collections so now they are both handled here. was there a concern or just curious? |
||
// Refocus and scroll the focused item into view if it exists within the scrollable region. | ||
let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement; | ||
if (element) { | ||
|
@@ -400,17 +401,21 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions | |
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
// If not virtualized, scroll the focused element into view when the focusedKey changes. | ||
// When virtualized, Virtualizer handles this internally. | ||
// Scroll the focused element into view when the focusedKey changes. | ||
let lastFocusedKey = useRef(manager.focusedKey); | ||
useEffect(() => { | ||
let modality = getInteractionModality(); | ||
if (manager.isFocused && manager.focusedKey != null && scrollRef?.current) { | ||
let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement; | ||
if (element && (modality === 'keyboard' || autoFocusRef.current)) { | ||
if (!isVirtualized) { | ||
scrollIntoView(scrollRef.current, element); | ||
} | ||
if (manager.isFocused && manager.focusedKey != null && manager.focusedKey !== lastFocusedKey.current && scrollRef?.current) { | ||
let modality = getInteractionModality(); | ||
let element = ref.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement; | ||
if (!element) { | ||
// If item element wasn't found, return early (don't update autoFocusRef and lastFocusedKey). | ||
// The collection may initially be empty (e.g. virtualizer), so wait until the element exists. | ||
return; | ||
} | ||
|
||
if (modality === 'keyboard' || autoFocusRef.current) { | ||
scrollIntoView(scrollRef.current, element); | ||
|
||
// Avoid scroll in iOS VO, since it may cause overlay to close (i.e. RAC submenu) | ||
if (modality !== 'virtual') { | ||
scrollIntoViewport(element, {containingElement: ref.current}); | ||
|
@@ -425,7 +430,13 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions | |
|
||
lastFocusedKey.current = manager.focusedKey; | ||
autoFocusRef.current = false; | ||
}, [isVirtualized, scrollRef, manager.focusedKey, manager.isFocused, ref]); | ||
}); | ||
|
||
// Intercept FocusScope restoration since virtualized collections can reuse DOM nodes. | ||
useEvent(ref, 'react-aria-focus-scope-restore', e => { | ||
e.preventDefault(); | ||
manager.setFocused(true); | ||
}); | ||
|
||
let handlers = { | ||
onKeyDown, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This custom event fixes restoring focus when closing an ActionBar, which previously only worked because of the animation removing rows in TableView. Without the animation, row elements are reused to represent a different object immediately, and focus appears to be restored to the wrong item. Dispatching a custom event allows
useSelectableCollection
to pick this up and apply its own logic instead of the default provided by FocusScope.