@thecb/components 7.7.8-beta.1 → 7.7.8-beta.10

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.1",
3
+ "version": "7.7.8-beta.10",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -67,7 +67,7 @@ const HighlightTabRow = ({
67
67
  color={themeValues.textColor}
68
68
  weight={FONT_WEIGHT_SEMIBOLD}
69
69
  extraStyles="display: block; white-space: nowrap;"
70
- id={`${t}-tab-text`}
70
+ id={`${t?.toLowerCase()?.replace(/\s/g, "-")}-tab-text`}
71
71
  >
72
72
  {t}
73
73
  </Text>
@@ -1,9 +1,8 @@
1
1
  import React, { Fragment, memo } from "react";
2
2
  import { themeComponent } from "../../../util/themeUtils";
3
3
  import { fallbackValues } from "./Module.theme";
4
- import Heading from "../../atoms/heading";
5
4
  import Title from "../../atoms/title";
6
- import { Box } from "../../atoms/layouts";
5
+ import { Box, Cluster } from "../../atoms/layouts";
7
6
 
8
7
  /*
9
8
  New (01/22) - updated <Module /> to use <Title /> atom
@@ -25,6 +24,8 @@ const Module = ({
25
24
  variant = "default",
26
25
  fontSize,
27
26
  as,
27
+ titleID = "",
28
+ rightTitleContent,
28
29
  children
29
30
  }) => {
30
31
  const themedFontSize =
@@ -37,20 +38,28 @@ const Module = ({
37
38
  const themedElemType =
38
39
  variant === "small" ? "h6" : variant === "default" ? "h5" : "h2";
39
40
  const computedElemType = as || themedElemType;
41
+ const headingText = (
42
+ <Title
43
+ weight={themeValues.fontWeight}
44
+ color={themeValues.fontColor}
45
+ margin={`${spacing} 0 ${themeValues.titleSpacing} 0`}
46
+ textAlign={themeValues.textAlign}
47
+ as={computedElemType}
48
+ extraStyles={`font-size: ${computedFontSize};`}
49
+ id={titleID}
50
+ >
51
+ {heading}
52
+ </Title>
53
+ );
40
54
 
41
55
  return (
42
56
  <Fragment>
43
- {heading && (
44
- <Title
45
- weight={themeValues.fontWeight}
46
- color={themeValues.fontColor}
47
- margin={`${spacing} 0 ${themeValues.titleSpacing} 0`}
48
- textAlign={themeValues.textAlign}
49
- as={computedElemType}
50
- extraStyles={`font-size: ${computedFontSize};`}
51
- >
52
- {heading}
53
- </Title>
57
+ {heading && !rightTitleContent && headingText}
58
+ {heading && rightTitleContent && (
59
+ <Cluster justify="space-between" align="center" nowrap>
60
+ {headingText}
61
+ {rightTitleContent}
62
+ </Cluster>
54
63
  )}
55
64
  <Box padding={`0 0 ${spacingBottom}`}>
56
65
  <Box
package/src/util/index.js CHANGED
@@ -4,6 +4,7 @@ import * as theme from "./themeUtils";
4
4
  import useFocusInvalidInput from "./focusFirstInvalidInputHook";
5
5
  import useOutsideClick from "./useOutsideClick";
6
6
  import useCheckElementsInViewport from "./useCheckElementsInViewport";
7
+ import useGetViewportOnResize from "./useGetViewportOnResize";
7
8
 
8
9
  export {
9
10
  formats,
@@ -11,5 +12,6 @@ export {
11
12
  theme,
12
13
  useFocusInvalidInput,
13
14
  useOutsideClick,
14
- useCheckElementsInViewport
15
+ useCheckElementsInViewport,
16
+ useGetViewportOnResize
15
17
  };
@@ -1,49 +1,55 @@
1
- import { useEffect, useState } from "react";
1
+ import { useEffect, useLayoutEffect, useState } from "react";
2
2
 
3
3
  /*
4
4
  Hook that determines whether every element in an array of DOM selectors is fully present
5
5
  within the user's current viewport.
6
6
 
7
- (elements: Array<String>) => Boolean;
7
+ (elements: Array<String>, Number, Number) => Boolean;
8
+ (["#submit-button", "#.module-small", "h2.alert-title"], 1024, 768) => true;
8
9
 
9
10
  Takes an array of strings that correspond to DOM selectors. Examples:
10
11
  "#submit-button", ".module-small", "h2.alert-title"
11
12
 
13
+ Also takes numbers that represent the current viewport width and height.
14
+ You can either calculate these once in your application before running this hook
15
+ (so that they only update when the component running the hook re-renders), or you can
16
+ use the useGetViewportOnResize hook to calculate them when the browser window is resized
17
+
12
18
  The document query function will return the *first* element in the document that matches
13
19
  the string given.
14
20
 
15
- A combination string of multiple selectors can also be provided, e.g.:
16
- ".alert-info, .alert-warning, .alert-error"
21
+ A combination string of multiple selectors can also be provided as an item in the array, e.g.:
22
+ ".alert-info, .alert-warning, .alert-error". This will return the first element that matches *any* of the provided selectors.
17
23
 
18
- This will return the first element that matches *any* of the provided selectors
24
+ If the element is present in the DOM (domEL !== null), the function examines the element's bounding box
25
+ to determine if the element is within the viewport. If any portion of the element is outside of
26
+ the viewport, the function returns false.
19
27
 
20
- If every element in the array is fully within the viewport, function returns true
28
+ If all elements that exist are within the viewport, the function returns true.
21
29
  */
22
30
 
23
- const useCheckElementsInViewport = (elements = []) => {
24
- const [elementsVisible, setElementsVisible] = useState(false);
25
- const viewportWidth =
26
- window.innerWidth || document.documentElement.clientWidth;
27
- const viewportHeight =
28
- window.innerHeight || document.documentElement.clientHeight;
29
-
30
- useEffect(() => {
31
- elements.forEach(element => {
32
- const domEl = document.querySelector(element);
33
- const boundingBox = domEl.getBoundingClientRect();
34
-
31
+ const useCheckElementsInViewport = (
32
+ elements = [],
33
+ viewportWidth,
34
+ viewportHeight
35
+ ) => {
36
+ for (let i = 0; i < elements.length; i++) {
37
+ const domEl = document.querySelector(elements[i]);
38
+ const boundingBox = domEl?.getBoundingClientRect();
39
+ timesRun++;
40
+
41
+ if (domEl !== null) {
35
42
  if (
36
- boundingBox.top >= 0 &&
37
- boundingBox.left >= 0 &&
38
- boundingBox.right <= viewportWidth &&
39
- boundingBox.bottom <= viewportHeight
43
+ boundingBox.top < 0 ||
44
+ boundingBox.left < 0 ||
45
+ boundingBox.right > viewportWidth ||
46
+ boundingBox.bottom > viewportHeight
40
47
  ) {
41
- setElementsVisible(true);
48
+ return false;
42
49
  }
43
- });
44
- }, [elements]);
45
-
46
- return elementsVisible;
50
+ }
51
+ }
52
+ return true;
47
53
  };
48
54
 
49
55
  export default useCheckElementsInViewport;
@@ -0,0 +1,47 @@
1
+ import { useEffect } from "react";
2
+
3
+ /*
4
+ Hook that adds a resize listener to the window and updates viewport size via
5
+ a provided handler function
6
+
7
+ (Object: {width: Number, height: Number}, Function) => undefined;
8
+
9
+ Provide the current value of the user's viewport and a handler function to receive updated values
10
+
11
+ Best used in combination with a useState hook in your component to receive updated values
12
+ */
13
+
14
+ const useGetViewportOnResize = (viewport, updateViewport) => {
15
+ let timeoutID = false;
16
+ let delay = 250;
17
+
18
+ const updateViewportValues = () => {
19
+ clearTimeout(timeoutID);
20
+
21
+ timeoutID = setTimeout(() => {
22
+ let newWidth = window.innerWidth || document.documentElement.clientWidth;
23
+ let newHeight =
24
+ window.innerHeight || document.documentElement.clientHeight;
25
+
26
+ if (
27
+ (viewport?.width && viewport?.width !== newWidth) ||
28
+ (viewport?.height && viewport?.height !== newHeight)
29
+ ) {
30
+ updateViewport({
31
+ width: newWidth,
32
+ height: newHeight
33
+ });
34
+ }
35
+ }, delay);
36
+ };
37
+
38
+ useEffect(() => {
39
+ window.addEventListener("resize", updateViewportValues);
40
+
41
+ return () => {
42
+ clearTimeout(timeoutID);
43
+ };
44
+ }, []);
45
+ };
46
+
47
+ export default useGetViewportOnResize;