@samitouri / QOS-React-2 / commits / dcf83f7c2d

Disable ScrollTimeline in Safari (#33499)

Stacked on #33501. This disables the use of ScrollTimeline when detected in Safari in the recommended SwipeRecognizer approach. I'm instead using a polyfill using touch events on iOS. Safari seems set to [release ScrollTimeline soon](https://webkit.org/blog/16993/news-from-wwdc25-web-technology-coming-this-fall-in-safari-26-beta/). Unfortunately it's not really what you'd expect. First of all, [it's not running in sync with the scroll](https://bugs.webkit.org/show_bug.cgi?id=288402) which is kind of its main point. Instead, it is running at 60fps and out of sync with the scroll just like JS. In fact, it is worse than JS because with JS you can at least spawn CSS animations that run at 120fps. So our polyfill can respond to touches at 60fps while gesturing and then run at 120fps upon release. That's better than with ScrollTimeline. Second, [there's a bug which interrupts scrolling if you start a ViewTransition](https://bugs.webkit.org/show_bug.cgi?id=288795) when the element is being removed as part of that. The element can still respond to touches so in a polyfill this isn't an issue. But it essentially makes it useless to use ScrollTimeline with swipe-away gestures. So we're better off in every scenario by not using it. The UA detection is a bit unfortunate. Not sure if there's something more specific but we also had to do a UA detection for Chrome for View Transitions. Those are the only two we have in all of React. ![safarimeme](https://github.com/user-attachments/assets/d4ca9eba-489e-4ade-b462-2ffeee3a470c)

Sebastian Markbåge committed Jul 2, 2025 at 17:01 UTC dcf83f7c2db2de64ec370df73294cedb658bd896
1 file changed +77 -11
fixtures/view-transition/src/components/SwipeRecognizer.js
+77 -11
@@ -6,6 +6,14 @@ import React, {
6 } from 'react';
7
8 import ScrollTimelinePolyfill from 'animation-timelines/scroll-timeline';
9 +import TouchPanTimeline from 'animation-timelines/touch-pan-timeline';
10 +
11 +const ua = typeof navigator === 'undefined' ? '' : navigator.userAgent;
12 +const isSafariMobile =
13 + ua.indexOf('Safari') !== -1 &&
14 + (ua.indexOf('iPhone') !== -1 ||
15 + ua.indexOf('iPad') !== -1 ||
16 + ua.indexOf('iPod') !== -1);
17
18 // Example of a Component that can recognize swipe gestures using a ScrollTimeline
19 // without scrolling its own content. Allowing it to be used as an inert gesture
@@ -23,13 +31,61 @@ export default function SwipeRecognizer({
31
32 const scrollRef = useRef(null);
33 const activeGesture = useRef(null);
34 + const touchTimeline = useRef(null);
35 +
36 + function onTouchStart(event) {
37 + if (!isSafariMobile && typeof ScrollTimeline === 'function') {
38 + // If not Safari and native ScrollTimeline is supported, then we use that.
39 + return;
40 + }
41 + if (touchTimeline.current) {
42 + // We can catch the gesture before it settles.
43 + return;
44 + }
45 + const scrollElement = scrollRef.current;
46 + const bounds =
47 + axis === 'x' ? scrollElement.clientWidth : scrollElement.clientHeight;
48 + const range =
49 + direction === 'left' || direction === 'up' ? [bounds, 0] : [0, -bounds];
50 + const timeline = new TouchPanTimeline({
51 + touch: event,
52 + source: scrollElement,
53 + axis: axis,
54 + range: range,
55 + snap: range,
56 + });
57 + touchTimeline.current = timeline;
58 + timeline.settled.then(() => {
59 + if (touchTimeline.current !== timeline) {
60 + return;
61 + }
62 + touchTimeline.current = null;
63 + const changed =
64 + direction === 'left' || direction === 'up'
65 + ? timeline.currentTime < 50
66 + : timeline.currentTime > 50;
67 + onGestureEnd(changed);
68 + });
69 + }
70 +
71 + function onTouchEnd() {
72 + if (activeGesture.current === null) {
73 + // If we didn't start a gesture before we release, we can release our
74 + // timeline.
75 + touchTimeline.current = null;
76 + }
77 + }
78 +
79 function onScroll() {
80 if (activeGesture.current !== null) {
81 return;
82 }
83
84 let scrollTimeline;
32 - if (typeof ScrollTimeline === 'function') {
85 + if (touchTimeline.current) {
86 + // We're in a polyfilled touch gesture. Let's use that timeline instead.
87 + scrollTimeline = touchTimeline.current;
88 + } else if (typeof ScrollTimeline === 'function') {
89 // eslint-disable-next-line no-undef
90 scrollTimeline = new ScrollTimeline({
91 source: scrollRef.current,
@@ -57,7 +113,23 @@ export default function SwipeRecognizer({
113 }
114 );
115 }
116 + function onGestureEnd(changed) {
117 + // Reset scroll
118 + if (changed) {
119 + // Trigger side-effects
120 + startTransition(action);
121 + }
122 + if (activeGesture.current !== null) {
123 + const cancelGesture = activeGesture.current;
124 + activeGesture.current = null;
125 + cancelGesture();
126 + }
127 + }
128 function onScrollEnd() {
129 + if (touchTimeline.current) {
130 + // We have a touch gesture controlling the swipe.
131 + return;
132 + }
133 let changed;
134 const scrollElement = scrollRef.current;
135 if (axis === 'x') {
@@ -75,16 +147,7 @@ export default function SwipeRecognizer({
147 ? scrollElement.scrollTop < halfway
148 : scrollElement.scrollTop > halfway;
149 }
78 - // Reset scroll
79 - if (changed) {
80 - // Trigger side-effects
81 - startTransition(action);
82 - }
83 - if (activeGesture.current !== null) {
84 - const cancelGesture = activeGesture.current;
85 - activeGesture.current = null;
86 - cancelGesture();
87 - }
150 + onGestureEnd(changed);
151 }
152
153 useEffect(() => {
@@ -176,6 +239,9 @@ export default function SwipeRecognizer({
239 return (
240 <div
241 style={scrollStyle}
242 + onTouchStart={onTouchStart}
243 + onTouchEnd={onTouchEnd}
244 + onTouchCancel={onTouchEnd}
245 onScroll={onScroll}
246 onScrollEnd={onScrollEnd}
247 ref={scrollRef}>