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
Add interruption handling
  • Loading branch information
brophdawg11 committed Sep 29, 2023
commit dda9693a25171d79c81feeab23ac7878dda7e323
3 changes: 2 additions & 1 deletion examples/view-transitions/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
view-transition-name: image-expand;
}

::view-transition-new(image-expand) {
::view-transition-old(image-expand):not(:only-child),
::view-transition-new(image-expand):not(:only-child) {
animation: none;
mix-blend-mode: normal;
}
Expand Down
101 changes: 60 additions & 41 deletions packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import {
interface ViewTransition {
finished: Promise<void>;
ready: Promise<void>;
updateCallbackDone: Promise<void>;
skipTransition(): void;
}

declare global {
Expand Down Expand Up @@ -155,18 +157,18 @@ export function RouterProvider({
future,
}: RouterProviderProps): React.ReactElement {
let [state, setStateImpl] = React.useState(router.state);
let [pendingState, setPendingState] = React.useState<RouterState | null>(
null
);
let [pendingState, setPendingState] = React.useState<RouterState>();
let [vtContext, setVtContext] = React.useState<ViewTransitionContextObject>({
isTransitioning: false,
});
let [renderDfd, setRenderDfd] = React.useState<Deferred<void>>();
let [transition, setTransition] = React.useState<any>();
let [transition, setTransition] = React.useState<ViewTransition>();
let [interruption, setInterruption] = React.useState<{
state: RouterState;
location: Location;
}>();
let { v7_startTransition } = future || {};

LOG("render", state.location.pathname);

let optInStartTransition = React.useCallback(
(cb: () => void) => {
if (v7_startTransition) {
Expand All @@ -180,26 +182,37 @@ export function RouterProvider({

let setState = React.useCallback<RouterSubscriber>(
(newState: RouterState, { viewTransitionOpts }) => {
LOG("subscriber callback", newState.location.pathname);
// Mid-navigation state update, or startViewTransition isn't available
if (
viewTransitionOpts &&
typeof document.startViewTransition === "function"
!viewTransitionOpts ||
typeof document.startViewTransition !== "function"
) {
// If this is a completed navigation update with opted-in view transitions,
// kick off a transition via the ViewTransitionContext
LOG("setVtContext({ isTransitioning: true })");
setPendingState(newState);
setVtContext({
isTransitioning: true,
nextLocation: viewTransitionOpts.nextLocation,
});
} else {
// Otherwise just setState and be done with it
optInStartTransition(() => setStateImpl(newState));
return;
}

// Interrupting an in-progress transition, cancel and let everything flush
// out, and then kick off a new transition from the interruption state
if (transition && renderDfd) {
LOG("❌ navigation interrupted");
renderDfd.resolve();
transition.skipTransition();
setInterruption({
state: newState,
location: viewTransitionOpts.nextLocation,
});
return;
}

// Completed navigation update with opted-in view transitions, let 'er rip
LOG("setting pendingState and vtContext.isTransitioning=true");
setPendingState(newState);
setVtContext({
isTransitioning: true,
nextLocation: viewTransitionOpts.nextLocation,
});
},
[optInStartTransition]
[optInStartTransition, transition, renderDfd]
);

// Need to use a layout effect here so we are subscribed early enough to
Expand All @@ -209,7 +222,6 @@ export function RouterProvider({
// When we start a view transition, create a Deferred we can use for the
// eventual "completed" render
React.useEffect(() => {
LOG("create deferred effect");
if (vtContext.isTransitioning) {
LOG("creating deferred");
setRenderDfd(new Deferred<void>());
Expand All @@ -220,47 +232,54 @@ export function RouterProvider({
// DOM and then wait on the Deferred to resolve (indicating the DOM update has
// happened)
React.useEffect(() => {
LOG("startViewTransition effect");
if (renderDfd && pendingState) {
let promise = renderDfd.promise;
let renderPromise = renderDfd.promise;
let newState = pendingState;
LOG("transition = document.startViewTransition()");
let transition = document.startViewTransition(async () => {
LOG("React.startTransition(() => setTransition()/setState())");
optInStartTransition(() => {
setTransition(transition);
setStateImpl(newState);
});
await promise;
LOG("done with view transition");
LOG("optInStartTransition(() => setStateImpl(newState))");
optInStartTransition(() => setStateImpl(newState));
await renderPromise;
LOG("view transition dom update complete, starting animation");
});

// TODO: Handle interrupted transitions with transition.skipTransition()
transition.finished.finally(() => {
LOG("cleaning up renderDfd/transition/pendingState/vtContext");
setRenderDfd(undefined);
setTransition(undefined);
setPendingState(undefined);
setVtContext({ isTransitioning: false });
});
setTransition(transition);
}
}, [optInStartTransition, pendingState, renderDfd]);

// When the new location finally renders and is committed to the DOM, this
// effect will run to resolve the transition
React.useEffect(() => {
LOG("resolve deferred effect");
if (
renderDfd &&
pendingState &&
state.location.key === pendingState.location.key
) {
transition.finished.finally(() => {
// TODO: Is this enough to not need the view transition callback functions?
LOG("setVtContext({ isTransitioning: false })");
setVtContext({ isTransitioning: false });
});
LOG("deferred.resolve()");
renderDfd.resolve();
setRenderDfd(undefined);
setTransition(undefined);
setPendingState(null);
}
}, [renderDfd, transition, state.location, pendingState]);

// If we get interrupted with a new navigation during a transition, we skip
// the active transition, let it cleanup, then kick it off again here
React.useEffect(() => {
if (!vtContext.isTransitioning && interruption) {
LOG("starting view transition after interruption");
setPendingState(interruption.state);
setVtContext({
isTransitioning: true,
nextLocation: interruption.location,
});
setInterruption(undefined);
}
}, [vtContext.isTransitioning, interruption]);

let navigator = React.useMemo((): Navigator => {
return {
createHref: router.createHref,
Expand Down
33 changes: 22 additions & 11 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,10 @@ export function createRouter(init: RouterInit): Router {
let pendingViewTransitionEnabled = false;

// Store applied view transitions so we can apply them on POP
let appliedViewTransitions: Set<string> = new Set<string>();
let appliedViewTransitions: Map<string, Set<string>> = new Map<
string,
Set<string>
>();

// We use this to avoid touching history in completeNavigation if a
// revalidation is entirely uninterrupted
Expand Down Expand Up @@ -1068,24 +1071,32 @@ export function createRouter(init: RouterInit): Router {

let viewTransitionOpts: ViewTransitionOpts | undefined;

// For POP navigations, we enable transitions if iff they were enabled on
// the original navigation
// TODO: Store off appliedViewTransitions in sessionStorage to be restored

// On POP, enable transitions if they were enabled on the original navigation
if (pendingAction === HistoryAction.Pop) {
if (appliedViewTransitions.has(location.key)) {
// POP backward - reversing a navigation that enabled transitions
// Forward takes precedence so they behave like the original navigation
let priorNavs = appliedViewTransitions.get(state.location.key);
if (priorNavs && priorNavs.has(location.key)) {
viewTransitionOpts = {
nextLocation: state.location,
nextLocation: location,
};
} else if (appliedViewTransitions.has(state.location.key)) {
// POP forward - replaying a navigation that enabled transitions
} else if (appliedViewTransitions.has(location.key)) {
// If we don't have a previous forward nav, assume we're popping back to
// the new location and enable if that location previously enabled
viewTransitionOpts = {
nextLocation: location,
nextLocation: state.location,
};
}
} else if (pendingViewTransitionEnabled) {
// Store the applied transition on PUSH/REPLACE
let transitionKey = state.location.key;
appliedViewTransitions.add(transitionKey);
let toKeys = appliedViewTransitions.get(state.location.key);
if (toKeys) {
toKeys.add(location.key);
} else {
toKeys = new Set<string>([location.key]);
appliedViewTransitions.set(state.location.key, toKeys);
}
viewTransitionOpts = {
nextLocation: location,
};
Expand Down