-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): Add support for View Transitions (#4354)
- Loading branch information
1 parent
1be6dac
commit 9d34d4f
Showing
11 changed files
with
178 additions
and
4 deletions.
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)); | ||
|
||
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