Skip to content

Commit

Permalink
Merge pull request #497 from inokawa/svelte-resize
Browse files Browse the repository at this point in the history
Fixed bug Svelte VList ignores items resize
  • Loading branch information
inokawa authored Aug 14, 2024
2 parents f8a29ac + 5cf933b commit 7014923
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/svelte/ListItem.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { afterUpdate, onDestroy } from "svelte";
import { isRTLDocument, type ItemResizeObserver } from "./core";
import { effect, styleToString } from "./utils";
import { styleToString } from "./utils";
export let index: number;
export let offset: number;
Expand All @@ -10,11 +11,16 @@
let elementRef: HTMLDivElement;
let cleanupResizer: (() => void) | undefined;
// The index may be changed if elements are inserted to or removed from the start of props.children
let prevIndex: number | undefined;
effect(() => {
afterUpdate(() => {
if (prevIndex === index) return;
return resizer(elementRef, (prevIndex = index));
if (cleanupResizer) cleanupResizer();
cleanupResizer = resizer(elementRef, (prevIndex = index));
});
onDestroy(() => {
if (cleanupResizer) cleanupResizer();
});
let style: string;
Expand Down
8 changes: 4 additions & 4 deletions src/svelte/VList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
SCROLL_TO_INDEX,
} from "./core";
import {
effect,
onUpdate,
type UnwrapCustomEvents,
defaultGetKey,
styleToString,
Expand Down Expand Up @@ -131,20 +131,20 @@
});
let prevLength = data.length;
effect(() => {
onUpdate(() => {
if (prevLength === data.length) return;
virtualizer[CHANGE_ITEM_LENGTH]((prevLength = data.length), shift);
}, true);
let prevJumpCount: number | undefined;
effect(() => {
onUpdate(() => {
if (prevJumpCount === jumpCount) return;
prevJumpCount = jumpCount;
virtualizer[FIX_SCROLL_JUMP]();
});
let prevRange: typeof range | undefined;
effect(() => {
onUpdate(() => {
if (prevRange && prevRange[0] === range[0] && prevRange[1] === range[1])
return;
prevRange = range;
Expand Down
13 changes: 3 additions & 10 deletions src/svelte/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { afterUpdate, beforeUpdate, onDestroy } from "svelte";

export const effect = (cb: () => (() => void) | void, before?: boolean) => {
let cleanup: (() => void) | void;
import { afterUpdate, beforeUpdate } from "svelte";

export const onUpdate = (cb: () => void, before?: boolean) => {
(before ? beforeUpdate : afterUpdate)(() => {
if (cleanup) cleanup();
cleanup = cb();
});

onDestroy(() => {
if (cleanup) cleanup();
cb();
});
};

Expand Down

0 comments on commit 7014923

Please sign in to comment.