Skip to content

Commit 33fc6b1

Browse files
committed
feat: page margin
1 parent f217dc7 commit 33fc6b1

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
const element = child as React.ReactElement<any>;
10+
return (
11+
<View
12+
style={[
13+
StyleSheet.absoluteFill,
14+
{ marginRight: pageMargin / 2, marginLeft: pageMargin / 2 },
15+
]}
16+
collapsable={false}
17+
>
18+
{React.cloneElement(element, {
19+
...element.props,
20+
style: [element.props.style, StyleSheet.absoluteFill],
21+
})}
22+
</View>
23+
);
24+
});
25+
};

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)