Skip to content

Commit 21226c9

Browse files
committed
Optimization of the scroll
1 parent 0dfa138 commit 21226c9

File tree

3 files changed

+101
-63
lines changed

3 files changed

+101
-63
lines changed

src/ImageHeaderScrollView.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ class ImageHeaderScrollView extends Component {
9090
}
9191

9292
renderHeader() {
93-
const headerHeight = this.interpolateOnImageHeight([
94-
this.props.maxHeight,
95-
this.props.minHeight,
96-
]);
9793
const overlayOpacity = this.interpolateOnImageHeight([
9894
this.props.minOverlayOpacity,
9995
this.props.maxOverlayOpacity,
@@ -105,7 +101,11 @@ class ImageHeaderScrollView extends Component {
105101
extrapolate: 'clamp',
106102
});
107103

108-
const headerTransformStyle = { height: headerHeight, transform: [{ scale: headerScale }] };
104+
const headerTransformStyle = {
105+
height: this.props.maxHeight,
106+
transform: [{ scale: headerScale }],
107+
};
108+
109109
return (
110110
<Animated.View style={[styles.header, headerTransformStyle]}>
111111
<Animated.View style={[styles.blackOverlay, { opacity: overlayOpacity }]} />
@@ -138,29 +138,43 @@ class ImageHeaderScrollView extends Component {
138138
}
139139

140140
render() {
141-
const headerScrollDistance = this.props.maxHeight - this.props.minHeight;
142141
const scrollViewProps = _.pick(this.props, _.keys(ScrollView.propTypes));
142+
143+
const headerScrollDistance = this.interpolateOnImageHeight([
144+
this.props.maxHeight,
145+
this.props.maxHeight - this.props.minHeight,
146+
]);
147+
const topMargin = this.interpolateOnImageHeight([0, this.props.minHeight]);
148+
149+
const childrenContainerStyle = StyleSheet.flatten([
150+
{ transform: [{ translateY: headerScrollDistance }] },
151+
{ backgroundColor: 'white' },
152+
this.props.childrenStyle,
153+
]);
154+
143155
return (
144156
<View
145157
style={styles.container}
146158
ref={(ref) => { this.container = ref; }}
147159
onLayout={() => this.container.measureInWindow((x, y) => this.setState({ pageY: y }))}
148160
>
149-
<ScrollView
150-
ref={(ref) => { this[SCROLLVIEW_REF] = ref; }}
151-
style={[styles.container, { marginTop: this.props.minHeight }]}
152-
scrollEventThrottle={16}
153-
onScroll={Animated.event(
154-
[{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
155-
)}
156-
{...scrollViewProps}
157-
>
158-
<Animated.View style={[{ paddingTop: headerScrollDistance }, this.props.childrenStyle]}>
159-
{this.props.children}
160-
</Animated.View>
161-
</ScrollView>
162161
{ this.renderHeader() }
163162
{ this.renderForeground() }
163+
<Animated.View style={[styles.container, { transform: [{ translateY: topMargin }] }]}>
164+
<ScrollView
165+
ref={(ref) => { this[SCROLLVIEW_REF] = ref; }}
166+
style={styles.container}
167+
scrollEventThrottle={16}
168+
onScroll={Animated.event(
169+
[{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
170+
)}
171+
{...scrollViewProps}
172+
>
173+
<Animated.View style={childrenContainerStyle}>
174+
{this.props.children}
175+
</Animated.View>
176+
</ScrollView>
177+
</Animated.View>
164178
</View>
165179
);
166180
}

src/__tests__/__snapshots__/TriggeringView.js.snap

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,6 @@ exports[`TriggeringView renders correctly by default 1`] = `
66
"flex": 1,
77
}
88
}>
9-
<ScrollView
10-
onScroll={[Function]}
11-
scrollEventThrottle={16}
12-
style={
13-
Array [
14-
Object {
15-
"flex": 1,
16-
},
17-
Object {
18-
"marginTop": 80,
19-
},
20-
]
21-
}>
22-
<View
23-
style={
24-
Object {
25-
"paddingTop": 45,
26-
}
27-
}>
28-
<View
29-
collapsable={false}
30-
onLayout={[Function]} />
31-
</View>
32-
</ScrollView>
339
<View
3410
style={
3511
Object {
@@ -93,6 +69,42 @@ exports[`TriggeringView renders correctly by default 1`] = `
9369
}>
9470
<View />
9571
</View>
72+
<View
73+
style={
74+
Object {
75+
"flex": 1,
76+
"transform": Array [
77+
Object {
78+
"translateY": 0,
79+
},
80+
],
81+
}
82+
}>
83+
<ScrollView
84+
onScroll={[Function]}
85+
scrollEventThrottle={16}
86+
style={
87+
Object {
88+
"flex": 1,
89+
}
90+
}>
91+
<View
92+
style={
93+
Object {
94+
"backgroundColor": "white",
95+
"transform": Array [
96+
Object {
97+
"translateY": 125,
98+
},
99+
],
100+
}
101+
}>
102+
<View
103+
collapsable={false}
104+
onLayout={[Function]} />
105+
</View>
106+
</ScrollView>
107+
</View>
96108
</View>
97109
`;
98110

src/__tests__/__snapshots__/index.js.snap

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ exports[`ImageHeaderScrollView renders correctly by default 1`] = `
66
"flex": 1,
77
}
88
}>
9-
<ScrollView
10-
onScroll={[Function]}
11-
scrollEventThrottle={16}
12-
style={
13-
Array [
14-
Object {
15-
"flex": 1,
16-
},
17-
Object {
18-
"marginTop": 80,
19-
},
20-
]
21-
}>
22-
<View
23-
style={
24-
Object {
25-
"paddingTop": 45,
26-
}
27-
} />
28-
</ScrollView>
299
<View
3010
style={
3111
Object {
@@ -89,5 +69,37 @@ exports[`ImageHeaderScrollView renders correctly by default 1`] = `
8969
}>
9070
<View />
9171
</View>
72+
<View
73+
style={
74+
Object {
75+
"flex": 1,
76+
"transform": Array [
77+
Object {
78+
"translateY": 0,
79+
},
80+
],
81+
}
82+
}>
83+
<ScrollView
84+
onScroll={[Function]}
85+
scrollEventThrottle={16}
86+
style={
87+
Object {
88+
"flex": 1,
89+
}
90+
}>
91+
<View
92+
style={
93+
Object {
94+
"backgroundColor": "white",
95+
"transform": Array [
96+
Object {
97+
"translateY": 125,
98+
},
99+
],
100+
}
101+
} />
102+
</ScrollView>
103+
</View>
92104
</View>
93105
`;

0 commit comments

Comments
 (0)