File tree 4 files changed +21
-4
lines changed
4 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " @szhsin/react-menu" ,
3
- "version" : " 0.10.6 " ,
3
+ "version" : " 0.10.7 " ,
4
4
"description" : " React menu components" ,
5
5
"author" : " Zheng Song" ,
6
6
"license" : " MIT" ,
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import {
18
18
SettingsContext ,
19
19
MenuListContext ,
20
20
initialHoverIndex ,
21
+ CloseReason ,
21
22
Keys ,
22
23
FocusPositions ,
23
24
HoverIndexActionTypes
@@ -45,6 +46,7 @@ export const MenuList = defineName(React.memo(function MenuList({
45
46
children,
46
47
onKeyDown,
47
48
onAnimationEnd,
49
+ onClose,
48
50
...restProps } ) {
49
51
50
52
const [ position , setPosition ] = useState ( { x : 0 , y : 0 } ) ;
@@ -518,6 +520,14 @@ export const MenuList = defineName(React.memo(function MenuList({
518
520
if ( isOpen ) handlePosition ( ) ;
519
521
} , [ isOpen , handlePosition ] ) ;
520
522
523
+ useEffect ( ( ) => {
524
+ if ( ! isOpen || ! onClose ) return ;
525
+
526
+ const handleResize = ( ) => onClose ( { reason : CloseReason . RESIZE } ) ;
527
+ window . addEventListener ( 'resize' , handleResize ) ;
528
+ return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
529
+ } , [ isOpen , onClose ] ) ;
530
+
521
531
useLayoutEffect ( ( ) => {
522
532
if ( animation && isMounted ) {
523
533
if ( isOpen ) {
Original file line number Diff line number Diff line change 1
- import React , { useRef , useMemo } from 'react' ;
1
+ import React , { useRef , useMemo , useCallback } from 'react' ;
2
2
import {
3
3
safeCall ,
4
4
bem ,
@@ -71,6 +71,10 @@ export const useMenuList = (
71
71
}
72
72
} ;
73
73
74
+ const handleMenuClose = useCallback ( e => {
75
+ safeCall ( onClose , e ) ;
76
+ } , [ onClose ] )
77
+
74
78
return (
75
79
< div id = { id }
76
80
className = { bem ( menuContainerClass ) ( ) }
@@ -81,7 +85,9 @@ export const useMenuList = (
81
85
82
86
< SettingsContext . Provider value = { settings } >
83
87
< EventHandlersContext . Provider value = { eventHandlers } >
84
- < MenuList { ...menuListProps } containerRef = { containerRef } >
88
+ < MenuList { ...menuListProps }
89
+ containerRef = { containerRef }
90
+ onClose = { handleMenuClose } >
85
91
{ children }
86
92
</ MenuList >
87
93
</ EventHandlersContext . Provider >
Original file line number Diff line number Diff line change @@ -41,5 +41,6 @@ export const HoverIndexActionTypes = Object.freeze({
41
41
export const CloseReason = Object . freeze ( {
42
42
'CLICK' : 'click' ,
43
43
'CANCEL' : 'cancel' ,
44
- 'BLUR' : 'blur'
44
+ 'BLUR' : 'blur' ,
45
+ 'RESIZE' : 'resize'
45
46
} ) ;
You can’t perform that action at this time.
0 commit comments