This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hook to scroll to top on tab press
- Loading branch information
Showing
11 changed files
with
96 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as NativeContainer } from './NativeContainer'; | ||
export { default as useBackButton } from './useBackButton'; | ||
export { default as useLinking } from './useLinking'; | ||
export { default as NativeContainer } from './NativeContainer'; | ||
export { default as useScrollToTop } from './useScrollToTop'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as React from 'react'; | ||
import { useNavigation, EventArg } from '@react-navigation/core'; | ||
|
||
type ScrollableView = { | ||
scrollTo(options: { x?: number; y?: number; animated?: boolean }): void; | ||
}; | ||
|
||
export default function useScrollToTop(ref: React.RefObject<ScrollableView>) { | ||
const navigation = useNavigation(); | ||
|
||
React.useEffect( | ||
() => | ||
// @ts-ignore | ||
// We don't wanna import tab types here to avoid extra deps | ||
// in addition, there are multiple tab implementations | ||
navigation.addListener('tabPress', (e: EventArg<'tabPress'>) => { | ||
// Run the operation in the next frame so we're sure all listeners have been run | ||
// This is necessary to know if preventDefault() has been called | ||
requestAnimationFrame(() => { | ||
if (navigation.isFocused() && !e.defaultPrevented && ref.current) { | ||
// When user taps on already focused tab, scroll to top | ||
ref.current.scrollTo({ y: 0 }); | ||
} | ||
}); | ||
}), | ||
[navigation, ref] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters