Skip to content

fix detect visibility without height #183

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions tests/visibility-sensor-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,24 @@ describe("VisibilitySensor", function() {
ReactDOM.render(element, node);
});

it("should return visible if the sensor has no size but contents size is not required", function (done) {
var firstTime = true;
var onChange = function (isVisible) {
if (firstTime) {
assert.equal(isVisible, true, "Component is visible");
done();
}
};

var element = (
<VisibilitySensor onChange={onChange} requireContentsSize={false}>
<div style={{ height: "0px", width: "0px" }} />
</VisibilitySensor>
);

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

it("should not return visible if the sensor is hidden", function(done) {
var firstTime = true;
var onChange = function(isVisible) {
Expand Down
14 changes: 10 additions & 4 deletions visibility-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default class VisibilitySensor extends React.Component {
delayedCall: false,
offset: {},
containment: null,
children: <span />
children: <span />,
requireContentsSize: true
};

static propTypes = {
Expand Down Expand Up @@ -70,7 +71,8 @@ export default class VisibilitySensor extends React.Component {
? PropTypes.instanceOf(window.Element)
: PropTypes.any,
children: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
minTopValue: PropTypes.number
minTopValue: PropTypes.number,
requireContentsSize: PropTypes.bool
};

constructor(props) {
Expand Down Expand Up @@ -268,15 +270,19 @@ export default class VisibilitySensor extends React.Component {
// https://github.com/joshwnj/react-visibility-sensor/pull/114
const hasSize = rect.height > 0 && rect.width > 0;

// Only register the rect as visible if it has size, unless requireContentsSize is false
const shouldDetectSize = hasSize || !this.props.requireContentsSize;


let isVisible =
hasSize &&
shouldDetectSize &&
visibilityRect.top &&
visibilityRect.left &&
visibilityRect.bottom &&
visibilityRect.right;

// check for partial visibility
if (hasSize && this.props.partialVisibility) {
if (shouldDetectSize && this.props.partialVisibility) {
let partialVisible =
rect.top <= containmentRect.bottom &&
rect.bottom >= containmentRect.top &&
Expand Down