Skip to content

Add useSwipeTransition Hook Behind Experimental Flag #32373

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

Merged
merged 9 commits into from
Feb 13, 2025

Conversation

sebmarkbage
Copy link
Collaborator

This Hook will be used to drive a View Transition based on a gesture.

const [value, startGesture] = useSwipeTransition(prev, current, next);

The enableSwipeTransition flag will depend on enableViewTransition flag but we may decide to ship them independently. This PR doesn't do anything interesting yet. There will be a lot more PRs to build out the actual functionality. This is just wiring up the plumbing for the new Hook.

This first PR is mainly concerned with how the whole starts (and stops). The core API is the startGesture function (although there will be other conveniences added in the future). You can call this to start a gesture with a source provider. You can call this multiple times in one event to batch multiple Hooks listening to the same provider. However, each render can only handle one source provider at a time and so it does one render per scheduled gesture provider.

This uses a separate GestureLane to drive gesture renders by marking the Hook as having an update on that lane. Then schedule a render. These renders should be blocking and in the same microtask as the startGesture to ensure it can block the paint. So it's similar to sync.

It may not be possible to finish it synchronously e.g. if something suspends. If so, it just tries again later when it can like any other render. This can also happen because it also may not be possible to drive more than one gesture at a time like if we're limited to one View Transition per document. So right now you can only run one gesture at a time in practice.

These renders never commit. This means that we can't clear the GestureLane the normal way. Instead, we have to clear only the root's pendingLanes if we don't have any new renders scheduled. Then wait until something else updates the Fiber after all gestures on it have stopped before it really clears.

We might ship this independently from the other View Transitions.
We'll need a separate lane to render these. We're out of bits so we steal
a bit from one of the TransitionLanes.
This is batched to allow for the same gesture provider to start on multiple
useSwipeTransition Hooks at once.
This way we know when processing the hook whether this gesture is currently
active for this specific hook.
This unifies the renders so that scheduled gestures renders in the same
schedule as other renders. We just try to finish them in a microtask before
any other scheduled tasks has a chance to render.
@react-sizebot
Copy link

react-sizebot commented Feb 13, 2025

Comparing: 5a78dd7...a6ef876

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.02% 515.62 kB 515.71 kB +0.01% 92.08 kB 92.09 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.64% 558.66 kB 562.25 kB +0.78% 99.31 kB 100.08 kB
facebook-www/ReactDOM-prod.classic.js +0.01% 636.61 kB 636.70 kB +0.02% 112.06 kB 112.08 kB
facebook-www/ReactDOM-prod.modern.js +0.01% 626.93 kB 627.02 kB +0.02% 110.48 kB 110.49 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-debug-tools/cjs/react-debug-tools.development.js +1.89% 31.74 kB 32.34 kB +1.15% 5.73 kB 5.79 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.development.js +1.89% 31.74 kB 32.34 kB +1.15% 5.73 kB 5.79 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.development.js +1.89% 31.74 kB 32.34 kB +1.15% 5.73 kB 5.79 kB
oss-experimental/react-debug-tools/cjs/react-debug-tools.production.js +1.85% 28.29 kB 28.81 kB +1.20% 5.60 kB 5.66 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.production.js +1.85% 28.29 kB 28.81 kB +1.20% 5.60 kB 5.66 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.production.js +1.85% 28.29 kB 28.81 kB +1.20% 5.60 kB 5.66 kB
oss-experimental/react-art/cjs/react-art.production.js +1.11% 321.34 kB 324.89 kB +1.40% 54.80 kB 55.57 kB
oss-experimental/react-art/cjs/react-art.development.js +0.89% 613.22 kB 618.69 kB +0.86% 97.80 kB 98.64 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.js +0.88% 428.01 kB 431.79 kB +1.15% 68.95 kB 69.74 kB
oss-experimental/react/cjs/react.production.js +0.84% 18.56 kB 18.71 kB +0.64% 4.82 kB 4.85 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.js +0.80% 480.92 kB 484.75 kB +1.12% 76.73 kB 77.59 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js +0.77% 715.77 kB 721.30 kB +0.81% 112.97 kB 113.88 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.64% 558.66 kB 562.25 kB +0.78% 99.31 kB 100.08 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.js +0.63% 573.39 kB 576.98 kB +0.76% 102.86 kB 103.64 kB
oss-experimental/react-dom/cjs/react-dom-profiling.profiling.js +0.59% 614.17 kB 617.80 kB +0.72% 108.00 kB 108.78 kB
oss-experimental/react-dom/cjs/react-dom-client.development.js +0.54% 1,030.81 kB 1,036.34 kB +0.52% 172.63 kB 173.53 kB
oss-experimental/react-dom/cjs/react-dom-profiling.development.js +0.53% 1,047.21 kB 1,052.74 kB +0.51% 175.48 kB 176.38 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js +0.53% 1,047.73 kB 1,053.26 kB +0.51% 176.37 kB 177.27 kB
oss-experimental/react/cjs/react.development.js +0.35% 46.85 kB 47.01 kB +0.24% 10.64 kB 10.67 kB
oss-experimental/react-server/cjs/react-server.production.js +0.22% 129.20 kB 129.49 kB +0.19% 22.46 kB 22.50 kB

Generated by 🚫 dangerJS against 8b8e2ef

@sebmarkbage sebmarkbage merged commit a53da6a into facebook:main Feb 13, 2025
16 of 18 checks passed
github-actions bot pushed a commit that referenced this pull request Feb 13, 2025
This Hook will be used to drive a View Transition based on a gesture.

```js
const [value, startGesture] = useSwipeTransition(prev, current, next);
```

The `enableSwipeTransition` flag will depend on `enableViewTransition`
flag but we may decide to ship them independently. This PR doesn't do
anything interesting yet. There will be a lot more PRs to build out the
actual functionality. This is just wiring up the plumbing for the new
Hook.

This first PR is mainly concerned with how the whole starts (and stops).
The core API is the `startGesture` function (although there will be
other conveniences added in the future). You can call this to start a
gesture with a source provider. You can call this multiple times in one
event to batch multiple Hooks listening to the same provider. However,
each render can only handle one source provider at a time and so it does
one render per scheduled gesture provider.

This uses a separate `GestureLane` to drive gesture renders by marking
the Hook as having an update on that lane. Then schedule a render. These
renders should be blocking and in the same microtask as the
`startGesture` to ensure it can block the paint. So it's similar to
sync.

It may not be possible to finish it synchronously e.g. if something
suspends. If so, it just tries again later when it can like any other
render. This can also happen because it also may not be possible to
drive more than one gesture at a time like if we're limited to one View
Transition per document. So right now you can only run one gesture at a
time in practice.

These renders never commit. This means that we can't clear the
`GestureLane` the normal way. Instead, we have to clear only the root's
`pendingLanes` if we don't have any new renders scheduled. Then wait
until something else updates the Fiber after all gestures on it have
stopped before it really clears.

DiffTrain build for [a53da6a](a53da6a)
github-actions bot pushed a commit that referenced this pull request Feb 13, 2025
This Hook will be used to drive a View Transition based on a gesture.

```js
const [value, startGesture] = useSwipeTransition(prev, current, next);
```

The `enableSwipeTransition` flag will depend on `enableViewTransition`
flag but we may decide to ship them independently. This PR doesn't do
anything interesting yet. There will be a lot more PRs to build out the
actual functionality. This is just wiring up the plumbing for the new
Hook.

This first PR is mainly concerned with how the whole starts (and stops).
The core API is the `startGesture` function (although there will be
other conveniences added in the future). You can call this to start a
gesture with a source provider. You can call this multiple times in one
event to batch multiple Hooks listening to the same provider. However,
each render can only handle one source provider at a time and so it does
one render per scheduled gesture provider.

This uses a separate `GestureLane` to drive gesture renders by marking
the Hook as having an update on that lane. Then schedule a render. These
renders should be blocking and in the same microtask as the
`startGesture` to ensure it can block the paint. So it's similar to
sync.

It may not be possible to finish it synchronously e.g. if something
suspends. If so, it just tries again later when it can like any other
render. This can also happen because it also may not be possible to
drive more than one gesture at a time like if we're limited to one View
Transition per document. So right now you can only run one gesture at a
time in practice.

These renders never commit. This means that we can't clear the
`GestureLane` the normal way. Instead, we have to clear only the root's
`pendingLanes` if we don't have any new renders scheduled. Then wait
until something else updates the Fiber after all gestures on it have
stopped before it really clears.

DiffTrain build for [a53da6a](a53da6a)
sebmarkbage added a commit that referenced this pull request Apr 1, 2025
Stacked on #32783. This will replace [the `useSwipeTransition`
API](#32373).

Instead, of a special Hook, you can make updates to `useOptimistic`
Hooks within the `startGestureTransition` scope.

```
import {unstable_startGestureTransition as startGestureTransition} from 'react';

const cancel = startGestureTransition(timeline, () => {
  setOptimistic(...);
}, options);
```

There are some downsides to this like you can't define two directions as
once and there's no "standard" direction protocol. It's instead up to
libraries to come up with their own conventions (although we can suggest
some).

The convention is still that a gesture recognizer has two props `action`
and `gesture`. The `gesture` prop is a Gesture concept which now behaves
more like an Action but 1) it can't be async 2) it shouldn't have
side-effects. For example you can't call `setState()` in it except on
`useOptimistic` since those can be reverted if needed. The `action` is
invoked with whatever side-effects you want after the gesture fulfills.

This is isomorphic and not associated with a specific renderer nor root
so it's a bit more complicated.

To implement this I unify with the `ReactSharedInternal.T` property to
contain a regular Transition or a Gesture Transition (the `gesture`
field). The benefit of this unification means that every time we
override this based on some scope like entering `flushSync` we also
override the `startGestureTransition` scope. We just have to be careful
when we read it to check the `gesture` field to know which one it is.
(E.g. I error for setState / requestFormReset.)

The other thing that's unique is the `cancel` return value to know when
to stop the gesture. That cancellation is no longer associated with any
particular Hook. It's more associated with the scope of the
`startGestureTransition`. Since the schedule of whether a particular
gesture has rendered or committed is associated with a root, we need to
somehow associate any scheduled gestures with a root.

We could track which roots we update inside the scope but instead, I
went with a model where I check all the roots and see if there's a
scheduled gesture matching the timeline. This means that you could
"retain" a gesture across roots. Meaning this wouldn't cancel until both
are cancelled:

```
const cancelA = startGestureTransition(timeline, () => {
  setOptimisticOnRootA(...);
}, options);

const cancelB = startGestureTransition(timeline, () => {
  setOptimisticOnRootB(...);
}, options);
```

It's more like it's a global transition than associated with the roots
that were updated.

Optimistic updates mostly just work but I now associate them with a
specific "ScheduledGesture" instance since we can only render one at a
time and so if it's not the current one, we leave it for later.

Clean up of optimistic updates is now lazy rather than when we cancel.
Allowing the cancel closure not to have to be associated with each
particular update.
github-actions bot pushed a commit that referenced this pull request Apr 1, 2025
Stacked on #32783. This will replace [the `useSwipeTransition`
API](#32373).

Instead, of a special Hook, you can make updates to `useOptimistic`
Hooks within the `startGestureTransition` scope.

```
import {unstable_startGestureTransition as startGestureTransition} from 'react';

const cancel = startGestureTransition(timeline, () => {
  setOptimistic(...);
}, options);
```

There are some downsides to this like you can't define two directions as
once and there's no "standard" direction protocol. It's instead up to
libraries to come up with their own conventions (although we can suggest
some).

The convention is still that a gesture recognizer has two props `action`
and `gesture`. The `gesture` prop is a Gesture concept which now behaves
more like an Action but 1) it can't be async 2) it shouldn't have
side-effects. For example you can't call `setState()` in it except on
`useOptimistic` since those can be reverted if needed. The `action` is
invoked with whatever side-effects you want after the gesture fulfills.

This is isomorphic and not associated with a specific renderer nor root
so it's a bit more complicated.

To implement this I unify with the `ReactSharedInternal.T` property to
contain a regular Transition or a Gesture Transition (the `gesture`
field). The benefit of this unification means that every time we
override this based on some scope like entering `flushSync` we also
override the `startGestureTransition` scope. We just have to be careful
when we read it to check the `gesture` field to know which one it is.
(E.g. I error for setState / requestFormReset.)

The other thing that's unique is the `cancel` return value to know when
to stop the gesture. That cancellation is no longer associated with any
particular Hook. It's more associated with the scope of the
`startGestureTransition`. Since the schedule of whether a particular
gesture has rendered or committed is associated with a root, we need to
somehow associate any scheduled gestures with a root.

We could track which roots we update inside the scope but instead, I
went with a model where I check all the roots and see if there's a
scheduled gesture matching the timeline. This means that you could
"retain" a gesture across roots. Meaning this wouldn't cancel until both
are cancelled:

```
const cancelA = startGestureTransition(timeline, () => {
  setOptimisticOnRootA(...);
}, options);

const cancelB = startGestureTransition(timeline, () => {
  setOptimisticOnRootB(...);
}, options);
```

It's more like it's a global transition than associated with the roots
that were updated.

Optimistic updates mostly just work but I now associate them with a
specific "ScheduledGesture" instance since we can only render one at a
time and so if it's not the current one, we leave it for later.

Clean up of optimistic updates is now lazy rather than when we cancel.
Allowing the cancel closure not to have to be associated with each
particular update.

DiffTrain build for [b286430](b286430)
github-actions bot pushed a commit that referenced this pull request Apr 1, 2025
Stacked on #32783. This will replace [the `useSwipeTransition`
API](#32373).

Instead, of a special Hook, you can make updates to `useOptimistic`
Hooks within the `startGestureTransition` scope.

```
import {unstable_startGestureTransition as startGestureTransition} from 'react';

const cancel = startGestureTransition(timeline, () => {
  setOptimistic(...);
}, options);
```

There are some downsides to this like you can't define two directions as
once and there's no "standard" direction protocol. It's instead up to
libraries to come up with their own conventions (although we can suggest
some).

The convention is still that a gesture recognizer has two props `action`
and `gesture`. The `gesture` prop is a Gesture concept which now behaves
more like an Action but 1) it can't be async 2) it shouldn't have
side-effects. For example you can't call `setState()` in it except on
`useOptimistic` since those can be reverted if needed. The `action` is
invoked with whatever side-effects you want after the gesture fulfills.

This is isomorphic and not associated with a specific renderer nor root
so it's a bit more complicated.

To implement this I unify with the `ReactSharedInternal.T` property to
contain a regular Transition or a Gesture Transition (the `gesture`
field). The benefit of this unification means that every time we
override this based on some scope like entering `flushSync` we also
override the `startGestureTransition` scope. We just have to be careful
when we read it to check the `gesture` field to know which one it is.
(E.g. I error for setState / requestFormReset.)

The other thing that's unique is the `cancel` return value to know when
to stop the gesture. That cancellation is no longer associated with any
particular Hook. It's more associated with the scope of the
`startGestureTransition`. Since the schedule of whether a particular
gesture has rendered or committed is associated with a root, we need to
somehow associate any scheduled gestures with a root.

We could track which roots we update inside the scope but instead, I
went with a model where I check all the roots and see if there's a
scheduled gesture matching the timeline. This means that you could
"retain" a gesture across roots. Meaning this wouldn't cancel until both
are cancelled:

```
const cancelA = startGestureTransition(timeline, () => {
  setOptimisticOnRootA(...);
}, options);

const cancelB = startGestureTransition(timeline, () => {
  setOptimisticOnRootB(...);
}, options);
```

It's more like it's a global transition than associated with the roots
that were updated.

Optimistic updates mostly just work but I now associate them with a
specific "ScheduledGesture" instance since we can only render one at a
time and so if it's not the current one, we leave it for later.

Clean up of optimistic updates is now lazy rather than when we cancel.
Allowing the cancel closure not to have to be associated with each
particular update.

DiffTrain build for [b286430](b286430)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants