Skip to content
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

Change jump compensation logic to maintain visible position always #207

Merged
merged 1 commit into from
Oct 12, 2023
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
22 changes: 8 additions & 14 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const createVirtualStore = (
let jumpCount = 0;
let jump: ScrollJump = 0;
let _scrollDirection: ScrollDirection = SCROLL_IDLE;
let _isShifting = false;
let _isManualScrollPremeasuring = false;
let _isManualScrolling = false;
let _resized = false;
let _prevRange: ItemsRange = [0, initialItemCount];
Expand Down Expand Up @@ -211,8 +211,8 @@ export const createVirtualStore = (

// Calculate jump
let diff = 0;
if (_isShifting || _isManualScrolling) {
// Should maintain visible position under specific situations
if (!_isManualScrollPremeasuring) {
// Should maintain visible position to minimize junks in appearance
const [startIndex] = _prevRange;

if (scrollOffset === 0) {
Expand All @@ -230,12 +230,8 @@ export const createVirtualStore = (
updated.filter(([index]) => index < startIndex)
);
}
} else if (_scrollDirection === SCROLL_UP) {
// We can assume jumps occurred on the upper outside during reverse scrolling
diff = calculateJump(cache, updated);
} else {
// Do nothing
}

if (diff) {
jump = diff;
jumpCount++;
Expand Down Expand Up @@ -286,7 +282,6 @@ export const createVirtualStore = (
jumpCount++;

mutated = UPDATE_SCROLL + UPDATE_JUMP;
_isShifting = true;
} else {
updateCacheLength(cache as Writeable<Cache>, payload[0]);
}
Expand Down Expand Up @@ -324,10 +319,8 @@ export const createVirtualStore = (
shouldSync = abs(delta) > viewportSize;

mutated += UPDATE_SCROLL_WITH_EVENT;

if (!isJustResized) {
_isShifting = false;
}
} else {
_isManualScrollPremeasuring = true;
}

scrollOffset = clampScrollOffset(payload);
Expand All @@ -338,10 +331,11 @@ export const createVirtualStore = (
if (updateScrollDirection(SCROLL_IDLE)) {
mutated = UPDATE_SCROLL_DIRECTION;
}
_isShifting = _isManualScrolling = false;
_isManualScrolling = _isManualScrollPremeasuring = false;
break;
}
case ACTION_MANUAL_SCROLL: {
_isManualScrollPremeasuring = false;
_isManualScrolling = true;
break;
}
Expand Down
6 changes: 1 addition & 5 deletions stories/advanced/Feed.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type ImageData = {
type: "image";
id: number;
src: string;
size: number;
};

type Data = TextData | ImageData;
Expand All @@ -58,7 +57,6 @@ export const Default: StoryObj = {
type: "image",
id: id.current++,
src: faker.image.url(),
size: 100 * (Math.floor(Math.random() * 4) + 1),
};
};
const createItems = (num: number) => range(num, createItem);
Expand All @@ -84,9 +82,7 @@ export const Default: StoryObj = {
items.map((d) => (
<Item
key={d.id}
content={
d.type === "image" ? <img src={d.src} height={d.size} /> : d.value
}
content={d.type === "image" ? <img src={d.src} /> : d.value}
/>
)),
[items]
Expand Down
Loading