@thecb/components 11.8.0-beta.5 → 11.8.0-beta.7

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": "11.8.0-beta.5",
3
+ "version": "11.8.0-beta.7",
4
4
  "description": "Common lib for CityBase react components",
5
5
  "main": "dist/index.cjs.js",
6
6
  "typings": "dist/index.d.ts",
@@ -1,28 +1,40 @@
1
- import React, { useState } from "react";
2
- import { themeComponent } from "../../../util/themeUtils";
1
+ import React, { useContext, useEffect, useRef, useState } from "react";
2
+ import { createThemeValues } from "../../../util/themeUtils";
3
+ import { ThemeContext } from "styled-components";
3
4
  import Text from "../../atoms/text";
4
5
  import Paragraph from "../../atoms/paragraph";
5
6
  import { Box } from "../../atoms/layouts";
6
7
  import ButtonWithAction from "../../atoms/button-with-action";
7
8
  import { noop, arrowBorder } from "../../../util/general";
8
- import { ESCAPE } from "../../../constants/keyboard";
9
- import { fallbackValues } from "./Tooltip.theme";
10
9
  import WarningIconXS from "../../atoms/icons/WarningIconXS";
11
- import { SELECTIVE_YELLOW } from "../../../constants/colors";
10
+ import {
11
+ MATISSE_BLUE,
12
+ PEACOCK_BLUE,
13
+ SAPPHIRE_BLUE
14
+ } from "../../../constants/colors";
15
+
16
+ const TOOLTIP_THEME_SOURCE = "Popover";
17
+
18
+ const fallbackValues = {
19
+ hoverColor: SAPPHIRE_BLUE,
20
+ activeColor: PEACOCK_BLUE,
21
+ popoverTriggerColor: MATISSE_BLUE,
22
+ borderColor: `rgba(255, 255, 255, 0.85)`
23
+ };
12
24
 
13
25
  const Tooltip = ({
14
- themeValues,
15
- triggerText = "",
16
- content = "",
26
+ tooltipID,
17
27
  hasIconTrigger = false,
18
28
  IconTrigger = WarningIconXS,
19
- iconHelpText = "",
20
- tooltipID = "tooltip-content",
21
- extraStyles = "",
22
- textExtraStyles = "",
23
- minWidth = "250px",
24
- maxWidth = "300px",
25
- height = "auto",
29
+ iconHelpText = "Open the tooltip",
30
+ triggerText = "Open the tooltip",
31
+ tooltipContent = "The contents of the tooltip go here.",
32
+ contentPosition = {
33
+ top: "-110px",
34
+ right: "auto",
35
+ bottom: "auto",
36
+ left: "-225px"
37
+ },
26
38
  arrowDirection = "down",
27
39
  arrowPosition = {
28
40
  arrowTop: "auto",
@@ -30,93 +42,137 @@ const Tooltip = ({
30
42
  arrowBottom: "-8px",
31
43
  arrowLeft: "auto"
32
44
  },
33
- position = {
34
- top: "-110px",
35
- right: "auto",
36
- bottom: "auto",
37
- left: "-225px"
38
- },
39
- iconColor = SELECTIVE_YELLOW,
40
- buttonExtraStyles,
41
- backgroundColor = "white",
42
- borderColor = "rgba(255, 255, 255, 0.85)",
43
- tooltipContentExtraStyles
45
+ minWidth = "250px",
46
+ maxWidth = "300px",
47
+ height = "auto",
48
+ containerExtraStyles = "",
49
+ triggerExtraStyles = "",
50
+ triggerButtonVariant = "smallGhost"
44
51
  }) => {
45
- const { hoverColor, activeColor, tooltipTriggerColor } = themeValues;
46
- const { top, right, bottom, left } = position ?? {};
47
- const { arrowTop, arrowRight, arrowBottom, arrowLeft } = arrowPosition ?? {};
48
-
52
+ const closeTimeoutRef = useRef(null);
49
53
  const [tooltipOpen, setTooltipOpen] = useState(false);
54
+ const themeContext = useContext(ThemeContext);
55
+ const themeValues = createThemeValues(
56
+ themeContext,
57
+ fallbackValues,
58
+ TOOLTIP_THEME_SOURCE
59
+ );
60
+
61
+ const {
62
+ borderColor,
63
+ popoverTriggerColor,
64
+ hoverColor,
65
+ activeColor
66
+ } = themeValues;
67
+
68
+ const { top, right, bottom, left } = contentPosition;
69
+ const { arrowTop, arrowRight, arrowBottom, arrowLeft } = arrowPosition;
50
70
 
51
- const handleToggleTooltip = tooltipState => {
52
- if (tooltipOpen !== tooltipState) {
53
- setTooltipOpen(tooltipState);
71
+ const handleToggleTooltip = desiredTooltipState => {
72
+ if (tooltipOpen !== desiredTooltipState) {
73
+ setTooltipOpen(desiredTooltipState);
54
74
  }
55
75
  };
56
76
 
57
77
  const handleKeyboardEvent = e => {
58
- if (e.key === ESCAPE) {
78
+ if (e.key === "Escape") {
59
79
  handleToggleTooltip(false);
60
80
  }
61
81
  };
62
82
 
83
+ const handleMouseEnter = () => {
84
+ if (closeTimeoutRef.current) {
85
+ clearTimeout(closeTimeoutRef.current);
86
+ closeTimeoutRef.current = null;
87
+ }
88
+ handleToggleTooltip(true);
89
+ };
90
+
91
+ const handleMouseLeave = () => {
92
+ closeTimeoutRef.current = setTimeout(() => {
93
+ handleToggleTooltip(false);
94
+ }, 300);
95
+ };
96
+
97
+ useEffect(() => {
98
+ return () => {
99
+ if (closeTimeoutRef.current) {
100
+ clearTimeout(closeTimeoutRef.current);
101
+ }
102
+ };
103
+ }, []);
104
+
63
105
  return (
64
- <Box padding="0" extraStyles={`position: relative; ${extraStyles}`}>
106
+ <Box
107
+ ref={closeTimeoutRef}
108
+ padding="0"
109
+ extraStyles={`position: relative; ${containerExtraStyles}`}
110
+ action={() => noop}
111
+ onMouseEnter={handleMouseEnter}
112
+ onMouseLeave={handleMouseLeave}
113
+ onKeyDown={handleKeyboardEvent}
114
+ data-qa="tooltip-container"
115
+ >
65
116
  <ButtonWithAction
66
- action={() => noop}
117
+ aria-describedby={tooltipID}
118
+ variant={triggerButtonVariant}
67
119
  onFocus={() => handleToggleTooltip(true)}
68
120
  onBlur={() => handleToggleTooltip(false)}
69
- onKeyDown={handleKeyboardEvent}
70
121
  onTouchStart={() => handleToggleTooltip(true)}
71
- onTouchEnd={() => handleToggleTooltip(false)}
72
- onMouseEnter={() => handleToggleTooltip(true)}
73
- onMouseLeave={() => handleToggleTooltip(false)}
122
+ data-qa="tooltip-trigger"
74
123
  contentOverride
75
- variant="smallGhost"
76
- tabIndex="0"
77
- aria-describedby={tooltipID}
78
- extraStyles={buttonExtraStyles}
79
124
  >
80
- {hasIconTrigger && (
125
+ {hasIconTrigger === true && (
81
126
  <>
82
127
  <Box aria-label="Open tooltip">
83
- <IconTrigger color={iconColor} />
128
+ <IconTrigger color={themeValues.links} />
84
129
  </Box>
85
130
  <Box padding="0" srOnly>
86
131
  <Text>{iconHelpText}</Text>
87
132
  </Box>
88
133
  </>
89
134
  )}
90
- {!hasIconTrigger && (
135
+ {hasIconTrigger === false && (
91
136
  <Text
92
- extraStyles={`color: ${tooltipTriggerColor}; &:hover {
93
- ${hoverColor}; &:active ${activeColor}; ${textExtraStyles}`}
137
+ color={popoverTriggerColor}
138
+ extraStyles={`
139
+ &:visited {
140
+ color: ${popoverTriggerColor};
141
+ }
142
+ &:hover {
143
+ color: ${hoverColor};
144
+ }
145
+ &:active {
146
+ color: ${activeColor};
147
+ }
148
+ ${triggerExtraStyles};
149
+ `}
94
150
  >
95
151
  {triggerText}
96
152
  </Text>
97
153
  )}
98
154
  </ButtonWithAction>
99
155
  <Box
100
- background={backgroundColor}
101
- borderRadius="4px"
102
- boxShadow="0px 2px 14px 0px rgb(246, 246, 249), 0px 3px 8px 0px rgb(202, 206, 216)"
103
- id={tooltipID}
104
156
  role="tooltip"
105
- minWidth={minWidth}
106
- maxWidth={maxWidth}
157
+ id={tooltipID}
107
158
  aria-hidden={!tooltipOpen}
159
+ data-qa="tooltip-contents"
108
160
  extraStyles={`
161
+ position: absolute;
109
162
  display: ${tooltipOpen ? "block" : "none"};
110
- position: absolute;
111
163
  top: ${top};
112
164
  right: ${right};
113
165
  bottom: ${bottom};
114
166
  left: ${left};
115
167
  height: ${height};
116
- ${tooltipContentExtraStyles};
117
168
  `}
169
+ boxShadow={`0px 2px 14px 0px rgb(246, 246, 249), 0px 3px 8px 0px rgb(202, 206, 216)`}
170
+ border={`1px solid transparent`}
171
+ borderRadius="4px"
172
+ minWidth={minWidth}
173
+ maxWidth={maxWidth}
118
174
  >
119
- <Paragraph>{content}</Paragraph>
175
+ <Paragraph>{tooltipContent}</Paragraph>
120
176
  <Box
121
177
  padding="0"
122
178
  extraStyles={`
@@ -137,4 +193,4 @@ const Tooltip = ({
137
193
  );
138
194
  };
139
195
 
140
- export default themeComponent(Tooltip, "Tooltip", fallbackValues);
196
+ export default Tooltip;
@@ -9,28 +9,27 @@ export default {
9
9
  controls: { expanded: true }
10
10
  },
11
11
  args: {
12
- triggerText: "",
13
- content: "",
12
+ tooltipTriggerText: "",
13
+ tooltipContent: "",
14
14
  hasIconTrigger: false,
15
15
  IconTrigger: WarningIconXS,
16
- iconHelpText: "",
17
- iconColor: undefined,
16
+ iconHelpText: "Open the tooltip",
18
17
  tooltipID: "tooltip-content",
19
- extraStyles: undefined,
20
- textExtraStyles: undefined,
18
+ tooltipContainerExtraStyles: undefined,
19
+ triggerTextExtraStyles: undefined,
21
20
  minWidth: "250px",
22
21
  maxWidth: "300px",
23
22
  height: "auto",
24
- position: undefined,
23
+ contentPosition: undefined,
25
24
  arrowPosition: undefined,
26
25
  arrowDirection: "down",
27
26
  buttonExtraStyles: undefined,
28
27
  backgroundColor: "white",
29
28
  borderColor: "rgba(255, 255, 255, 0.85)",
30
- tooltipContentExtraStyles: undefined
29
+ contentExtraStyles: undefined
31
30
  },
32
31
  argTypes: {
33
- triggerText: {
32
+ tooltipTriggerText: {
34
33
  description:
35
34
  "Text element that tooltip is anchored to. Only used if hasIconTrigger is false.",
36
35
  table: {
@@ -38,7 +37,7 @@ export default {
38
37
  defaultValue: { summary: "" }
39
38
  }
40
39
  },
41
- content: {
40
+ tooltipContent: {
42
41
  description: "Content of the tooltip",
43
42
  table: {
44
43
  type: { summary: "string" },
@@ -65,14 +64,7 @@ export default {
65
64
  description: "Accessible description of the icon",
66
65
  table: {
67
66
  type: { summary: "string" },
68
- defaultValue: { summary: undefined }
69
- }
70
- },
71
- iconColor: {
72
- description: "Color of the icon trigger",
73
- table: {
74
- type: { summary: "string" },
75
- defaultValue: { summary: undefined }
67
+ defaultValue: { summary: "Open the tooltip" }
76
68
  }
77
69
  },
78
70
  tooltipID: {
@@ -83,7 +75,7 @@ export default {
83
75
  defaultValue: { summary: "tooltip-content" }
84
76
  }
85
77
  },
86
- extraStyles: {
78
+ tooltipContainerExtraStyles: {
87
79
  description:
88
80
  "Extra CSS styles to apply to the wrapper component around the trigger and tooltip",
89
81
  table: {
@@ -91,7 +83,7 @@ export default {
91
83
  defaultValue: { summary: undefined }
92
84
  }
93
85
  },
94
- textExtraStyles: {
86
+ triggerTextExtraStyles: {
95
87
  description: "Extra styles to apply to the text trigger of the tooltip",
96
88
  table: {
97
89
  type: { summary: "CSS string" },
@@ -119,7 +111,7 @@ export default {
119
111
  defaultValue: { summary: "auto" }
120
112
  }
121
113
  },
122
- position: {
114
+ contentPosition: {
123
115
  description:
124
116
  "Object containing values for top/right/bottom/left position of tooltip relative to trigger",
125
117
  table: {
@@ -164,7 +156,7 @@ export default {
164
156
  defaultValue: { summary: "rgba(255, 255, 255, 0.85)" }
165
157
  }
166
158
  },
167
- tooltipContentExtraStyles: {
159
+ contentExtraStyles: {
168
160
  description: "Extra styles to apply to the tooltip content box",
169
161
  table: {
170
162
  type: { summary: "string" },
@@ -176,8 +168,8 @@ export default {
176
168
 
177
169
  export const BasicTooltip = {
178
170
  args: {
179
- triggerText: "Help",
180
- content:
171
+ tooltipTriggerText: "Help",
172
+ tooltipContent:
181
173
  "Contact support at 1-800-CTY-VILL for help with your account balance."
182
174
  }
183
175
  };
@@ -186,17 +178,17 @@ export const IconTooltip = {
186
178
  args: {
187
179
  hasIconTrigger: true,
188
180
  IconTrigger: WarningIconXS,
189
- content:
181
+ tooltipContent:
190
182
  "Contact support at 1-800-CTY-VILL for help with your account balance."
191
183
  }
192
184
  };
193
185
 
194
186
  export const UnderneathTooltip = {
195
187
  args: {
196
- triggerText: "What's this?",
197
- content:
188
+ tooltipTriggerText: "What's this?",
189
+ tooltipContent:
198
190
  "Contact support at 1-800-CTY-VILL for help with your account balance.",
199
- position: { top: "50px", left: "-100px" },
191
+ contentPosition: { top: "50px", left: "-100px" },
200
192
  arrowPosition: { arrowTop: "-8px", arrowLeft: "50%" },
201
193
  arrowDirection: "up"
202
194
  }