-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scrollview): add ScrollView component
- Loading branch information
Artur Bien
committed
Dec 5, 2020
1 parent
38094ba
commit aa5682f
Showing
11 changed files
with
262 additions
and
22 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
File renamed without changes.
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,182 @@ | ||
import React from 'react'; | ||
import { | ||
StyleSheet, | ||
View, | ||
ScrollView as RNScrollView, | ||
ViewStyle, | ||
ImageBackground, | ||
Image, | ||
} from 'react-native'; | ||
import { original as theme } from '../common/themes'; | ||
|
||
import { Panel, Button } from '..'; | ||
|
||
type ScrollViewProps = React.ComponentProps<typeof RNScrollView> & { | ||
style?: ViewStyle; | ||
children: React.ReactNode; | ||
}; | ||
|
||
const scrollbarSize = 30; | ||
|
||
const Icon = ( | ||
<Image | ||
// border to compensate for Border | ||
style={[ | ||
{ | ||
width: 18, | ||
height: 18, | ||
marginTop: 6, | ||
}, | ||
]} | ||
source={{ | ||
uri: | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAALElEQVQoU2NkIAIwEqGGgWRF/7GYCjYE3SRkhXA5bNaBFKKIk+wmnB4lyiQAAsgDCqkPxTcAAAAASUVORK5CYII=', | ||
}} | ||
/> | ||
); | ||
|
||
const ScrollView = ({ children, style, ...rest }: ScrollViewProps) => { | ||
const scrollView = React.useRef(null); | ||
const [contentOffset, setContentOffset] = React.useState({ x: 0, y: 0 }); | ||
const [contentSize, setContentSize] = React.useState(0); | ||
const [scrollViewHeight, setScrollViewHeight] = React.useState(0); | ||
|
||
const scrollElementHeightPercent = 100 * (scrollViewHeight / contentSize); | ||
|
||
const scrollPerc = | ||
(contentOffset.y / (contentSize - scrollViewHeight)) * | ||
(100 - scrollElementHeightPercent); | ||
|
||
const thumbPosition = Math.max( | ||
0, | ||
Math.min(100 - scrollElementHeightPercent, Math.round(scrollPerc || 0)), | ||
); | ||
|
||
const moveScroll = (direction: -1 | 1) => { | ||
if (scrollView.current) { | ||
scrollView.current.scrollTo({ y: contentOffset.y + 24 * direction }); | ||
} | ||
}; | ||
|
||
return ( | ||
<View style={[styles.wrapper, style]}> | ||
<View style={[styles.content]}> | ||
<RNScrollView | ||
showsVerticalScrollIndicator={false} | ||
scrollEventThrottle={10} | ||
ref={scrollView} | ||
onScroll={e => { | ||
setContentOffset(e.nativeEvent.contentOffset); | ||
}} | ||
onContentSizeChange={(_, height) => { | ||
setContentSize(height); | ||
}} | ||
onLayout={e => { | ||
setScrollViewHeight(e.nativeEvent.layout.height); | ||
}} | ||
{...rest} | ||
> | ||
{children} | ||
</RNScrollView> | ||
</View> | ||
{contentSize > scrollViewHeight && ( | ||
<View style={[styles.scrollbarTrack]}> | ||
<ImageBackground | ||
style={[styles.background]} | ||
imageStyle={{ | ||
resizeMode: 'repeat', | ||
}} | ||
source={{ | ||
// TODO: create util function for generating checkered background | ||
uri: | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAIUlEQVQoU2P8////fwYkwMjIyIjCp4MCZPtAbAwraa8AAEGrH/nfAIhgAAAAAElFTkSuQmCC', | ||
}} | ||
/> | ||
<Button | ||
variant='outside' | ||
onPress={() => moveScroll(-1)} | ||
style={[styles.scrollbarButton]} | ||
> | ||
<View | ||
style={{ | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
// flex: 1, | ||
transform: [{ translateY: 6 }, { rotate: '180deg' }], | ||
}} | ||
> | ||
{Icon} | ||
</View> | ||
</Button> | ||
<View style={[styles.scrollbar]}> | ||
<Panel | ||
variant='outside' | ||
style={[ | ||
styles.scrollbarBar, | ||
{ | ||
position: 'absolute', | ||
top: `${thumbPosition}%`, | ||
height: `${scrollElementHeightPercent}%`, | ||
}, | ||
]} | ||
/> | ||
</View> | ||
<Button | ||
variant='outside' | ||
onPress={() => moveScroll(1)} | ||
style={[styles.scrollbarButton]} | ||
> | ||
<View | ||
style={{ | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
// flex: 1, | ||
}} | ||
> | ||
{Icon} | ||
</View> | ||
</Button> | ||
</View> | ||
)} | ||
</View> | ||
); | ||
}; | ||
|
||
export default ScrollView; | ||
|
||
const styles = StyleSheet.create({ | ||
wrapper: { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
height: 'auto', | ||
position: 'relative', | ||
}, | ||
content: { | ||
flexGrow: 1, | ||
}, | ||
scrollbarTrack: { | ||
height: '100%', | ||
backgroundColor: theme.material, | ||
}, | ||
scrollbarButton: { | ||
height: scrollbarSize, | ||
width: scrollbarSize, | ||
padding: 0, | ||
}, | ||
scrollbar: { | ||
// height: '100%', | ||
width: scrollbarSize, | ||
overflow: 'hidden', | ||
flex: 1, | ||
}, | ||
background: { | ||
position: 'absolute', | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
}, | ||
scrollbarBar: { | ||
width: '100%', | ||
}, | ||
}); |
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 @@ | ||
export { default } from './ScrollView'; |
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
Oops, something went wrong.