-
Notifications
You must be signed in to change notification settings - Fork 734
Fader - a component to easily add fade look to other components #847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ac82003
withScrollFader - initial commit
M-i-k-e-l 74435d7
withScrollReached - add docs
M-i-k-e-l 25337a5
Clean up the code
M-i-k-e-l 87fc62f
Move from onLayout to 4 images
M-i-k-e-l 5ee8751
Remove eslint ignores
M-i-k-e-l 02d7b3e
Do not export images
M-i-k-e-l 2e6f769
Merge branch 'master' into feat/with-scroll-fader
M-i-k-e-l e8e76a2
Create and use Fader (instead of withScrollFader)
M-i-k-e-l 28e1173
Rename withScrollFader --> Fader
M-i-k-e-l a099e1f
Move visible into the render
M-i-k-e-l 774d215
FaderLocation -> FaderPosition
M-i-k-e-l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,90 @@ | ||
import _ from 'lodash'; | ||
import React, {Component} from 'react'; | ||
import {StyleSheet, ScrollView} from 'react-native'; | ||
import { | ||
Colors, | ||
Text, | ||
View, | ||
Fader, | ||
withScrollReached, | ||
WithScrollReachedProps | ||
} from 'react-native-ui-lib'; | ||
import {renderHeader} from '../ExampleScreenPresenter'; | ||
|
||
const numberOfItems = 3; | ||
const faderPosition = Fader.position.BOTTOM; | ||
const itemWidth = 100; | ||
const itemHeight = 100; | ||
const tintColor = undefined; | ||
|
||
const horizontal = | ||
faderPosition === Fader.position.LEFT || | ||
faderPosition === Fader.position.RIGHT; | ||
|
||
class FaderScreen extends Component<WithScrollReachedProps> { | ||
renderItem = (index: number) => { | ||
return ( | ||
<View key={index} style={styles.item}> | ||
<Text>{index + 1}</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
render() { | ||
const {scrollReachedProps} = this.props; | ||
const visible = | ||
faderPosition === Fader.position.BOTTOM || | ||
faderPosition === Fader.position.RIGHT | ||
? !scrollReachedProps.isScrollAtEnd | ||
: !scrollReachedProps.isScrollAtStart; | ||
|
||
return ( | ||
<View margin-10> | ||
{renderHeader('Fader', {'marginB-10': true})} | ||
<View center> | ||
<View style={styles.container}> | ||
<ScrollView | ||
horizontal={horizontal} | ||
style={styles.scrollView} | ||
showsVerticalScrollIndicator={false} | ||
showsHorizontalScrollIndicator={false} | ||
scrollEventThrottle={16} | ||
onScroll={scrollReachedProps.onScroll} | ||
> | ||
{_.times(numberOfItems, this.renderItem)} | ||
</ScrollView> | ||
<Fader | ||
visible={visible} | ||
position={faderPosition} | ||
tintColor={tintColor} | ||
/> | ||
</View> | ||
</View> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
export default withScrollReached(FaderScreen, { | ||
horizontal | ||
}); | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
width: horizontal ? undefined : itemWidth, | ||
height: horizontal ? itemHeight : undefined | ||
}, | ||
scrollView: { | ||
width: horizontal ? 240 : undefined, | ||
height: horizontal ? undefined : 240 | ||
}, | ||
item: { | ||
height: itemHeight, | ||
width: itemWidth, | ||
backgroundColor: Colors.grey60, | ||
borderColor: Colors.grey40, | ||
borderWidth: 2, | ||
alignItems: 'center', | ||
justifyContent: 'center' | ||
} | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
/// <reference types="react" /> | ||
export declare enum FaderPosition { | ||
LEFT = "LEFT", | ||
RIGHT = "RIGHT", | ||
TOP = "TOP", | ||
BOTTOM = "BOTTOM" | ||
} | ||
export declare type FaderProps = { | ||
/** | ||
* Whether the fader is visible (default is true) | ||
*/ | ||
visible?: boolean; | ||
/** | ||
* The position of the fader (the image is different), defaults to Fader.position.RIGHT | ||
*/ | ||
position?: FaderPosition; | ||
/** | ||
* Set to change from the default size (50) of the fade view. | ||
*/ | ||
size?: number; | ||
/** | ||
* Change the default (white) tint color of the fade view. | ||
*/ | ||
tintColor?: string; | ||
}; | ||
declare function Fader(props: FaderProps): JSX.Element; | ||
declare namespace Fader { | ||
var displayName: string; | ||
var position: typeof FaderPosition; | ||
} | ||
export default Fader; |
This file contains hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,119 @@ | ||
import _ from 'lodash'; | ||
import React, {useCallback} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import View from '../view'; | ||
import Image from '../image'; | ||
|
||
export enum FaderPosition { | ||
LEFT = 'LEFT', | ||
RIGHT = 'RIGHT', | ||
TOP = 'TOP', | ||
BOTTOM = 'BOTTOM' | ||
} | ||
|
||
export type FaderProps = { | ||
/** | ||
* Whether the fader is visible (default is true) | ||
*/ | ||
visible?: boolean; | ||
/** | ||
* The position of the fader (the image is different), defaults to Fader.position.RIGHT | ||
*/ | ||
position?: FaderPosition; | ||
/** | ||
* Set to change from the default size (50) of the fade view. | ||
*/ | ||
size?: number; | ||
/** | ||
* Change the default (white) tint color of the fade view. | ||
*/ | ||
tintColor?: string; | ||
}; | ||
|
||
const DEFAULT_FADE_SIZE = 50; | ||
|
||
function Fader(props: FaderProps) { | ||
const getFadeSize = useCallback(() => { | ||
return props.size || DEFAULT_FADE_SIZE; | ||
}, [props.size]); | ||
|
||
const fadeSize = getFadeSize(); | ||
const getStyles = useCallback(() => { | ||
const position = props.position || FaderPosition.RIGHT; | ||
let containerStyle, imageStyle, imageSource; | ||
switch (position) { | ||
case FaderPosition.LEFT: | ||
containerStyle = {...staticStyles.containerLeft, width: fadeSize}; | ||
imageStyle = {height: '100%', width: fadeSize}; | ||
imageSource = require('./gradientLeft.png'); | ||
break; | ||
case FaderPosition.RIGHT: | ||
containerStyle = {...staticStyles.containerRight, width: fadeSize}; | ||
imageStyle = {height: '100%', width: fadeSize}; | ||
imageSource = require('./gradientRight.png'); | ||
break; | ||
case FaderPosition.TOP: | ||
containerStyle = {...staticStyles.containerTop, height: fadeSize}; | ||
imageStyle = {height: fadeSize, width: '100%'}; | ||
imageSource = require('./gradientTop.png'); | ||
break; | ||
case FaderPosition.BOTTOM: | ||
containerStyle = { | ||
...staticStyles.containerBottom, | ||
height: fadeSize | ||
}; | ||
imageStyle = {height: fadeSize, width: '100%'}; | ||
imageSource = require('./gradientBottom.png'); | ||
break; | ||
} | ||
|
||
return { | ||
containerStyle, | ||
imageStyle, | ||
imageSource | ||
}; | ||
}, [fadeSize, props.position]); | ||
|
||
const styles = getStyles(); | ||
|
||
return ( | ||
<View pointerEvents={'none'} style={styles.containerStyle}> | ||
{(props.visible || _.isUndefined(props.visible)) && ( | ||
<Image | ||
source={styles.imageSource} | ||
tintColor={props.tintColor} | ||
style={styles.imageStyle} | ||
resizeMode={'stretch'} | ||
/> | ||
)} | ||
</View> | ||
); | ||
} | ||
|
||
Fader.displayName = 'Fader'; | ||
Fader.position = FaderPosition; | ||
|
||
export default Fader; | ||
|
||
const staticStyles = StyleSheet.create({ | ||
containerTop: { | ||
position: 'absolute', | ||
top: 0, | ||
width: '100%' | ||
}, | ||
containerBottom: { | ||
position: 'absolute', | ||
bottom: 0, | ||
width: '100%' | ||
}, | ||
containerLeft: { | ||
position: 'absolute', | ||
left: 0, | ||
height: '100%' | ||
}, | ||
containerRight: { | ||
position: 'absolute', | ||
right: 0, | ||
height: '100%' | ||
} | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Very simple and easy to understand the component 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10x