Skip to content

Commit a8f3577

Browse files
authored
Finalize View Transition event names (#8181)
* Finalize View Transition event names * Use hyphenated names
1 parent d87837e commit a8f3577

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

.changeset/proud-fans-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Finalize View Transition event names

packages/astro/components/ViewTransitions.astro

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ const { fallback = 'animate' } = Astro.props as Props;
1717
index: number;
1818
scrollY: number;
1919
};
20-
type Events = 'astro:load' | 'astro:beforeload';
20+
type Events = 'astro:page-load' | 'astro:after-swap';
2121

2222
const persistState = (state: State) => history.replaceState(state, '');
2323
const supportsViewTransitions = !!document.startViewTransition;
2424
const transitionEnabledOnThisPage = () =>
2525
!!document.querySelector('[name="astro-view-transitions-enabled"]');
2626
const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name));
27-
const onload = () => triggerEvent('astro:load');
27+
const onPageLoad = () => triggerEvent('astro:page-load');
2828
const PERSIST_ATTR = 'data-astro-transition-persist';
2929

3030
// The History API does not tell you if navigation is forward or back, so
@@ -193,7 +193,7 @@ const { fallback = 'animate' } = Astro.props as Props;
193193
scrollTo(0, state.scrollY); // usings default scrollBehavior
194194
}
195195

196-
triggerEvent('astro:beforeload');
196+
triggerEvent('astro:after-swap');
197197
};
198198

199199
// Wait on links to finish, to prevent FOUC
@@ -263,7 +263,7 @@ const { fallback = 'animate' } = Astro.props as Props;
263263
// document.documentElement.removeAttribute('data-astro-transition');
264264
await runScripts();
265265
markScriptsExec();
266-
onload();
266+
onPageLoad();
267267
}
268268
}
269269

@@ -383,7 +383,7 @@ const { fallback = 'animate' } = Astro.props as Props;
383383
{ passive: true, capture: true }
384384
);
385385
});
386-
addEventListener('load', onload);
386+
addEventListener('load', onPageLoad);
387387
// There's not a good way to record scroll position before a back button.
388388
// So the way we do it is by listening to scroll and just continuously recording it.
389389
addEventListener(

packages/astro/e2e/fixtures/view-transitions/src/components/DarkMode.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
}
77

88
toggle();
9-
document.addEventListener('astro:beforeload', () => {
9+
document.addEventListener('astro:after-swap', () => {
1010
toggle();
1111
})
1212
</script>

packages/astro/e2e/fixtures/view-transitions/src/pages/two.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Layout from '../components/Layout.astro';
66
<article id="twoarticle"></article>
77
</Layout>
88
<script>
9-
document.addEventListener('astro:load', () => {
9+
document.addEventListener('astro:page-load', () => {
1010
document.getElementById('twoarticle')!.textContent = 'works';
1111
}, { once: true });
1212
</script>

packages/astro/e2e/view-transitions.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test.describe('View Transitions', () => {
181181
await expect(p, 'imported CSS updated').toHaveCSS('font-size', '24px');
182182
});
183183

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

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

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

0 commit comments

Comments
 (0)