Skip to content

Commit a24876c

Browse files
committed
Add ScrollTimlinePolyfill
This is just a basic one. We'll need a better one.
1 parent d010de3 commit a24876c

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Example of a polyfill of ScrollTimeline that implements React's CustomTimeline protocol.
2+
3+
export default class ScrollTimelinePolyfill {
4+
constructor({source, axis}) {
5+
this.source = source;
6+
this.axis = axis;
7+
}
8+
get currentTime() {
9+
const source = this.source;
10+
const axis = this.axis;
11+
if (axis === 'block' || axis === 'x') {
12+
return (
13+
(100 * source.scrollLeft) / (source.scrollWidth - source.clientWidth)
14+
);
15+
} else {
16+
return (
17+
(100 * source.scrollTop) / (source.scrollHeight - source.clientHeight)
18+
);
19+
}
20+
}
21+
animate(animation) {
22+
animation.playbackRate = 0;
23+
const source = this.source;
24+
const update = () => {
25+
animation.currentTime = this.currentTime;
26+
};
27+
update();
28+
source.addEventListener('scroll', update);
29+
return () => {
30+
source.removeEventListener('scroll', update);
31+
};
32+
}
33+
}

fixtures/view-transition/src/components/SwipeRecognizer.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import React, {
55
unstable_startGestureTransition as startGestureTransition,
66
} from 'react';
77

8+
import ScrollTimelinePolyfill from './ScrollTimelinePolyfill';
9+
810
// Example of a Component that can recognize swipe gestures using a ScrollTimeline
911
// without scrolling its own content. Allowing it to be used as an inert gesture
1012
// recognizer to drive a View Transition.
@@ -26,20 +28,19 @@ export default function SwipeRecognizer({
2628
return;
2729
}
2830

29-
const ua = navigator.userAgent;
30-
const supportsScrollTimeline =
31-
typeof ScrollTimeline === 'function' &&
32-
(ua.indexOf('Safari') === -1 ||
33-
(ua.indexOf('iPhone') === -1 && ua.indexOf('iPad') === -1));
34-
if (!supportsScrollTimeline) {
35-
// TODO: Polyfill
36-
return;
31+
let scrollTimeline;
32+
if (typeof ScrollTimeline === 'function') {
33+
// eslint-disable-next-line no-undef
34+
scrollTimeline = new ScrollTimeline({
35+
source: scrollRef.current,
36+
axis: axis,
37+
});
38+
} else {
39+
scrollTimeline = new ScrollTimelinePolyfill({
40+
source: scrollRef.current,
41+
axis: axis,
42+
});
3743
}
38-
// eslint-disable-next-line no-undef
39-
const scrollTimeline = new ScrollTimeline({
40-
source: scrollRef.current,
41-
axis: axis,
42-
});
4344
activeGesture.current = startGestureTransition(
4445
scrollTimeline,
4546
() => {

0 commit comments

Comments
 (0)