Skip to content
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

Add parallax intensity property #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ class SystretchyScrollView extends Component {

These are default properties that is available for all stretchy components

| Prop | Default | Description |
| --------------- | :-------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| backgroundColor | `#FFF` | Background color of the inner content |
| image | `null` | The image of the stretchy header ([RN image source][2]) |
| imageHeight | `null` | Height of the stretchy header image (keep ratio of `image` if not provided) |
| imageResizeMode | `'cover'` | ResizeMode of the stretchy header image. [You can use one of these values](https://facebook.github.io/react-native/docs/image.html#resizemode) |
| gradient | null | Gradient config object. See [LinearGradientProps][3] |
| foreground | `null` | A RN Component for foreground content of background |
| onScroll | `null` | A callback function with these arguments:<br>`position`: current position of scroll<br>`reachedToTheBottomOfHeader`: boolean flag to specify whether the scroll has reached to the bottom of header or not |
| Prop | Default | Description |
| ----------------- | :-------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| backgroundColor | `#FFF` | Background color of the inner content |
| image | `null` | The image of the stretchy header ([RN image source][2]) |
| imageHeight | `null` | Height of the stretchy header image (keep ratio of `image` if not provided) |
| imageResizeMode | `'cover'` | ResizeMode of the stretchy header image. [You can use one of these values](https://facebook.github.io/react-native/docs/image.html#resizemode) |
| gradient | null | Gradient config object. See [LinearGradientProps][3] |
| foreground | `null` | A RN Component for foreground content of background |
| onScroll | `null` | A callback function with these arguments:<br>`position`: current position of scroll<br>`reachedToTheBottomOfHeader`: boolean flag to specify whether the scroll has reached to the bottom of header or not |
| parallaxIntensity | `null` | Parallax intensity of stretchy header image (no parallax when set -1) |

## 💁‍♂️ Components

Expand Down
20 changes: 14 additions & 6 deletions lib/src/components/stretchyImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface StretchyImageProps
animation: Animated.Value;
imageHeight: number;
onLayout(event: LayoutChangeEvent): void;
parallaxIntensity?: number;
}

export const StretchyImage: React.FC<StretchyImageProps> = ({
Expand All @@ -20,14 +21,22 @@ export const StretchyImage: React.FC<StretchyImageProps> = ({
imageWrapperStyle,
imageHeight,
onLayout,
parallaxIntensity,
}) => {
const transformStyles = useMemo(
() => ({
const transformStyles = useMemo(() => {
const parallaxOffset =
parallaxIntensity === undefined ? 0 : Math.max(parallaxIntensity, -1);

return {
transform: [
{
translateY: animation.interpolate({
inputRange: [-imageHeight, 0, imageHeight],
outputRange: [imageHeight / 2, 0, -imageHeight / 2],
outputRange: [
imageHeight / 2,
0,
-imageHeight / (2 + parallaxOffset),
],
}),
},
{
Expand All @@ -37,9 +46,8 @@ export const StretchyImage: React.FC<StretchyImageProps> = ({
}),
},
],
}),
[animation, imageHeight],
);
};
}, [animation, imageHeight, parallaxIntensity]);

return (
<View
Expand Down
2 changes: 2 additions & 0 deletions lib/src/components/withStretchy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const WithStretchy = <T extends {}>(
imageWrapperStyle,
imageResizeMode,
onScroll,
parallaxIntensity,
} = props;

const stretchy = useStretchy({
Expand All @@ -43,6 +44,7 @@ export const WithStretchy = <T extends {}>(
animation={stretchy.animation}
imageHeight={imageHeight || stretchy.heightBasedOnRatio}
onLayout={stretchy.onImageWrapperLayout}
parallaxIntensity={parallaxIntensity}
/>
<WrappedComponent stretchy={stretchy} {...props} />
</View>
Expand Down
1 change: 1 addition & 0 deletions lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface StretchyProps {
| 'angleCenter'
| 'angle'
>;
parallaxIntensity?: number;
}