@thecb/components 11.11.0-beta.8 → 11.11.0
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
|
@@ -38,7 +38,13 @@ const Tooltip = ({
|
|
|
38
38
|
customTriggerRole,
|
|
39
39
|
backgroundColor = WHITE
|
|
40
40
|
}) => {
|
|
41
|
+
/**
|
|
42
|
+
* closeTimeoutRef is used internally to store a timer ID for delaying tooltip close. It will have a `.current` property (the timer ID) when the effect or event handlers set it.
|
|
43
|
+
*/
|
|
41
44
|
const closeTimeoutRef = useRef(null);
|
|
45
|
+
/**
|
|
46
|
+
* containerRef is used to store a reference to the container element. It will have a `.current` property (the container element) when the effect or event handlers set it.
|
|
47
|
+
*/
|
|
42
48
|
const containerRef = useRef(null);
|
|
43
49
|
|
|
44
50
|
const [tooltipOpen, setTooltipOpen] = useState(false);
|
|
@@ -65,6 +71,7 @@ const Tooltip = ({
|
|
|
65
71
|
};
|
|
66
72
|
|
|
67
73
|
/**
|
|
74
|
+
* @function renderTrigger
|
|
68
75
|
* Renders the tooltip trigger element.
|
|
69
76
|
*
|
|
70
77
|
* When `hasCustomTrigger` is true, the provided child element is cloned and
|
|
@@ -91,7 +98,9 @@ const Tooltip = ({
|
|
|
91
98
|
|
|
92
99
|
if (hasCustomTrigger && children) {
|
|
93
100
|
const child = React.Children.only(children);
|
|
94
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Capture the child's existing handlers before overwriting
|
|
103
|
+
*/
|
|
95
104
|
const {
|
|
96
105
|
onFocus: childOnFocus,
|
|
97
106
|
onBlur: childOnBlur,
|
|
@@ -99,8 +108,12 @@ const Tooltip = ({
|
|
|
99
108
|
onTouchStart: childOnTouchStart
|
|
100
109
|
} = child.props ?? {};
|
|
101
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Clone the child element and add the necessary event handlers
|
|
113
|
+
*/
|
|
102
114
|
return React.cloneElement(child, {
|
|
103
115
|
tabIndex: child.props?.tabIndex ?? 0,
|
|
116
|
+
ariaDescribedby: tooltipID,
|
|
104
117
|
style: { cursor: `pointer`, ...child.props?.style },
|
|
105
118
|
onFocus: e => {
|
|
106
119
|
childOnFocus?.(e);
|
|
@@ -130,7 +143,7 @@ const Tooltip = ({
|
|
|
130
143
|
variant={triggerButtonVariant}
|
|
131
144
|
text={triggerText}
|
|
132
145
|
tabIndex={0}
|
|
133
|
-
|
|
146
|
+
aria-describedby={tooltipID}
|
|
134
147
|
onFocus={() => handleToggleTooltip(true)}
|
|
135
148
|
onBlur={() => handleToggleTooltip(false)}
|
|
136
149
|
onTouchStart={() => handleToggleTooltip(true)}
|
|
@@ -145,6 +158,12 @@ const Tooltip = ({
|
|
|
145
158
|
);
|
|
146
159
|
}
|
|
147
160
|
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @function handleMouseEnter
|
|
164
|
+
* Handles the mouse enter event for the tooltip container.
|
|
165
|
+
* It clears any existing timeout and opens the tooltip.
|
|
166
|
+
*/
|
|
148
167
|
const handleMouseEnter = () => {
|
|
149
168
|
if (closeTimeoutRef.current) {
|
|
150
169
|
clearTimeout(closeTimeoutRef.current);
|
|
@@ -152,13 +171,22 @@ const Tooltip = ({
|
|
|
152
171
|
}
|
|
153
172
|
handleToggleTooltip(true);
|
|
154
173
|
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @function handleMouseLeave
|
|
177
|
+
* Handles the mouse leave event for the tooltip container.
|
|
178
|
+
* It sets a timeout to close the tooltip after 200ms.
|
|
179
|
+
*/
|
|
155
180
|
const handleMouseLeave = () => {
|
|
156
181
|
closeTimeoutRef.current = setTimeout(() => {
|
|
157
182
|
handleToggleTooltip(false);
|
|
158
|
-
},
|
|
183
|
+
}, 200);
|
|
159
184
|
};
|
|
160
185
|
|
|
161
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Handles the touch start event for the tooltip container.
|
|
188
|
+
* It closes the tooltip if the touch is outside the container.
|
|
189
|
+
*/
|
|
162
190
|
useEffect(() => {
|
|
163
191
|
if (!tooltipOpen) return;
|
|
164
192
|
|
|
@@ -171,10 +199,12 @@ const Tooltip = ({
|
|
|
171
199
|
return () => document.removeEventListener("touchstart", handleOutsideTouch);
|
|
172
200
|
}, [tooltipOpen]);
|
|
173
201
|
|
|
174
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Cleans up the timeout when the component unmounts.
|
|
204
|
+
*/
|
|
175
205
|
useEffect(() => {
|
|
176
206
|
return () => {
|
|
177
|
-
if (closeTimeoutRef
|
|
207
|
+
if (closeTimeoutRef?.current) clearTimeout(closeTimeoutRef.current);
|
|
178
208
|
};
|
|
179
209
|
}, []);
|
|
180
210
|
|