Skip to content
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
8 changes: 5 additions & 3 deletions src/IntersectionObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ class Observer extends Component {
this.init();
this.subscribers.forEach(({ target }) => this.io.observe(target));
}
subscribe(target, onEnter, onLeave) {
subscribe(target, onEnter, onLeave, onEveryThreshold) {
if (!this.io) this.init();
this.subscribers = this.subscribers.concat({
target,
onEnter,
onLeave
onLeave,
onEveryThreshold,
});
this.io.observe(target);
}
Expand All @@ -67,7 +68,8 @@ class Observer extends Component {
const instance = this.subscribers.find(
({ target }) => target === entry.target
);
this.isComing(entry, observer) &&
instance.onEveryThreshold && instance.onEveryThreshold(entry.intersectionRatio);
this.isComing(entry, observer, instance) &&
instance.onEnter &&
instance.onEnter(entry);
this.isLeaving(entry, observer) &&
Expand Down
13 changes: 10 additions & 3 deletions src/Observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,37 @@ class Observable extends Component {
this.onRef = this.onRef.bind(this);
this.onEnter = this.onEnter.bind(this);
this.onLeave = this.onLeave.bind(this);
this.onEveryThreshold = this.onEveryThreshold.bind(this);
}
onRef(ref) {
this.ref = ref;
}
onEnter() {
this.setState({ isVisible: true }, () => {
const { onLeave, onEnter, children, onceOnly, ...props } = this.props;
const { onLeave, onEnter, children, onceOnly, onEveryThreshold, ...props } = this.props;
this.props.onEnter && this.props.onEnter(props);
this.props.onceOnly && this.unsubscribe(this.ref);
});
}
onLeave() {
const { onLeave, onEnter, children, onceOnly, ...rest } = this.props;
const { onLeave, onEnter, children, onceOnly, onEveryThreshold, ...rest } = this.props;
this.setState({ isVisible: false }, () => {
this.props.onLeave && this.props.onLeave(rest);
});
}
onEveryThreshold(intersectionRatio){
const { onLeave, onEnter, children, onceOnly, onEveryThreshold, ...rest } = this.props;
this.setState({ isVisible: true }, () => {
this.props.onEveryThreshold && this.props.onEveryThreshold({intersectionRatio, ...rest});
});
}
componentDidMount() {
if (!this.context.subscribe) {
throw new Error(
"Expected Observable to be mounted within IntersectionObserver"
);
}
this.context.subscribe(this.ref, this.onLeave, this.onEnter);
this.context.subscribe(this.ref, this.onLeave, this.onEnter, this.onEveryThreshold);
}
render() {
const { isVisible } = this.state;
Expand Down