Skip to content

Commit abecdb2

Browse files
committed
feat: page margin
1 parent f217dc7 commit abecdb2

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/PagerView.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,23 @@ export class PagerView extends React.Component<NativeProps> {
151151
ref={(ref) => {
152152
this.pagerView = ref;
153153
}}
154-
style={this.props.style}
154+
style={[
155+
this.props.style,
156+
Platform.OS === 'ios' && this.props.pageMargin
157+
? {
158+
marginHorizontal: -this.props.pageMargin / 2,
159+
}
160+
: null,
161+
]}
155162
layoutDirection={this.deducedLayoutDirection}
156163
onPageScroll={this._onPageScroll}
157164
onPageScrollStateChanged={this._onPageScrollStateChanged}
158165
onPageSelected={this._onPageSelected}
159166
onMoveShouldSetResponderCapture={this._onMoveShouldSetResponderCapture}
160-
children={childrenWithOverriddenStyle(this.props.children)}
167+
children={childrenWithOverriddenStyle(
168+
this.props.children,
169+
this.props.pageMargin
170+
)}
161171
/>
162172
);
163173
}

src/utils.ios.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React, { Children, ReactNode } from 'react';
2+
import { StyleSheet, View } from 'react-native';
3+
4+
export const childrenWithOverriddenStyle = (
5+
children?: ReactNode,
6+
pageMargin = 0
7+
) => {
8+
return Children.map(children, (child) => {
9+
return (
10+
<View
11+
style={[
12+
StyleSheet.absoluteFill,
13+
{ marginRight: pageMargin / 2, marginLeft: pageMargin / 2 },
14+
]}
15+
collapsable={false}
16+
>
17+
{child}
18+
</View>
19+
);
20+
});
21+
};

src/utils.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React, { Children, ReactNode } from 'react';
22
import { StyleSheet, View } from 'react-native';
33

4-
export const childrenWithOverriddenStyle = (children?: ReactNode) => {
4+
export const childrenWithOverriddenStyle = (
5+
children?: ReactNode,
6+
_pageMargin = 0
7+
) => {
58
return Children.map(children, (child) => {
69
const element = child as React.ReactElement<any>;
710
return (
8-
// Add a wrapper to ensure layout is calculated correctly
911
<View style={StyleSheet.absoluteFill} collapsable={false}>
1012
{React.cloneElement(element, {
1113
...element.props,
12-
// Override styles so that each page will fill the parent.
1314
style: [element.props.style, StyleSheet.absoluteFill],
1415
})}
1516
</View>

0 commit comments

Comments
 (0)