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

Change view transitions rule #9991

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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();
martrapp marked this conversation as resolved.
Show resolved Hide resolved

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'));
martrapp marked this conversation as resolved.
Show resolved Hide resolved
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
Loading