|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {Popup} from '@gravity-ui/uikit'; |
| 4 | +import debounce from 'lodash/debounce'; |
| 5 | + |
| 6 | +import {cn} from '../../utils/cn'; |
| 7 | + |
| 8 | +import './HoverPopup.scss'; |
| 9 | + |
| 10 | +const b = cn('hover-popup'); |
| 11 | + |
| 12 | +const DEBOUNCE_TIMEOUT = 100; |
| 13 | + |
| 14 | +interface HoverPopupProps { |
| 15 | + children: React.ReactNode; |
| 16 | + popupContent: React.ReactNode; |
| 17 | + showPopup?: boolean; |
| 18 | + offset?: [number, number]; |
| 19 | + anchorRef?: React.RefObject<HTMLElement>; |
| 20 | + onShowPopup?: VoidFunction; |
| 21 | + onHidePopup?: VoidFunction; |
| 22 | +} |
| 23 | + |
| 24 | +export const HoverPopup = ({ |
| 25 | + children, |
| 26 | + popupContent, |
| 27 | + showPopup, |
| 28 | + offset, |
| 29 | + anchorRef, |
| 30 | + onShowPopup, |
| 31 | + onHidePopup, |
| 32 | +}: HoverPopupProps) => { |
| 33 | + const [isPopupVisible, setIsPopupVisible] = React.useState(false); |
| 34 | + const anchor = React.useRef<HTMLDivElement>(null); |
| 35 | + |
| 36 | + const debouncedHandleShowPopup = React.useMemo( |
| 37 | + () => |
| 38 | + debounce(() => { |
| 39 | + setIsPopupVisible(true); |
| 40 | + onShowPopup?.(); |
| 41 | + }, DEBOUNCE_TIMEOUT), |
| 42 | + [onShowPopup], |
| 43 | + ); |
| 44 | + |
| 45 | + const hidePopup = React.useCallback(() => { |
| 46 | + setIsPopupVisible(false); |
| 47 | + onHidePopup?.(); |
| 48 | + }, [onHidePopup]); |
| 49 | + |
| 50 | + const debouncedHandleHidePopup = React.useMemo( |
| 51 | + () => debounce(hidePopup, DEBOUNCE_TIMEOUT), |
| 52 | + [hidePopup], |
| 53 | + ); |
| 54 | + |
| 55 | + const onMouseEnter = debouncedHandleShowPopup; |
| 56 | + |
| 57 | + const onMouseLeave = () => { |
| 58 | + debouncedHandleShowPopup.cancel(); |
| 59 | + debouncedHandleHidePopup(); |
| 60 | + }; |
| 61 | + |
| 62 | + const [isPopupContentHovered, setIsPopupContentHovered] = React.useState(false); |
| 63 | + const [isFocused, setIsFocused] = React.useState(false); |
| 64 | + |
| 65 | + const onPopupMouseEnter = React.useCallback(() => { |
| 66 | + setIsPopupContentHovered(true); |
| 67 | + }, []); |
| 68 | + |
| 69 | + const onPopupMouseLeave = React.useCallback(() => { |
| 70 | + setIsPopupContentHovered(false); |
| 71 | + }, []); |
| 72 | + |
| 73 | + const onPopupContextMenu = React.useCallback(() => { |
| 74 | + setIsFocused(true); |
| 75 | + }, []); |
| 76 | + |
| 77 | + const onPopupBlur = React.useCallback(() => { |
| 78 | + setIsFocused(false); |
| 79 | + }, []); |
| 80 | + |
| 81 | + const onPopupEscapeKeyDown = React.useCallback(() => { |
| 82 | + setIsFocused(false); |
| 83 | + setIsPopupContentHovered(false); |
| 84 | + hidePopup(); |
| 85 | + }, [hidePopup]); |
| 86 | + |
| 87 | + const open = isPopupVisible || showPopup || isPopupContentHovered || isFocused; |
| 88 | + |
| 89 | + return ( |
| 90 | + <React.Fragment> |
| 91 | + <div ref={anchor} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}> |
| 92 | + {children} |
| 93 | + </div> |
| 94 | + <Popup |
| 95 | + contentClassName={b()} |
| 96 | + anchorRef={anchorRef || anchor} |
| 97 | + open={open} |
| 98 | + onMouseEnter={onPopupMouseEnter} |
| 99 | + onMouseLeave={onPopupMouseLeave} |
| 100 | + onEscapeKeyDown={onPopupEscapeKeyDown} |
| 101 | + onBlur={onPopupBlur} |
| 102 | + placement={['top', 'bottom']} |
| 103 | + hasArrow |
| 104 | + // bigger offset for easier switching to neighbour nodes |
| 105 | + // matches the default offset for popup with arrow out of a sense of beauty |
| 106 | + offset={offset || [0, 12]} |
| 107 | + > |
| 108 | + <div onContextMenu={onPopupContextMenu}>{popupContent}</div> |
| 109 | + </Popup> |
| 110 | + </React.Fragment> |
| 111 | + ); |
| 112 | +}; |
0 commit comments