Skip to content

Commit 85d6805

Browse files
Fix: bind:scroll resets scroll state (#11469)
* Fixed: bind:scroll resets scroll state * failing test * bail if value is undefined * changeset --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 6bd6f09 commit 85d6805

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

.changeset/green-fishes-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: only initiate scroll if scroll binding has existing value

packages/svelte/src/internal/client/dom/elements/bindings/window.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export function bind_window_scroll(type, get_value, update) {
3232
};
3333

3434
render_effect(() => {
35-
latest_value = get_value() || 0;
35+
latest_value = get_value();
36+
if (latest_value === undefined) return;
37+
3638
if (!scrolling) {
3739
scrolling = true;
3840
clearTimeout(timeout);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let scrollY;
3+
</script>
4+
5+
<svelte:window bind:scrollY/>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
/** @type {Window['scrollTo']} */
5+
let original_scrollTo;
6+
7+
export default test({
8+
before_test() {
9+
original_scrollTo = window.scrollTo;
10+
11+
// @ts-ignore
12+
window.scrollTo = (x, y) => {
13+
window.scrollX = x;
14+
window.scrollY = y;
15+
};
16+
},
17+
18+
after_test() {
19+
window.scrollTo = original_scrollTo;
20+
},
21+
22+
async test({ assert, component, window, target }) {
23+
window.scrollTo(0, 500);
24+
25+
const button = target.querySelector('button');
26+
flushSync(() => button?.click());
27+
28+
assert.equal(window.scrollY, 500);
29+
}
30+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import Child from './Child.svelte';
3+
4+
let show = false;
5+
</script>
6+
7+
<div style='width: 100%; height: 9999px;'></div>
8+
9+
<button on:click={() => show = !show}>toggle</button>
10+
11+
{#if show}
12+
<Child />
13+
{/if}

0 commit comments

Comments
 (0)