Skip to content

Commit

Permalink
Fix 8083 (#8105)
Browse files Browse the repository at this point in the history
* override wrong positions in browser history

* Lost events are taken into account during throttling
  • Loading branch information
martrapp authored Aug 16, 2023
1 parent 9dd09e2 commit 0e0fa60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-planes-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

ViewTransition: bug fix for lost scroll position in browser history
15 changes: 13 additions & 2 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ const { fallback = 'animate' } = Astro.props as Props;

const throttle = (cb: (...args: any[]) => any, delay: number) => {
let wait = false;
// During the waiting time additional events are lost.
// So repeat the callback at the end if we have swallowed events.
let onceMore = false;
return (...args: any[]) => {
if (wait) return;

if (wait) {
onceMore = true;
return;
}
cb(...args);
wait = true;
setTimeout(() => {
if (onceMore) {
onceMore = false;
cb(...args);
}
wait = false;
}, delay);
};
Expand Down Expand Up @@ -163,6 +172,8 @@ const { fallback = 'animate' } = Astro.props as Props;
}
if (state?.scrollY != null) {
scrollTo(0, state.scrollY);
// Overwrite erroneous updates by the scroll handler during transition
persistState(state);
}

triggerEvent('astro:beforeload');
Expand Down

0 comments on commit 0e0fa60

Please sign in to comment.