@thecb/components 11.11.0-beta.8 → 11.11.0-beta.9
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,6 +108,9 @@ 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,
|
|
104
116
|
style: { cursor: `pointer`, ...child.props?.style },
|
|
@@ -145,6 +157,12 @@ const Tooltip = ({
|
|
|
145
157
|
);
|
|
146
158
|
}
|
|
147
159
|
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @function handleMouseEnter
|
|
163
|
+
* Handles the mouse enter event for the tooltip container.
|
|
164
|
+
* It clears any existing timeout and opens the tooltip.
|
|
165
|
+
*/
|
|
148
166
|
const handleMouseEnter = () => {
|
|
149
167
|
if (closeTimeoutRef.current) {
|
|
150
168
|
clearTimeout(closeTimeoutRef.current);
|
|
@@ -152,13 +170,22 @@ const Tooltip = ({
|
|
|
152
170
|
}
|
|
153
171
|
handleToggleTooltip(true);
|
|
154
172
|
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @function handleMouseLeave
|
|
176
|
+
* Handles the mouse leave event for the tooltip container.
|
|
177
|
+
* It sets a timeout to close the tooltip after 200ms.
|
|
178
|
+
*/
|
|
155
179
|
const handleMouseLeave = () => {
|
|
156
180
|
closeTimeoutRef.current = setTimeout(() => {
|
|
157
181
|
handleToggleTooltip(false);
|
|
158
|
-
},
|
|
182
|
+
}, 200);
|
|
159
183
|
};
|
|
160
184
|
|
|
161
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Handles the touch start event for the tooltip container.
|
|
187
|
+
* It closes the tooltip if the touch is outside the container.
|
|
188
|
+
*/
|
|
162
189
|
useEffect(() => {
|
|
163
190
|
if (!tooltipOpen) return;
|
|
164
191
|
|
|
@@ -171,10 +198,12 @@ const Tooltip = ({
|
|
|
171
198
|
return () => document.removeEventListener("touchstart", handleOutsideTouch);
|
|
172
199
|
}, [tooltipOpen]);
|
|
173
200
|
|
|
174
|
-
|
|
201
|
+
/**
|
|
202
|
+
* Cleans up the timeout when the component unmounts.
|
|
203
|
+
*/
|
|
175
204
|
useEffect(() => {
|
|
176
205
|
return () => {
|
|
177
|
-
if (closeTimeoutRef
|
|
206
|
+
if (closeTimeoutRef?.current) clearTimeout(closeTimeoutRef.current);
|
|
178
207
|
};
|
|
179
208
|
}, []);
|
|
180
209
|
|