Skip to content

Commit

Permalink
Remove style and className, and monitor children instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Jun 15, 2015
1 parent 48e5ca2 commit 34f8808
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
6 changes: 3 additions & 3 deletions example/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var Example = React.createClass({
<div>
<p className='msg'>{this.state.msg}</p>
<div className='before'></div>
<div className='sensor'>
<VisibilitySensor containment={this.props.containment} onChange={this.onChange} />
</div>
<VisibilitySensor containment={this.props.containment} onChange={this.onChange}>

This comment has been minimized.

Copy link
@kof

kof Jun 16, 2015

Collaborator

Good Idea, please increase major version next time, because this broke my code without I noticed that.

This comment has been minimized.

Copy link
@gaearon

gaearon Jun 16, 2015

Author Contributor

Right.. Usually it's up to the maintainer to increase the version on breaking changes (they might want to accumulate them in master for a while), but I should have articulated that it's a breaking one better.

<div className='sensor' />
</VisibilitySensor>
<div className='after'></div>
</div>
);
Expand Down
63 changes: 47 additions & 16 deletions tests/visibility-sensor-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@ var React = require('react/addons');
var assert = require('assert');

describe('VisibilitySensor', function () {
var elem;
var node;

beforeEach(function () {
elem = document.createElement('div');
document.body.appendChild(elem);
node = document.createElement('div');
document.body.appendChild(node);
});

afterEach(function () {
React.unmountComponentAtNode(elem);
document.body.removeChild(elem);
React.unmountComponentAtNode(node);
document.body.removeChild(node);
});

var VisibilitySensor = React.createFactory(require('../visibility-sensor.js'));

it('should notify of changes to visibility', function (done) {
var component;
it('should notify of changes to visibility when parent moves', function (done) {
var firstTime = true;
var onChange = function (isVisible) {
// by default we expect the sensor to be visible
if (firstTime) {
firstTime = false;
assert.equal(isVisible, true, 'Component starts out visible');
elem.setAttribute('style', 'position:absolute; width:100px; left:-101px');
node.setAttribute('style', 'position:absolute; width:100px; left:-101px');
}
// after moving the sensor it should be not visible anymore
else {
Expand All @@ -37,28 +36,60 @@ describe('VisibilitySensor', function () {
}
};

component = (
var element = (
<VisibilitySensor delay={10} onChange={onChange} />
);

React.render(component, elem);
React.render(element, node);
});

it('should notify of changes to visibility when child moves', function (done) {
var firstTime = true;
var style = {};
var onChange = function (isVisible) {
// by default we expect the sensor to be visible
if (firstTime) {
firstTime = false;
assert.equal(isVisible, true, 'Component starts out visible');
style = {
position: 'absolute',
width: 100,
left: -101
};
React.render(getElement(style), node);
}
// after moving the sensor it should be not visible anymore
else {
assert.equal(isVisible, false, 'Component has moved out of the visible viewport');
done();
}
};

function getElement(style) {
return (
<VisibilitySensor delay={10} onChange={onChange}>
<div style={style} />
</VisibilitySensor>
);
}

React.render(getElement(), node);
});


it('should notify of changes to visibility', function (done) {
var component;
var onChange = function (isVisible) {
assert.equal(isVisible, true, 'Component starts out visible');
done();
};
component = (
var element = (
<VisibilitySensor delay={1} onChange={onChange} />
);

React.render(component, elem);
React.render(element, node);
});

it('should not notify when deactivated', function (done) {
var component;
var wasCallbackCalled = false;
var onChange = function (isVisible) {
wasCallbackCalled = true;
Expand All @@ -69,10 +100,10 @@ describe('VisibilitySensor', function () {
done();
}, 20);

component = (
var element = (
<VisibilitySensor active={false} delay={1} onChange={onChange} />
);

React.render(component, elem);
React.render(element, node);
});
});
11 changes: 4 additions & 7 deletions visibility-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ module.exports = React.createClass({
active: React.PropTypes.bool,
delay: React.PropTypes.number,
containment: React.PropTypes.instanceOf(Element),
className: React.PropTypes.string,
style: React.PropTypes.object
children: React.PropTypes.element
},

getDefaultProps: function () {
return {
active: true,
delay: 1000,
containment: null
containment: null,
children: React.createElement('span')
};
},

Expand Down Expand Up @@ -104,9 +104,6 @@ module.exports = React.createClass({
},

render: function () {
return React.createElement('div', {
className: this.props.className,
style: this.props.style
}, [this.props.children]);
return React.Children.only(this.props.children);
}
});

0 comments on commit 34f8808

Please sign in to comment.