UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useScrollToTop;
7
8var _core = require("@react-navigation/core");
9
10var React = _interopRequireWildcard(require("react"));
11
12function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
14function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
16function getScrollableNode(ref) {
17 if (ref.current == null) {
18 return null;
19 }
20
21 if ('scrollToTop' in ref.current || 'scrollTo' in ref.current || 'scrollToOffset' in ref.current || 'scrollResponderScrollTo' in ref.current) {
22 // This is already a scrollable node.
23 return ref.current;
24 } else if ('getScrollResponder' in ref.current) {
25 // If the view is a wrapper like FlatList, SectionList etc.
26 // We need to use `getScrollResponder` to get access to the scroll responder
27 return ref.current.getScrollResponder();
28 } else if ('getNode' in ref.current) {
29 // When a `ScrollView` is wraped in `Animated.createAnimatedComponent`
30 // we need to use `getNode` to get the ref to the actual scrollview.
31 // Note that `getNode` is deprecated in newer versions of react-native
32 // this is why we check if we already have a scrollable node above.
33 return ref.current.getNode();
34 } else {
35 return ref.current;
36 }
37}
38
39function useScrollToTop(ref) {
40 const navigation = (0, _core.useNavigation)();
41 const route = (0, _core.useRoute)();
42 React.useEffect(() => {
43 let current = navigation; // The screen might be inside another navigator such as stack nested in tabs
44 // We need to find the closest tab navigator and add the listener there
45
46 while (current && current.getState().type !== 'tab') {
47 current = current.getParent();
48 }
49
50 if (!current) {
51 return;
52 }
53
54 const unsubscribe = current.addListener( // We don't wanna import tab types here to avoid extra deps
55 // in addition, there are multiple tab implementations
56 // @ts-expect-error
57 'tabPress', e => {
58 // We should scroll to top only when the screen is focused
59 const isFocused = navigation.isFocused(); // In a nested stack navigator, tab press resets the stack to first screen
60 // So we should scroll to top only when we are on first screen
61
62 const isFirst = navigation === current || navigation.getState().routes[0].key === route.key; // Run the operation in the next frame so we're sure all listeners have been run
63 // This is necessary to know if preventDefault() has been called
64
65 requestAnimationFrame(() => {
66 const scrollable = getScrollableNode(ref);
67
68 if (isFocused && isFirst && scrollable && !e.defaultPrevented) {
69 if ('scrollToTop' in scrollable) {
70 scrollable.scrollToTop();
71 } else if ('scrollTo' in scrollable) {
72 scrollable.scrollTo({
73 x: 0,
74 y: 0,
75 animated: true
76 });
77 } else if ('scrollToOffset' in scrollable) {
78 scrollable.scrollToOffset({
79 offset: 0,
80 animated: true
81 });
82 } else if ('scrollResponderScrollTo' in scrollable) {
83 scrollable.scrollResponderScrollTo({
84 y: 0,
85 animated: true
86 });
87 }
88 }
89 });
90 });
91 return unsubscribe;
92 }, [navigation, ref, route.key]);
93}
94//# sourceMappingURL=useScrollToTop.js.map
\No newline at end of file