| 1 |
import { cloneElement, isValidElement, useCallback, useEffect, useMemo, useRef, useState } from 'react'; |
| 2 |
import * as ReactDOM from 'react-dom'; |
| 3 |
import { usePopper } from './thirdparty/react-popper/usePopper'; |
| 4 |
import { Options as OffsetOptions } from './thirdparty/popper-core/modifiers/offset'; |
| 5 |
import { Placement, Modifier } from './thirdparty/popper-core'; |
| 6 |
import { clearTimeouts } from '../util'; |
| 7 |
import { css } from '@patternfly/react-styles'; |
| 8 |
import '@patternfly/react-styles/css/components/Popper/Popper.css'; |
| 9 |
import { getLanguageDirection } from '../util'; |
| 10 |
|
| 11 |
const hash = { |
| 12 |
left: 'right', |
| 13 |
right: 'left', |
| 14 |
bottom: 'top', |
| 15 |
top: 'bottom', |
| 16 |
'top-start': 'bottom-end', |
| 17 |
'top-end': 'bottom-start', |
| 18 |
'bottom-start': 'top-end', |
| 19 |
'bottom-end': 'top-start', |
| 20 |
'left-start': 'right-end', |
| 21 |
'left-end': 'right-start', |
| 22 |
'right-start': 'left-end', |
| 23 |
'right-end': 'left-start' |
| 24 |
}; |
| 25 |
|
| 26 |
const getOppositePlacement = (placement: Placement): any => |
| 27 |
placement.replace( |
| 28 |
/left|right|bottom|top|top-start|top-end|bottom-start|bottom-end|right-start|right-end|left-start|left-end/g, |
| 29 |
(matched: string) => |
| 30 |
hash[ |
| 31 |
matched as |
| 32 |
| 'left' |
| 33 |
| 'right' |
| 34 |
| 'bottom' |
| 35 |
| 'top' |
| 36 |
| 'top-start' |
| 37 |
| 'top-end' |
| 38 |
| 'bottom-start' |
| 39 |
| 'bottom-end' |
| 40 |
| 'right-start' |
| 41 |
| 'right-end' |
| 42 |
| 'left-start' |
| 43 |
| 'left-end' |
| 44 |
] as Placement |
| 45 |
); |
| 46 |
|
| 47 |
export const getOpacityTransition = (animationDuration: number) => |
| 48 |
`opacity ${animationDuration}ms cubic-bezier(.54, 1.5, .38, 1.11)`; |
| 49 |
|
| 50 |
/** Properties of Popper that can be used to customize its behavior. */ |
| 51 |
export interface PopperOptions { |
| 52 |
/** popper direction */ |
| 53 |
direction?: 'up' | 'down'; |
| 54 |
/** Horizontal position of the popper. Use the additional prop preventOverflow if seeing clipping. */ |
| 55 |
position?: 'right' | 'left' | 'center' | 'start' | 'end'; |
| 56 |
/** Instead of direction and position can set the placement of the popper */ |
| 57 |
placement?: Placement; |
| 58 |
/** Custom width of the popper. If the value is "trigger", it will set the width to the trigger element's width */ |
| 59 |
width?: string | 'trigger'; |
| 60 |
/** Minimum width of the popper. If the value is "trigger", it will set the min width to the trigger element's width */ |
| 61 |
minWidth?: string | 'trigger'; |
| 62 |
/** Maximum width of the popper. If the value is "trigger", it will set the max width to the trigger element's width */ |
| 63 |
maxWidth?: string | 'trigger'; |
| 64 |
/** The container to append the popper to. Defaults to 'inline'. */ |
| 65 |
appendTo?: HTMLElement | (() => HTMLElement) | 'inline'; |
| 66 |
/** z-index of the popper element */ |
| 67 |
zIndex?: number; |
| 68 |
/** |
| 69 |
* Map class names to positions, for example: |
| 70 |
* { |
| 71 |
* top: styles.modifiers.top, |
| 72 |
* bottom: styles.modifiers.bottom, |
| 73 |
* left: styles.modifiers.left, |
| 74 |
* right: styles.modifiers.right |
| 75 |
* } |
| 76 |
*/ |
| 77 |
positionModifiers?: { |
| 78 |
top?: string; |
| 79 |
right?: string; |
| 80 |
bottom?: string; |
| 81 |
left?: string; |
| 82 |
topStart?: string; |
| 83 |
topEnd?: string; |
| 84 |
bottomStart?: string; |
| 85 |
bottomEnd?: string; |
| 86 |
leftStart?: string; |
| 87 |
leftEnd?: string; |
| 88 |
rightStart?: string; |
| 89 |
rightEnd?: string; |
| 90 |
}; |
| 91 |
/** Distance of the popper to the trigger */ |
| 92 |
distance?: number; |
| 93 |
/** Override for the popper's offset */ |
| 94 |
offset?: OffsetOptions['offset']; |
| 95 |
/** Callback function when mouse enters trigger */ |
| 96 |
onMouseEnter?: (event?: MouseEvent) => void; |
| 97 |
/** Callback function when mouse leaves trigger */ |
| 98 |
onMouseLeave?: (event?: MouseEvent) => void; |
| 99 |
/** Callback function when trigger is focused */ |
| 100 |
onFocus?: (event?: FocusEvent) => void; |
| 101 |
/** Callback function when trigger is blurred (focus leaves) */ |
| 102 |
onBlur?: (event?: FocusEvent) => void; |
| 103 |
/** Callback function when trigger is clicked */ |
| 104 |
onTriggerClick?: (event?: MouseEvent) => void; |
| 105 |
/** Callback function when Enter key is used on trigger */ |
| 106 |
onTriggerEnter?: (event?: KeyboardEvent) => void; |
| 107 |
/** Callback function when popper is clicked */ |
| 108 |
onPopperClick?: (event?: MouseEvent) => void; |
| 109 |
/** Callback function when mouse enters popper content */ |
| 110 |
onPopperMouseEnter?: (event?: MouseEvent) => void; |
| 111 |
/** Callback function when mouse leaves popper content */ |
| 112 |
onPopperMouseLeave?: (event?: MouseEvent) => void; |
| 113 |
/** Callback function when document is clicked */ |
| 114 |
onDocumentClick?: (event?: MouseEvent, triggerElement?: HTMLElement, popperElement?: HTMLElement) => void; |
| 115 |
/** Callback function when keydown event occurs on document */ |
| 116 |
onDocumentKeyDown?: (event?: KeyboardEvent) => void; |
| 117 |
/** Enable to flip the popper when it reaches the boundary */ |
| 118 |
enableFlip?: boolean; |
| 119 |
/** The behavior of how the popper flips when it reaches the boundary */ |
| 120 |
flipBehavior?: |
| 121 |
| 'flip' |
| 122 |
| ( |
| 123 |
| 'top' |
| 124 |
| 'bottom' |
| 125 |
| 'left' |
| 126 |
| 'right' |
| 127 |
| 'top-start' |
| 128 |
| 'top-end' |
| 129 |
| 'bottom-start' |
| 130 |
| 'bottom-end' |
| 131 |
| 'left-start' |
| 132 |
| 'left-end' |
| 133 |
| 'right-start' |
| 134 |
| 'right-end' |
| 135 |
)[]; |
| 136 |
/** The duration of the CSS fade transition animation. */ |
| 137 |
animationDuration?: number; |
| 138 |
/** Delay in ms before the popper appears */ |
| 139 |
entryDelay?: number; |
| 140 |
/** Delay in ms before the popper disappears */ |
| 141 |
exitDelay?: number; |
| 142 |
/** Callback when popper's hide transition has finished executing */ |
| 143 |
onHidden?: () => void; |
| 144 |
/** |
| 145 |
* Lifecycle function invoked when the popper begins to transition out. |
| 146 |
*/ |
| 147 |
onHide?: () => void; |
| 148 |
/** |
| 149 |
* Lifecycle function invoked when the popper has been mounted to the DOM. |
| 150 |
*/ |
| 151 |
onMount?: () => void; |
| 152 |
/** |
| 153 |
* Lifecycle function invoked when the popper begins to transition in. |
| 154 |
*/ |
| 155 |
onShow?: () => void; |
| 156 |
/** |
| 157 |
* Lifecycle function invoked when the popper has fully transitioned in. |
| 158 |
*/ |
| 159 |
onShown?: () => void; |
| 160 |
/** Flag to prevent the popper from overflowing its container and becoming partially obscured. */ |
| 161 |
preventOverflow?: boolean; |
| 162 |
} |
| 163 |
|
| 164 |
/** Extends PopperOptions */ |
| 165 |
export interface PopperProps extends PopperOptions { |
| 166 |
/** |
| 167 |
* Trigger reference element to which the popper is relatively placed to. |
| 168 |
*/ |
| 169 |
trigger?: React.ReactNode; |
| 170 |
/** |
| 171 |
* A reference to the trigger reference element that can be passed instead of or along |
| 172 |
* with the trigger prop. When passed along with the trigger prop, the div element that |
| 173 |
* wraps the trigger will be removed. |
| 174 |
*/ |
| 175 |
triggerRef?: HTMLElement | (() => HTMLElement) | React.RefObject<any>; |
| 176 |
/** The popper (menu/tooltip/popover) element */ |
| 177 |
popper: React.ReactElement<any>; |
| 178 |
/** |
| 179 |
* Reference to the popper (menu/tooltip/popover) element. |
| 180 |
* Passing this prop will remove the wrapper div element from the popper. |
| 181 |
*/ |
| 182 |
popperRef?: HTMLElement | (() => HTMLElement) | React.RefObject<any>; |
| 183 |
/** True to make the popper visible */ |
| 184 |
isVisible?: boolean; |
| 185 |
} |
| 186 |
|
| 187 |
export const Popper: React.FunctionComponent<PopperProps> = ({ |
| 188 |
trigger, |
| 189 |
popper, |
| 190 |
direction = 'down', |
| 191 |
position = 'start', |
| 192 |
placement, |
| 193 |
width, |
| 194 |
minWidth = 'trigger', |
| 195 |
maxWidth, |
| 196 |
appendTo = () => document.body, |
| 197 |
zIndex = 9999, |
| 198 |
isVisible = true, |
| 199 |
positionModifiers, |
| 200 |
distance = 0, |
| 201 |
offset, |
| 202 |
onMouseEnter, |
| 203 |
onMouseLeave, |
| 204 |
onFocus, |
| 205 |
onBlur, |
| 206 |
onDocumentClick, |
| 207 |
onTriggerClick, |
| 208 |
onTriggerEnter, |
| 209 |
onPopperClick, |
| 210 |
onPopperMouseEnter, |
| 211 |
onPopperMouseLeave, |
| 212 |
onDocumentKeyDown, |
| 213 |
enableFlip = true, |
| 214 |
flipBehavior = 'flip', |
| 215 |
triggerRef, |
| 216 |
popperRef, |
| 217 |
animationDuration = 0, |
| 218 |
entryDelay = 0, |
| 219 |
exitDelay = 0, |
| 220 |
onHidden = () => {}, |
| 221 |
onHide = () => {}, |
| 222 |
onMount = () => {}, |
| 223 |
onShow = () => {}, |
| 224 |
onShown = () => {}, |
| 225 |
preventOverflow = false |
| 226 |
}) => { |
| 227 |
const [triggerElement, setTriggerElement] = useState(null); |
| 228 |
const [refElement, setRefElement] = useState<HTMLElement>(null); |
| 229 |
const [popperElement, setPopperElement] = useState<HTMLElement>(null); |
| 230 |
const [popperContent, setPopperContent] = useState(null); |
| 231 |
const [ready, setReady] = useState(false); |
| 232 |
const [opacity, setOpacity] = useState(0); |
| 233 |
const [internalIsVisible, setInternalIsVisible] = useState(isVisible); |
| 234 |
const transitionTimerRef = useRef(null); |
| 235 |
const showTimerRef = useRef(null); |
| 236 |
const hideTimerRef = useRef(null); |
| 237 |
const prevExitDelayRef = useRef<number>(undefined); |
| 238 |
|
| 239 |
const refOrTrigger = refElement || triggerElement; |
| 240 |
const showPopper = isVisible || internalIsVisible; |
| 241 |
|
| 242 |
const triggerParent = ((triggerRef as React.RefObject<any>)?.current || triggerElement)?.parentElement; |
| 243 |
const languageDirection = getLanguageDirection(triggerParent); |
| 244 |
|
| 245 |
const internalPosition = useMemo<'left' | 'right' | 'center'>(() => { |
| 246 |
const fixedPositions = { left: 'left', right: 'right', center: 'center' }; |
| 247 |
|
| 248 |
const positionMap = { |
| 249 |
ltr: { |
| 250 |
start: 'left', |
| 251 |
end: 'right', |
| 252 |
...fixedPositions |
| 253 |
}, |
| 254 |
rtl: { |
| 255 |
start: 'right', |
| 256 |
end: 'left', |
| 257 |
...fixedPositions |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
return positionMap[languageDirection][position] as 'left' | 'right' | 'center'; |
| 262 |
}, [position, languageDirection]); |
| 263 |
|
| 264 |
const onDocumentClickCallback = useCallback( |
| 265 |
(event: MouseEvent) => onDocumentClick(event, refOrTrigger, popperElement), |
| 266 |
[showPopper, triggerElement, refElement, popperElement, onDocumentClick] |
| 267 |
); |
| 268 |
|
| 269 |
useEffect(() => { |
| 270 |
setReady(true); |
| 271 |
onMount(); |
| 272 |
}, []); |
| 273 |
|
| 274 |
// Cancel all timers on unmount |
| 275 |
useEffect( |
| 276 |
() => () => { |
| 277 |
clearTimeouts([transitionTimerRef, hideTimerRef, showTimerRef]); |
| 278 |
}, |
| 279 |
[] |
| 280 |
); |
| 281 |
|
| 282 |
useEffect(() => { |
| 283 |
if (triggerRef) { |
| 284 |
if ((triggerRef as React.RefObject<any>).current) { |
| 285 |
setRefElement((triggerRef as React.RefObject<any>).current); |
| 286 |
} else if (typeof triggerRef === 'function') { |
| 287 |
setRefElement(triggerRef()); |
| 288 |
} |
| 289 |
} |
| 290 |
}, [triggerRef, trigger]); |
| 291 |
useEffect(() => { |
| 292 |
// When the popperRef is defined or the popper visibility changes, ensure the popper element is up to date |
| 293 |
if (popperRef) { |
| 294 |
if ((popperRef as React.RefObject<any>).current) { |
| 295 |
setPopperElement((popperRef as React.RefObject<any>).current); |
| 296 |
} else if (typeof popperRef === 'function') { |
| 297 |
setPopperElement(popperRef()); |
| 298 |
} |
| 299 |
} |
| 300 |
}, [showPopper, popperRef]); |
| 301 |
useEffect(() => { |
| 302 |
// Trigger a Popper update when content changes. |
| 303 |
const observer = new MutationObserver(() => { |
| 304 |
update && update(); |
| 305 |
}); |
| 306 |
popperElement && observer.observe(popperElement, { attributes: true, childList: true, subtree: true }); |
| 307 |
|
| 308 |
return () => { |
| 309 |
observer.disconnect(); |
| 310 |
}; |
| 311 |
}, [popperElement]); |
| 312 |
|
| 313 |
const addEventListener = (listener: any, element: Document | HTMLElement, event: string, capture = false) => { |
| 314 |
if (listener && element) { |
| 315 |
element.addEventListener(event, listener, { capture }); |
| 316 |
} |
| 317 |
}; |
| 318 |
const removeEventListener = (listener: any, element: Document | HTMLElement, event: string, capture = false) => { |
| 319 |
if (listener && element) { |
| 320 |
element.removeEventListener(event, listener, { capture }); |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
useEffect(() => { |
| 325 |
addEventListener(onMouseEnter, refOrTrigger, 'mouseenter'); |
| 326 |
addEventListener(onMouseLeave, refOrTrigger, 'mouseleave'); |
| 327 |
addEventListener(onFocus, refOrTrigger, 'focus'); |
| 328 |
addEventListener(onBlur, refOrTrigger, 'blur'); |
| 329 |
addEventListener(onTriggerClick, refOrTrigger, 'click'); |
| 330 |
addEventListener(onTriggerEnter, refOrTrigger, 'keydown'); |
| 331 |
addEventListener(onPopperClick, popperElement, 'click'); |
| 332 |
addEventListener(onPopperMouseEnter, popperElement, 'mouseenter'); |
| 333 |
addEventListener(onPopperMouseLeave, popperElement, 'mouseleave'); |
| 334 |
onDocumentClick && addEventListener(onDocumentClickCallback, document, 'click', true); |
| 335 |
addEventListener(onDocumentKeyDown, document, 'keydown', true); |
| 336 |
|
| 337 |
return () => { |
| 338 |
removeEventListener(onMouseEnter, refOrTrigger, 'mouseenter'); |
| 339 |
removeEventListener(onMouseLeave, refOrTrigger, 'mouseleave'); |
| 340 |
removeEventListener(onFocus, refOrTrigger, 'focus'); |
| 341 |
removeEventListener(onBlur, refOrTrigger, 'blur'); |
| 342 |
removeEventListener(onTriggerClick, refOrTrigger, 'click'); |
| 343 |
removeEventListener(onTriggerEnter, refOrTrigger, 'keydown'); |
| 344 |
removeEventListener(onPopperClick, popperElement, 'click'); |
| 345 |
removeEventListener(onPopperMouseEnter, popperElement, 'mouseenter'); |
| 346 |
removeEventListener(onPopperMouseLeave, popperElement, 'mouseleave'); |
| 347 |
onDocumentClick && removeEventListener(onDocumentClickCallback, document, 'click', true); |
| 348 |
removeEventListener(onDocumentKeyDown, document, 'keydown', true); |
| 349 |
}; |
| 350 |
}, [ |
| 351 |
triggerElement, |
| 352 |
popperElement, |
| 353 |
onMouseEnter, |
| 354 |
onMouseLeave, |
| 355 |
onFocus, |
| 356 |
onBlur, |
| 357 |
onTriggerClick, |
| 358 |
onTriggerEnter, |
| 359 |
onPopperClick, |
| 360 |
onPopperMouseEnter, |
| 361 |
onPopperMouseLeave, |
| 362 |
onDocumentClick, |
| 363 |
onDocumentKeyDown, |
| 364 |
refElement |
| 365 |
]); |
| 366 |
const getPlacement = () => { |
| 367 |
if (placement) { |
| 368 |
return placement; |
| 369 |
} |
| 370 |
let convertedPlacement = direction === 'up' ? 'top' : 'bottom'; |
| 371 |
if (internalPosition !== 'center') { |
| 372 |
convertedPlacement = `${convertedPlacement}-${internalPosition === 'right' ? 'end' : 'start'}`; |
| 373 |
} |
| 374 |
return convertedPlacement as Placement; |
| 375 |
}; |
| 376 |
const getPlacementMemo = useMemo(getPlacement, [direction, internalPosition, placement]); |
| 377 |
const getOppositePlacementMemo = useMemo( |
| 378 |
() => getOppositePlacement(getPlacement()), |
| 379 |
[direction, internalPosition, placement] |
| 380 |
); |
| 381 |
|
| 382 |
const widthMods: Modifier<'widthMods', {}> = useMemo( |
| 383 |
() => ({ |
| 384 |
name: 'widthMods', |
| 385 |
enabled: width !== undefined || minWidth !== undefined || maxWidth !== undefined, |
| 386 |
phase: 'beforeWrite', |
| 387 |
requires: ['computeStyles'], |
| 388 |
fn: ({ state }) => { |
| 389 |
const triggerWidth = state.rects.reference.width; |
| 390 |
if (width) { |
| 391 |
state.styles.popper.width = width === 'trigger' ? `${triggerWidth}px` : width; |
| 392 |
} |
| 393 |
|
| 394 |
if (minWidth) { |
| 395 |
state.styles.popper.minWidth = minWidth === 'trigger' ? `${triggerWidth}px` : minWidth; |
| 396 |
} |
| 397 |
|
| 398 |
if (maxWidth) { |
| 399 |
state.styles.popper.maxWidth = maxWidth === 'trigger' ? `${triggerWidth}px` : maxWidth; |
| 400 |
} |
| 401 |
}, |
| 402 |
effect: ({ state }) => { |
| 403 |
const triggerWidth = (state.elements.reference as HTMLElement).offsetWidth; |
| 404 |
if (width) { |
| 405 |
state.elements.popper.style.width = width === 'trigger' ? `${triggerWidth}px` : width; |
| 406 |
} |
| 407 |
|
| 408 |
if (minWidth) { |
| 409 |
state.elements.popper.style.minWidth = minWidth === 'trigger' ? `${triggerWidth}px` : minWidth; |
| 410 |
} |
| 411 |
|
| 412 |
if (maxWidth) { |
| 413 |
state.elements.popper.style.maxWidth = maxWidth === 'trigger' ? `${triggerWidth}px` : maxWidth; |
| 414 |
} |
| 415 |
return () => {}; |
| 416 |
} |
| 417 |
}), |
| 418 |
[width, minWidth, maxWidth] |
| 419 |
); |
| 420 |
|
| 421 |
const { |
| 422 |
styles: popperStyles, |
| 423 |
attributes, |
| 424 |
update, |
| 425 |
forceUpdate |
| 426 |
} = usePopper(refOrTrigger, popperElement, { |
| 427 |
placement: getPlacementMemo, |
| 428 |
modifiers: [ |
| 429 |
{ |
| 430 |
name: 'offset', |
| 431 |
options: { |
| 432 |
offset: offset || [0, distance] |
| 433 |
} |
| 434 |
}, |
| 435 |
{ |
| 436 |
name: 'preventOverflow', |
| 437 |
enabled: preventOverflow |
| 438 |
}, |
| 439 |
{ |
| 440 |
// adds attribute [data-popper-reference-hidden] to the popper element which can be used to hide it using CSS |
| 441 |
name: 'hide', |
| 442 |
enabled: true |
| 443 |
}, |
| 444 |
{ |
| 445 |
name: 'flip', |
| 446 |
enabled: getPlacementMemo.startsWith('auto') || enableFlip, |
| 447 |
options: { |
| 448 |
fallbackPlacements: flipBehavior === 'flip' ? [getOppositePlacementMemo] : flipBehavior |
| 449 |
} |
| 450 |
}, |
| 451 |
widthMods |
| 452 |
] |
| 453 |
}); |
| 454 |
|
| 455 |
/** We want to forceUpdate only when a tooltip's content is dynamically updated. |
| 456 |
* TODO: Investigate into 3rd party libraries for a less limited/specific solution |
| 457 |
*/ |
| 458 |
useEffect(() => { |
| 459 |
// currentPopperContent = {tooltip children} || {dropdown children} |
| 460 |
const currentPopperContent = |
| 461 |
popper?.props?.children?.[1]?.props?.children || popper?.props?.children?.props?.children; |
| 462 |
setPopperContent(currentPopperContent); |
| 463 |
|
| 464 |
if (currentPopperContent && popperContent && currentPopperContent !== popperContent) { |
| 465 |
forceUpdate && forceUpdate(); |
| 466 |
} |
| 467 |
}, [popper]); |
| 468 |
|
| 469 |
useEffect(() => { |
| 470 |
if (prevExitDelayRef.current < exitDelay) { |
| 471 |
clearTimeouts([transitionTimerRef, hideTimerRef]); |
| 472 |
hideTimerRef.current = setTimeout(() => { |
| 473 |
transitionTimerRef.current = setTimeout(() => { |
| 474 |
setInternalIsVisible(false); |
| 475 |
}, animationDuration); |
| 476 |
}, exitDelay); |
| 477 |
} |
| 478 |
prevExitDelayRef.current = exitDelay; |
| 479 |
}, [exitDelay]); |
| 480 |
|
| 481 |
const show = () => { |
| 482 |
onShow(); |
| 483 |
clearTimeouts([transitionTimerRef, hideTimerRef]); |
| 484 |
showTimerRef.current = setTimeout(() => { |
| 485 |
setInternalIsVisible(true); |
| 486 |
setOpacity(1); |
| 487 |
onShown(); |
| 488 |
}, entryDelay); |
| 489 |
}; |
| 490 |
|
| 491 |
const hide = () => { |
| 492 |
onHide(); |
| 493 |
clearTimeouts([showTimerRef]); |
| 494 |
hideTimerRef.current = setTimeout(() => { |
| 495 |
setOpacity(0); |
| 496 |
transitionTimerRef.current = setTimeout(() => { |
| 497 |
setInternalIsVisible(false); |
| 498 |
setPopperElement(null); |
| 499 |
onHidden(); |
| 500 |
}, animationDuration); |
| 501 |
}, exitDelay); |
| 502 |
}; |
| 503 |
|
| 504 |
useEffect(() => { |
| 505 |
if (isVisible) { |
| 506 |
show(); |
| 507 |
} else { |
| 508 |
hide(); |
| 509 |
} |
| 510 |
}, [isVisible]); |
| 511 |
|
| 512 |
// Returns the CSS modifier class in order to place the Popper's arrow properly |
| 513 |
// Depends on the position of the Popper relative to the reference element |
| 514 |
const modifierFromPopperPosition = () => { |
| 515 |
if (attributes && attributes.popper && attributes.popper['data-popper-placement']) { |
| 516 |
const popperPlacement = attributes.popper['data-popper-placement'] as keyof typeof positionModifiers; |
| 517 |
return positionModifiers[popperPlacement]; |
| 518 |
} |
| 519 |
return positionModifiers.top; |
| 520 |
}; |
| 521 |
|
| 522 |
const options = { |
| 523 |
className: css(popper.props && popper.props.className, positionModifiers && modifierFromPopperPosition()), |
| 524 |
style: { |
| 525 |
...((popper.props && popper.props.style) || {}), |
| 526 |
...popperStyles.popper, |
| 527 |
zIndex, |
| 528 |
opacity, |
| 529 |
transition: getOpacityTransition(animationDuration) |
| 530 |
}, |
| 531 |
...attributes.popper |
| 532 |
}; |
| 533 |
|
| 534 |
const getMenuWithPopper = () => { |
| 535 |
const localPopper = cloneElement(popper, options); |
| 536 |
|
| 537 |
return popperRef ? ( |
| 538 |
localPopper |
| 539 |
) : ( |
| 540 |
<div |
| 541 |
style={{ display: 'contents' }} |
| 542 |
ref={(node) => { |
| 543 |
setPopperElement(node?.firstElementChild as HTMLElement); |
| 544 |
}} |
| 545 |
> |
| 546 |
{localPopper} |
| 547 |
</div> |
| 548 |
); |
| 549 |
}; |
| 550 |
|
| 551 |
const getPopper = () => { |
| 552 |
if (appendTo === 'inline') { |
| 553 |
return getMenuWithPopper(); |
| 554 |
} else { |
| 555 |
const target = typeof appendTo === 'function' ? appendTo() : appendTo; |
| 556 |
return ReactDOM.createPortal(getMenuWithPopper(), target); |
| 557 |
} |
| 558 |
}; |
| 559 |
|
| 560 |
return ( |
| 561 |
<> |
| 562 |
{!triggerRef && trigger && isValidElement(trigger) && ( |
| 563 |
<div |
| 564 |
style={{ display: 'contents' }} |
| 565 |
ref={(node) => { |
| 566 |
setTriggerElement(node?.firstElementChild as HTMLElement); |
| 567 |
}} |
| 568 |
> |
| 569 |
{trigger} |
| 570 |
</div> |
| 571 |
)} |
| 572 |
{triggerRef && trigger && isValidElement(trigger) && trigger} |
| 573 |
{ready && showPopper && getPopper()} |
| 574 |
</> |
| 575 |
); |
| 576 |
}; |
| 577 |
Popper.displayName = 'Popper'; |