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

Add astro:beforeload event #7823

Merged
merged 4 commits into from
Jul 27, 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/chilled-elephants-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Adds an `astro:beforeload` event for the dark mode use-case
9 changes: 7 additions & 2 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { fallback = 'animate' } = Astro.props as Props;
<script>
type Fallback = 'none' | 'animate' | 'swap';
type Direction = 'forward' | 'back';
type Events = 'astro:load' | 'astro:beforeload';

// The History API does not tell you if navigation is forward or back, so
// you can figure it using an index. On pushState the index is incremented so you
Expand All @@ -25,7 +26,8 @@ const { fallback = 'animate' } = Astro.props as Props;
const supportsViewTransitions = !!document.startViewTransition;
const transitionEnabledOnThisPage = () =>
!!document.querySelector('[name="astro-view-transitions-enabled"]');
const onload = () => document.dispatchEvent(new Event('astro:load'));
const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name));
const onload = () => triggerEvent('astro:load');

async function getHTML(href: string) {
const res = await fetch(href);
Expand Down Expand Up @@ -65,7 +67,10 @@ const { fallback = 'animate' } = Astro.props as Props;
async function updateDOM(dir: Direction, html: string, fallback?: Fallback) {
const doc = parser.parseFromString(html, 'text/html');
doc.documentElement.dataset.astroTransition = dir;
const swap = () => document.documentElement.replaceWith(doc.documentElement);
const swap = () => {
document.documentElement.replaceWith(doc.documentElement);
triggerEvent('astro:beforeload')
}

// Wait on links to finish, to prevent FOUC
const links = Array.from(doc.querySelectorAll('head link[rel=stylesheet]')).map(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
const toggle = () => {
if(localStorage.darkMode) {
document.documentElement.dataset.dark = '';
}
}

toggle();
document.addEventListener('astro:beforeload', () => {
toggle();
})
</script>
<style is:global>
html[data-dark] body {
background: black;
color: white;
}
</style>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { ViewTransitions } from 'astro:transitions';
import DarkMode from './DarkMode.astro';

interface Props {
link?: string;
Expand All @@ -12,6 +13,7 @@ const { link } = Astro.props as Props;
<title>Testing</title>
{link ? <link rel="stylesheet" href={link} > : ''}
<ViewTransitions />
<DarkMode />
</head>
<body>
<header transition:animate="morph">
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ test.describe('View Transitions', () => {
await expect(article, 'should have script content').toHaveText('works');
});

test('astro:beforeload event fires right before the swap', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');

// go to page 2
await page.click('#click-two');
p = page.locator('#two');
const h = page.locator('html');
await expect(h, 'imported CSS updated').toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
})

test('click hash links does not do navigation', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/one'));
Expand Down