Skip to content

Commit 7393d09

Browse files
committed
Update README.md and cleanup
1 parent 80677d5 commit 7393d09

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This project scaffolding is from [react-component-template](https://github.com/c
1010

1111
Try out the demo at [https://compulim.github.io/react-scroll-to-bottom/](https://compulim.github.io/react-scroll-to-bottom/).
1212

13-
# Sample
13+
# Sample code
1414

1515
```jsx
1616
import { css } from 'glamor';
@@ -31,6 +31,25 @@ export default props =>
3131

3232
> We use [`glamor`](https://github.com/threepointone/glamor/) for component styles. It is not required, but we don't support `style` props for performance reason.
3333
34+
## Props
35+
36+
| Name | Default | Description |
37+
| - | - | - |
38+
| `mode` | `"bottom"` | Set it to `"bottom"` for scroll-to-bottom, `"top"` for scroll-to-top. |
39+
40+
## Context
41+
42+
| Name | Type | Description |
43+
| - | - | - |
44+
| `atBottom` | Boolean | `true` if the panel is currently near bottom (see `threshold`) |
45+
| `atEnd` | Boolean | `true` if the panel is currently near the end (see `mode` and `threshold` |
46+
| `atTop` | Boolean | `true` if the panel is currently near top (see `threshold`) |
47+
| `mode` | String | `"bottom"` for scroll-to-bottom, `"top"` for scroll-to-top |
48+
| `scrollToBottom` | Function | Call to scroll panel to bottom |
49+
| `scrollToEnd` | Function | Call to scroll panel to end |
50+
| `scrollToTop` | Function | Call to scroll panel to top |
51+
| `threshold` | Number | Threshold in pixels to consider the panel is near top/bottom, read-only and only set thru `props` |
52+
3453
# Contributions
3554

3655
Like us? [Star](https://github.com/compulim/react-scroll-to-bottom/stargazers) us.

packages/component/src/ScrollToBottom/Composer.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,24 @@ export default class ScrollToBottomComposer extends React.Component {
2727
})),
2828
scrollTop: null,
2929
setTarget: target => this.setState(() => ({ target })),
30-
target: null
30+
target: null,
31+
threshold: 10
3132
};
3233
}
3334

3435
componentWillReceiveProps(nextProps) {
35-
this.setState(() => ({ mode: nextProps.mode === 'top' ? 'top' : 'bottom' }));
36+
this.setState(() => ({
37+
mode: nextProps.mode === 'top' ? 'top' : 'bottom',
38+
threshold: nextProps.threshold
39+
}));
3640
}
3741

3842
handleScroll() {
39-
this.setState(({ mode, target }) => {
43+
this.setState(({ mode, target, threshold }) => {
4044
if (target) {
4145
const { offsetHeight, scrollHeight, scrollTop } = target;
42-
const atBottom = scrollHeight - scrollTop - offsetHeight <= this.props.threshold;
43-
const atTop = scrollTop <= this.props.threshold;
46+
const atBottom = scrollHeight - scrollTop - offsetHeight <= threshold;
47+
const atTop = scrollTop <= threshold;
4448

4549
return {
4650
atBottom,
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import React from 'react';
22

33
export default React.createContext({
4-
bottom: true,
4+
atBottom: true,
5+
atEnd: true,
6+
atTop: true,
7+
mode: 'bottom',
58
handleUpdate: () => 0,
69
scrollToBottom: () => 0,
10+
scrollToEnd: () => 0,
11+
scrollToTop: () => 0,
712
scrollTop: null,
813
setTarget: () => 0,
9-
target: null
14+
target: null,
15+
threshold: 10
1016
});

0 commit comments

Comments
 (0)