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

property in anchor element forces a reload of the target #8153

Merged
merged 1 commit 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/lovely-ducks-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

fix: View Transition: swap attributes of document's root element
12 changes: 12 additions & 0 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ const { fallback = 'animate' } = Astro.props as Props;
// Remove them before swapping.
doc.querySelectorAll('head noscript').forEach((el) => el.remove());

// swap attributes of the html element
// - delete all attributes from the current document
// - insert all attributes from doc
// - reinsert all original attributes that are named 'data-astro-*'
const html = document.documentElement;
const astro = [...html.attributes].filter(
({ name }) => (html.removeAttribute(name), name.startsWith('data-astro-'))
);
[...doc.documentElement.attributes, ...astro].forEach(({ name, value }) =>
html.setAttribute(name, value)
);

// Swap head
for (const el of Array.from(document.head.children)) {
const newEl = persistedHeadElement(el);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import { ViewTransitions } from 'astro:transitions';
import { HTMLAttributes } from 'astro/types';

interface Props extends HTMLAttributes<'html'> {}
---
<html {...Astro.props}>
<head>
<title>Testing</title>
<ViewTransitions />
</head>
<body>
<main transition:animate="slide">
<slot />
</main>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import AttributeLayout from '../components/AttributedLayout.astro';
---
<AttributeLayout
lang="es"
style="background-color: green"
data-other-name="value"
data-astro-fake="value"
data-astro-transition="downward">
<p id="heading">Page with other attributes</p>
</AttributeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import AttributedLayout from '../components/AttributedLayout.astro';
---
<AttributedLayout lang="en" class="ugly">
<p id="heading">Page with some attributes</p>
<a id="click-other-attributes" href="/other-attributes">Other attributes</a>
</AttributedLayout>
22 changes: 22 additions & 0 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,25 @@ test.describe('View Transitions', () => {
).toEqual(1);
});
});

test('Navigation also swaps the attributes of the document root', async ({ page, astro }) => {
page.on('console', (msg) => console.log(msg.text()));
await page.goto(astro.resolveUrl('/some-attributes'));
let p = page.locator('#heading');
await expect(p, 'should have content').toHaveText('Page with some attributes');

let h = page.locator('html');
await expect(h, 'should have content').toHaveAttribute('lang', 'en');

await page.click('#click-other-attributes');
p = page.locator('#heading');
await expect(p, 'should have content').toHaveText('Page with other attributes');

h = page.locator('html');
await expect(h, 'should have content').toHaveAttribute('lang', 'es');
await expect(h, 'should have content').toHaveAttribute('style', 'background-color: green');
await expect(h, 'should have content').toHaveAttribute('data-other-name', 'value');
await expect(h, 'should have content').toHaveAttribute('data-astro-fake', 'value');
await expect(h, 'should have content').toHaveAttribute('data-astro-transition', 'forward');
await expect(h, 'should be absent').not.toHaveAttribute('class', /.*/);
});