Skip to content
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

fix: reinsert attribute to specify direction of ViewTransition #8109

Merged
merged 1 commit into from
Aug 17, 2023
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
5 changes: 5 additions & 0 deletions .changeset/wet-foxes-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

fix: reinsert attribute to specify direction of ViewTransition (forward / back)
14 changes: 7 additions & 7 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ const { fallback = 'animate' } = Astro.props as Props;

const parser = new DOMParser();

async function updateDOM(dir: Direction, html: string, state?: State, fallback?: Fallback) {
async function updateDOM(html: string, state?: State, fallback?: Fallback) {
const doc = parser.parseFromString(html, 'text/html');
doc.documentElement.dataset.astroTransition = dir;

// Check for a head element that should persist, either because it has the data
// attribute or is a link el.
Expand Down Expand Up @@ -233,15 +232,17 @@ const { fallback = 'animate' } = Astro.props as Props;
location.href = href;
return;
}
document.documentElement.dataset.astroTransition = dir;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why here and not inside of updateDOM?

Copy link
Member Author

@martrapp martrapp Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

  1. symmetry: removing the attribute is done a few lines further down in "Navigate". Same level.
  2. When I removed doc.documentElement.dataset.astroTransition = dir from updateDOM, it was clear that updating the DOM does semantically not depend on a direction. (this was the only usage of dir in this function)
    Thought the code only moved there, because of the dependency on the parsed doc-DOM (doc)
  3. simpler, less parameters for updateDOM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, trust your judgment here, thanks!

if (supportsViewTransitions) {
finished = document.startViewTransition(() => updateDOM(dir, html, state)).finished;
finished = document.startViewTransition(() => updateDOM(html, state)).finished;
} else {
finished = updateDOM(dir, html, state, getFallback());
finished = updateDOM(html, state, getFallback());
}
try {
await finished;
} finally {
document.documentElement.removeAttribute('data-astro-transition');
// skip this for the moment as it tends to stop fallback animations
// document.documentElement.removeAttribute('data-astro-transition');
await runScripts();
markScriptsExec();
onload();
Expand Down Expand Up @@ -291,8 +292,7 @@ const { fallback = 'animate' } = Astro.props as Props;
transitionEnabledOnThisPage()
) {
ev.preventDefault();
navigate('forward', link.href, { index: currentHistoryIndex, scrollY: 0 });
currentHistoryIndex++;
navigate('forward', link.href, { index: ++currentHistoryIndex, scrollY: 0 });
const newState: State = { index: currentHistoryIndex, scrollY };
persistState({ index: currentHistoryIndex - 1, scrollY });
history.pushState(newState, '', link.href);
Expand Down