Skip to content

Instantly share code, notes, and snippets.

@hamidhadi
Created April 22, 2020 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamidhadi/8a40d9ceead159fc9850d410a5bd1082 to your computer and use it in GitHub Desktop.
Save hamidhadi/8a40d9ceead159fc9850d410a5bd1082 to your computer and use it in GitHub Desktop.
WithStretchy
import React from 'react';
import { View, Animated } from 'react-native';
import { commonStyles } from './styles';
import { StretchyProps } from '../types';
import { StretchyImage } from './stretchyImage';
import { useStretchy, UseStretchyOutput } from '../hooks/useStretchy';
export interface WithStretchyProps {
stretchy: UseStretchyOutput;
}
export type StretchyComponentProps<T> = StretchyProps &
Omit<Animated.AnimatedProps<T>, 'onScroll'>;
export const WithStretchy = <T extends {}>(
WrappedComponent: React.FC<
StretchyComponentProps<T> & WithStretchyProps
>,
) => {
const EnhancedComponent: React.FC<StretchyComponentProps<T>> = (props) => {
const {
backgroundColor,
gradient,
image,
imageHeight,
imageWrapperStyle,
imageResizeMode,
onScroll,
} = props;
const stretchy = useStretchy({
image,
scrollListener: onScroll,
});
return (
<View style={[commonStyles.container, { backgroundColor }]}>
<StretchyImage
image={image}
imageResizeMode={imageResizeMode}
imageWrapperStyle={imageWrapperStyle}
gradient={gradient}
animation={stretchy.animation}
imageHeight={imageHeight || stretchy.heightBasedOnRatio}
onLayout={stretchy.onImageWrapperLayout}
/>
<WrappedComponent stretchy={stretchy} {...props} />
</View>
);
};
return EnhancedComponent;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment