-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add startViewTransition support (#10916)
- Loading branch information
1 parent
f77743a
commit feebfc0
Showing
30 changed files
with
3,003 additions
and
75 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,46 @@ | ||
--- | ||
"react-router-dom": minor | ||
"react-router": minor | ||
"@remix-run/router": minor | ||
--- | ||
|
||
Add support for the [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/ViewTransition) via `document.startViewTransition` to enable CSS animated transitions on SPA navigations in your application. | ||
|
||
The simplest approach to enabling a View Transition in your React Router app is via the new `<Link unstable_viewTransition>` prop. This will cause the navigation DOM update to be wrapped in `document.startViewTransition` which will enable transitions for the DOM update. Without any additional CSS styles, you'll get a basic cross-fade animation for your page. | ||
|
||
If you need to apply more fine-grained styles for your animations, you can leverage the `unstable_useViewTransitionState` hook which will tell you when a transition is in progress and you can use that to apply classes or styles: | ||
|
||
```jsx | ||
function ImageLink(to, src, alt) { | ||
let isTransitioning = unstable_useViewTransitionState(to); | ||
return ( | ||
<Link to={to} unstable_viewTransition> | ||
<img | ||
src={src} | ||
alt={alt} | ||
style={{ | ||
viewTransitionName: isTransitioning ? "image-expand" : "", | ||
}} | ||
/> | ||
</Link> | ||
); | ||
} | ||
``` | ||
|
||
You can also use the `<NavLink unstable_viewTransition>` shorthand which will manage the hook usage for you and automatically add a `transitioning` class to the `<a>` during the transition: | ||
|
||
```css | ||
a.transitioning img { | ||
view-transition-name: "image-expand"; | ||
} | ||
``` | ||
|
||
```jsx | ||
<NavLink to={to} unstable_viewTransition> | ||
<img src={src} alt={alt} /> | ||
</NavLink> | ||
``` | ||
|
||
For an example usage of View Transitions with React Router, check out [our fork](/~https://github.com/brophdawg11/react-router-records) of the [Astro Records](/~https://github.com/Charca/astro-records) demo. | ||
|
||
For more information on using the View Transitions API, please refer to the [Smooth and simple transitions with the View Transitions API](https://developer.chrome.com/docs/web-platform/view-transitions/) guide from the Google Chrome team. |
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,50 @@ | ||
--- | ||
title: unstable_useViewTransitionState | ||
--- | ||
|
||
# `unstable_useViewTransitionState` | ||
|
||
<details> | ||
<summary>Type declaration</summary> | ||
|
||
```tsx | ||
declare function unstable_useViewTransitionState( | ||
to: To, | ||
opts: { relative?: "route" : "path" } = {} | ||
): boolean; | ||
|
||
type To = string | Partial<Path>; | ||
|
||
interface Path { | ||
pathname: string; | ||
search: string; | ||
hash: string; | ||
} | ||
``` | ||
|
||
</details> | ||
|
||
This hook returns `true` when there is an active [View Transition][view-transitions] to the specified location. This can be used to apply finer-grained styles to elements to further customize the view transition. This requires that view transitions have been enabled for the given navigation via the [unstable_viewTransition][link-view-transition] prop on the `Link` (or the `Form`, `navigate`, or `submit` call). | ||
|
||
Consider clicking on an image in a list that you need to expand into the hero image on the destination page: | ||
|
||
```jsx | ||
function NavImage({ src, alt, id }) { | ||
let to = `/images/${idx}`; | ||
let vt = unstable_useViewTransitionState(href); | ||
return ( | ||
<Link to={to} unstable_viewTransition> | ||
<img | ||
src={src} | ||
alt={alt} | ||
style={{ | ||
viewTransitionName: vt ? "image-expand" : "", | ||
}} | ||
/> | ||
</Link> | ||
); | ||
} | ||
``` | ||
|
||
[link-view-transition]: ../components/link#unstable_viewtransition | ||
[view-transitions]: https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API |
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 @@ | ||
node_modules | ||
.DS_Store | ||
dist | ||
dist-ssr | ||
*.local |
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,4 @@ | ||
{ | ||
"installDependencies": true, | ||
"startCommand": "npm run dev" | ||
} |
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,14 @@ | ||
--- | ||
title: View Transitions | ||
toc: false | ||
--- | ||
|
||
# startViewTransition (Experimental) | ||
|
||
This example demonstrates a simple usage of a Data Router with `document.startViewTransition` enabled. | ||
|
||
## Preview | ||
|
||
Open this example on [StackBlitz](https://stackblitz.com): | ||
|
||
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/remix-run/react-router/tree/brophdawg11/start-view-transition/examples/view-transitions?file=src/App.tsx) |
Oops, something went wrong.