@thecb/components 9.1.7-beta.4 → 9.1.7-beta.5

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": "9.1.7-beta.4",
3
+ "version": "9.1.7-beta.5",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -16,12 +16,11 @@ import {
16
16
  GREY_CHATEAU,
17
17
  MINESHAFT_GREY,
18
18
  STORM_GREY,
19
- TRANSPARENT,
20
19
  WHITE
21
20
  } from "../../../constants/colors";
22
21
  import { fallbackValues } from "./Dropdown.theme";
23
22
  import { themeComponent } from "../../../util/themeUtils";
24
- import { addOpacity } from "../../../util/colorUtils";
23
+ import { darken } from "../../../util/colorUtils";
25
24
 
26
25
  const IconWrapper = styled.div`
27
26
  position: absolute;
@@ -69,30 +68,34 @@ const DropdownItemWrapper = styled.li`
69
68
  cursor: ${({ disabled }) => (disabled ? "default" : "pointer")};
70
69
 
71
70
  &:hover {
72
- background: ${({ disabled, themeValues }) =>
73
- disabled ? WHITE : addOpacity(themeValues.selectedColor, 0.2, 0, 0, 0)};
74
- border-color: ${({ selected, themeValues }) =>
75
- selected ? TRANSPARENT : themeValues.hoverColor};
71
+ border-color: ${({ disabled, selected, themeValues }) =>
72
+ selected
73
+ ? darken(themeValues.selectedColor, 0.2)
74
+ : disabled
75
+ ? WHITE
76
+ : themeValues.hoverColor};
76
77
  > * {
77
78
  background: ${({ selected, disabled, themeValues }) =>
78
79
  selected
79
- ? addOpacity(themeValues.selectedColor, 0.2, 0, 0, 0)
80
+ ? darken(themeValues.selectedColor, 0.2)
81
+ : disabled
82
+ ? WHITE
83
+ : themeValues.hoverColor};
84
+ border-color: ${({ selected, disabled, themeValues }) =>
85
+ selected
86
+ ? darken(themeValues.selectedColor, 0.2)
80
87
  : disabled
81
88
  ? WHITE
82
89
  : themeValues.hoverColor};
83
- border-color: transparent;
84
90
  }
85
91
  }
86
92
  &:focus {
87
93
  outline: none;
88
- background: ${({ selected, themeValues }) =>
89
- selected ? addOpacity(themeValues.selectedColor, 0.2, 0, 0, 0) : ""};
90
- border-color: ${({ selected, themeValues }) =>
91
- selected ? TRANSPARENT : themeValues.selectedColor};
94
+ border-color: ${({ themeValues }) => themeValues.selectedColor};
92
95
  > * {
93
96
  background: ${({ selected, disabled, themeValues }) =>
94
97
  selected
95
- ? addOpacity(themeValues.selectedColor, 0.2, 0, 0, 0)
98
+ ? darken(themeValues.selectedColor, 0.2)
96
99
  : disabled
97
100
  ? WHITE
98
101
  : themeValues.hoverColor};
@@ -1,13 +1,4 @@
1
- /*
2
-
3
- A utility function that can generate box-shadow values for components
4
- Takes a string representing an rgb color value and returns an object
5
- with values for standard, inset, and overlay shadows.
6
-
7
- The objects for standard and inset shadows contain versions approiate
8
- for base, hover, and active interaction states.
9
-
10
- */
1
+ import { TRANSPARENT } from "../constants/colors";
11
2
 
12
3
  /*
13
4
  Function to convert string representing rgb color to rgba value with provided opacity
@@ -28,6 +19,16 @@ const rgbToRgba = (rgbValue = "", opacity = "") => {
28
19
  )}, ${opacity}${rgbValue.slice(-1)}`;
29
20
  };
30
21
 
22
+ /*
23
+
24
+ A utility function that can generate box-shadow values for components
25
+ Takes a string representing an rgb color value and returns an object
26
+ with values for standard, inset, and overlay shadows.
27
+
28
+ The objects for standard and inset shadows contain versions approiate
29
+ for base, hover, and active interaction states.
30
+
31
+ */
31
32
  export const generateShadows = (baseColorRGB = "rgb(41, 42, 51)") => {
32
33
  const colorTen = rgbToRgba(baseColorRGB, "0.1") || "rgba(41, 42, 51, 0.1)";
33
34
  const colorTwenty = rgbToRgba(baseColorRGB, "0.2") || "rgba(41, 42, 51, 0.2)";
@@ -59,8 +60,53 @@ export const generateShadows = (baseColorRGB = "rgb(41, 42, 51)") => {
59
60
  };
60
61
  };
61
62
 
62
- export const addOpacity = (color, opacity, r, g, b) => {
63
- const rgba = `rgba(${r}, ${g}, ${b}, ${opacity})`;
63
+ /*
64
+ Function to convert a string representing hex color to rgb color
65
+ hexToRbg("#00ff00") => [0, 255, 0]
66
+ */
67
+ const hexToRbg = hexColor => {
68
+ const hexArr = hexColor.replace("#", "");
69
+ const red = parseInt(hexArr.slice(0, 2), 16);
70
+ const green = parseInt(hexArr.slice(2, 4), 16);
71
+ const blue = parseInt(hexArr.slice(4, 6), 16);
72
+
73
+ return [red, green, blue];
74
+ };
75
+
76
+ /*
77
+ Function to calculate a new hex color given an original
78
+ hex color, a hex background color, and an opacity number
79
+ between 0 and 1 to apply to the background.
64
80
 
65
- return `linear-gradient(0deg, ${rgba}, ${rgba}), linear-gradient(0deg, ${color}, ${color});`;
81
+ For example, to add a 20% black opacity (20% darker) to a green color
82
+ ("#00ff00"):
83
+ addOpacityToHex("00ff00", "#000000", .2) => "rgb(0, 204, 0)"
84
+ */
85
+ const addOpacityToHex = (hexColor, background, opacity) => {
86
+ if (hexColor == TRANSPARENT) {
87
+ return hexColor;
88
+ }
89
+
90
+ const color = hexToRbg(hexColor);
91
+ const backgroundRbg = hexToRbg(background);
92
+ const newColor = color.map((colorElem, index) =>
93
+ Math.floor(opacity * backgroundRbg[index] + (1 - opacity) * colorElem)
94
+ );
95
+
96
+ return `rgb(${newColor[0]}, ${newColor[1]}, ${newColor[2]})`;
66
97
  };
98
+
99
+ /*
100
+ Functions to lighten or darken a hex color by a certain percent. Under the hood,
101
+ it merges together a black or white opacity color with the original.
102
+
103
+ Lighten a color by 50%
104
+ lighten("#505050", .5) => "rgb(167, 167, 167)"
105
+
106
+ Darken a color by 50%
107
+ darken("#505050", .5) => "rgb(40, 40, 40)"
108
+ */
109
+ export const lighten = (hexColor, opacity) =>
110
+ addOpacityToHex(hexColor, "#ffffff", opacity);
111
+ export const darken = (hexColor, opacity) =>
112
+ addOpacityToHex(hexColor, "#000000", opacity);