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
Updates
  • Loading branch information
brophdawg11 committed Sep 28, 2023
commit 27f65c1da3b05d792b926a9645996d157d6a644c
19 changes: 19 additions & 0 deletions examples/view-transitions/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const router = createBrowserRouter(
<p>Image Number {idx}</p>
<img src={src} alt={`Img ${idx}`} />
</NavLink>
// <NavImage key={src} src={src} idx={idx} />
))}
</div>
</div>
Expand Down Expand Up @@ -190,6 +191,24 @@ const router = createBrowserRouter(
}
);

function NavImage({ src, idx }: { src: string; idx: number }) {
let href = `/images/${idx}`;
let isTransitioning = unstable_useViewTransition(href);
console.log(href, isTransitioning);
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>
);
}

const rootElement = document.getElementById("root") as HTMLElement;
ReactDOMClient.createRoot(rootElement).render(
<React.StrictMode>
Expand Down
88 changes: 59 additions & 29 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
UNSAFE_warning as warning,
matchRoutes,
matchPath,
parsePath,
} from "@remix-run/router";

import type {
Expand Down Expand Up @@ -647,7 +648,7 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
let routerState = React.useContext(DataRouterStateContext);
let { navigator } = React.useContext(NavigationContext);
let isTransitioning = useViewTransition(
unstable_viewTransition ? path : undefined
unstable_viewTransition ? path : false
);

let toPathname = navigator.encodeLocation
Expand Down Expand Up @@ -1518,6 +1519,17 @@ let viewTransitionId = 0;
* If you return false from your function, then this specific hook will not enable
* view transitions (but other hook invocations still may)
*
* @example
* useViewTransitions(() => {
* // before dom update
* return (transition) => {
* // after dom update
* transition.finished.finally(() => {
* // after animation
* })
* };
* })
*
* @param viewTransition Optional function to determine
*/
function useViewTransitions(
Expand All @@ -1538,16 +1550,24 @@ function useViewTransitions(
}

/**
* Localized version of useViewTransitions to enable view transitions for a
* Localized version of useViewTransition to enable view transitions for a
* specific destination href. Returns an isTransitioning value that you can
* leverage to render CSS classes or viewTransitionName styles onto your elements
*
* @param href The destination href you wish to enable view transitions for
* @returns
*/
function useViewTransition(to?: To) {
function useViewTransitionImpl(isFrom: boolean, location?: To | boolean) {
let vtContext = React.useContext(ViewTransitionContext);
let href = to == null ? null : typeof to === "string" ? to : createPath(to);
let href =
typeof location === "boolean"
? location
: location == null
? null
: typeof location === "string"
? location
: // TODO: Handle relative paths here?
location.pathname || "";

// This function has to use point-in-time passed-in values if we want it to
// work on POP navigations because we store off this function to be executed
Expand All @@ -1559,44 +1579,54 @@ function useViewTransition(to?: To) {
// prior time? Router could store key tuples that opted into transitions on
// PUSH/REPLACE and then automatically opt-in on POP for the reverse?
let shouldTransition = React.useCallback<ViewTransitionFunction>(
({ nextLocation }) => {
if (href == null) {
({ currentLocation, nextLocation }) => {
if (href === false) {
return href;
} else if (typeof href === "string") {
let { pathname } = isFrom ? currentLocation : nextLocation;
return matchPath(href, pathname) != null;
} else {
// TODO: Add subtree matching
return true;
} else {
return matchPath(href, nextLocation.pathname) != null;
}
},
[href]
[isFrom, href]
);

useViewTransitions(shouldTransition);

return vtContext.isTransitioning && shouldTransition(vtContext) === true;
}

export { useViewTransition as unstable_useViewTransition };

function useViewTransitionFrom(from?: To) {
let vtContext = React.useContext(ViewTransitionContext);
let href =
from == null ? null : typeof from === "string" ? from : createPath(from);

let shouldTransition = React.useCallback<ViewTransitionFunction>(
({ currentLocation }) => {
if (href == null) {
// TODO: Add subtree matching
return true;
} else {
return matchPath(href, currentLocation.pathname) != null;
}
},
[href]
);
/**
* Enable view transitions for a specific destination href. Returns an
* isTransitioning value that you can leverage to render CSS classes or
* viewTransitionName styles onto your elements.
*
* @param [to=false] The destination href you wish to enable view transitions
* for, or true/false to enable.disable for the route sub-tree
* @returns
*/
function useViewTransition(to?: To | boolean) {
return useViewTransitionImpl(false, to);
}

useViewTransitions(shouldTransition);
export { useViewTransition as unstable_useViewTransition };

return vtContext.isTransitioning && shouldTransition(vtContext) === true;
/**
* Enable view transitions from a specific source href. Returns an
* isTransitioning value that you can leverage to render CSS classes or
* viewTransitionName styles onto your elements.
*
* @param [from=false] The source href you wish to enable view transitions
* for, or true/false to enable.disable for the route sub-tree
* @returns
*/
function useViewTransitionFrom(from?: To | boolean) {
// TODO: Needed at the moment to be able to conditionally turn on the image
// detail transition only when coming from /images so iut doesn't take control
// when navigating /images/:id -> /home, etc.
return useViewTransitionImpl(true, from);
}

export { useViewTransitionFrom as unstable_useViewTransitionFrom };
Expand Down