Created
April 4, 2024 08:32
-
-
Save cubuspl42/11160cdcb88d4978bd856f4ceb399ae7 to your computer and use it in GitHub Desktop.
BottomAlignedScrollView.tsx
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
import React, {useState} from 'react'; | |
import {LayoutChangeEvent, ScrollView, StyleSheet} from 'react-native'; | |
type BottomAlignedScrollViewProps = { | |
readonly children: React.ReactNode; | |
} | |
const BottomAlignedScrollView = ({children}: BottomAlignedScrollViewProps) => { | |
const [viewHeight, setViewHeight] = useState(0); | |
const [contentHeight, setContentHeight] = useState(0); | |
const onLayout = (event: LayoutChangeEvent) => { | |
const {height} = event.nativeEvent.layout; | |
setViewHeight(height); | |
} | |
const onContentLayout = (_: number, height: number) => { | |
setContentHeight(height); | |
} | |
return ( | |
<ScrollView | |
onLayout={onLayout} | |
onContentSizeChange={onContentLayout} | |
contentContainerStyle={{ | |
...styles.scrollView, | |
paddingTop: contentHeight < viewHeight ? viewHeight - contentHeight : 0, | |
}} | |
> | |
{children} | |
</ScrollView> | |
); | |
} | |
const styles = StyleSheet.create({ | |
scrollView: { | |
flexGrow: 1, | |
justifyContent: 'flex-end', | |
}, | |
}); | |
export default BottomAlignedScrollView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment