From 3cefb7ba8505864ef17719d27b8c1b15d3194bf5 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Mon, 7 Oct 2019 08:14:57 -0600 Subject: [PATCH] fix: style and layout props for pager, add containerStyle prop with defaults --- README.md | 3 +- example/App.tsx | 2 +- example/src/basic-example.tsx | 7 +- example/src/panhandler-width.tsx | 25 +++++--- example/src/stack.tsx | 3 +- example/src/swipe-cards.tsx | 7 +- src/pager.tsx | 106 ++++++++++++++++++------------- 7 files changed, 89 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 271d923..1cdd87f 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ threshold?: number; - percentage (0 - 1), how far should the user drag before sn minIndex?: number; - minimum index to swipe to (default 0) maxIndex?: number; - maximum index to swipe to (default children.length - 1) adjacentChildOffset?: number; - the number of children adjacent to the activeIndex to render -style?: ViewStyle; - container style for the pager +style?: ViewStyle; - style for pages +containerStyle?: ViewStyle - style for pan handler container animatedValue?: Animated.Value; - total translation value of the pager animatedIndex?: Animated.Value; - activeIndex as an animated value e.g intermediate values type?: 'horizontal' | 'vertical'; - target horizontal swipes or vertical swipes diff --git a/example/App.tsx b/example/App.tsx index 16a0963..15f4c73 100644 --- a/example/App.tsx +++ b/example/App.tsx @@ -34,7 +34,7 @@ const App = () => { - + diff --git a/example/src/basic-example.tsx b/example/src/basic-example.tsx index 9ea6388..6fb6c6f 100644 --- a/example/src/basic-example.tsx +++ b/example/src/basic-example.tsx @@ -9,7 +9,7 @@ function MyPager() { const [activeIndex, onChange] = usePager(); return ( - + - + {children} diff --git a/example/src/panhandler-width.tsx b/example/src/panhandler-width.tsx index 01c82f2..5308b73 100644 --- a/example/src/panhandler-width.tsx +++ b/example/src/panhandler-width.tsx @@ -36,16 +36,21 @@ const pagerConfig: iPageInterpolation = { const ContainerStyle = () => { const [activeIndex, onChange] = usePager(); return ( - - - - {[...Array(10).keys()].map(n => ( - {n} - ))} - - + + + {[...Array(10).keys()].map(n => ( + {n} + ))} + diff --git a/example/src/stack.tsx b/example/src/stack.tsx index 2e0c443..22c536c 100644 --- a/example/src/stack.tsx +++ b/example/src/stack.tsx @@ -34,6 +34,7 @@ function Stack() { clamp={{prev: 0.4}} clampDrag={{next: 0}} adjacentChildOffset={4} + containerStyle={{height: 200}} style={{ height: 200, width: 200, @@ -41,7 +42,7 @@ function Stack() { }} pageInterpolation={stackConfig}> {Array.from({length: activeIndex + 3}, (_, i) => ( - + ))} diff --git a/example/src/swipe-cards.tsx b/example/src/swipe-cards.tsx index f3991ce..970c375 100644 --- a/example/src/swipe-cards.tsx +++ b/example/src/swipe-cards.tsx @@ -52,7 +52,12 @@ function SwipeCards() { {Array.from({length: activeIndex + 4}, (_, i) => ( diff --git a/src/pager.tsx b/src/pager.tsx index cf365d6..45da341 100644 --- a/src/pager.tsx +++ b/src/pager.tsx @@ -114,6 +114,7 @@ export interface iPager { maxIndex?: number; adjacentChildOffset?: number; style?: ViewStyle; + containerStyle?: ViewStyle; animatedValue?: Animated.Value; animatedIndex?: Animated.Value; type?: 'horizontal' | 'vertical'; @@ -142,6 +143,7 @@ function Pager({ adjacentChildOffset = 10, animatedValue, style, + containerStyle, type = 'horizontal', pageInterpolation, clamp = { @@ -251,6 +253,7 @@ function Pager({ const [width, setWidth] = useState(0); const [height, setHeight] = useState(0); + const dimension = memoize(new Value(0)); function handleLayout({ nativeEvent: { layout } }: LayoutChangeEvent) { @@ -493,58 +496,71 @@ function Pager({ // set the total width of the container view to the sum width of all the screens const totalDimension = memoize(multiply(dimension, numberOfScreens)); + // grabbing the height property from the style prop if there is no container style, this reduces + // the chances of messing up the layout with containerStyle configurations + // can be overridden by the prop itself, but its likely that this is what is intended most of the time + // also has the benefit of covering 100% width of container, meaning better pan coverage on android + const defaultContainerStyle = + style && style.height ? { height: style.height } : undefined; + + // extra Animated.Views below may seem redundant but they preserve applied styles e.g padding and margin + // of the page views return ( - + - - - {width === 0 - ? null - : adjacentChildren.map((child: any, i) => { - // use map instead of React.Children because we want to track - // the keys of these children by there index - // React.Children shifts these key values intelligently, but it - // causes issues with the memoized values in components - let index = i; - - if (adjacentChildOffset !== undefined) { - index = - activeIndex <= adjacentChildOffset - ? i - : activeIndex - adjacentChildOffset + i; - } - - return ( - - - - {child} - - - - ); - })} + + + + {width === 0 + ? null + : adjacentChildren.map((child: any, i) => { + // use map instead of React.Children because we want to track + // the keys of these children by there index + // React.Children shifts these key values intelligently, but it + // causes issues with the memoized values in components + let index = i; + + if (adjacentChildOffset !== undefined) { + index = + activeIndex <= adjacentChildOffset + ? i + : activeIndex - adjacentChildOffset + i; + } + + return ( + + + + {child} + + + + ); + })} +