@thecb/components 7.7.8-beta.6 → 7.7.8-beta.8
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/dist/index.cjs.js +12 -15
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +12 -15
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/util/useCheckElementsInViewport.js +18 -15
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useLayoutEffect } from "react";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
Hook that determines whether every element in an array of DOM selectors is fully present
|
|
@@ -13,34 +13,37 @@ import { useEffect, useState } from "react";
|
|
|
13
13
|
the string given.
|
|
14
14
|
|
|
15
15
|
A combination string of multiple selectors can also be provided as an item in the array, e.g.:
|
|
16
|
-
".alert-info, .alert-warning, .alert-error"
|
|
16
|
+
".alert-info, .alert-warning, .alert-error". This will return the first element that matches *any* of the provided selectors.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
If the element is present in the DOM (domEL !== null), the function examines the element's bounding box
|
|
19
|
+
to determine if the element is within the viewport. If any portion of the element is outside of
|
|
20
|
+
the viewport, the function returns false.
|
|
19
21
|
|
|
20
|
-
If
|
|
22
|
+
If all elements that exist are within the viewport, the function returns true.
|
|
21
23
|
*/
|
|
22
24
|
|
|
23
25
|
const useCheckElementsInViewport = (elements = []) => {
|
|
24
|
-
|
|
26
|
+
let elementsVisible = true;
|
|
25
27
|
const viewportWidth =
|
|
26
28
|
window.innerWidth || document.documentElement.clientWidth;
|
|
27
29
|
const viewportHeight =
|
|
28
30
|
window.innerHeight || document.documentElement.clientHeight;
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
useLayoutEffect(() => {
|
|
31
33
|
elements.forEach(element => {
|
|
32
|
-
console.log("element is", element);
|
|
33
34
|
const domEl = document.querySelector(element);
|
|
34
|
-
console.log("domEl is", domEl);
|
|
35
35
|
const boundingBox = domEl?.getBoundingClientRect();
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
// Skip any elements not in the DOM
|
|
38
|
+
if (domEl !== null) {
|
|
39
|
+
if (
|
|
40
|
+
boundingBox.top < 0 ||
|
|
41
|
+
boundingBox.left < 0 ||
|
|
42
|
+
boundingBox.right > viewportWidth ||
|
|
43
|
+
boundingBox.bottom > viewportHeight
|
|
44
|
+
) {
|
|
45
|
+
elementsVisible = false;
|
|
46
|
+
}
|
|
44
47
|
}
|
|
45
48
|
});
|
|
46
49
|
}, [elements, viewportWidth, viewportHeight]);
|