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

Finalize View Transition event names #8181

Merged
merged 2 commits into from
Aug 24, 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/proud-fans-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Finalize View Transition event names
10 changes: 5 additions & 5 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { fallback = 'animate' } = Astro.props as Props;
index: number;
scrollY: number;
};
type Events = 'astro:load' | 'astro:beforeload';
type Events = 'astro:page-load' | 'astro:after-swap';

const persistState = (state: State) => history.replaceState(state, '');

Expand All @@ -33,7 +33,7 @@ const { fallback = 'animate' } = Astro.props as Props;
const transitionEnabledOnThisPage = () =>
!!document.querySelector('[name="astro-view-transitions-enabled"]');
const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name));
const onload = () => triggerEvent('astro:load');
const onPageLoad = () => triggerEvent('astro:page-load');
const PERSIST_ATTR = 'data-astro-transition-persist';

const throttle = (cb: (...args: any[]) => any, delay: number) => {
Expand Down Expand Up @@ -175,7 +175,7 @@ const { fallback = 'animate' } = Astro.props as Props;
persistState(state);
}

triggerEvent('astro:beforeload');
triggerEvent('astro:after-swap');
};

// Wait on links to finish, to prevent FOUC
Expand Down Expand Up @@ -245,7 +245,7 @@ const { fallback = 'animate' } = Astro.props as Props;
// document.documentElement.removeAttribute('data-astro-transition');
await runScripts();
markScriptsExec();
onload();
onPageLoad();
}
}

Expand Down Expand Up @@ -339,7 +339,7 @@ const { fallback = 'animate' } = Astro.props as Props;
{ passive: true, capture: true }
);
});
addEventListener('load', onload);
addEventListener('load', onPageLoad);
// There's not a good way to record scroll position before a back button.
// So the way we do it is by listening to scroll and just continuously recording it.
addEventListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}

toggle();
document.addEventListener('astro:beforeload', () => {
document.addEventListener('astro:after-swap', () => {
toggle();
})
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Layout from '../components/Layout.astro';
<article id="twoarticle"></article>
</Layout>
<script>
document.addEventListener('astro:load', () => {
document.addEventListener('astro:page-load', () => {
document.getElementById('twoarticle')!.textContent = 'works';
}, { once: true });
</script>
6 changes: 3 additions & 3 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ test.describe('View Transitions', () => {
await expect(p, 'imported CSS updated').toHaveCSS('font-size', '24px');
});

test('astro:load event fires when navigating to new page', async ({ page, astro }) => {
test('astro:page-load event fires when navigating to new page', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
const p = page.locator('#one');
Expand All @@ -159,14 +159,14 @@ test.describe('View Transitions', () => {
await expect(article, 'should have script content').toHaveText('works');
});

test('astro:load event fires when navigating directly to a page', async ({ page, astro }) => {
test('astro:page-load event fires when navigating directly to a page', async ({ page, astro }) => {
// Go to page 2
await page.goto(astro.resolveUrl('/two'));
const article = page.locator('#twoarticle');
await expect(article, 'should have script content').toHaveText('works');
});

test('astro:beforeload event fires right before the swap', async ({ page, astro }) => {
test('astro:after-swap event fires right after the swap', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
Expand Down