@thecb/components 11.8.0-beta.6 → 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.6",
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,24 +1,34 @@
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 { fallbackValues } from "./Tooltip.theme";
9
9
  import WarningIconXS from "../../atoms/icons/WarningIconXS";
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
+ };
10
24
 
11
25
  const Tooltip = ({
12
- tooltipContainerExtraStyles = "",
13
- themeValues,
14
- tooltipID = "tooltip-content",
26
+ tooltipID,
15
27
  hasIconTrigger = false,
16
28
  IconTrigger = WarningIconXS,
17
29
  iconHelpText = "Open the tooltip",
18
- tooltipContent = "",
19
- tooltipTriggerText = "",
20
- triggerTextExtraStyles = "",
21
- contentExtraStyles = "",
30
+ triggerText = "Open the tooltip",
31
+ tooltipContent = "The contents of the tooltip go here.",
22
32
  contentPosition = {
23
33
  top: "-110px",
24
34
  right: "auto",
@@ -35,19 +45,32 @@ const Tooltip = ({
35
45
  minWidth = "250px",
36
46
  maxWidth = "300px",
37
47
  height = "auto",
38
- buttonExtraStyles,
39
- backgroundColor = "white",
40
- borderColor = "rgba(255, 255, 255, 0.85)"
48
+ containerExtraStyles = "",
49
+ triggerExtraStyles = "",
50
+ triggerButtonVariant = "smallGhost"
41
51
  }) => {
42
- const { hoverColor, activeColor, tooltipTriggerColor } = themeValues;
43
- const { top, right, bottom, left } = contentPosition ?? {};
44
- const { arrowTop, arrowRight, arrowBottom, arrowLeft } = arrowPosition ?? {};
45
-
52
+ const closeTimeoutRef = useRef(null);
46
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;
47
67
 
48
- const handleToggleTooltip = tooltipState => {
49
- if (tooltipOpen !== tooltipState) {
50
- setTooltipOpen(tooltipState);
68
+ const { top, right, bottom, left } = contentPosition;
69
+ const { arrowTop, arrowRight, arrowBottom, arrowLeft } = arrowPosition;
70
+
71
+ const handleToggleTooltip = desiredTooltipState => {
72
+ if (tooltipOpen !== desiredTooltipState) {
73
+ setTooltipOpen(desiredTooltipState);
51
74
  }
52
75
  };
53
76
 
@@ -57,64 +80,97 @@ const Tooltip = ({
57
80
  }
58
81
  };
59
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
+
60
105
  return (
61
106
  <Box
107
+ ref={closeTimeoutRef}
62
108
  padding="0"
63
- extraStyles={`position: relative; ${tooltipContainerExtraStyles}`}
109
+ extraStyles={`position: relative; ${containerExtraStyles}`}
110
+ action={() => noop}
111
+ onMouseEnter={handleMouseEnter}
112
+ onMouseLeave={handleMouseLeave}
113
+ onKeyDown={handleKeyboardEvent}
114
+ data-qa="tooltip-container"
64
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={tooltipTriggerColor} />
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}; ${triggerTextExtraStyles}`}
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
- {tooltipTriggerText}
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
- ${contentExtraStyles};
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
175
  <Paragraph>{tooltipContent}</Paragraph>
120
176
  <Box
@@ -137,4 +193,4 @@ const Tooltip = ({
137
193
  );
138
194
  };
139
195
 
140
- export default themeComponent(Tooltip, "Tooltip", fallbackValues);
196
+ export default Tooltip;