-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lists)!: make additionally rendered items less error prone
Before this change, putting 1 as additionnally rendered items would crash the list. The minimum was 2 and it is not very explicit. Now, minimum is 0, and default is 2. This is breaking change because it shifts the additionaNumberOfItemsRendered value by 2.
- Loading branch information
Showing
3 changed files
with
44 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 26 additions & 4 deletions
30
...atial-navigation/components/virtualizedList/helpers/getAdditionalNumberOfItemsRendered.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,36 @@ | ||
import { ScrollBehavior } from '../VirtualizedList'; | ||
|
||
/** | ||
* If list rendered elements is too small, it creates spatial navigation bugs | ||
* like not being able to go back on the left | ||
* | ||
* There are other ways to fix this than forcing a minimum number of additional items to render | ||
* but having a minimum number items to render inferior to the window size makes no sense anyway | ||
*/ | ||
const MINIMUM_ADDITIONAL_ITEMS_TO_WORK = 2; | ||
|
||
export const getAdditionalNumberOfItemsRendered = ( | ||
scrollBehavior: ScrollBehavior, | ||
numberOfElementsVisibleOnScreen: number, | ||
additionalNumberOfItemsRendered: number, | ||
) => { | ||
if (additionalNumberOfItemsRendered < 0) { | ||
console.error( | ||
'[VirtualizedList] Negative number of additional items to render was given, no elements will be rendered', | ||
); | ||
} | ||
|
||
switch (scrollBehavior) { | ||
case 'stick-to-start': | ||
case 'stick-to-end': | ||
return 4 + numberOfElementsVisibleOnScreen; | ||
case 'jump-on-scroll': | ||
return 2 * numberOfElementsVisibleOnScreen + 1; | ||
// This is a special case | ||
// Since we're jumping on scroll, we need to render more items to make sure that a whole | ||
// window is ready when we jump! | ||
return 2 * numberOfElementsVisibleOnScreen + 1 + additionalNumberOfItemsRendered; | ||
default: | ||
return ( | ||
numberOfElementsVisibleOnScreen + | ||
MINIMUM_ADDITIONAL_ITEMS_TO_WORK + | ||
additionalNumberOfItemsRendered | ||
); | ||
} | ||
}; |