|
| 1 | +import { useLayoutEffect, useRef, useState } from 'react'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { Box, FlexBox } from '../../Box'; |
| 5 | +import { Popover } from '../../Popover'; |
| 6 | +import { TargetContainer } from '../shared/elements'; |
| 7 | +import { escapeKeyPressHandler, getPopoverAlignment } from '../shared/utils'; |
| 8 | +import { |
| 9 | + deprecatedTooltipDefaultProps, |
| 10 | + DeprecatedToolTipPlacementComponentProps, |
| 11 | +} from './types'; |
| 12 | +import { getDeprecatedAccessibilityProps } from './utils'; |
| 13 | + |
| 14 | +export const DeprecatedFloatingToolTip: React.FC< |
| 15 | + DeprecatedToolTipPlacementComponentProps |
| 16 | +> = ({ |
| 17 | + alignment = deprecatedTooltipDefaultProps.alignment, |
| 18 | + children, |
| 19 | + focusable, |
| 20 | + id, |
| 21 | + target, |
| 22 | + widthMode = deprecatedTooltipDefaultProps.widthMode, |
| 23 | +}) => { |
| 24 | + const ref = useRef<HTMLDivElement>(null); |
| 25 | + const [offset, setOffset] = useState(0); |
| 26 | + const [isOpen, setIsOpen] = useState(false); |
| 27 | + const [isFocused, setIsFocused] = useState(false); |
| 28 | + |
| 29 | + useLayoutEffect(() => { |
| 30 | + if (ref?.current) { |
| 31 | + setOffset(-ref.current.clientWidth / 2 + 32); |
| 32 | + } |
| 33 | + }, []); |
| 34 | + |
| 35 | + const accessibilityProps = getDeprecatedAccessibilityProps({ |
| 36 | + focusable, |
| 37 | + id, |
| 38 | + isOpenPopoverToolTip: isOpen, |
| 39 | + }); |
| 40 | + |
| 41 | + const popoverAlignments = getPopoverAlignment({ alignment }); |
| 42 | + |
| 43 | + const handleShowHideAction = ({ |
| 44 | + type, |
| 45 | + }: |
| 46 | + | React.FocusEvent<HTMLDivElement, Element> |
| 47 | + | React.MouseEvent<HTMLDivElement, MouseEvent>) => { |
| 48 | + if (type === 'focus' && !isOpen) { |
| 49 | + setIsOpen(true); |
| 50 | + setIsFocused(true); |
| 51 | + } |
| 52 | + if (type === 'blur' && isOpen) { |
| 53 | + setIsOpen(false); |
| 54 | + setIsFocused(false); |
| 55 | + } |
| 56 | + if (type === 'mouseenter' && !isOpen) { |
| 57 | + setIsOpen(true); |
| 58 | + } |
| 59 | + if (type === 'mouseleave' && isOpen && !isFocused) { |
| 60 | + setIsOpen(false); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <Box |
| 66 | + position="relative" |
| 67 | + display="inline-flex" |
| 68 | + onMouseLeave={(e) => handleShowHideAction(e)} |
| 69 | + > |
| 70 | + <TargetContainer |
| 71 | + onKeyDown={(e) => escapeKeyPressHandler(e)} |
| 72 | + onFocus={(e) => { |
| 73 | + handleShowHideAction(e); |
| 74 | + }} |
| 75 | + onBlur={(e) => { |
| 76 | + handleShowHideAction(e); |
| 77 | + }} |
| 78 | + onMouseEnter={(e) => handleShowHideAction(e)} |
| 79 | + ref={ref} |
| 80 | + {...accessibilityProps} |
| 81 | + > |
| 82 | + {target} |
| 83 | + </TargetContainer> |
| 84 | + <Popover |
| 85 | + {...popoverAlignments} |
| 86 | + animation="fade" |
| 87 | + aria-live="polite" |
| 88 | + horizontalOffset={offset} |
| 89 | + isOpen={isOpen} |
| 90 | + outline |
| 91 | + role="tooltip" |
| 92 | + variant="secondary" |
| 93 | + skipFocusTrap |
| 94 | + targetRef={ref} |
| 95 | + widthRestricted={widthMode === 'standard'} |
| 96 | + > |
| 97 | + <FlexBox id={id} alignItems="flex-start" flexDirection="column"> |
| 98 | + {children} |
| 99 | + </FlexBox> |
| 100 | + </Popover> |
| 101 | + </Box> |
| 102 | + ); |
| 103 | +}; |
0 commit comments