From 19c08434caa24ccce7cbad63413426268995e4c1 Mon Sep 17 00:00:00 2001 From: MarceloLudovino Date: Tue, 13 Apr 2021 14:16:08 -0300 Subject: [PATCH] fix detect visibility without height --- tests/visibility-sensor-spec.jsx | 18 ++++++++++++++++++ visibility-sensor.js | 14 ++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/visibility-sensor-spec.jsx b/tests/visibility-sensor-spec.jsx index 742dad1..b4a8a79 100644 --- a/tests/visibility-sensor-spec.jsx +++ b/tests/visibility-sensor-spec.jsx @@ -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 = ( + +
+ + ); + + ReactDOM.render(element, node); + }); + it("should not return visible if the sensor is hidden", function(done) { var firstTime = true; var onChange = function(isVisible) { diff --git a/visibility-sensor.js b/visibility-sensor.js index 067b3c4..72fbcac 100644 --- a/visibility-sensor.js +++ b/visibility-sensor.js @@ -33,7 +33,8 @@ export default class VisibilitySensor extends React.Component { delayedCall: false, offset: {}, containment: null, - children: + children: , + requireContentsSize: true }; static propTypes = { @@ -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) { @@ -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 &&