| 1 |
import { cloneElement, ReactElement, createRef, useEffect, useState } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Tooltip/tooltip'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { TooltipContent } from './TooltipContent'; |
| 5 |
import { TooltipArrow } from './TooltipArrow'; |
| 6 |
import { KeyTypes } from '../../helpers/constants'; |
| 7 |
import { useSSRSafeId } from '../../helpers'; |
| 8 |
import tooltipMaxWidth from '@patternfly/react-tokens/dist/esm/c_tooltip_MaxWidth'; |
| 9 |
import { Popper } from '../../helpers/Popper/Popper'; |
| 10 |
|
| 11 |
export enum TooltipPosition { |
| 12 |
auto = 'auto', |
| 13 |
top = 'top', |
| 14 |
bottom = 'bottom', |
| 15 |
left = 'left', |
| 16 |
right = 'right', |
| 17 |
topStart = 'top-start', |
| 18 |
topEnd = 'top-end', |
| 19 |
bottomStart = 'bottom-start', |
| 20 |
bottomEnd = 'bottom-end', |
| 21 |
leftStart = 'left-start', |
| 22 |
leftEnd = 'left-end', |
| 23 |
rightStart = 'right-start', |
| 24 |
rightEnd = 'right-end' |
| 25 |
} |
| 26 |
|
| 27 |
export interface TooltipProps extends Omit<React.HTMLProps<HTMLDivElement>, 'content'> { |
| 28 |
/** The element to append the tooltip to, defaults to body */ |
| 29 |
appendTo?: HTMLElement | ((ref?: HTMLElement) => HTMLElement); |
| 30 |
/** |
| 31 |
* aria-labelledby or aria-describedby for tooltip. |
| 32 |
* The trigger will be cloned to add the aria attribute, and the corresponding id in the form of 'pf-tooltip-#' is added to the content container. |
| 33 |
* If you don't want that or prefer to add the aria attribute yourself on the trigger, set aria to 'none'. |
| 34 |
*/ |
| 35 |
aria?: 'describedby' | 'labelledby' | 'none'; |
| 36 |
/** |
| 37 |
* Determines whether the tooltip is an aria-live region. If the triggerRef prop is passed in the |
| 38 |
* default behavior is 'polite' in order to ensure the tooltip contents is announced to |
| 39 |
* assistive technologies. Otherwise the default behavior is 'off'. |
| 40 |
*/ |
| 41 |
'aria-live'?: 'off' | 'polite'; |
| 42 |
/** |
| 43 |
* The trigger reference element to which the Tooltip is relatively placed to. |
| 44 |
* If you cannot wrap the element with the Tooltip, you can use the triggerRef prop instead. |
| 45 |
* Usage: <Tooltip><Button>Reference</Button></Tooltip> |
| 46 |
*/ |
| 47 |
children?: ReactElement<any>; |
| 48 |
/** |
| 49 |
* The trigger reference element to which the Tooltip is relatively placed to. |
| 50 |
* If you can wrap the element with the Tooltip, you can use the children prop instead, or both props together. |
| 51 |
* When passed along with the trigger prop, the div element that wraps the trigger will be removed. |
| 52 |
* Usage: <Tooltip triggerRef={() => document.getElementById('reference-element')} /> |
| 53 |
*/ |
| 54 |
triggerRef?: HTMLElement | (() => HTMLElement) | React.RefObject<any>; |
| 55 |
/** Tooltip additional class */ |
| 56 |
className?: string; |
| 57 |
/** Tooltip content */ |
| 58 |
content: React.ReactNode; |
| 59 |
/** Distance of the tooltip to its target, defaults to 15 */ |
| 60 |
distance?: number; |
| 61 |
/** If true, tries to keep the tooltip in view by flipping it if necessary */ |
| 62 |
enableFlip?: boolean; |
| 63 |
/** Delay in ms before the tooltip appears */ |
| 64 |
entryDelay?: number; |
| 65 |
/** Delay in ms before the tooltip disappears, Avoid passing in a value of "0", as users should |
| 66 |
* be given ample time to move their mouse from the trigger to the tooltip content without the content |
| 67 |
* being hidden. |
| 68 |
*/ |
| 69 |
exitDelay?: number; |
| 70 |
/** |
| 71 |
* The desired position to flip the tooltip to if the initial position is not possible. |
| 72 |
* By setting this prop to 'flip' it attempts to flip the tooltip to the opposite side if there is no space. |
| 73 |
* You can also pass an array of positions that determines the flip order. It should contain the initial position |
| 74 |
* followed by alternative positions if that position is unavailable. |
| 75 |
* Example: Initial position is 'top'. Button with tooltip is in the top right corner. 'flipBehavior' is set to |
| 76 |
* ['top', 'right', 'left']. Since there is no space to the top, it checks if right is available. There's also no |
| 77 |
* space to the right, so it finally shows the tooltip on the left. |
| 78 |
*/ |
| 79 |
flipBehavior?: |
| 80 |
| 'flip' |
| 81 |
| ( |
| 82 |
| 'top' |
| 83 |
| 'bottom' |
| 84 |
| 'left' |
| 85 |
| 'right' |
| 86 |
| 'top-start' |
| 87 |
| 'top-end' |
| 88 |
| 'bottom-start' |
| 89 |
| 'bottom-end' |
| 90 |
| 'left-start' |
| 91 |
| 'left-end' |
| 92 |
| 'right-start' |
| 93 |
| 'right-end' |
| 94 |
)[]; |
| 95 |
/** Minimum width of the tooltip. If set to "trigger", the minimum width will be set to the reference element width. */ |
| 96 |
minWidth?: string | 'trigger'; |
| 97 |
/** Maximum width of the tooltip (default 18.75rem) */ |
| 98 |
maxWidth?: string; |
| 99 |
/** Callback when tooltip's hide transition has finished executing */ |
| 100 |
onTooltipHidden?: () => void; |
| 101 |
/** |
| 102 |
* Tooltip position. Note: With 'enableFlip' set to true, |
| 103 |
* it will change the position if there is not enough space for the starting position. |
| 104 |
* The behavior of where it flips to can be controlled through the flipBehavior prop. |
| 105 |
* The 'auto' position chooses the side with the most space. |
| 106 |
* The 'auto' position requires the 'enableFlip' prop to be true. |
| 107 |
*/ |
| 108 |
position?: |
| 109 |
| TooltipPosition |
| 110 |
| 'auto' |
| 111 |
| 'top' |
| 112 |
| 'bottom' |
| 113 |
| 'left' |
| 114 |
| 'right' |
| 115 |
| 'top-start' |
| 116 |
| 'top-end' |
| 117 |
| 'bottom-start' |
| 118 |
| 'bottom-end' |
| 119 |
| 'left-start' |
| 120 |
| 'left-end' |
| 121 |
| 'right-start' |
| 122 |
| 'right-end'; |
| 123 |
/** |
| 124 |
* Tooltip trigger: click, mouseenter, focus, manual |
| 125 |
* Set to manual to trigger tooltip programmatically (through the isVisible prop) |
| 126 |
*/ |
| 127 |
trigger?: string; |
| 128 |
/** Flag to indicate that the text content is left aligned */ |
| 129 |
isContentLeftAligned?: boolean; |
| 130 |
/** value for visibility when trigger is 'manual' */ |
| 131 |
isVisible?: boolean; |
| 132 |
/** z-index of the tooltip */ |
| 133 |
zIndex?: number; |
| 134 |
/** id of the tooltip */ |
| 135 |
id?: string; |
| 136 |
/** CSS fade transition animation duration */ |
| 137 |
animationDuration?: number; |
| 138 |
} |
| 139 |
|
| 140 |
export const Tooltip: React.FunctionComponent<TooltipProps> = ({ |
| 141 |
content: bodyContent, |
| 142 |
position = 'top', |
| 143 |
trigger = 'mouseenter focus', |
| 144 |
isVisible = false, |
| 145 |
isContentLeftAligned = false, |
| 146 |
enableFlip = true, |
| 147 |
className = '', |
| 148 |
entryDelay = 300, |
| 149 |
exitDelay = 300, |
| 150 |
appendTo = () => document.body, |
| 151 |
zIndex = 9999, |
| 152 |
minWidth, |
| 153 |
maxWidth = tooltipMaxWidth.value, |
| 154 |
distance = 15, |
| 155 |
aria = 'describedby', |
| 156 |
// For every initial starting position, there are 3 escape positions |
| 157 |
flipBehavior = ['top', 'right', 'bottom', 'left', 'top', 'right', 'bottom'], |
| 158 |
id: idProp, |
| 159 |
children, |
| 160 |
animationDuration = 300, |
| 161 |
triggerRef, |
| 162 |
'aria-live': ariaLive = triggerRef ? 'polite' : 'off', |
| 163 |
onTooltipHidden = () => {}, |
| 164 |
...rest |
| 165 |
}: TooltipProps) => { |
| 166 |
const generatedId = useSSRSafeId('pf-tooltip-'); |
| 167 |
const id = idProp ?? generatedId; |
| 168 |
// could make this a prop in the future (true | false | 'toggle') |
| 169 |
const hideOnClick = true; |
| 170 |
const triggerOnMouseenter = trigger.includes('mouseenter'); |
| 171 |
const triggerOnFocus = trigger.includes('focus'); |
| 172 |
const triggerOnClick = trigger.includes('click'); |
| 173 |
const triggerManually = trigger === 'manual'; |
| 174 |
const [visible, setVisible] = useState(false); |
| 175 |
const popperRef = createRef<HTMLDivElement>(); |
| 176 |
|
| 177 |
const getTriggerRefElement = (): HTMLElement | null => { |
| 178 |
if (typeof triggerRef === 'function') { |
| 179 |
return triggerRef(); |
| 180 |
} else if (triggerRef && typeof triggerRef === 'object' && 'current' in triggerRef) { |
| 181 |
return triggerRef.current; |
| 182 |
} else if (triggerRef instanceof HTMLElement) { |
| 183 |
return triggerRef; |
| 184 |
} |
| 185 |
return null; |
| 186 |
}; |
| 187 |
|
| 188 |
const getAriaAttributeName = () => (aria !== 'none' ? `aria-${aria}` : null); |
| 189 |
|
| 190 |
const addAriaToRefElement = (element: HTMLElement): void => { |
| 191 |
const attributeName = getAriaAttributeName(); |
| 192 |
if (!element || !attributeName) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
const existingAria = element.getAttribute(attributeName); |
| 197 |
if (!existingAria || !existingAria.includes(id)) { |
| 198 |
const newAria = existingAria ? `${existingAria} ${id}` : id; |
| 199 |
element.setAttribute(attributeName, newAria); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
const removeAriaFromRefElement = (element: HTMLElement): void => { |
| 204 |
const attributeName = getAriaAttributeName(); |
| 205 |
if (!element || !attributeName) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
const existingAria = element.getAttribute(attributeName); |
| 210 |
if (!existingAria) { |
| 211 |
return; |
| 212 |
} |
| 213 |
const newAria = existingAria |
| 214 |
.split(/\s+/) |
| 215 |
.filter((token) => token !== id) |
| 216 |
.join(' '); |
| 217 |
if (newAria) { |
| 218 |
element.setAttribute(attributeName, newAria); |
| 219 |
} else { |
| 220 |
element.removeAttribute(attributeName); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
const updateTriggerElementAria = (shouldAddAria: boolean): void => { |
| 225 |
if (aria === 'none' || !triggerRef || children) { |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
const triggerElement = getTriggerRefElement(); |
| 230 |
if (!triggerElement) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
if (shouldAddAria) { |
| 235 |
addAriaToRefElement(triggerElement); |
| 236 |
} else { |
| 237 |
removeAriaFromRefElement(triggerElement); |
| 238 |
} |
| 239 |
}; |
| 240 |
|
| 241 |
useEffect(() => { |
| 242 |
updateTriggerElementAria(visible); |
| 243 |
|
| 244 |
return () => { |
| 245 |
updateTriggerElementAria(false); |
| 246 |
}; |
| 247 |
}, [visible, aria, triggerRef, children, id]); |
| 248 |
|
| 249 |
const onDocumentKeyDown = (event: KeyboardEvent) => { |
| 250 |
if (!triggerManually) { |
| 251 |
if (event.key === KeyTypes.Escape && visible) { |
| 252 |
hide(); |
| 253 |
} |
| 254 |
} |
| 255 |
}; |
| 256 |
const onTriggerEnter = (event: KeyboardEvent) => { |
| 257 |
if (event.key === KeyTypes.Enter) { |
| 258 |
if (!visible) { |
| 259 |
show(); |
| 260 |
} else { |
| 261 |
hide(); |
| 262 |
} |
| 263 |
} |
| 264 |
}; |
| 265 |
useEffect(() => { |
| 266 |
if (isVisible) { |
| 267 |
show(); |
| 268 |
} else { |
| 269 |
hide(); |
| 270 |
} |
| 271 |
}, [isVisible]); |
| 272 |
|
| 273 |
const show = () => { |
| 274 |
setVisible(true); |
| 275 |
}; |
| 276 |
const hide = () => { |
| 277 |
setVisible(false); |
| 278 |
}; |
| 279 |
const positionModifiers = { |
| 280 |
top: styles.modifiers.top, |
| 281 |
bottom: styles.modifiers.bottom, |
| 282 |
left: styles.modifiers.left, |
| 283 |
right: styles.modifiers.right, |
| 284 |
'top-start': styles.modifiers.topLeft, |
| 285 |
'top-end': styles.modifiers.topRight, |
| 286 |
'bottom-start': styles.modifiers.bottomLeft, |
| 287 |
'bottom-end': styles.modifiers.bottomRight, |
| 288 |
'left-start': styles.modifiers.leftTop, |
| 289 |
'left-end': styles.modifiers.leftBottom, |
| 290 |
'right-start': styles.modifiers.rightTop, |
| 291 |
'right-end': styles.modifiers.rightBottom |
| 292 |
}; |
| 293 |
const hasCustomMaxWidth = maxWidth !== tooltipMaxWidth.value; |
| 294 |
const content = ( |
| 295 |
<div |
| 296 |
aria-live={ariaLive} |
| 297 |
className={css(styles.tooltip, className)} |
| 298 |
role="tooltip" |
| 299 |
id={id} |
| 300 |
style={{ |
| 301 |
maxWidth: hasCustomMaxWidth ? maxWidth : null |
| 302 |
}} |
| 303 |
ref={popperRef} |
| 304 |
{...rest} |
| 305 |
> |
| 306 |
<TooltipArrow /> |
| 307 |
<TooltipContent isLeftAligned={isContentLeftAligned}>{bodyContent}</TooltipContent> |
| 308 |
</div> |
| 309 |
); |
| 310 |
|
| 311 |
const onDocumentClick = (event: MouseEvent, triggerElement: HTMLElement) => { |
| 312 |
// event.currentTarget = document |
| 313 |
// event.target could be triggerElement or something else |
| 314 |
if (hideOnClick === true) { |
| 315 |
// hide on inside the toggle as well as on outside clicks |
| 316 |
if (visible) { |
| 317 |
hide(); |
| 318 |
} else if (event.target === triggerElement) { |
| 319 |
show(); |
| 320 |
} |
| 321 |
} else if (hideOnClick === 'toggle' && event.target === triggerElement) { |
| 322 |
// prevent outside clicks from hiding but allow it to still be toggled on toggle click |
| 323 |
if (visible) { |
| 324 |
hide(); |
| 325 |
} else { |
| 326 |
show(); |
| 327 |
} |
| 328 |
} else if (hideOnClick === false && !visible && event.target === triggerElement) { |
| 329 |
show(); |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
const addAriaToChildren = () => { |
| 334 |
if (!children) { |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
if (aria === 'describedby' && children.props && !children.props['aria-describedby']) { |
| 339 |
return cloneElement(children, { 'aria-describedby': id }); |
| 340 |
} else if (aria === 'labelledby' && children.props && !children.props['aria-labelledby']) { |
| 341 |
return cloneElement(children, { 'aria-labelledby': id }); |
| 342 |
} |
| 343 |
return children; |
| 344 |
}; |
| 345 |
|
| 346 |
return ( |
| 347 |
<Popper |
| 348 |
trigger={aria !== 'none' && visible ? addAriaToChildren() : children} |
| 349 |
triggerRef={triggerRef} |
| 350 |
popper={content} |
| 351 |
popperRef={popperRef} |
| 352 |
minWidth={minWidth !== undefined ? minWidth : 'revert'} |
| 353 |
appendTo={appendTo} |
| 354 |
isVisible={visible} |
| 355 |
positionModifiers={positionModifiers} |
| 356 |
distance={distance} |
| 357 |
placement={position} |
| 358 |
onMouseEnter={triggerOnMouseenter && show} |
| 359 |
onMouseLeave={triggerOnMouseenter && hide} |
| 360 |
onPopperMouseEnter={triggerOnMouseenter && show} |
| 361 |
onPopperMouseLeave={triggerOnMouseenter && hide} |
| 362 |
onFocus={triggerOnFocus && show} |
| 363 |
onBlur={triggerOnFocus && hide} |
| 364 |
onDocumentClick={triggerOnClick && onDocumentClick} |
| 365 |
onDocumentKeyDown={triggerManually ? null : onDocumentKeyDown} |
| 366 |
onTriggerEnter={triggerManually ? null : onTriggerEnter} |
| 367 |
enableFlip={enableFlip} |
| 368 |
zIndex={zIndex} |
| 369 |
flipBehavior={flipBehavior} |
| 370 |
animationDuration={animationDuration} |
| 371 |
entryDelay={entryDelay} |
| 372 |
exitDelay={exitDelay} |
| 373 |
onHidden={onTooltipHidden} |
| 374 |
/> |
| 375 |
); |
| 376 |
}; |
| 377 |
Tooltip.displayName = 'Tooltip'; |