Skip to content

Commit

Permalink
Change view transitions rule (#9991)
Browse files Browse the repository at this point in the history
* WIP: change view transitions rule

* Apply suggestions from code review

Co-authored-by: Martin Trapp <[email protected]>

* add changeset

* Apply suggestions from code review

Co-authored-by: Martin Trapp <[email protected]>

---------

Co-authored-by: Martin Trapp <[email protected]>
  • Loading branch information
ktym4a and martrapp authored Feb 7, 2024
1 parent ef00224 commit 8fb67c8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-singers-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Increases compatibility with standard browser behavior by changing where view transitions occur on browser back navigation.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Layout from '../components/Layout.astro';
<Layout>
<article id="longpage">
<div><a id="click-one" href="/one">go to 1</a></div>
<div><a id="click-self" href="/long-page">go to self</a></div>
<div><a id="click-scroll-down" href="#click-one-again">go further down</a></div>

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Dictum varius duis at consectetur lorem donec massa sapien faucibus. Amet mauris commodo quis imperdiet massa. Sed pulvinar proin gravida hendrerit lectus a. Magna ac placerat vestibulum lectus. Blandit cursus risus at ultrices mi tempus. Luctus venenatis lectus magna fringilla urna porttitor. Auctor eu augue ut lectus arcu bibendum at varius vel. Tristique senectus et netus et. In fermentum et sollicitudin ac orci phasellus egestas tellus rutrum. Eget lorem dolor sed viverra ipsum nunc aliquet. Amet consectetur adipiscing elit ut aliquam purus. Accumsan lacus vel facilisis volutpat est velit egestas. Felis imperdiet proin fermentum leo vel. Ut tellus elementum sagittis vitae et leo duis ut diam. Nisl pretium fusce id velit. Lorem donec massa sapien faucibus et. Nibh sed pulvinar proin gravida hendrerit lectus a. In est ante in nibh mauris cursus mattis molestie.
Expand Down
82 changes: 79 additions & 3 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,7 @@ test.describe('View Transitions', () => {
await expect(locator).toBeInViewport();

// Scroll back to top
// back returns immediately, but we need to wait for navigate() to complete
const waitForReady = page.waitForEvent('console');
await page.goBack();
await waitForReady;
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();

Expand All @@ -426,6 +423,85 @@ test.describe('View Transitions', () => {
await expect(locator).toBeInViewport();
});

test('View Transitions Rule', async ({ page, astro }) => {
let consoleCount = 0;
page.on('console', (msg) => {
// This count is used for transition events
if (msg.text() === 'ready') consoleCount++;
});
// Don't test back and forward '' to '', because They are not stored in the history.
// click '' to '' (transition)
await page.goto(astro.resolveUrl('/long-page'));
let locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
let consolePromise = page.waitForEvent('console');
await page.click('#click-self');
await consolePromise;
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// click '' to 'hash' (no transition)
await page.click('#click-scroll-down');
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// back 'hash' to '' (no transition)
await page.goBack();
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// forward '' to 'hash' (no transition)
await page.goForward();
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// click 'hash' to 'hash' (no transition)
await page.click('#click-scroll-up');
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// back 'hash' to 'hash' (no transition)
await page.goBack();
locator = page.locator('#click-one-again');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// forward 'hash' to 'hash' (no transition)
await page.goForward();
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(1);

// click 'hash' to '' (transition)
consolePromise = page.waitForEvent('console');
await page.click('#click-self');
await consolePromise;
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(2);

// back '' to 'hash' (transition)
consolePromise = page.waitForEvent('console');
await page.goBack();
await consolePromise;
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(3);

// forward 'hash' to '' (transition)
consolePromise = page.waitForEvent('console');
await page.goForward();
await consolePromise;
locator = page.locator('#longpage');
await expect(locator).toBeInViewport();
expect(consoleCount).toEqual(4);
});

test('<Image /> component forwards transitions to the <img>', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/image-one'));
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/transitions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,11 @@ async function transition(
if (navigationType !== 'traverse') {
updateScrollPosition({ scrollX, scrollY });
}
if (samePage(from, to) && !!to.hash) {
moveToLocation(to, from, options, document.title, historyState);
return;
if (samePage(from, to)) {
if ((direction !== 'back' && to.hash) || (direction === 'back' && from.hash)) {
moveToLocation(to, from, options, document.title, historyState);
return;
}
}

const prepEvent = await doPreparation(
Expand Down

0 comments on commit 8fb67c8

Please sign in to comment.