@thecb/components 7.7.8-beta.7 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thecb/components",
3
- "version": "7.7.8-beta.7",
3
+ "version": "7.7.8-beta.8",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,4 +1,4 @@
1
- import { useLayoutEffect, useState } from "react";
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,15 +13,17 @@ import { useLayoutEffect, 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
- This will return the first element that matches *any* of the provided selectors
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 every element in the array is fully within the viewport, function returns true
22
+ If all elements that exist are within the viewport, the function returns true.
21
23
  */
22
24
 
23
25
  const useCheckElementsInViewport = (elements = []) => {
24
- const [elementsVisible, setElementsVisible] = useState(false);
26
+ let elementsVisible = true;
25
27
  const viewportWidth =
26
28
  window.innerWidth || document.documentElement.clientWidth;
27
29
  const viewportHeight =
@@ -29,18 +31,19 @@ const useCheckElementsInViewport = (elements = []) => {
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
- if (
38
- boundingBox?.top >= 0 &&
39
- boundingBox?.left >= 0 &&
40
- boundingBox?.right <= viewportWidth &&
41
- boundingBox?.bottom <= viewportHeight
42
- ) {
43
- setElementsVisible(true);
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]);