-
Notifications
You must be signed in to change notification settings - Fork 291
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
feat(astro): Add support for View Transitions #4354
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
84773f6
feat(astro): Add support for View Transitions
wobsoriano 4311c3c
chore: add changeset
wobsoriano 5727a57
chore: Improve variables and comments
wobsoriano 1c59384
chore: keep div element created by Clerk in new document
wobsoriano 8765cd4
test(astro): Add initial e2e
wobsoriano 1946e59
test: improve view transition e2e
wobsoriano 12568f7
test: fix vite error
wobsoriano 2324c73
test: fix vite error
wobsoriano 9160f13
test: add more guide comments
wobsoriano 695e25f
test: add vite fix to hybrid test
wobsoriano 9201794
Merge branch 'main' into rob/eco-64-astro-view-transitions-support
wobsoriano 6963147
test: fix vite optimize deps error
wobsoriano a2afb95
chore: improve navigation
wobsoriano 8e9a101
Merge branch 'main' into rob/eco-64-astro-view-transitions-support
wobsoriano b58e76b
chore: exclude astro transitions imports from optimized deps
wobsoriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/astro": minor | ||
--- | ||
|
||
Add support for Astro View Transitions |
27 changes: 27 additions & 0 deletions
27
integration/templates/astro-node/src/layouts/ViewTransitionsLayout.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
import { ViewTransitions } from 'astro:transitions'; | ||
|
||
interface Props { | ||
title: string; | ||
} | ||
|
||
const { title } = Astro.props; | ||
--- | ||
|
||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="description" content="Astro description" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" /> | ||
<meta name="generator" content={Astro.generator} /> | ||
<title>{title}</title> | ||
<ViewTransitions /> | ||
</head> | ||
<body> | ||
<main> | ||
<slot /> | ||
</main> | ||
</body> | ||
</html> |
15 changes: 15 additions & 0 deletions
15
integration/templates/astro-node/src/pages/transitions/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
import { SignedIn, SignedOut, UserButton } from "@clerk/astro/components"; | ||
import Layout from "../../layouts/ViewTransitionsLayout.astro"; | ||
--- | ||
|
||
<Layout title="Sign in"> | ||
<div class="w-full flex justify-center"> | ||
<SignedOut> | ||
<a href="/transitions/sign-in">Sign in</a> | ||
</SignedOut> | ||
<SignedIn> | ||
<UserButton /> | ||
</SignedIn> | ||
</div> | ||
</Layout> |
10 changes: 10 additions & 0 deletions
10
integration/templates/astro-node/src/pages/transitions/sign-in.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
import { SignIn } from "@clerk/astro/components"; | ||
import Layout from "../../layouts/ViewTransitionsLayout.astro"; | ||
--- | ||
|
||
<Layout title="Sign in"> | ||
<div class="w-full flex justify-center"> | ||
<SignIn forceRedirectUrl="/transitions" /> | ||
</div> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// eslint-disable-next-line import/no-unresolved | ||
import { swapFunctions } from 'astro:transitions/client'; | ||
|
||
const PERSIST_ATTR = 'data-astro-transition-persist'; | ||
const EMOTION_ATTR = 'data-emotion'; | ||
|
||
/** | ||
* @internal | ||
* Custom swap function to make mounting and styling | ||
* of Clerk components work with View Transitions in Astro. | ||
* | ||
* See https://docs.astro.build/en/guides/view-transitions/#building-a-custom-swap-function | ||
*/ | ||
export function swapDocument(doc: Document) { | ||
swapFunctions.deselectScripts(doc); | ||
swapFunctions.swapRootAttributes(doc); | ||
|
||
// Keep the elements created by `@emotion/cache` | ||
const emotionElements = document.querySelectorAll(`style[${EMOTION_ATTR}]`); | ||
swapHeadElements(doc, Array.from(emotionElements)); | ||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
const restoreFocusFunction = swapFunctions.saveFocus(); | ||
swapFunctions.swapBodyElement(doc.body, document.body); | ||
restoreFocusFunction(); | ||
} | ||
|
||
/** | ||
* This function is a copy of the original `swapHeadElements` function from `astro:transitions/client`. | ||
* The difference is that you can pass a list of elements that should not be removed | ||
* in the new document. | ||
* | ||
* See https://github.com/withastro/astro/blob/d6f17044d3873df77cfbc73230cb3194b5a7d82a/packages/astro/src/transitions/swap-functions.ts#L51 | ||
*/ | ||
function swapHeadElements(doc: Document, ignoredElements: Element[]) { | ||
for (const el of Array.from(document.head.children)) { | ||
const newEl = persistedHeadElement(el, doc); | ||
|
||
if (newEl) { | ||
newEl.remove(); | ||
} else { | ||
if (!ignoredElements.includes(el)) { | ||
el.remove(); | ||
} | ||
} | ||
} | ||
|
||
document.head.append(...doc.head.children); | ||
} | ||
|
||
function persistedHeadElement(el: Element, newDoc: Document) { | ||
const id = el.getAttribute(PERSIST_ATTR); | ||
const newEl = id && newDoc.head.querySelector(`[${PERSIST_ATTR}="${id}"]`); | ||
if (newEl) { | ||
return newEl; | ||
} | ||
if (el.matches('link[rel=stylesheet]')) { | ||
const href = el.getAttribute('href'); | ||
return newDoc.head.querySelector(`link[rel=stylesheet][href="${href}"]`); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Is this always bundled with Astro ? Is there a case where the module will not exists causing our integration to break ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is indeed bundled with Astro (
astro/virtual-modules/transitions-swap-functions.js
), but is not available in Astro v3.Also we let the Astro Vite plugin handle it by excluding from
vite.optimizeDeps
:javascript/packages/astro/src/integration/vite-plugin-astro-config.ts
Line 30 in b58e76b