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

Remove style and className, and monitor children instead #13

Merged
merged 1 commit into from
Jun 16, 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
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}>
<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);
}
});