From 85d680582b5435c4dbe93db7c6c28d98685f2b47 Mon Sep 17 00:00:00 2001 From: Vladislav Logvin <122863429+Black1358@users.noreply.github.com> Date: Tue, 7 May 2024 15:44:49 +0300 Subject: [PATCH] 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 --- .changeset/green-fishes-lie.md | 5 ++++ .../client/dom/elements/bindings/window.js | 4 ++- .../window-bind-scroll-update-2/Child.svelte | 5 ++++ .../window-bind-scroll-update-2/_config.js | 30 +++++++++++++++++++ .../window-bind-scroll-update-2/main.svelte | 13 ++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .changeset/green-fishes-lie.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/Child.svelte create mode 100644 packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/main.svelte diff --git a/.changeset/green-fishes-lie.md b/.changeset/green-fishes-lie.md new file mode 100644 index 0000000000..2d890d16d7 --- /dev/null +++ b/.changeset/green-fishes-lie.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: only initiate scroll if scroll binding has existing value diff --git a/packages/svelte/src/internal/client/dom/elements/bindings/window.js b/packages/svelte/src/internal/client/dom/elements/bindings/window.js index 8cab87122b..55c0b645de 100644 --- a/packages/svelte/src/internal/client/dom/elements/bindings/window.js +++ b/packages/svelte/src/internal/client/dom/elements/bindings/window.js @@ -32,7 +32,9 @@ export function bind_window_scroll(type, get_value, update) { }; render_effect(() => { - latest_value = get_value() || 0; + latest_value = get_value(); + if (latest_value === undefined) return; + if (!scrolling) { scrolling = true; clearTimeout(timeout); diff --git a/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/Child.svelte b/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/Child.svelte new file mode 100644 index 0000000000..952d3dd1ca --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/Child.svelte @@ -0,0 +1,5 @@ + + + diff --git a/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/_config.js b/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/_config.js new file mode 100644 index 0000000000..29100e3e70 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/_config.js @@ -0,0 +1,30 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +/** @type {Window['scrollTo']} */ +let original_scrollTo; + +export default test({ + before_test() { + original_scrollTo = window.scrollTo; + + // @ts-ignore + window.scrollTo = (x, y) => { + window.scrollX = x; + window.scrollY = y; + }; + }, + + after_test() { + window.scrollTo = original_scrollTo; + }, + + async test({ assert, component, window, target }) { + window.scrollTo(0, 500); + + const button = target.querySelector('button'); + flushSync(() => button?.click()); + + assert.equal(window.scrollY, 500); + } +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/main.svelte b/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/main.svelte new file mode 100644 index 0000000000..9b3de0b307 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/window-bind-scroll-update-2/main.svelte @@ -0,0 +1,13 @@ + + +
+ + + +{#if show} + +{/if}