Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onScrolled callback #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# react-overflow

A React component that detects when it's overflowed by its content.
A React component that detects when it's overflowed by its content. It can also tell you if you are at the edge of the scroll area.

## Installation

Expand All @@ -12,6 +12,8 @@ npm install --save react-overflow

## Usage

Using the `onOverFlowChange` callback:

```jsx
import { OverflowDetector } from 'react-overflow';

Expand All @@ -27,6 +29,30 @@ function handleOverflowChange(isOverflowed) {
</OverflowDetector>
```

Using the `onScrolled` callback:

```jsx
import { OverflowDetector } from 'react-overflow';

class TextWithArrows extends React.Component {
state = { atTop: true, atBottom: true, atLeft: true, atRight: true };
handleScrolled = pos => this.setState(pos);
render() {
const { text } = this.props;
const { atTop, atBottom, atLeft, atRight } = this.state;
return (
<ConstrainedTextDiv>
<OverflowDetector onScrolled={this.handleScrolled}>
{!atTop && <UpArrow />}
{text}
{!atBottom && <DownArrow />}
</OverflowDetector>
</ConstrainedTextDiv>
);
}
}
```

## License

[MIT](https://github.com/nickuraltsev/react-overflow/blob/master/LICENSE)
41 changes: 40 additions & 1 deletion src/OverflowDetector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ export default class OverflowDetector extends Component {
super(props);
this.isOverflowed = false;
this.domElement = null;
this.scrollState = {
atTop: true,
atBottom: true,
atLeft: true,
atRight: true,
};
this.setDOMElement = this.setDOMElement.bind(this);
this.checkOverflow = this.checkOverflow.bind(this);
this.handleScroll = this.handleScroll.bind(this);
}

componentDidMount() {
Expand All @@ -43,16 +50,48 @@ export default class OverflowDetector extends Component {
if (this.props.onOverflowChange) {
this.props.onOverflowChange(isOverflowed);
}
if (!isOverflowed) {
this.handleScroll();
}
}
}

handleScroll() {
const {
clientHeight,
clientWidth,
scrollTop,
scrollLeft,
scrollHeight,
scrollWidth,
} = this.domElement;
const atTop = scrollTop === 0;
const atBottom = scrollTop + clientHeight === scrollHeight;
const atLeft = scrollLeft === 0;
const atRight = scrollLeft + clientWidth === scrollWidth;
const s = this.scrollState;
if (
s.atTop !== atTop ||
s.atBottom !== atBottom ||
s.atLeft !== atLeft ||
s.atRight !== atRight
) {
const scrollState = { atTop, atBottom, atLeft, atRight };
this.scrollState = scrollState;
this.props.onScrolled(scrollState);
}
}

render() {
const { style, className, children } = this.props;
const { style, className, children, onScrolled } = this.props;
const onScroll =
this.isOverflowed && onScrolled ? this.handleScroll : undefined;
return (
<div
ref={this.setDOMElement}
style={{ ...style, position: 'relative' }}
className={className}
onScroll={onScroll}
>
{children}
<ResizeDetector onResize={this.checkOverflow} />
Expand Down