@futurejj/react-native-visibility-sensor 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +70 -0
- package/lib/commonjs/VisibilitySensor.js +106 -0
- package/lib/commonjs/VisibilitySensor.js.map +1 -0
- package/lib/commonjs/index.js +14 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/visibilitySensor.types.js +6 -0
- package/lib/commonjs/visibilitySensor.types.js.map +1 -0
- package/lib/module/VisibilitySensor.js +97 -0
- package/lib/module/VisibilitySensor.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/visibilitySensor.types.js +2 -0
- package/lib/module/visibilitySensor.types.js.map +1 -0
- package/lib/typescript/VisibilitySensor.d.ts +5 -0
- package/lib/typescript/VisibilitySensor.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/visibilitySensor.types.d.ts +26 -0
- package/lib/typescript/visibilitySensor.types.d.ts.map +1 -0
- package/package.json +169 -0
- package/src/VisibilitySensor.tsx +142 -0
- package/src/index.ts +13 -0
- package/src/visibilitySensor.types.ts +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Andrea De Luca
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @futurejj/react-native-visibility-sensor
|
|
2
|
+
|
|
3
|
+
🔍 Component visibility sensor wrapper to sense whether or not a component is in viewport.
|
|
4
|
+
|
|
5
|
+
It works on **Android, iOS & Web** .
|
|
6
|
+
|
|
7
|
+
<table border="0">
|
|
8
|
+
<tr>
|
|
9
|
+
<td style="width: 50%; border-color: transparent;">
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
#### using npm
|
|
14
|
+
```sh
|
|
15
|
+
npm install @futurejj/react-native-visibility-sensor
|
|
16
|
+
```
|
|
17
|
+
#### using Yarn
|
|
18
|
+
```sh
|
|
19
|
+
yarn add @futurejj/react-native-visibility-sensor
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { VisibilitySensor } from '@futurejj/react-native-visibility-sensor';
|
|
27
|
+
|
|
28
|
+
const [isInView, setIsInView] = useState(false)
|
|
29
|
+
|
|
30
|
+
const checkVisible = (isVisible: boolean) => {
|
|
31
|
+
if (isVisible){
|
|
32
|
+
setIsInView(isVisible)
|
|
33
|
+
} else {
|
|
34
|
+
setIsInView(isVisible)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
<ScrollView>
|
|
39
|
+
<VisibilitySensor onChange={(isVisible) => this.checkVisible(isVisible)}>
|
|
40
|
+
<View style={[styles.item, {backgroundColor: isInView ? 'yellow' : '#f9c2ff'}]}>
|
|
41
|
+
<Text>This View is currently visible? {isInView ? 'yes': 'no'}</Text>
|
|
42
|
+
</View>
|
|
43
|
+
</VisibilitySensor>
|
|
44
|
+
</ScrollView>
|
|
45
|
+
|
|
46
|
+
// ...
|
|
47
|
+
```
|
|
48
|
+
</td>
|
|
49
|
+
|
|
50
|
+
<td style="width: 50%; min-width: 200px; border-color: transparent; text-align: center;">
|
|
51
|
+
|
|
52
|
+
## Demo
|
|
53
|
+
|
|
54
|
+
<img src="https://drive.google.com/uc?export=view&id=1jjU2o1SnUEMvFR9MMqQHOaGtPOVdW7M-" style="width: 250px; max-width: 100%; height: auto" title="The original legends in a demo." alt="demo"/>
|
|
55
|
+
</td>
|
|
56
|
+
</tr>
|
|
57
|
+
</table>
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
|
|
62
|
+
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
MIT
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
10
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
11
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
12
|
+
function useInterval(callback, delay) {
|
|
13
|
+
const savedCallback = (0, _react.useRef)(callback);
|
|
14
|
+
(0, _react.useEffect)(() => {
|
|
15
|
+
savedCallback.current = callback;
|
|
16
|
+
}, [callback]);
|
|
17
|
+
(0, _react.useEffect)(() => {
|
|
18
|
+
if (delay === null || delay === undefined) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const id = setInterval(() => savedCallback.current(), delay);
|
|
22
|
+
return () => clearInterval(id);
|
|
23
|
+
}, [delay]);
|
|
24
|
+
}
|
|
25
|
+
const VisibilitySensor = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
26
|
+
const {
|
|
27
|
+
onChange,
|
|
28
|
+
disabled = false,
|
|
29
|
+
triggerOnce = false,
|
|
30
|
+
delay,
|
|
31
|
+
threshold = {},
|
|
32
|
+
children,
|
|
33
|
+
...rest
|
|
34
|
+
} = props;
|
|
35
|
+
const localRef = (0, _react.useRef)(null);
|
|
36
|
+
(0, _react.useImperativeHandle)(ref, () => ({
|
|
37
|
+
getInnerRef: () => localRef.current
|
|
38
|
+
}));
|
|
39
|
+
const [rectDimensions, setRectDimensions] = (0, _react.useState)({
|
|
40
|
+
rectTop: 0,
|
|
41
|
+
rectBottom: 0,
|
|
42
|
+
rectLeft: 0,
|
|
43
|
+
rectRight: 0,
|
|
44
|
+
rectWidth: 0,
|
|
45
|
+
rectHeight: 0
|
|
46
|
+
});
|
|
47
|
+
const [lastValue, setLastValue] = (0, _react.useState)(undefined);
|
|
48
|
+
const [active, setActive] = (0, _react.useState)(false);
|
|
49
|
+
const measureInnerView = () => {
|
|
50
|
+
var _localRef$current;
|
|
51
|
+
if (!active) return;
|
|
52
|
+
(_localRef$current = localRef.current) === null || _localRef$current === void 0 || _localRef$current.measure((_x, _y, width, height, pageX, pageY) => {
|
|
53
|
+
const dimensions = {
|
|
54
|
+
rectTop: pageY,
|
|
55
|
+
rectBottom: pageY + height,
|
|
56
|
+
rectLeft: pageX,
|
|
57
|
+
rectRight: pageX + width,
|
|
58
|
+
rectWidth: width,
|
|
59
|
+
rectHeight: height
|
|
60
|
+
};
|
|
61
|
+
if (rectDimensions.rectTop !== dimensions.rectTop || rectDimensions.rectBottom !== dimensions.rectBottom || rectDimensions.rectLeft !== dimensions.rectLeft || rectDimensions.rectRight !== dimensions.rectRight || rectDimensions.rectWidth !== dimensions.rectWidth || rectDimensions.rectHeight !== dimensions.rectHeight) {
|
|
62
|
+
setRectDimensions(dimensions);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
useInterval(measureInnerView, delay || 100);
|
|
67
|
+
const startWatching = (0, _react.useCallback)(() => {
|
|
68
|
+
if (!active) setActive(true);
|
|
69
|
+
}, [active]);
|
|
70
|
+
const stopWatching = (0, _react.useCallback)(() => {
|
|
71
|
+
if (active) setActive(false);
|
|
72
|
+
}, [active]);
|
|
73
|
+
(0, _react.useEffect)(() => {
|
|
74
|
+
if (!disabled) {
|
|
75
|
+
startWatching();
|
|
76
|
+
}
|
|
77
|
+
return () => {
|
|
78
|
+
stopWatching();
|
|
79
|
+
};
|
|
80
|
+
}, [disabled, startWatching, stopWatching]);
|
|
81
|
+
(0, _react.useEffect)(() => {
|
|
82
|
+
const window = _reactNative.Dimensions.get('window');
|
|
83
|
+
const isVisible = rectDimensions.rectTop + (threshold.top || 0) <= window.height &&
|
|
84
|
+
// Top edge is within the bottom of the window
|
|
85
|
+
rectDimensions.rectBottom - (threshold.bottom || 0) >= 0 &&
|
|
86
|
+
// Bottom edge is within the top of the window
|
|
87
|
+
rectDimensions.rectLeft + (threshold.left || 0) <= window.width &&
|
|
88
|
+
// Left edge is within the right of the window
|
|
89
|
+
rectDimensions.rectRight - (threshold.right || 0) >= 0; // Right edge is within the left of the window
|
|
90
|
+
|
|
91
|
+
if (lastValue !== isVisible) {
|
|
92
|
+
setLastValue(isVisible);
|
|
93
|
+
onChange(isVisible);
|
|
94
|
+
if (isVisible && triggerOnce) {
|
|
95
|
+
stopWatching();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
99
|
+
}, [rectDimensions, lastValue]);
|
|
100
|
+
return /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({
|
|
101
|
+
ref: localRef
|
|
102
|
+
}, rest), children);
|
|
103
|
+
});
|
|
104
|
+
var _default = VisibilitySensor;
|
|
105
|
+
exports.default = _default;
|
|
106
|
+
//# sourceMappingURL=VisibilitySensor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","_extends","assign","bind","target","i","arguments","length","source","apply","useInterval","callback","delay","savedCallback","useRef","useEffect","current","undefined","id","setInterval","clearInterval","VisibilitySensor","forwardRef","props","ref","onChange","disabled","triggerOnce","threshold","children","rest","localRef","useImperativeHandle","getInnerRef","rectDimensions","setRectDimensions","useState","rectTop","rectBottom","rectLeft","rectRight","rectWidth","rectHeight","lastValue","setLastValue","active","setActive","measureInnerView","_localRef$current","measure","_x","_y","width","height","pageX","pageY","dimensions","startWatching","useCallback","stopWatching","window","Dimensions","isVisible","top","bottom","left","right","createElement","View","_default","exports"],"sourceRoot":"../../src","sources":["VisibilitySensor.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAQA,IAAAC,YAAA,GAAAD,OAAA;AAAiE,SAAAE,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAJ,wBAAAQ,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAd,GAAA,EAAAW,GAAA,SAAAI,IAAA,GAAAR,qBAAA,G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GAAG,CAAC,QAAQ,CAAC;IACnD,MAAMyE,SAAkB,GACtB5B,cAAc,CAACG,OAAO,IAAIT,SAAS,CAACmC,GAAG,IAAI,CAAC,CAAC,IAAIH,MAAM,CAACP,MAAM;IAAI;IAClEnB,cAAc,CAACI,UAAU,IAAIV,SAAS,CAACoC,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC;IAAI;IAC5D9B,cAAc,CAACK,QAAQ,IAAIX,SAAS,CAACqC,IAAI,IAAI,CAAC,CAAC,IAAIL,MAAM,CAACR,KAAK;IAAI;IACnElB,cAAc,CAACM,SAAS,IAAIZ,SAAS,CAACsC,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;;IAE1D,IAAIvB,SAAS,KAAKmB,SAAS,EAAE;MAC3BlB,YAAY,CAACkB,SAAS,CAAC;MACvBrC,QAAQ,CAACqC,SAAS,CAAC;MACnB,IAAIA,SAAS,IAAInC,WAAW,EAAE;QAC5BgC,YAAY,CAAC,CAAC;MAChB;IACF;IACA;EACF,CAAC,EAAE,CAACzB,cAAc,EAAES,SAAS,CAAC,CAAC;EAE/B,oBACEpE,MAAA,CAAAW,OAAA,CAAAiF,aAAA,CAACzF,YAAA,CAAA0F,IAAI,EAAAnE,QAAA;IAACuB,GAAG,EAAEO;EAAS,GAAKD,IAAI,GAC1BD,QACG,CAAC;AAEX,CACF,CAAC;AAAC,IAAAwC,QAAA,GAEahD,gBAAgB;AAAAiD,OAAA,CAAApF,OAAA,GAAAmF,QAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "VisibilitySensor", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _VisibilitySensor.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
var _VisibilitySensor = _interopRequireDefault(require("./VisibilitySensor"));
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_VisibilitySensor","_interopRequireDefault","require","obj","__esModule","default"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;AAAA,IAAAA,iBAAA,GAAAC,sBAAA,CAAAC,OAAA;AAAkD,SAAAD,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["visibilitySensor.types.ts"],"mappings":""}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
import React, { useCallback, useEffect, useRef, useState, forwardRef, useImperativeHandle } from 'react';
|
|
3
|
+
import { Dimensions, View } from 'react-native';
|
|
4
|
+
function useInterval(callback, delay) {
|
|
5
|
+
const savedCallback = useRef(callback);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
savedCallback.current = callback;
|
|
8
|
+
}, [callback]);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
if (delay === null || delay === undefined) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const id = setInterval(() => savedCallback.current(), delay);
|
|
14
|
+
return () => clearInterval(id);
|
|
15
|
+
}, [delay]);
|
|
16
|
+
}
|
|
17
|
+
const VisibilitySensor = /*#__PURE__*/forwardRef((props, ref) => {
|
|
18
|
+
const {
|
|
19
|
+
onChange,
|
|
20
|
+
disabled = false,
|
|
21
|
+
triggerOnce = false,
|
|
22
|
+
delay,
|
|
23
|
+
threshold = {},
|
|
24
|
+
children,
|
|
25
|
+
...rest
|
|
26
|
+
} = props;
|
|
27
|
+
const localRef = useRef(null);
|
|
28
|
+
useImperativeHandle(ref, () => ({
|
|
29
|
+
getInnerRef: () => localRef.current
|
|
30
|
+
}));
|
|
31
|
+
const [rectDimensions, setRectDimensions] = useState({
|
|
32
|
+
rectTop: 0,
|
|
33
|
+
rectBottom: 0,
|
|
34
|
+
rectLeft: 0,
|
|
35
|
+
rectRight: 0,
|
|
36
|
+
rectWidth: 0,
|
|
37
|
+
rectHeight: 0
|
|
38
|
+
});
|
|
39
|
+
const [lastValue, setLastValue] = useState(undefined);
|
|
40
|
+
const [active, setActive] = useState(false);
|
|
41
|
+
const measureInnerView = () => {
|
|
42
|
+
var _localRef$current;
|
|
43
|
+
if (!active) return;
|
|
44
|
+
(_localRef$current = localRef.current) === null || _localRef$current === void 0 || _localRef$current.measure((_x, _y, width, height, pageX, pageY) => {
|
|
45
|
+
const dimensions = {
|
|
46
|
+
rectTop: pageY,
|
|
47
|
+
rectBottom: pageY + height,
|
|
48
|
+
rectLeft: pageX,
|
|
49
|
+
rectRight: pageX + width,
|
|
50
|
+
rectWidth: width,
|
|
51
|
+
rectHeight: height
|
|
52
|
+
};
|
|
53
|
+
if (rectDimensions.rectTop !== dimensions.rectTop || rectDimensions.rectBottom !== dimensions.rectBottom || rectDimensions.rectLeft !== dimensions.rectLeft || rectDimensions.rectRight !== dimensions.rectRight || rectDimensions.rectWidth !== dimensions.rectWidth || rectDimensions.rectHeight !== dimensions.rectHeight) {
|
|
54
|
+
setRectDimensions(dimensions);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
useInterval(measureInnerView, delay || 100);
|
|
59
|
+
const startWatching = useCallback(() => {
|
|
60
|
+
if (!active) setActive(true);
|
|
61
|
+
}, [active]);
|
|
62
|
+
const stopWatching = useCallback(() => {
|
|
63
|
+
if (active) setActive(false);
|
|
64
|
+
}, [active]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!disabled) {
|
|
67
|
+
startWatching();
|
|
68
|
+
}
|
|
69
|
+
return () => {
|
|
70
|
+
stopWatching();
|
|
71
|
+
};
|
|
72
|
+
}, [disabled, startWatching, stopWatching]);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
const window = Dimensions.get('window');
|
|
75
|
+
const isVisible = rectDimensions.rectTop + (threshold.top || 0) <= window.height &&
|
|
76
|
+
// Top edge is within the bottom of the window
|
|
77
|
+
rectDimensions.rectBottom - (threshold.bottom || 0) >= 0 &&
|
|
78
|
+
// Bottom edge is within the top of the window
|
|
79
|
+
rectDimensions.rectLeft + (threshold.left || 0) <= window.width &&
|
|
80
|
+
// Left edge is within the right of the window
|
|
81
|
+
rectDimensions.rectRight - (threshold.right || 0) >= 0; // Right edge is within the left of the window
|
|
82
|
+
|
|
83
|
+
if (lastValue !== isVisible) {
|
|
84
|
+
setLastValue(isVisible);
|
|
85
|
+
onChange(isVisible);
|
|
86
|
+
if (isVisible && triggerOnce) {
|
|
87
|
+
stopWatching();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
91
|
+
}, [rectDimensions, lastValue]);
|
|
92
|
+
return /*#__PURE__*/React.createElement(View, _extends({
|
|
93
|
+
ref: localRef
|
|
94
|
+
}, rest), children);
|
|
95
|
+
});
|
|
96
|
+
export default VisibilitySensor;
|
|
97
|
+
//# sourceMappingURL=VisibilitySensor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","useCallback","useEffect","useRef","useState","forwardRef","useImperativeHandle","Dimensions","View","useInterval","callback","delay","savedCallback","current","undefined","id","setInterval","clearInterval","VisibilitySensor","props","ref","onChange","disabled","triggerOnce","threshold","children","rest","localRef","getInnerRef","rectDimensions","setRectDimensions","rectTop","rectBottom","rectLeft","rectRight","rectWidth","rectHeight","lastValue","setLastValue","active","setActive","measureInnerView","_localRef$current","measure","_x","_y","width","height","pageX","pageY","dimensions","startWatching","stopWatching","window","get","isVisible","top","bottom","left","right","createElement","_extends"],"sourceRoot":"../../src","sources":["VisibilitySensor.tsx"],"mappings":";AAAA,OAAOA,KAAK,IACVC,WAAW,EACXC,SAAS,EACTC,MAAM,EACNC,QAAQ,EACRC,UAAU,EACVC,mBAAmB,QACd,OAAO;AACd,SAASC,UAAU,EAAmBC,IAAI,QAAQ,cAAc;AAOhE,SAASC,WAAWA,CAACC,QAAoB,EAAEC,KAAoB,EAAE;EAC/D,MAAMC,aAAa,GAAGT,MAAM,CAACO,QAAQ,CAAC;EAEtCR,SAAS,CAAC,MAAM;IACdU,aAAa,CAACC,OAAO,GAAGH,QAAQ;EAClC,CAAC,EAAE,CAACA,QAAQ,CAAC,CAAC;EAEdR,SAAS,CAAC,MAAM;IACd,IAAIS,KAAK,KAAK,IAAI,IAAIA,KAAK,KAAKG,SAAS,EAAE;MACzC;IACF;IAEA,MAAMC,EAAE,GAAGC,WAAW,CAAC,MAAMJ,aAAa,CAACC,OAAO,CAAC,CAAC,EAAEF,KAAK,CAAC;IAC5D,OAAO,MAAMM,aAAa,CAACF,EAAE,CAAC;EAChC,CAAC,EAAE,CAACJ,KAAK,CAAC,CAAC;AACb;AAEA,MAAMO,gBAAgB,gBAAGb,UAAU,CACjC,CAACc,KAAK,EAAEC,GAAG,KAAK;EACd,MAAM;IACJC,QAAQ;IACRC,QAAQ,GAAG,KAAK;IAChBC,WAAW,GAAG,KAAK;IACnBZ,KAAK;IACLa,SAAS,GAAG,CAAC,CAAC;IACdC,QAAQ;IACR,GAAGC;EACL,CAAC,GAAGP,KAAK;EAET,MAAMQ,QAAQ,GAAGxB,MAAM,CAAO,IAAI,CAAC;EAEnCG,mBAAmB,CAACc,GAAG,EAAE,OAAO;IAC9BQ,WAAW,EAAEA,CAAA,KAAMD,QAAQ,CAACd;EAC9B,CAAC,CAAC,CAAC;EAEH,MAAM,CAAC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AAIhC,WAAW,EAAE;QAC5B6B,YAAY,CAAC,CAAC;MAChB;IACF;IACA;EACF,CAAC,EAAE,CAACvB,cAAc,EAAEQ,SAAS,CAAC,CAAC;EAE/B,oBACErC,KAAA,CAAA4D,aAAA,CAACpD,IAAI,EAAAqD,QAAA;IAACzC,GAAG,EAAEO;EAAS,GAAKD,IAAI,GAC1BD,QACG,CAAC;AAEX,CACF,CAAC;AAED,eAAeP,gBAAgB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["VisibilitySensor"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,OAAOA,gBAAgB,MAAM,oBAAoB;AAOjD,SAASA,gBAAgB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["visibilitySensor.types.ts"],"mappings":""}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { VisibilitySensorRef, VisibilitySensorProps } from './visibilitySensor.types';
|
|
3
|
+
declare const VisibilitySensor: React.ForwardRefExoticComponent<VisibilitySensorProps & React.RefAttributes<VisibilitySensorRef>>;
|
|
4
|
+
export default VisibilitySensor;
|
|
5
|
+
//# sourceMappingURL=VisibilitySensor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VisibilitySensor.d.ts","sourceRoot":"","sources":["../../src/VisibilitySensor.tsx"],"names":[],"mappings":"AAAA,OAAO,KAON,MAAM,OAAO,CAAC;AAEf,OAAO,KAAK,EACV,mBAAmB,EACnB,qBAAqB,EAEtB,MAAM,0BAA0B,CAAC;AAmBlC,QAAA,MAAM,gBAAgB,mGA2GrB,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import VisibilitySensor from './VisibilitySensor';
|
|
2
|
+
import type { VisibilitySensorProps, VisibilitySensorRef, VisibilitySensorThreshold } from './visibilitySensor.types';
|
|
3
|
+
export { VisibilitySensor };
|
|
4
|
+
export type { VisibilitySensorProps, VisibilitySensorRef, VisibilitySensorThreshold, };
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EACV,qBAAqB,EACrB,mBAAmB,EACnB,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAC5B,YAAY,EACV,qBAAqB,EACrB,mBAAmB,EACnB,yBAAyB,GAC1B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { View, ViewProps } from 'react-native';
|
|
2
|
+
export interface VisibilitySensorProps extends ViewProps {
|
|
3
|
+
onChange: (visible: boolean) => void;
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
triggerOnce?: boolean;
|
|
6
|
+
delay?: number | undefined;
|
|
7
|
+
threshold?: VisibilitySensorThreshold;
|
|
8
|
+
}
|
|
9
|
+
export interface VisibilitySensorRef {
|
|
10
|
+
getInnerRef: () => View | null;
|
|
11
|
+
}
|
|
12
|
+
export interface VisibilitySensorThreshold {
|
|
13
|
+
top?: number;
|
|
14
|
+
bottom?: number;
|
|
15
|
+
left?: number;
|
|
16
|
+
right?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface RectDimensionsState {
|
|
19
|
+
rectTop: number;
|
|
20
|
+
rectBottom: number;
|
|
21
|
+
rectWidth: number;
|
|
22
|
+
rectHeight: number;
|
|
23
|
+
rectLeft: number;
|
|
24
|
+
rectRight: number;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=visibilitySensor.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"visibilitySensor.types.d.ts","sourceRoot":"","sources":["../../src/visibilitySensor.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,WAAW,qBAAsB,SAAQ,SAAS;IACtD,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;IACrC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,SAAS,CAAC,EAAE,yBAAyB,CAAC;CACvC;AAED,MAAM,WAAW,mBAAmB;IAClC,WAAW,EAAE,MAAM,IAAI,GAAG,IAAI,CAAC;CAChC;AAED,MAAM,WAAW,yBAAyB;IACxC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@futurejj/react-native-visibility-sensor",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A React Native wrapper to check whether a component is in the view port to track impressions and clicks",
|
|
5
|
+
"main": "lib/commonjs/index",
|
|
6
|
+
"module": "lib/module/index",
|
|
7
|
+
"types": "lib/typescript/index.d.ts",
|
|
8
|
+
"react-native": "src/index",
|
|
9
|
+
"source": "src/index",
|
|
10
|
+
"private": false,
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"lib",
|
|
14
|
+
"android",
|
|
15
|
+
"ios",
|
|
16
|
+
"cpp",
|
|
17
|
+
"*.podspec",
|
|
18
|
+
"!lib/typescript/example",
|
|
19
|
+
"!ios/build",
|
|
20
|
+
"!android/build",
|
|
21
|
+
"!android/gradle",
|
|
22
|
+
"!android/gradlew",
|
|
23
|
+
"!android/gradlew.bat",
|
|
24
|
+
"!android/local.properties",
|
|
25
|
+
"!**/__tests__",
|
|
26
|
+
"!**/__fixtures__",
|
|
27
|
+
"!**/__mocks__",
|
|
28
|
+
"!**/.*"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "jest",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
34
|
+
"prepack": "bob build",
|
|
35
|
+
"release": "release-it",
|
|
36
|
+
"example": "yarn --cwd example",
|
|
37
|
+
"bootstrap": "yarn example && yarn install"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"react-native",
|
|
41
|
+
"ios",
|
|
42
|
+
"android",
|
|
43
|
+
"visibility-sensor",
|
|
44
|
+
"visibility",
|
|
45
|
+
"sensor",
|
|
46
|
+
"visibilitysensor",
|
|
47
|
+
"in-view",
|
|
48
|
+
"inview"
|
|
49
|
+
],
|
|
50
|
+
"repository": "https://github.com/JairajJangle/react-native-visibility-sensor",
|
|
51
|
+
"author": "https://github.com/se09deluca",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/JairajJangle/react-native-visibility-sensor/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://github.com/JairajJangle/react-native-visibility-sensor#readme",
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"registry": "https://registry.npmjs.org/"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@commitlint/config-conventional": "^17.0.2",
|
|
62
|
+
"@evilmartians/lefthook": "^1.2.2",
|
|
63
|
+
"@react-native-community/eslint-config": "^3.0.2",
|
|
64
|
+
"@release-it/conventional-changelog": "^5.0.0",
|
|
65
|
+
"@types/jest": "^28.1.2",
|
|
66
|
+
"@types/react": "~17.0.21",
|
|
67
|
+
"@types/react-native": "0.70.0",
|
|
68
|
+
"commitlint": "^17.0.2",
|
|
69
|
+
"del-cli": "^5.0.0",
|
|
70
|
+
"eslint": "^8.4.1",
|
|
71
|
+
"eslint-config-prettier": "^8.5.0",
|
|
72
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
73
|
+
"jest": "^28.1.1",
|
|
74
|
+
"pod-install": "^0.1.0",
|
|
75
|
+
"prettier": "^2.0.5",
|
|
76
|
+
"react": "18.2.0",
|
|
77
|
+
"react-native": "0.72.4",
|
|
78
|
+
"react-native-builder-bob": "^0.20.0",
|
|
79
|
+
"release-it": "^15.0.0",
|
|
80
|
+
"typescript": "^5.0.2"
|
|
81
|
+
},
|
|
82
|
+
"resolutions": {
|
|
83
|
+
"@types/react": "17.0.21"
|
|
84
|
+
},
|
|
85
|
+
"peerDependencies": {
|
|
86
|
+
"react": "*",
|
|
87
|
+
"react-native": "*"
|
|
88
|
+
},
|
|
89
|
+
"engines": {
|
|
90
|
+
"node": ">= 16.0.0"
|
|
91
|
+
},
|
|
92
|
+
"packageManager": "^yarn@1.22.15",
|
|
93
|
+
"jest": {
|
|
94
|
+
"preset": "react-native",
|
|
95
|
+
"modulePathIgnorePatterns": [
|
|
96
|
+
"<rootDir>/example/node_modules",
|
|
97
|
+
"<rootDir>/lib/"
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
"commitlint": {
|
|
101
|
+
"extends": [
|
|
102
|
+
"@commitlint/config-conventional"
|
|
103
|
+
]
|
|
104
|
+
},
|
|
105
|
+
"release-it": {
|
|
106
|
+
"git": {
|
|
107
|
+
"commitMessage": "chore: release ${version}",
|
|
108
|
+
"tagName": "v${version}"
|
|
109
|
+
},
|
|
110
|
+
"npm": {
|
|
111
|
+
"publish": true,
|
|
112
|
+
"publishArgs": [
|
|
113
|
+
"--access public"
|
|
114
|
+
]
|
|
115
|
+
},
|
|
116
|
+
"github": {
|
|
117
|
+
"release": true
|
|
118
|
+
},
|
|
119
|
+
"plugins": {
|
|
120
|
+
"@release-it/conventional-changelog": {
|
|
121
|
+
"preset": "angular"
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
"eslintConfig": {
|
|
126
|
+
"root": true,
|
|
127
|
+
"extends": [
|
|
128
|
+
"@react-native-community",
|
|
129
|
+
"prettier"
|
|
130
|
+
],
|
|
131
|
+
"rules": {
|
|
132
|
+
"prettier/prettier": [
|
|
133
|
+
"error",
|
|
134
|
+
{
|
|
135
|
+
"quoteProps": "consistent",
|
|
136
|
+
"singleQuote": true,
|
|
137
|
+
"tabWidth": 2,
|
|
138
|
+
"trailingComma": "es5",
|
|
139
|
+
"useTabs": false
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
"eslintIgnore": [
|
|
145
|
+
"node_modules/",
|
|
146
|
+
"lib/"
|
|
147
|
+
],
|
|
148
|
+
"prettier": {
|
|
149
|
+
"quoteProps": "consistent",
|
|
150
|
+
"singleQuote": true,
|
|
151
|
+
"tabWidth": 2,
|
|
152
|
+
"trailingComma": "es5",
|
|
153
|
+
"useTabs": false
|
|
154
|
+
},
|
|
155
|
+
"react-native-builder-bob": {
|
|
156
|
+
"source": "src",
|
|
157
|
+
"output": "lib",
|
|
158
|
+
"targets": [
|
|
159
|
+
"commonjs",
|
|
160
|
+
"module",
|
|
161
|
+
[
|
|
162
|
+
"typescript",
|
|
163
|
+
{
|
|
164
|
+
"project": "tsconfig.build.json"
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
forwardRef,
|
|
7
|
+
useImperativeHandle,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import { Dimensions, type ScaledSize, View } from 'react-native';
|
|
10
|
+
import type {
|
|
11
|
+
VisibilitySensorRef,
|
|
12
|
+
VisibilitySensorProps,
|
|
13
|
+
RectDimensionsState,
|
|
14
|
+
} from './visibilitySensor.types';
|
|
15
|
+
|
|
16
|
+
function useInterval(callback: () => void, delay: number | null) {
|
|
17
|
+
const savedCallback = useRef(callback);
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
savedCallback.current = callback;
|
|
21
|
+
}, [callback]);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (delay === null || delay === undefined) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const id = setInterval(() => savedCallback.current(), delay);
|
|
29
|
+
return () => clearInterval(id);
|
|
30
|
+
}, [delay]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const VisibilitySensor = forwardRef<VisibilitySensorRef, VisibilitySensorProps>(
|
|
34
|
+
(props, ref) => {
|
|
35
|
+
const {
|
|
36
|
+
onChange,
|
|
37
|
+
disabled = false,
|
|
38
|
+
triggerOnce = false,
|
|
39
|
+
delay,
|
|
40
|
+
threshold = {},
|
|
41
|
+
children,
|
|
42
|
+
...rest
|
|
43
|
+
} = props;
|
|
44
|
+
|
|
45
|
+
const localRef = useRef<View>(null);
|
|
46
|
+
|
|
47
|
+
useImperativeHandle(ref, () => ({
|
|
48
|
+
getInnerRef: () => localRef.current,
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
const [rectDimensions, setRectDimensions] = useState<RectDimensionsState>({
|
|
52
|
+
rectTop: 0,
|
|
53
|
+
rectBottom: 0,
|
|
54
|
+
rectLeft: 0,
|
|
55
|
+
rectRight: 0,
|
|
56
|
+
rectWidth: 0,
|
|
57
|
+
rectHeight: 0,
|
|
58
|
+
});
|
|
59
|
+
const [lastValue, setLastValue] = useState<boolean | undefined>(undefined);
|
|
60
|
+
const [active, setActive] = useState<boolean>(false);
|
|
61
|
+
|
|
62
|
+
const measureInnerView = () => {
|
|
63
|
+
if (!active) return;
|
|
64
|
+
|
|
65
|
+
localRef.current?.measure(
|
|
66
|
+
(
|
|
67
|
+
_x: number,
|
|
68
|
+
_y: number,
|
|
69
|
+
width: number,
|
|
70
|
+
height: number,
|
|
71
|
+
pageX: number,
|
|
72
|
+
pageY: number
|
|
73
|
+
) => {
|
|
74
|
+
const dimensions = {
|
|
75
|
+
rectTop: pageY,
|
|
76
|
+
rectBottom: pageY + height,
|
|
77
|
+
rectLeft: pageX,
|
|
78
|
+
rectRight: pageX + width,
|
|
79
|
+
rectWidth: width,
|
|
80
|
+
rectHeight: height,
|
|
81
|
+
};
|
|
82
|
+
if (
|
|
83
|
+
rectDimensions.rectTop !== dimensions.rectTop ||
|
|
84
|
+
rectDimensions.rectBottom !== dimensions.rectBottom ||
|
|
85
|
+
rectDimensions.rectLeft !== dimensions.rectLeft ||
|
|
86
|
+
rectDimensions.rectRight !== dimensions.rectRight ||
|
|
87
|
+
rectDimensions.rectWidth !== dimensions.rectWidth ||
|
|
88
|
+
rectDimensions.rectHeight !== dimensions.rectHeight
|
|
89
|
+
) {
|
|
90
|
+
setRectDimensions(dimensions);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
useInterval(measureInnerView, delay || 100);
|
|
97
|
+
|
|
98
|
+
const startWatching = useCallback(() => {
|
|
99
|
+
if (!active) setActive(true);
|
|
100
|
+
}, [active]);
|
|
101
|
+
|
|
102
|
+
const stopWatching = useCallback(() => {
|
|
103
|
+
if (active) setActive(false);
|
|
104
|
+
}, [active]);
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (!disabled) {
|
|
108
|
+
startWatching();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return () => {
|
|
112
|
+
stopWatching();
|
|
113
|
+
};
|
|
114
|
+
}, [disabled, startWatching, stopWatching]);
|
|
115
|
+
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
const window: ScaledSize = Dimensions.get('window');
|
|
118
|
+
const isVisible: boolean =
|
|
119
|
+
rectDimensions.rectTop + (threshold.top || 0) <= window.height && // Top edge is within the bottom of the window
|
|
120
|
+
rectDimensions.rectBottom - (threshold.bottom || 0) >= 0 && // Bottom edge is within the top of the window
|
|
121
|
+
rectDimensions.rectLeft + (threshold.left || 0) <= window.width && // Left edge is within the right of the window
|
|
122
|
+
rectDimensions.rectRight - (threshold.right || 0) >= 0; // Right edge is within the left of the window
|
|
123
|
+
|
|
124
|
+
if (lastValue !== isVisible) {
|
|
125
|
+
setLastValue(isVisible);
|
|
126
|
+
onChange(isVisible);
|
|
127
|
+
if (isVisible && triggerOnce) {
|
|
128
|
+
stopWatching();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
132
|
+
}, [rectDimensions, lastValue]);
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<View ref={localRef} {...rest}>
|
|
136
|
+
{children}
|
|
137
|
+
</View>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
export default VisibilitySensor;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import VisibilitySensor from './VisibilitySensor';
|
|
2
|
+
import type {
|
|
3
|
+
VisibilitySensorProps,
|
|
4
|
+
VisibilitySensorRef,
|
|
5
|
+
VisibilitySensorThreshold,
|
|
6
|
+
} from './visibilitySensor.types';
|
|
7
|
+
|
|
8
|
+
export { VisibilitySensor };
|
|
9
|
+
export type {
|
|
10
|
+
VisibilitySensorProps,
|
|
11
|
+
VisibilitySensorRef,
|
|
12
|
+
VisibilitySensorThreshold,
|
|
13
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { View, ViewProps } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export interface VisibilitySensorProps extends ViewProps {
|
|
4
|
+
onChange: (visible: boolean) => void;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
triggerOnce?: boolean;
|
|
7
|
+
delay?: number | undefined;
|
|
8
|
+
threshold?: VisibilitySensorThreshold;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface VisibilitySensorRef {
|
|
12
|
+
getInnerRef: () => View | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface VisibilitySensorThreshold {
|
|
16
|
+
top?: number;
|
|
17
|
+
bottom?: number;
|
|
18
|
+
left?: number;
|
|
19
|
+
right?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RectDimensionsState {
|
|
23
|
+
rectTop: number;
|
|
24
|
+
rectBottom: number;
|
|
25
|
+
rectWidth: number;
|
|
26
|
+
rectHeight: number;
|
|
27
|
+
rectLeft: number;
|
|
28
|
+
rectRight: number;
|
|
29
|
+
}
|