From ba6697d31daa16a3a2cd7a4cb8b7593bcdd754f9 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 28 May 2024 16:39:45 +0200 Subject: [PATCH] fix: robustify initial scroll value detection when scroll is smooth (#11802) - the previous assumption was wrong: browser don't fire a scroll event initially when the scroll isn't smooth - the previous logic wasn't using the "is scrolling now" logic which meant the render effect fired immediately after, causing smooth scrolling to start too late to be overridden adjusted the comment and reused the scroll handler function to guard against the race condition fixes #11623 --- .changeset/tough-tomatoes-explain.md | 5 +++++ .../internal/client/dom/elements/bindings/window.js | 10 ++-------- 2 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 .changeset/tough-tomatoes-explain.md diff --git a/.changeset/tough-tomatoes-explain.md b/.changeset/tough-tomatoes-explain.md new file mode 100644 index 0000000000..ac6b0ba0f2 --- /dev/null +++ b/.changeset/tough-tomatoes-explain.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: robustify initial scroll value detection when scroll is smooth 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 608f1ffbd2..13fa9d4225 100644 --- a/packages/svelte/src/internal/client/dom/elements/bindings/window.js +++ b/packages/svelte/src/internal/client/dom/elements/bindings/window.js @@ -49,14 +49,8 @@ export function bind_window_scroll(type, get_value, update) { } }); - // Browsers fire the scroll event only if the scroll position is not 0. - // This effect is (almost) guaranteed to run after the scroll event would've fired. - effect(() => { - var value = window[is_scrolling_x ? 'scrollX' : 'scrollY']; - if (value === 0) { - yield_updates(() => update(value)); - } - }); + // Browsers don't fire the scroll event for the initial scroll position when scroll style isn't set to smooth + effect(target_handler); render_effect(() => { return () => {