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 useSwipeTransition Hook Behind Experimental Flag #32373

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)
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