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
26 changes: 26 additions & 0 deletions tests/visibility-sensor-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,32 @@ describe('VisibilitySensor', function () {
ReactDOM.render(element, node);
});

it('with the stayVisible prop it stays visible once it has been seen once', function (done) {
var firstTime = true;
let callCount = 0;
node.setAttribute('style', 'position:absolute; width:100px; height:100px; top:-49px');

var onChange = function (isVisible) {
callCount++;
assert.equal(isVisible, true, 'Component starts out visible');
node.setAttribute('style', 'position:absolute; width:100px; height:100px; top:-51px');
setTimeout(function () {
assert.equal(
callCount,
1,
'Component is out of the viewport but stays visible and onChange is not called again'
);
done();
}, 200)
}

var element = (
<VisibilitySensor onChange={onChange} offset={{top:-50}} intervalDelay={10} stayVisible={true} />
);

ReactDOM.render(element, node);
});

it('should call child function with state', function (done) {
var wasChildrenCalled = false;
var children = function (props) {
Expand Down
7 changes: 7 additions & 0 deletions visibility-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = createReactClass({
PropTypes.bool,
PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
]),
stayVisible: PropTypes.bool,
delayedCall: PropTypes.bool,
offset: PropTypes.oneOfType([
PropTypes.shape({
Expand Down Expand Up @@ -82,6 +83,7 @@ module.exports = createReactClass({
return {
active: true,
partialVisibility: false,
stayVisible: false,
minTopValue: 0,
scrollCheck: false,
scrollDelay: 250,
Expand Down Expand Up @@ -229,6 +231,11 @@ module.exports = createReactClass({
return this.state;
}

if (this.state.isVisible === true && this.props.stayVisible === true) {
this.stopWatching()
return this.state;
};

rect = el.getBoundingClientRect();

if (this.props.containment) {
Expand Down