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 partialVisiblity property. #15

Merged
merged 1 commit into from
Jun 17, 2015
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Props

- `onChange`: callback for whenever the element changes from being within the window viewport or not. Function is called with 1 argument `(isVisible: boolean)`
- `active`: (default `true`) boolean flag for enabling / disabling the sensor. When `active !== true` the sensor will not fire the `onChange` callback.
- `partialVisibility`: (default `false`) consider element visible if only part of it is visible.
- `delay`: (default `1000`) integer, number of milliseconds between checking the element's position in relation the the window viewport. Making this number too low will have a negative impact on performance.
- `containment`: (optional) element to use as a viewport when checking visibility. Default behaviour is to use the browser window as viewport.
- `className`: (optional) className for the sensor element.
Expand Down
26 changes: 21 additions & 5 deletions visibility-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = React.createClass({
propTypes: {
onChange: React.PropTypes.func.isRequired,
active: React.PropTypes.bool,
partialVisibility: React.PropTypes.bool,
delay: React.PropTypes.number,
containment: React.PropTypes.instanceOf(Element),
children: React.PropTypes.element
Expand All @@ -16,6 +17,7 @@ module.exports = React.createClass({
getDefaultProps: function () {
return {
active: true,
partialVisibility: false,
delay: 1000,
containment: null,
children: React.createElement('span')
Expand Down Expand Up @@ -84,13 +86,27 @@ module.exports = React.createClass({
right: rect.right <= containmentRect.right
};

var isVisible = (
visibilityRect.top &&
visibilityRect.left &&
visibilityRect.bottom &&
visibilityRect.right
var fullVisible = (
visibilityRect.top &&
visibilityRect.left &&
visibilityRect.bottom &&
visibilityRect.right
);

var partialVertical =
(rect.top >= containmentRect.top && rect.top <= containmentRect.bottom)
|| (rect.bottom >= containmentRect.top && rect.bottom <= containmentRect.bottom);

var partialHorizontal =
(rect.left >= containmentRect.left && rect.left <= containmentRect.right)
|| (rect.right >= containmentRect.left && rect.right <= containmentRect.right);

var partialVisible = partialVertical && partialHorizontal;

var isVisible = this.props.partialVisibility
? partialVisible
: fullVisible;

// notify the parent when the value changes
if (this.state.isVisible !== isVisible) {
this.setState({
Expand Down