1- import { useState } from 'react' ;
1+ import { forwardRef , useState } from 'react' ;
22
33import { Ellipsis as DotsColumn , GripHorizontal as GripColumn } from '@gravity-ui/icons' ;
44import {
@@ -12,7 +12,7 @@ import {
1212
1313import { useBooleanState } from 'src/react-utils' ;
1414
15- import { FloatingPopup , type FloatingPopupProps } from '../FloatingPopup' ;
15+ import { FloatingPopup , type FloatingPopupProps , type FloatingPopupRef } from '../FloatingPopup' ;
1616
1717const popupOffset : FloatingPopupProps [ 'offset' ] = {
1818 mainAxis : - 9.5 ,
@@ -21,88 +21,104 @@ const popupOffset: FloatingPopupProps['offset'] = {
2121export type FloatingMenuProps = {
2222 dirtype : 'row' | 'column' ;
2323 canDrag : boolean ;
24- anchorElement : Element ;
24+ anchorElement : NonNullable < FloatingPopupProps [ 'anchorElement' ] > ;
2525 dropdownItems : DropdownMenuProps < unknown > [ 'items' ] ;
2626 switcherMouseProps ?: Pick <
2727 ButtonButtonProps ,
2828 'onMouseDown' | 'onMouseMove' | 'onMouseUp' | 'onMouseLeave'
2929 > ;
3030 onOpenToggle : NonNullable < DropdownMenuProps < unknown > [ 'onOpenToggle' ] > ;
31+ floatingStyles ?: FloatingPopupProps [ 'floatingStyles' ] ;
3132} ;
3233
33- export const FloatingMenu : React . FC < FloatingMenuProps > = function YfmTableFloatingMenu ( props ) {
34- const { dirtype, canDrag, anchorElement, dropdownItems, switcherMouseProps, onOpenToggle} =
35- props ;
34+ export const FloatingMenu = forwardRef < FloatingPopupRef , FloatingMenuProps > (
35+ function YfmTableFloatingMenu ( props , ref ) {
36+ const {
37+ dirtype,
38+ canDrag,
39+ anchorElement,
40+ dropdownItems,
41+ switcherMouseProps,
42+ floatingStyles,
43+ onOpenToggle,
44+ } = props ;
3645
37- const [ isMenuOpened , setMenuOpened ] = useState ( false ) ;
38- const [ isHovered , setHovered , unsetHovered ] = useBooleanState ( false ) ;
46+ const [ isMenuOpened , setMenuOpened ] = useState ( false ) ;
47+ const [ isHovered , setHovered , unsetHovered ] = useBooleanState ( false ) ;
3948
40- const showActionView = isMenuOpened || isHovered ;
41- const isRowType = dirtype === 'row' ;
49+ const showActionView = isMenuOpened || isHovered ;
50+ const isRowType = dirtype === 'row' ;
4251
43- return (
44- < FloatingPopup
45- open
46- offset = { popupOffset }
47- anchorElement = { anchorElement }
48- placement = { isRowType ? 'left' : 'top' }
49- floatingStyles = { {
50- lineHeight : 'initial' ,
51- } }
52- style = { {
53- backgroundColor : 'transparent' ,
54- } }
55- >
56- < DropdownMenu
57- onOpenToggle = { ( ...args ) => {
58- setMenuOpened ( ...args ) ;
59- onOpenToggle ( ...args ) ;
52+ return (
53+ < FloatingPopup
54+ open
55+ ref = { ref }
56+ offset = { popupOffset }
57+ anchorElement = { anchorElement }
58+ placement = { isRowType ? 'left' : 'top' }
59+ floatingStyles = { {
60+ lineHeight : 'initial' ,
61+ ...floatingStyles ,
6062 } }
61- renderSwitcher = { ( switcherProps ) => (
62- < Flex
63- centerContent
64- width = { 20 } // xs button
65- height = { 20 } // xs button
66- style = { {
67- borderRadius : 'var(--g-border-radius-xs)' ,
68- backgroundColor : showActionView
69- ? 'var(--g-color-base-background)'
70- : undefined ,
71- } }
72- onMouseEnter = { setHovered }
73- onMouseLeave = { unsetHovered }
74- >
75- < Button
63+ style = { {
64+ backgroundColor : 'transparent' ,
65+ } }
66+ >
67+ < DropdownMenu
68+ onOpenToggle = { ( ...args ) => {
69+ setMenuOpened ( ...args ) ;
70+ onOpenToggle ( ...args ) ;
71+ } }
72+ renderSwitcher = { ( switcherProps ) => (
73+ < Flex
74+ centerContent
75+ width = { 20 } // xs button
76+ height = { 20 } // xs button
7677 style = { {
77- cursor : canDrag ? 'grab' : undefined ,
78- transform : isRowType ? 'rotate(90deg)' : undefined ,
79- '--g-button-height' : showActionView ? undefined : '5px' ,
80- '--_--background-color' : showActionView
81- ? undefined
82- : 'var(--g-color-base-background)' ,
78+ borderRadius : 'var(--g-border-radius-xs)' ,
79+ backgroundColor : showActionView
80+ ? 'var(--g-color-base-background)'
81+ : undefined ,
8382 } }
84- view = { isMenuOpened ? 'outlined-action' : 'outlined' }
85- pin = { showActionView ? 'round-round' : 'circle-circle' }
86- size = "xs"
87- qa = { isRowType ? 'g-md-yfm-table-row-btn' : 'g-md-yfm-table-column-btn' }
88- { ...switcherProps }
89- { ...switcherMouseProps }
83+ onMouseEnter = { setHovered }
84+ onMouseLeave = { unsetHovered }
9085 >
91- { showActionView ? (
92- < Icon data = { canDrag ? GripColumn : DotsColumn } />
93- ) : (
94- String . fromCharCode ( 8194 ) // en space
95- ) }
96- </ Button >
97- </ Flex >
98- ) }
99- popupProps = { {
100- zIndex : 1010 ,
101- placement : isRowType ? 'right-start' : 'bottom-start' ,
102- } }
103- menuProps = { { qa : `g-md-yfm-table-${ dirtype } -menu` } }
104- items = { dropdownItems }
105- />
106- </ FloatingPopup >
107- ) ;
108- } ;
86+ < Button
87+ style = { {
88+ cursor : canDrag ? 'grab' : undefined ,
89+ transform : isRowType ? 'rotate(90deg)' : undefined ,
90+ '--g-button-height' : showActionView ? undefined : '5px' ,
91+ '--_--background-color' : showActionView
92+ ? undefined
93+ : 'var(--g-color-base-background)' ,
94+ } }
95+ view = { isMenuOpened ? 'outlined-action' : 'outlined' }
96+ pin = { showActionView ? 'round-round' : 'circle-circle' }
97+ size = "xs"
98+ qa = {
99+ isRowType
100+ ? 'g-md-yfm-table-row-btn'
101+ : 'g-md-yfm-table-column-btn'
102+ }
103+ { ...switcherProps }
104+ { ...switcherMouseProps }
105+ >
106+ { showActionView ? (
107+ < Icon data = { canDrag ? GripColumn : DotsColumn } />
108+ ) : (
109+ String . fromCharCode ( 8194 ) // en space
110+ ) }
111+ </ Button >
112+ </ Flex >
113+ ) }
114+ popupProps = { {
115+ zIndex : 1010 ,
116+ placement : isRowType ? 'right-start' : 'bottom-start' ,
117+ } }
118+ menuProps = { { qa : `g-md-yfm-table-${ dirtype } -menu` } }
119+ items = { dropdownItems }
120+ />
121+ </ FloatingPopup >
122+ ) ;
123+ } ,
124+ ) ;
0 commit comments