|
| 1 | +import {useEffect, useRef} from 'react'; |
| 2 | + |
| 3 | +import {ChevronRight, BucketPaint as ColorPalette} from '@gravity-ui/icons'; |
| 4 | +import {Icon, Menu, Popup, sp} from '@gravity-ui/uikit'; |
| 5 | + |
| 6 | +import {i18n} from 'src/i18n/yfm-table'; |
| 7 | +import {useBooleanState, useElementState} from 'src/react-utils'; |
| 8 | + |
| 9 | +import {CellBgPalette} from '../CellBgPalette'; |
| 10 | + |
| 11 | +const CLOSE_DELAY = 120; |
| 12 | + |
| 13 | +export type CellBgMenuItemProps = { |
| 14 | + qa?: string; |
| 15 | + currentCellBg?: string | null; |
| 16 | + onCellBgChange: (color: string | null) => void; |
| 17 | +}; |
| 18 | + |
| 19 | +export const CellBgMenuItem: React.FC<CellBgMenuItemProps> = function YfmTableCellBgMenuItem({ |
| 20 | + qa, |
| 21 | + currentCellBg, |
| 22 | + onCellBgChange, |
| 23 | +}) { |
| 24 | + const [anchorElement, setAnchorElement] = useElementState<HTMLDivElement>(); |
| 25 | + const closeTimer = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 26 | + const [open, openPopup, closePopup] = useBooleanState(false); |
| 27 | + |
| 28 | + const cancelClose = () => { |
| 29 | + if (closeTimer.current) { |
| 30 | + clearTimeout(closeTimer.current); |
| 31 | + closeTimer.current = null; |
| 32 | + } |
| 33 | + }; |
| 34 | + const scheduleClose = () => { |
| 35 | + cancelClose(); |
| 36 | + closeTimer.current = setTimeout(closePopup, CLOSE_DELAY); |
| 37 | + }; |
| 38 | + |
| 39 | + useEffect(() => () => cancelClose(), []); |
| 40 | + |
| 41 | + return ( |
| 42 | + <Menu.Item |
| 43 | + qa={qa} |
| 44 | + ref={setAnchorElement} |
| 45 | + iconStart={<Icon data={ColorPalette} />} |
| 46 | + iconEnd={<Icon data={ChevronRight} />} |
| 47 | + onClick={() => { |
| 48 | + cancelClose(); |
| 49 | + openPopup(); |
| 50 | + }} |
| 51 | + extraProps={{ |
| 52 | + onMouseEnter: () => { |
| 53 | + cancelClose(); |
| 54 | + openPopup(); |
| 55 | + }, |
| 56 | + onMouseLeave: scheduleClose, |
| 57 | + }} |
| 58 | + > |
| 59 | + {i18n('cells.bg')} |
| 60 | + <Popup |
| 61 | + open={open} |
| 62 | + hasArrow={false} |
| 63 | + placement="right-start" |
| 64 | + anchorElement={anchorElement} |
| 65 | + > |
| 66 | + <div onMouseEnter={cancelClose} onMouseLeave={scheduleClose} className={sp({p: 2})}> |
| 67 | + <CellBgPalette value={currentCellBg} onSelect={onCellBgChange} /> |
| 68 | + </div> |
| 69 | + </Popup> |
| 70 | + </Menu.Item> |
| 71 | + ); |
| 72 | +}; |
0 commit comments