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 startViewTransition support #10916

Merged
merged 43 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0d5dd29
POC support for startViewTransition
brophdawg11 Mar 9, 2023
069b77f
Add example using startViwTransition
brophdawg11 Jul 14, 2023
9d6f330
Update dep
brophdawg11 Aug 22, 2023
cbd6aad
Update approach and add future flags
brophdawg11 Aug 29, 2023
b774db3
Rename future flag
brophdawg11 Aug 29, 2023
dbac4b1
Updates
brophdawg11 Aug 30, 2023
560a894
Switch to useViewTransition hook
brophdawg11 Sep 19, 2023
d4087e9
Add more updates - proxy through Link
brophdawg11 Sep 20, 2023
c383e8f
Add unstable prefix
brophdawg11 Sep 20, 2023
48303d0
Revert "Add unstable prefix"
brophdawg11 Sep 20, 2023
449d51e
Remove viewTransition API from router.navigate
brophdawg11 Sep 20, 2023
c1dee37
Rename useViewTransitions
brophdawg11 Sep 20, 2023
f516ded
Allow return false
brophdawg11 Sep 20, 2023
f800892
POC
brophdawg11 Sep 22, 2023
adf0b78
Get POP navigations working
brophdawg11 Sep 25, 2023
d8bafcd
Lift viewTransition function execution up to router
brophdawg11 Sep 25, 2023
18cc33b
Handle forward pops
brophdawg11 Sep 25, 2023
27f65c1
Updates
brophdawg11 Sep 26, 2023
9fcf8df
Update approch to use prop and hook together
brophdawg11 Sep 28, 2023
7d6a724
Remove currentLocation from ViewTransitionContext
brophdawg11 Sep 28, 2023
5768b76
Fix image freeze on detail->home
brophdawg11 Sep 28, 2023
e43453e
Remove defer value param
brophdawg11 Sep 28, 2023
f456698
Remove usage of fallbackOnDeferRevalidation
brophdawg11 Sep 28, 2023
2359232
Remove fallbackOnDeferRevalidation flag
brophdawg11 Sep 28, 2023
360206f
Remove debug logs
brophdawg11 Sep 28, 2023
15961e1
Clean ups
brophdawg11 Sep 28, 2023
2f716d3
Revert "Remove debug logs"
brophdawg11 Sep 28, 2023
dda9693
Add interruption handling
brophdawg11 Sep 29, 2023
7263d4a
Remove debug logs
brophdawg11 Sep 29, 2023
098efb7
Remove unused flushSync
brophdawg11 Sep 29, 2023
b9f0c8a
Persist applied transitions to sessionStorage
brophdawg11 Sep 29, 2023
3912731
Add docs
brophdawg11 Oct 2, 2023
4a8a492
Handle basename and PUSH navs that reverse a transition
brophdawg11 Oct 4, 2023
7383054
Udpate docs with example
brophdawg11 Oct 4, 2023
d5e1a89
Udpate docs with example
brophdawg11 Oct 4, 2023
35c4680
Add changeset
brophdawg11 Oct 4, 2023
1e8dcaa
Only leverage useViewTransitionState in DataRouter NavLink usages
brophdawg11 Oct 5, 2023
6ddc868
Fix tests
brophdawg11 Oct 11, 2023
04857aa
Unit tests
brophdawg11 Oct 11, 2023
e687cac
Bump bundle
brophdawg11 Oct 11, 2023
34ace81
Bump bundle
brophdawg11 Oct 11, 2023
54298be
Update changeset
brophdawg11 Oct 11, 2023
f3db06f
Rename viewTransitionOpts -> unstable_viewTransitionOpts
brophdawg11 Oct 11, 2023
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
Prev Previous commit
Next Next commit
Update approch to use prop and hook together
  • Loading branch information
brophdawg11 committed Sep 28, 2023
commit 9fcf8df45ff3344e606fd87beed6b70d4caf3f25
199 changes: 139 additions & 60 deletions examples/view-transitions/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import {
Await,
createBrowserRouter,
defer,
Form,
json,
Link,
NavLink,
Outlet,
RouterProvider,
unstable_useViewTransition,
unstable_useViewTransitionFrom,
unstable_useViewTransitionState,
useActionData,
useLoaderData,
useLocation,
useNavigate,
useNavigation,
useParams,
useSubmit,
} from "react-router-dom";
import "./index.css";

Expand All @@ -24,19 +28,11 @@ const images = [
"https://remix.run/blog-images/headers/remix-conf.jpg",
];

// FIXME: Remove
function LOG(...args: any[]) {
console.debug(new Date().toISOString(), ...args);
}

const router = createBrowserRouter(
[
{
path: "/",
Component() {
// turn on basic view transitions for the whole app
unstable_useViewTransition();

let navigation = useNavigation();

return (
Expand All @@ -55,6 +51,9 @@ const router = createBrowserRouter(
{
index: true,
Component() {
React.useEffect(() => {
document.title = "Home";
}, []);
return <h1>Home</h1>;
},
},
Expand All @@ -66,14 +65,36 @@ const router = createBrowserRouter(
},
Component() {
let data = useLoaderData() as { message: string };
React.useEffect(() => {
document.title = "Loader";
}, []);
return (
<>
<h1>Test</h1>
<h1>Loader Page</h1>
<p>Loader Data: {data.message}</p>
</>
);
},
},
{
path: "action",
async action() {
await new Promise((r) => setTimeout(r, 1000));
return json({ message: "ACTION DATA" });
},
Component() {
let data = useActionData() as { message: string };
React.useEffect(() => {
document.title = "Action";
}, []);
return (
<>
<h1>Action Page</h1>
<p>Action Data: {data.message}</p>
</>
);
},
},
{
path: "defer",
async loader({ request }) {
Expand All @@ -92,12 +113,16 @@ const router = createBrowserRouter(
critical: string;
lazy: Promise<string>;
};
React.useEffect(() => {
document.title = "Defer";
}, []);
return (
<>
<h1>Defer {data.value}</h1>
<p>Critical Data: {data.critical}</p>
<React.Suspense
fallback={<p>Suspense boundary in the route...</p>}
key={useLocation().key}
>
<Await resolve={data.lazy}>
{(value) => <p>Lazy Data: {value}</p>}
Expand Down Expand Up @@ -126,6 +151,9 @@ const router = createBrowserRouter(
critical: string;
lazy: Promise<string>;
};
React.useEffect(() => {
document.title = "Defer (No Boundary)";
}, []);
return (
<>
<h1>Defer No Boundary {data.value}</h1>
Expand All @@ -142,19 +170,35 @@ const router = createBrowserRouter(
{
path: "images",
Component() {
React.useEffect(() => {
document.title = "Images";
}, []);
return (
<div className="image-list">
<h1>Image List</h1>
<div>
{images.map((src, idx) => (
<NavLink
key={src}
to={`/images/${idx}`}
unstable_viewTransition
>
// Adds 'transitioning' class to the <a> during the transition
<NavLink key={src} to={`/images/${idx}`}>
<p>Image Number {idx}</p>
<img src={src} alt={`Img ${idx}`} />
</NavLink>

// Render prop approach similar to isActive/isPending
// <NavLink
// key={src}
// to={`/images/${idx}`}
// unstable_viewTransition
// >
// {({ isTransitioning }) => (
// <div className={isTransitioning ? "transitioning" : ""}>
// <p>Image Number {idx}</p>
// <img src={src} alt={`Img ${idx}`} />
// </div>
// )}
// </NavLink>

// Manual hook based approach
// <NavImage key={src} src={src} idx={idx} />
))}
</div>
Expand All @@ -166,13 +210,16 @@ const router = createBrowserRouter(
path: "images/:id",
Component() {
let params = useParams();
let isTransitioning = unstable_useViewTransitionFrom("/images");
// Conditionally apply the transition styles only when navigating _to_
// this route. If we apply them statically, clicking back to the home
// page from this view leaves the image stuck during the animation
// since it has no corresponding element to shrink into
let vt = unstable_useViewTransitionState(".");
React.useEffect(() => {
document.title = "Image " + params.id;
}, [params.id]);
return (
<div
className={`image-detail ${
isTransitioning ? "transitioning" : ""
}`}
>
<div className={`image-detail ${vt ? "transitioning" : ""}`}>
<h1>Image Number {params.id}</h1>
<img src={images[Number(params.id)]} alt={`${params.id}`} />
</div>
Expand All @@ -193,19 +240,20 @@ const router = createBrowserRouter(

function NavImage({ src, idx }: { src: string; idx: number }) {
let href = `/images/${idx}`;
let isTransitioning = unstable_useViewTransition(href);
console.log(href, isTransitioning);
let vt = unstable_useViewTransitionState(href);
return (
<NavLink to={href}>
<p style={{ viewTransitionName: isTransitioning ? "image-title" : "" }}>
Image Number {idx}
</p>
<img
src={src}
alt={`Img ${idx}`}
style={{ viewTransitionName: isTransitioning ? "image-expand" : "" }}
/>
</NavLink>
<>
<Link to={href} unstable_viewTransition>
<p style={{ viewTransitionName: vt ? "image-title" : "" }}>
Image Number {idx}
</p>
<img
src={src}
alt={`Img ${idx}`}
style={{ viewTransitionName: vt ? "image-expand" : "" }}
/>
</Link>
</>
);
}

Expand All @@ -223,6 +271,8 @@ ReactDOMClient.createRoot(rootElement).render(
);

function Nav() {
let navigate = useNavigate();
let submit = useSubmit();
let value = Math.round(Math.random() * 100);
return (
<nav>
Expand All @@ -237,51 +287,80 @@ function Nav() {
</ul>
</li>
<li>
<Link to="/loader">Loader Delay</Link>
<Link to="/loader" unstable_viewTransition>
Loader with delay
</Link>{" "}
<button
style={{ display: "inline-block" }}
onClick={() =>
navigate("/loader", { unstable_viewTransition: true })
}
>
via useNavigate
</button>
<ul>
<li>
The /loader route has a 1 second loader delay, and updates the DOM
synchronously upon completion
</li>
</ul>
</li>
<li>
<Form
method="post"
action="/action"
style={{ display: "inline-block" }}
>
<button type="submit" style={{ display: "inline-block" }}>
Action with delay
</button>
</Form>{" "}
<button
style={{ display: "inline-block" }}
onClick={() =>
submit(
{},
{
method: "post",
action: "/action",
unstable_viewTransition: true,
}
)
}
>
via useSubmit
</button>
<ul>
<li>
The /action route has a 1 second action delay, and updates the DOM
synchronously upon completion
</li>
</ul>
</li>
<li>
<Link to="/images">Image Gallery Example</Link>
</li>
<li>
<Link to={`/defer?value=${value}`}>Deferred Data</Link>
<Link to={`/defer?value=${value}`} unstable_viewTransition>
Deferred Data
</Link>
<ul>
<li>
The /defer route has 1s defer call that will Suspend in the
destination route
</li>
<li>
Due to flushSync, it will always re-fallback on navigations - even
without a key on the suspense boundary
The /defer route has 1s defer call that suspends and has it's own
Suspense boundary
</li>
</ul>
</li>
<li>
<Link to="/defer-no-boundary">Deferred Data (without boundary)</Link>
<Link to="/defer-no-boundary" unstable_viewTransition>
Deferred Data (without boundary)
</Link>
<ul>
<li>
The /defer-no-boundary route has a 1s defer call without a
Suspense boundary in the destination route
</li>
<li>❌ This is where things go wrong</li>
<li>
Calling React.flushSync inside React.startTransition breaks the
"freezing" of the UI since suspending UI doesn't seem to know it's
inside of startTransition anymore. This makes some sense since
they're sort of inherently opposites
</li>
<li>
However, we need to call React.flushSync inside
document.startViewTransition for more complex animations
</li>
<li>
So do we just disable startTransition if startViewTransitions have
been enabled? We still get the same error...
The /defer-no-boundary route has a 1s defer that suspends without
a Suspense boundary in the destination route. This relies on
React.startTransition to "freeze" the current UI until the
deferred data resolves
</li>
</ul>
</li>
Expand Down
5 changes: 5 additions & 0 deletions packages/react-router-dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export interface SubmitOptions {
* navigation when using the <ScrollRestoration> component
*/
preventScrollReset?: boolean;

/**
* Enable view transitions on this submission navigation
*/
unstable_viewTransition?: boolean;
}

const supportedFormEncTypes: Set<FormEncType> = new Set([
Expand Down
Loading