Skip to content

compensate for changed row heights #14

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

Merged
merged 1 commit into from
May 8, 2018
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
13 changes: 13 additions & 0 deletions src/VirtualList.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@
start: newStart,
end: newEnd
});

if (newStart < start) {
let d = 0;

for (let i = newStart; i < start; i += 1) {
const expectedHeight = this.heightMap[i];
const actualHeight = this.rows[i - newStart].offsetHeight;

d += actualHeight - expectedHeight;
}

this.refs.viewport.scrollTo(0, this.refs.viewport.scrollTop + d);
}
}
}
};
Expand Down
38 changes: 38 additions & 0 deletions test/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,43 @@ test('updates when items change from an empty list', t => {
list.destroy();
});

test('handles unexpected height changes when scrolling up', async t => {
const Row = svelte.create(`
<div style="height: {rowHeight}px;">test</div>
`);

const list = new VirtualList({
target,
data: {
items: Array(20).fill().map(() => ({})),
component: Row,
height: '500px',
rowHeight: 50
}
});

const { viewport } = list.refs;

await scroll(viewport, 500);
assert.equal(viewport.scrollTop, 500);

list.set({ rowHeight: 100 });
await scroll(viewport, 475);
assert.equal(viewport.scrollTop, 525);

list.destroy();
});

function scroll(element, y) {
return new Promise(fulfil => {
element.addEventListener('scroll', function handler() {
element.removeEventListener('scroll', handler);
fulfil();
});

element.scrollTo(0, y);
});
}

// this allows us to close puppeteer once tests have completed
window.done = done;