|
| 1 | +import React from 'react'; |
| 2 | +import IconHighlightTextAnnotation from 'box-ui-elements/es/icon/fill/AnnotationsHighlight16'; |
| 3 | +import noop from 'lodash/noop'; |
| 4 | +import { FormattedMessage } from 'react-intl'; |
| 5 | +import messages from './messages'; |
| 6 | +import PopupBase from './PopupBase'; |
| 7 | +import { Options } from './Popper'; |
| 8 | +import { Shape } from '../../@types/model'; |
| 9 | +import './PopupHighlight.scss'; |
| 10 | + |
| 11 | +export type Props = { |
| 12 | + onClick?: (event: React.MouseEvent) => void; |
| 13 | + shape: Shape; |
| 14 | +}; |
| 15 | + |
| 16 | +const options: Partial<Options> = { |
| 17 | + modifiers: [ |
| 18 | + { |
| 19 | + name: 'arrow', |
| 20 | + options: { |
| 21 | + element: '.ba-Popup-arrow', |
| 22 | + }, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'eventListeners', |
| 26 | + options: { |
| 27 | + scroll: false, |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'offset', |
| 32 | + options: { |
| 33 | + offset: [0, 8], |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: 'preventOverflow', |
| 38 | + options: { |
| 39 | + padding: 5, |
| 40 | + }, |
| 41 | + }, |
| 42 | + ], |
| 43 | + placement: 'bottom', |
| 44 | +}; |
| 45 | + |
| 46 | +export default function PopupHighlight({ onClick = noop, shape }: Props): JSX.Element { |
| 47 | + const buttonRef = React.useRef<HTMLButtonElement>(null); |
| 48 | + const { height, width, x, y } = shape; |
| 49 | + |
| 50 | + const reference = { |
| 51 | + getBoundingClientRect: () => ({ |
| 52 | + bottom: y + height, |
| 53 | + height, |
| 54 | + left: x, |
| 55 | + right: x + width, |
| 56 | + top: y, |
| 57 | + width, |
| 58 | + }), |
| 59 | + }; |
| 60 | + |
| 61 | + const handleEvent = (event: Event): void => { |
| 62 | + event.preventDefault(); |
| 63 | + event.stopPropagation(); |
| 64 | + }; |
| 65 | + |
| 66 | + const handleClick = (event: React.MouseEvent): void => { |
| 67 | + onClick(event); |
| 68 | + }; |
| 69 | + |
| 70 | + // Prevent events from propagating to upper elements. The upper elements' |
| 71 | + // mouse handlers are attached to real dom, so the belows have to attach to real dom too |
| 72 | + React.useEffect(() => { |
| 73 | + const { current: buttonEl } = buttonRef; |
| 74 | + |
| 75 | + if (buttonEl) { |
| 76 | + buttonEl.addEventListener('mousedown', handleEvent); |
| 77 | + buttonEl.addEventListener('mouseup', handleEvent); |
| 78 | + } |
| 79 | + |
| 80 | + return () => { |
| 81 | + if (buttonEl) { |
| 82 | + buttonEl.removeEventListener('mousedown', handleEvent); |
| 83 | + buttonEl.removeEventListener('mouseup', handleEvent); |
| 84 | + } |
| 85 | + }; |
| 86 | + }, [buttonRef]); |
| 87 | + |
| 88 | + return ( |
| 89 | + <PopupBase className="ba-PopupHighlight" options={options} reference={reference}> |
| 90 | + <button |
| 91 | + ref={buttonRef} |
| 92 | + className="ba-PopupHighlight-button" |
| 93 | + data-testid="ba-PopupHighlight-button" |
| 94 | + onClick={handleClick} |
| 95 | + type="button" |
| 96 | + > |
| 97 | + <IconHighlightTextAnnotation className="ba-PopupHighlight-icon" /> |
| 98 | + <FormattedMessage {...messages.popupHighlightPromoter} /> |
| 99 | + </button> |
| 100 | + </PopupBase> |
| 101 | + ); |
| 102 | +} |
0 commit comments