@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,28 +1,40 @@
|
|
|
1
|
-
import React, { useState } from "react";
|
|
2
|
-
import {
|
|
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 {
|
|
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
|
-
|
|
15
|
-
triggerText = "",
|
|
16
|
-
content = "",
|
|
26
|
+
tooltipID,
|
|
17
27
|
hasIconTrigger = false,
|
|
18
28
|
IconTrigger = WarningIconXS,
|
|
19
|
-
iconHelpText = "",
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
|
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 =
|
|
52
|
-
if (tooltipOpen !==
|
|
53
|
-
setTooltipOpen(
|
|
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 ===
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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={
|
|
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
|
-
{
|
|
135
|
+
{hasIconTrigger === false && (
|
|
91
136
|
<Text
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
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>{
|
|
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
|
|
196
|
+
export default Tooltip;
|
|
@@ -9,28 +9,27 @@ export default {
|
|
|
9
9
|
controls: { expanded: true }
|
|
10
10
|
},
|
|
11
11
|
args: {
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
20
|
-
|
|
18
|
+
tooltipContainerExtraStyles: undefined,
|
|
19
|
+
triggerTextExtraStyles: undefined,
|
|
21
20
|
minWidth: "250px",
|
|
22
21
|
maxWidth: "300px",
|
|
23
22
|
height: "auto",
|
|
24
|
-
|
|
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
|
-
|
|
29
|
+
contentExtraStyles: undefined
|
|
31
30
|
},
|
|
32
31
|
argTypes: {
|
|
33
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
180
|
-
|
|
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
|
-
|
|
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
|
-
|
|
197
|
-
|
|
188
|
+
tooltipTriggerText: "What's this?",
|
|
189
|
+
tooltipContent:
|
|
198
190
|
"Contact support at 1-800-CTY-VILL for help with your account balance.",
|
|
199
|
-
|
|
191
|
+
contentPosition: { top: "50px", left: "-100px" },
|
|
200
192
|
arrowPosition: { arrowTop: "-8px", arrowLeft: "50%" },
|
|
201
193
|
arrowDirection: "up"
|
|
202
194
|
}
|