Skip to content

Commit

Permalink
feat: add sticky section headers enabled prop
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Jun 9, 2021
1 parent eabc264 commit f392310
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
8 changes: 8 additions & 0 deletions docs/Props.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ sectionFooterHeight(section: number)
| ---------------- | -------- | ------- |
| number, function | No | `0` |

### `stickySectionHeadersEnabled`

Makes section headers stick to the top of the screen until the next one pushes it off.

| Type | Required | Default |
| ------- | -------- | ------- |
| boolean | No | `true` |

### `scrollTopValue`

Specify the initial scroll from the top of the list.
Expand Down
49 changes: 26 additions & 23 deletions lib/BigList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,19 @@ class BigList extends PureComponent {
* Component did mount.
*/
componentDidMount() {
const { stickySectionHeadersEnabled } = this.props;
const scrollView = this.getNativeScrollRef();
if (scrollView != null) {
if (Platform.OS !== "web") {
// Disabled on web
this.scrollTopValueAttachment = Animated.attachNativeEvent(
scrollView,
"onScroll",
[{ nativeEvent: { contentOffset: { y: this.scrollTopValue } } }],
);
}
if (
stickySectionHeadersEnabled &&
scrollView != null &&
Platform.OS !== "web"
) {
// Disabled on web
this.scrollTopValueAttachment = Animated.attachNativeEvent(
scrollView,
"onScroll",
[{ nativeEvent: { contentOffset: { y: this.scrollTopValue } } }],
);
}
}

Expand Down Expand Up @@ -656,10 +659,21 @@ class BigList extends PureComponent {
insetTop,
insetBottom,
actionSheetScrollRef,
stickySectionHeadersEnabled,
...props
} = this.props;

const wrapper = renderActionSheetScrollViewWrapper || ((val) => val);
const handleScroll =
stickySectionHeadersEnabled && Platform.OS === "web"
? Animated.event(
[{ nativeEvent: { contentOffset: { y: this.scrollTopValue } } }],
{
listener: (event) => this.onScroll(event),
useNativeDriver: false,
},
)
: this.onScroll;
const scrollViewProps = {
...props,
...{
Expand All @@ -669,20 +683,7 @@ class BigList extends PureComponent {
actionSheetScrollRef.current = ref;
}
},
onScroll:
Platform.OS === "web"
? Animated.event(
[
{
nativeEvent: { contentOffset: { y: this.scrollTopValue } },
},
],
{
listener: (event) => this.onScroll(event),
useNativeDriver: false,
},
)
: this.onScroll,
onScroll: handleScroll,
onLayout: this.onLayout,
onMomentumScrollEnd: this.onScrollEnd,
onScrollEndDrag: this.onScrollEnd,
Expand Down Expand Up @@ -783,6 +784,7 @@ BigList.propTypes = {
PropTypes.func,
]),
sections: PropTypes.array,
stickySectionHeadersEnabled: PropTypes.bool,
};

BigList.defaultProps = {
Expand All @@ -802,6 +804,7 @@ BigList.defaultProps = {
sectionHeight: 0,
sectionFooterHeight: 0,
// Scroll
stickySectionHeadersEnabled: true,
removeClippedSubviews: false,
scrollEventThrottle: Platform.OS === "web" ? 5 : 16,
// Keyboard
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface BigListProps<ItemT> extends ScrollViewProps {
sectionFooterHeight?: string | number | ((section: number) => number);
sectionHeight?: string | number | ((section: number) => number);
sections?: ItemT[][];
stickySectionHeadersEnabled: boolean;
children?: null | undefined;
}
export default class BigList<ItemT = any> extends PureComponent<
Expand Down

0 comments on commit f392310

Please sign in to comment.