@xhsreds/reds-rn-next 0.9.0 → 0.9.1-feat-image-2202510231126
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/coverage/.tmp/coverage-0.json +1 -1
- package/coverage/.tmp/coverage-1.json +1 -1
- package/coverage/.tmp/coverage-10.json +1 -1
- package/coverage/.tmp/coverage-11.json +1 -1
- package/coverage/.tmp/coverage-12.json +1 -1
- package/coverage/.tmp/coverage-13.json +1 -1
- package/coverage/.tmp/coverage-14.json +1 -1
- package/coverage/.tmp/coverage-15.json +1 -1
- package/coverage/.tmp/coverage-16.json +1 -1
- package/coverage/.tmp/coverage-17.json +1 -1
- package/coverage/.tmp/coverage-2.json +1 -1
- package/coverage/.tmp/coverage-21.json +1 -1
- package/coverage/.tmp/coverage-22.json +1 -1
- package/coverage/.tmp/coverage-23.json +1 -1
- package/coverage/.tmp/coverage-24.json +1 -1
- package/coverage/.tmp/coverage-25.json +1 -1
- package/coverage/.tmp/coverage-26.json +1 -1
- package/coverage/.tmp/coverage-27.json +1 -1
- package/coverage/.tmp/coverage-29.json +1 -1
- package/coverage/.tmp/coverage-3.json +1 -1
- package/coverage/.tmp/coverage-30.json +1 -1
- package/coverage/.tmp/coverage-34.json +1 -1
- package/coverage/.tmp/coverage-35.json +1 -1
- package/coverage/.tmp/coverage-36.json +1 -1
- package/coverage/.tmp/coverage-38.json +1 -1
- package/coverage/.tmp/coverage-39.json +1 -1
- package/coverage/.tmp/coverage-4.json +1 -1
- package/coverage/.tmp/coverage-40.json +1 -1
- package/coverage/.tmp/coverage-41.json +1 -1
- package/coverage/.tmp/coverage-5.json +1 -1
- package/coverage/.tmp/coverage-6.json +1 -1
- package/coverage/.tmp/coverage-7.json +1 -1
- package/coverage/.tmp/coverage-8.json +1 -1
- package/coverage/.tmp/coverage-9.json +1 -1
- package/lib/cjs/components/Image/VisibilitySensor.js +18 -76
- package/lib/cjs/components/Image/VisibilitySensor.js.map +1 -1
- package/lib/esm/components/Image/VisibilitySensor.js +20 -78
- package/lib/esm/components/Image/VisibilitySensor.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Image/VisibilitySensor.tsx +30 -84
|
@@ -6,20 +6,17 @@ var _rollupPluginBabelHelpers = require('../../_chunks/C9tZEm0t.js');
|
|
|
6
6
|
var React = require('react');
|
|
7
7
|
var reactNative = require('react-native');
|
|
8
8
|
|
|
9
|
-
var _excluded = ["onChange", "disabled", "
|
|
9
|
+
var _excluded = ["onChange", "disabled", "threshold", "children"];
|
|
10
10
|
var VisibilitySensor = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
11
11
|
var onChange = props.onChange,
|
|
12
12
|
_props$disabled = props.disabled,
|
|
13
13
|
disabled = _props$disabled === void 0 ? false : _props$disabled,
|
|
14
|
-
_props$
|
|
15
|
-
triggerOnce = _props$triggerOnce === void 0 ? false : _props$triggerOnce;
|
|
16
|
-
props.delay;
|
|
17
|
-
var _props$threshold = props.threshold,
|
|
14
|
+
_props$threshold = props.threshold,
|
|
18
15
|
threshold = _props$threshold === void 0 ? {} : _props$threshold,
|
|
19
16
|
children = props.children,
|
|
20
17
|
rest = _rollupPluginBabelHelpers._objectWithoutProperties(props, _excluded);
|
|
21
18
|
var localRef = React.useRef(null);
|
|
22
|
-
var
|
|
19
|
+
var measuredOnceRef = React.useRef(false);
|
|
23
20
|
React.useImperativeHandle(ref, function () {
|
|
24
21
|
return {
|
|
25
22
|
getInnerRef: function getInnerRef() {
|
|
@@ -27,83 +24,28 @@ var VisibilitySensor = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
27
24
|
}
|
|
28
25
|
};
|
|
29
26
|
});
|
|
30
|
-
var
|
|
31
|
-
rectTop: 0,
|
|
32
|
-
rectBottom: 0,
|
|
33
|
-
rectLeft: 0,
|
|
34
|
-
rectRight: 0,
|
|
35
|
-
rectWidth: 0,
|
|
36
|
-
rectHeight: 0
|
|
37
|
-
}),
|
|
38
|
-
_useState2 = _rollupPluginBabelHelpers._slicedToArray(_useState, 2),
|
|
39
|
-
rectDimensions = _useState2[0],
|
|
40
|
-
setRectDimensions = _useState2[1];
|
|
41
|
-
var _useState3 = React.useState(void 0),
|
|
42
|
-
_useState4 = _rollupPluginBabelHelpers._slicedToArray(_useState3, 2),
|
|
43
|
-
lastValue = _useState4[0],
|
|
44
|
-
setLastValue = _useState4[1];
|
|
45
|
-
var _useState5 = React.useState(false),
|
|
46
|
-
_useState6 = _rollupPluginBabelHelpers._slicedToArray(_useState5, 2),
|
|
47
|
-
active = _useState6[0],
|
|
48
|
-
setActive = _useState6[1];
|
|
49
|
-
var measureInnerView = function measureInnerView() {
|
|
27
|
+
var measureOnce = React.useCallback(function () {
|
|
50
28
|
var _localRef$current;
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
}
|
|
29
|
+
if (disabled || measuredOnceRef.current) return;
|
|
30
|
+
var win = reactNative.Dimensions.get("window");
|
|
31
|
+
(_localRef$current = localRef.current) === null || _localRef$current === void 0 || _localRef$current.measure(function (_, __, width, height, pageX, pageY) {
|
|
32
|
+
if (measuredOnceRef.current) return;
|
|
33
|
+
var isVisible = pageY + (threshold.top || 0) <= win.height && pageY + height - (threshold.bottom || 0) >= 0 && pageX + (threshold.left || 0) <= win.width && pageX + width - (threshold.right || 0) >= 0;
|
|
34
|
+
measuredOnceRef.current = true;
|
|
35
|
+
onChange(isVisible);
|
|
64
36
|
});
|
|
65
|
-
};
|
|
66
|
-
var startWatching = React.useCallback(function () {
|
|
67
|
-
if (!active) setActive(true);
|
|
68
|
-
}, [active]);
|
|
69
|
-
var stopWatching = React.useCallback(function () {
|
|
70
|
-
if (active) setActive(false);
|
|
71
|
-
}, [active]);
|
|
72
|
-
React.useEffect(function () {
|
|
73
|
-
if (!disabled) {
|
|
74
|
-
startWatching();
|
|
75
|
-
}
|
|
76
|
-
return function () {
|
|
77
|
-
stopWatching();
|
|
78
|
-
};
|
|
79
|
-
}, [disabled, startWatching, stopWatching]);
|
|
37
|
+
}, [disabled, onChange, threshold]);
|
|
80
38
|
React.useEffect(function () {
|
|
81
|
-
if (!
|
|
82
|
-
|
|
83
|
-
|
|
39
|
+
if (!disabled && !measuredOnceRef.current) {
|
|
40
|
+
requestAnimationFrame(function () {
|
|
41
|
+
return measureOnce();
|
|
42
|
+
});
|
|
84
43
|
}
|
|
85
|
-
|
|
86
|
-
var isVisible = rectDimensions.rectTop + (threshold.top || 0) <= window.height &&
|
|
87
|
-
// Top edge is within the bottom of the window
|
|
88
|
-
rectDimensions.rectBottom - (threshold.bottom || 0) >= 0 &&
|
|
89
|
-
// Bottom edge is within the top of the window
|
|
90
|
-
rectDimensions.rectLeft + (threshold.left || 0) <= window.width &&
|
|
91
|
-
// Left edge is within the right of the window
|
|
92
|
-
rectDimensions.rectRight - (threshold.right || 0) >= 0;
|
|
93
|
-
if (lastValue !== isVisible) {
|
|
94
|
-
setLastValue(isVisible);
|
|
95
|
-
onChange(isVisible);
|
|
96
|
-
if (isVisible && triggerOnce) {
|
|
97
|
-
stopWatching();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}, [rectDimensions, lastValue]);
|
|
44
|
+
}, [disabled, measureOnce]);
|
|
101
45
|
return /* @__PURE__ */React.createElement(reactNative.View, _rollupPluginBabelHelpers._objectSpread2(_rollupPluginBabelHelpers._objectSpread2({
|
|
102
46
|
ref: localRef
|
|
103
47
|
}, rest), {}, {
|
|
104
|
-
onLayout:
|
|
105
|
-
return measureInnerView();
|
|
106
|
-
}
|
|
48
|
+
onLayout: measureOnce
|
|
107
49
|
}), children);
|
|
108
50
|
});
|
|
109
51
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VisibilitySensor.js","sources":["../../../../src/components/Image/VisibilitySensor.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useRef,
|
|
1
|
+
{"version":3,"file":"VisibilitySensor.js","sources":["../../../../src/components/Image/VisibilitySensor.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useRef, forwardRef, useImperativeHandle } from \"react\";\nimport { Dimensions, View } from \"react-native\";\nimport type { VisibilitySensorRef, VisibilitySensorProps } from \"./interface/visibilitySensor\";\n\n// Measure visibility once on first layout to avoid state-driven re-render loops\nconst VisibilitySensor = forwardRef<VisibilitySensorRef, VisibilitySensorProps>((props, ref) => {\n const { onChange, disabled = false, threshold = {}, children, ...rest } = props;\n const localRef = useRef<View>(null);\n const measuredOnceRef = useRef(false);\n\n useImperativeHandle(ref, () => ({\n getInnerRef: () => localRef.current,\n }));\n\n const measureOnce = useCallback(() => {\n if (disabled || measuredOnceRef.current) return;\n const win = Dimensions.get(\"window\");\n localRef.current?.measure((_, __, width: number, height: number, pageX: number, pageY: number) => {\n if (measuredOnceRef.current) return; // guard against double-calls in same frame\n const isVisible =\n pageY + (threshold.top || 0) <= win.height &&\n pageY + height - (threshold.bottom || 0) >= 0 &&\n pageX + (threshold.left || 0) <= win.width &&\n pageX + width - (threshold.right || 0) >= 0;\n measuredOnceRef.current = true;\n onChange(isVisible);\n });\n }, [disabled, onChange, threshold]);\n\n // If initially disabled, measure once when enabled later\n useEffect(() => {\n if (!disabled && !measuredOnceRef.current) {\n requestAnimationFrame(() => measureOnce());\n }\n }, [disabled, measureOnce]);\n\n return (\n <View ref={localRef} {...rest} onLayout={measureOnce}>\n {children}\n </View>\n );\n});\n\nexport default VisibilitySensor;\n"],"names":["VisibilitySensor","forwardRef","props","ref","onChange","_props$disabled","disabled","_props$threshold","threshold","children","rest","_objectWithoutProperties","_excluded","localRef","useRef","measuredOnceRef","useImperativeHandle","getInnerRef","current","measureOnce","useCallback","_localRef$current","win","Dimensions","get","measure","_","__","width","height","pageX","pageY","isVisible","top","bottom","left","right","useEffect","requestAnimationFrame","React","createElement","View","onLayout"],"mappings":";;;;;;;;;AAKMA,IAAAA,gBAAmB,gBAAAC,gBAAA,CAAuD,UAACC,KAAA,EAAOC,GAAQ,EAAA;AACxF,EAAA,IAAEC,QAAU,GAAwDF,KAAA,CAAlEE,QAAU;IAAAC,eAAA,GAAwDH,KAAA,CAAxDI,QAAA;AAAAA,IAAAA,QAAA,GAAAD,eAAA,KAAW,KAAA,CAAA,GAAA,KAAO,GAAAA,eAAA;IAAAE,gBAAA,GAAsCL,KAAA,CAAtCM,SAAA;AAAAA,IAAAA,SAAA,GAAAD,gBAAA,KAAA,KAAA,CAAA,GAAY,EAAI,GAAAA,gBAAA;IAAAE,QAAA,GAAsBP,KAAA,CAAtBO,QAAA;AAAaC,IAAAA,IAAA,GAAAC,kDAAA,CAAST,KAAA,EAAAU,SAAA,CAAA,CAAA;AACpE,EAAA,IAAAC,QAAA,GAAWC,aAAa,IAAI,CAAA,CAAA;AAC5B,EAAA,IAAAC,eAAA,GAAkBD,aAAO,KAAK,CAAA,CAAA;EAEpCE,yBAAA,CAAoBb,KAAK,YAAA;IAAA,OAAO;MAC9Bc,WAAA,EAAa,SAAbA,WAAAA,GAAA;QAAA,OAAmBJ,QAAS,CAAAK,OAAA,CAAA;AAAA,OAAA;KAC5B,CAAA;AAAA,GAAA,CAAA,CAAA;AAEI,EAAA,IAAAC,WAAA,GAAcC,kBAAY,YAAM;AAAA,IAAA,IAAAC,iBAAA,CAAA;AAChC,IAAA,IAAAf,QAAA,IAAYS,gBAAgBG,OAAS,EAAA,OAAA;AACnC,IAAA,IAAAI,GAAA,GAAMC,sBAAW,CAAAC,GAAA,CAAI,QAAQ,CAAA,CAAA;IAC1B,CAAAH,iBAA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i as _objectWithoutProperties,
|
|
2
|
-
import React, { forwardRef, useRef, useImperativeHandle,
|
|
1
|
+
import { i as _objectWithoutProperties, a as _objectSpread2 } from '../../_chunks/CAkwE9ZD.js';
|
|
2
|
+
import React, { forwardRef, useRef, useImperativeHandle, useCallback, useEffect } from 'react';
|
|
3
3
|
import { Dimensions, View } from 'react-native';
|
|
4
4
|
|
|
5
|
-
var _excluded = ["onChange", "disabled", "
|
|
5
|
+
var _excluded = ["onChange", "disabled", "threshold", "children"];
|
|
6
6
|
var VisibilitySensor = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
7
7
|
var onChange = props.onChange,
|
|
8
8
|
_props$disabled = props.disabled,
|
|
9
9
|
disabled = _props$disabled === void 0 ? false : _props$disabled,
|
|
10
|
-
_props$
|
|
11
|
-
triggerOnce = _props$triggerOnce === void 0 ? false : _props$triggerOnce;
|
|
12
|
-
props.delay;
|
|
13
|
-
var _props$threshold = props.threshold,
|
|
10
|
+
_props$threshold = props.threshold,
|
|
14
11
|
threshold = _props$threshold === void 0 ? {} : _props$threshold,
|
|
15
12
|
children = props.children,
|
|
16
13
|
rest = _objectWithoutProperties(props, _excluded);
|
|
17
14
|
var localRef = useRef(null);
|
|
18
|
-
var
|
|
15
|
+
var measuredOnceRef = useRef(false);
|
|
19
16
|
useImperativeHandle(ref, function () {
|
|
20
17
|
return {
|
|
21
18
|
getInnerRef: function getInnerRef() {
|
|
@@ -23,83 +20,28 @@ var VisibilitySensor = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
23
20
|
}
|
|
24
21
|
};
|
|
25
22
|
});
|
|
26
|
-
var
|
|
27
|
-
rectTop: 0,
|
|
28
|
-
rectBottom: 0,
|
|
29
|
-
rectLeft: 0,
|
|
30
|
-
rectRight: 0,
|
|
31
|
-
rectWidth: 0,
|
|
32
|
-
rectHeight: 0
|
|
33
|
-
}),
|
|
34
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
35
|
-
rectDimensions = _useState2[0],
|
|
36
|
-
setRectDimensions = _useState2[1];
|
|
37
|
-
var _useState3 = useState(void 0),
|
|
38
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
39
|
-
lastValue = _useState4[0],
|
|
40
|
-
setLastValue = _useState4[1];
|
|
41
|
-
var _useState5 = useState(false),
|
|
42
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
43
|
-
active = _useState6[0],
|
|
44
|
-
setActive = _useState6[1];
|
|
45
|
-
var measureInnerView = function measureInnerView() {
|
|
23
|
+
var measureOnce = useCallback(function () {
|
|
46
24
|
var _localRef$current;
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
rectWidth: width,
|
|
55
|
-
rectHeight: height
|
|
56
|
-
};
|
|
57
|
-
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) {
|
|
58
|
-
setRectDimensions(dimensions);
|
|
59
|
-
}
|
|
25
|
+
if (disabled || measuredOnceRef.current) return;
|
|
26
|
+
var win = Dimensions.get("window");
|
|
27
|
+
(_localRef$current = localRef.current) === null || _localRef$current === void 0 || _localRef$current.measure(function (_, __, width, height, pageX, pageY) {
|
|
28
|
+
if (measuredOnceRef.current) return;
|
|
29
|
+
var isVisible = pageY + (threshold.top || 0) <= win.height && pageY + height - (threshold.bottom || 0) >= 0 && pageX + (threshold.left || 0) <= win.width && pageX + width - (threshold.right || 0) >= 0;
|
|
30
|
+
measuredOnceRef.current = true;
|
|
31
|
+
onChange(isVisible);
|
|
60
32
|
});
|
|
61
|
-
};
|
|
62
|
-
var startWatching = useCallback(function () {
|
|
63
|
-
if (!active) setActive(true);
|
|
64
|
-
}, [active]);
|
|
65
|
-
var stopWatching = useCallback(function () {
|
|
66
|
-
if (active) setActive(false);
|
|
67
|
-
}, [active]);
|
|
68
|
-
useEffect(function () {
|
|
69
|
-
if (!disabled) {
|
|
70
|
-
startWatching();
|
|
71
|
-
}
|
|
72
|
-
return function () {
|
|
73
|
-
stopWatching();
|
|
74
|
-
};
|
|
75
|
-
}, [disabled, startWatching, stopWatching]);
|
|
33
|
+
}, [disabled, onChange, threshold]);
|
|
76
34
|
useEffect(function () {
|
|
77
|
-
if (!
|
|
78
|
-
|
|
79
|
-
|
|
35
|
+
if (!disabled && !measuredOnceRef.current) {
|
|
36
|
+
requestAnimationFrame(function () {
|
|
37
|
+
return measureOnce();
|
|
38
|
+
});
|
|
80
39
|
}
|
|
81
|
-
|
|
82
|
-
var isVisible = rectDimensions.rectTop + (threshold.top || 0) <= window.height &&
|
|
83
|
-
// Top edge is within the bottom of the window
|
|
84
|
-
rectDimensions.rectBottom - (threshold.bottom || 0) >= 0 &&
|
|
85
|
-
// Bottom edge is within the top of the window
|
|
86
|
-
rectDimensions.rectLeft + (threshold.left || 0) <= window.width &&
|
|
87
|
-
// Left edge is within the right of the window
|
|
88
|
-
rectDimensions.rectRight - (threshold.right || 0) >= 0;
|
|
89
|
-
if (lastValue !== isVisible) {
|
|
90
|
-
setLastValue(isVisible);
|
|
91
|
-
onChange(isVisible);
|
|
92
|
-
if (isVisible && triggerOnce) {
|
|
93
|
-
stopWatching();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}, [rectDimensions, lastValue]);
|
|
40
|
+
}, [disabled, measureOnce]);
|
|
97
41
|
return /* @__PURE__ */React.createElement(View, _objectSpread2(_objectSpread2({
|
|
98
42
|
ref: localRef
|
|
99
43
|
}, rest), {}, {
|
|
100
|
-
onLayout:
|
|
101
|
-
return measureInnerView();
|
|
102
|
-
}
|
|
44
|
+
onLayout: measureOnce
|
|
103
45
|
}), children);
|
|
104
46
|
});
|
|
105
47
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VisibilitySensor.js","sources":["../../../../src/components/Image/VisibilitySensor.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useRef,
|
|
1
|
+
{"version":3,"file":"VisibilitySensor.js","sources":["../../../../src/components/Image/VisibilitySensor.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useRef, forwardRef, useImperativeHandle } from \"react\";\nimport { Dimensions, View } from \"react-native\";\nimport type { VisibilitySensorRef, VisibilitySensorProps } from \"./interface/visibilitySensor\";\n\n// Measure visibility once on first layout to avoid state-driven re-render loops\nconst VisibilitySensor = forwardRef<VisibilitySensorRef, VisibilitySensorProps>((props, ref) => {\n const { onChange, disabled = false, threshold = {}, children, ...rest } = props;\n const localRef = useRef<View>(null);\n const measuredOnceRef = useRef(false);\n\n useImperativeHandle(ref, () => ({\n getInnerRef: () => localRef.current,\n }));\n\n const measureOnce = useCallback(() => {\n if (disabled || measuredOnceRef.current) return;\n const win = Dimensions.get(\"window\");\n localRef.current?.measure((_, __, width: number, height: number, pageX: number, pageY: number) => {\n if (measuredOnceRef.current) return; // guard against double-calls in same frame\n const isVisible =\n pageY + (threshold.top || 0) <= win.height &&\n pageY + height - (threshold.bottom || 0) >= 0 &&\n pageX + (threshold.left || 0) <= win.width &&\n pageX + width - (threshold.right || 0) >= 0;\n measuredOnceRef.current = true;\n onChange(isVisible);\n });\n }, [disabled, onChange, threshold]);\n\n // If initially disabled, measure once when enabled later\n useEffect(() => {\n if (!disabled && !measuredOnceRef.current) {\n requestAnimationFrame(() => measureOnce());\n }\n }, [disabled, measureOnce]);\n\n return (\n <View ref={localRef} {...rest} onLayout={measureOnce}>\n {children}\n </View>\n );\n});\n\nexport default VisibilitySensor;\n"],"names":["VisibilitySensor","forwardRef","props","ref","onChange","_props$disabled","disabled","_props$threshold","threshold","children","rest","_objectWithoutProperties","_excluded","localRef","useRef","measuredOnceRef","useImperativeHandle","getInnerRef","current","measureOnce","useCallback","_localRef$current","win","Dimensions","get","measure","_","__","width","height","pageX","pageY","isVisible","top","bottom","left","right","use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IAAIhB,gBAAgBG,OAAS,EAAA,OAAA;MACvB,IAAAc,SAAA,GACJD,SAASvB,SAAU,CAAAyB,GAAA,IAAO,MAAMX,GAAI,CAAAO,MAAA,IACpCE,KAAQ,GAAAF,MAAA,IAAUrB,SAAU,CAAA0B,MAAA,IAAU,MAAM,CAC5C,IAAAJ,KAAA,IAAStB,SAAU,CAAA2B,IAAA,IAAQ,CAAM,CAAA,IAAAb,GAAA,CAAIM,SACrCE,KAAQ,GAAAF,KAAA,IAASpB,SAAU,CAAA4B,KAAA,IAAS,CAAM,CAAA,IAAA,CAAA,CAAA;MAC5CrB,eAAA,CAAgBG,OAAU,GAAA,IAAA,CAAA;MAC1Bd,QAAA,CAAS4B,SAAS,CAAA,CAAA;AACpB,KAAC,CAAA,CAAA;GACA,EAAA,CAAC1B,QAAU,EAAAF,QAAA,EAAUI,SAAS,CAAC,CAAA,CAAA;AAGlC6B,EAAAA,SAAA,CAAU,YAAM;AACd,IAAA,IAAI,CAAC/B,QAAA,IAAY,CAACS,eAAA,CAAgBG,OAAS,EAAA;AACnBoB,MAAAA,qBAAA,CAAA,YAAA;QAAA,OAAMnB,aAAa,CAAA;OAAA,CAAA,CAAA;AAC3C,KAAA;AACF,GAAG,EAAA,CAACb,QAAU,EAAAa,WAAW,CAAC,CAAA,CAAA;EAGxB,sBAAAoB,KAAA,CAAAC,aAAA,CAACC;AAAKtC,IAAAA,GAAK,EAAAU,QAAAA;AAAA,GAAA,EAAcH,IAAM,CAAA,EAAA,EAAA,EAAA;AAAAgC,IAAAA,QAAA,EAAUvB,WAAAA;MACtCV,QACH,CAAA,CAAA;AAEJ,CAAC;;;;"}
|
package/package.json
CHANGED
|
@@ -1,98 +1,44 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useRef,
|
|
2
|
-
import { Dimensions,
|
|
3
|
-
import type { VisibilitySensorRef, VisibilitySensorProps
|
|
4
|
-
|
|
5
|
-
//
|
|
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
|
-
// }
|
|
1
|
+
import React, { useCallback, useEffect, useRef, forwardRef, useImperativeHandle } from "react";
|
|
2
|
+
import { Dimensions, View } from "react-native";
|
|
3
|
+
import type { VisibilitySensorRef, VisibilitySensorProps } from "./interface/visibilitySensor";
|
|
4
|
+
|
|
5
|
+
// Measure visibility once on first layout to avoid state-driven re-render loops
|
|
17
6
|
const VisibilitySensor = forwardRef<VisibilitySensorRef, VisibilitySensorProps>((props, ref) => {
|
|
18
|
-
const { onChange, disabled = false,
|
|
7
|
+
const { onChange, disabled = false, threshold = {}, children, ...rest } = props;
|
|
19
8
|
const localRef = useRef<View>(null);
|
|
20
|
-
const
|
|
9
|
+
const measuredOnceRef = useRef(false);
|
|
10
|
+
|
|
21
11
|
useImperativeHandle(ref, () => ({
|
|
22
12
|
getInnerRef: () => localRef.current,
|
|
23
13
|
}));
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const dimensions = {
|
|
38
|
-
rectTop: pageY,
|
|
39
|
-
rectBottom: pageY + height,
|
|
40
|
-
rectLeft: pageX,
|
|
41
|
-
rectRight: pageX + width,
|
|
42
|
-
rectWidth: width,
|
|
43
|
-
rectHeight: height,
|
|
44
|
-
};
|
|
45
|
-
if (
|
|
46
|
-
rectDimensions.rectTop !== dimensions.rectTop ||
|
|
47
|
-
rectDimensions.rectBottom !== dimensions.rectBottom ||
|
|
48
|
-
rectDimensions.rectLeft !== dimensions.rectLeft ||
|
|
49
|
-
rectDimensions.rectRight !== dimensions.rectRight ||
|
|
50
|
-
rectDimensions.rectWidth !== dimensions.rectWidth ||
|
|
51
|
-
rectDimensions.rectHeight !== dimensions.rectHeight
|
|
52
|
-
) {
|
|
53
|
-
setRectDimensions(dimensions);
|
|
54
|
-
}
|
|
14
|
+
|
|
15
|
+
const measureOnce = useCallback(() => {
|
|
16
|
+
if (disabled || measuredOnceRef.current) return;
|
|
17
|
+
const win = Dimensions.get("window");
|
|
18
|
+
localRef.current?.measure((_, __, width: number, height: number, pageX: number, pageY: number) => {
|
|
19
|
+
if (measuredOnceRef.current) return; // guard against double-calls in same frame
|
|
20
|
+
const isVisible =
|
|
21
|
+
pageY + (threshold.top || 0) <= win.height &&
|
|
22
|
+
pageY + height - (threshold.bottom || 0) >= 0 &&
|
|
23
|
+
pageX + (threshold.left || 0) <= win.width &&
|
|
24
|
+
pageX + width - (threshold.right || 0) >= 0;
|
|
25
|
+
measuredOnceRef.current = true;
|
|
26
|
+
onChange(isVisible);
|
|
55
27
|
});
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (!active) setActive(true);
|
|
60
|
-
}, [active]);
|
|
61
|
-
const stopWatching = useCallback(() => {
|
|
62
|
-
if (active) setActive(false);
|
|
63
|
-
}, [active]);
|
|
64
|
-
useEffect(() => {
|
|
65
|
-
if (!disabled) {
|
|
66
|
-
startWatching();
|
|
67
|
-
}
|
|
68
|
-
return () => {
|
|
69
|
-
stopWatching();
|
|
70
|
-
};
|
|
71
|
-
}, [disabled, startWatching, stopWatching]);
|
|
28
|
+
}, [disabled, onChange, threshold]);
|
|
29
|
+
|
|
30
|
+
// If initially disabled, measure once when enabled later
|
|
72
31
|
useEffect(() => {
|
|
73
|
-
if (!
|
|
74
|
-
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const window: ScaledSize = Dimensions.get("window");
|
|
78
|
-
const isVisible: boolean =
|
|
79
|
-
rectDimensions.rectTop + (threshold.top || 0) <= window.height && // Top edge is within the bottom of the window
|
|
80
|
-
rectDimensions.rectBottom - (threshold.bottom || 0) >= 0 && // Bottom edge is within the top of the window
|
|
81
|
-
rectDimensions.rectLeft + (threshold.left || 0) <= window.width && // Left edge is within the right of the window
|
|
82
|
-
rectDimensions.rectRight - (threshold.right || 0) >= 0; // Right edge is within the left of the window
|
|
83
|
-
if (lastValue !== isVisible) {
|
|
84
|
-
setLastValue(isVisible);
|
|
85
|
-
onChange(isVisible);
|
|
86
|
-
if (isVisible && triggerOnce) {
|
|
87
|
-
stopWatching();
|
|
88
|
-
}
|
|
32
|
+
if (!disabled && !measuredOnceRef.current) {
|
|
33
|
+
requestAnimationFrame(() => measureOnce());
|
|
89
34
|
}
|
|
90
|
-
|
|
91
|
-
|
|
35
|
+
}, [disabled, measureOnce]);
|
|
36
|
+
|
|
92
37
|
return (
|
|
93
|
-
<View ref={localRef} {...rest} onLayout={
|
|
38
|
+
<View ref={localRef} {...rest} onLayout={measureOnce}>
|
|
94
39
|
{children}
|
|
95
40
|
</View>
|
|
96
41
|
);
|
|
97
42
|
});
|
|
43
|
+
|
|
98
44
|
export default VisibilitySensor;
|