mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
Change view transitions rule (#9991)
* WIP: change view transitions rule * Apply suggestions from code review Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com> * add changeset * Apply suggestions from code review Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com> --------- Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com>
This commit is contained in:
parent
ef00224a86
commit
8fb67c81bb
4 changed files with 90 additions and 6 deletions
5
.changeset/wild-singers-turn.md
Normal file
5
.changeset/wild-singers-turn.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Increases compatibility with standard browser behavior by changing where view transitions occur on browser back navigation.
|
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
@ -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'));
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Reference in a new issue