Support onGestureEnter/Exit/Share/Update events (#35556)

This is like the onEnter/Exit/Share/Update events but for gestures. It
allows manually controlling the animation using the passed timeline.
This commit is contained in:
Sebastian Markbåge
2026-01-19 19:26:09 -05:00
committed by GitHub
parent 41b3e9a670
commit 4bcf67e746
5 changed files with 154 additions and 6 deletions

View File

@@ -86,6 +86,48 @@ export default function Page({url, navigate}) {
viewTransition.new.animate(keyframes, 250);
}
function onGestureTransition(
timeline,
{rangeStart, rangeEnd},
viewTransition,
types
) {
const keyframes = [
{rotate: '0deg', transformOrigin: '30px 8px'},
{rotate: '360deg', transformOrigin: '30px 8px'},
];
const reverse = rangeStart > rangeEnd;
if (timeline instanceof AnimationTimeline) {
// Native Timeline
const options = {
timeline: timeline,
direction: reverse ? 'normal' : 'reverse',
rangeStart: (reverse ? rangeEnd : rangeStart) + '%',
rangeEnd: (reverse ? rangeStart : rangeEnd) + '%',
};
viewTransition.old.animate(keyframes, options);
viewTransition.new.animate(keyframes, options);
} else {
// Custom Timeline
const options = {
direction: reverse ? 'normal' : 'reverse',
// We set the delay and duration to represent the span of the range.
delay: reverse ? rangeEnd : rangeStart,
duration: reverse ? rangeStart - rangeEnd : rangeEnd - rangeStart,
};
const animation1 = viewTransition.old.animate(keyframes, options);
const animation2 = viewTransition.new.animate(keyframes, options);
// Let the custom timeline take control of driving the animations.
const cleanup1 = timeline.animate(animation1);
const cleanup2 = timeline.animate(animation2);
// TODO: Support returning a clean up function from ViewTransition events.
// return () => {
// cleanup1();
// cleanup2();
// };
}
}
function swipeAction() {
navigate(show ? '/?a' : '/?b');
}
@@ -131,7 +173,10 @@ export default function Page({url, navigate}) {
);
const exclamation = (
<ViewTransition name="exclamation" onShare={onTransition}>
<ViewTransition
name="exclamation"
onShare={onTransition}
onGestureShare={onGestureTransition}>
<span>
<div>!</div>
</span>