@@ -71,6 +71,10 @@ export interface DropdownProps extends MenuProps, OUIAProps {
71
71
maxMenuHeight ?: string ;
72
72
/** @beta Flag indicating the first menu item should be focused after opening the dropdown. */
73
73
shouldFocusFirstItemOnOpen ?: boolean ;
74
+ /** Flag indicating if scroll on focus of the first menu item should occur. */
75
+ shouldPreventScrollOnItemFocus ?: boolean ;
76
+ /** Time in ms to wait before firing the toggles' focus event. Defaults to 0 */
77
+ focusTimeoutDelay ?: number ;
74
78
}
75
79
76
80
const DropdownBase : React . FunctionComponent < DropdownProps > = ( {
@@ -92,6 +96,8 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
92
96
menuHeight,
93
97
maxMenuHeight,
94
98
shouldFocusFirstItemOnOpen = false ,
99
+ shouldPreventScrollOnItemFocus = true ,
100
+ focusTimeoutDelay = 0 ,
95
101
...props
96
102
} : DropdownProps ) => {
97
103
const localMenuRef = React . useRef < HTMLDivElement > ( ) ;
@@ -112,8 +118,8 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
112
118
const firstElement = menuRef ?. current ?. querySelector (
113
119
'li button:not(:disabled),li input:not(:disabled),li a:not([aria-disabled="true"])'
114
120
) ;
115
- firstElement && ( firstElement as HTMLElement ) . focus ( ) ;
116
- } , 10 ) ;
121
+ firstElement && ( firstElement as HTMLElement ) . focus ( { preventScroll : shouldPreventScrollOnItemFocus } ) ;
122
+ } , focusTimeoutDelay ) ;
117
123
}
118
124
119
125
prevIsOpen . current = isOpen ;
@@ -151,7 +157,16 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
151
157
window . removeEventListener ( 'keydown' , handleMenuKeys ) ;
152
158
window . removeEventListener ( 'click' , handleClick ) ;
153
159
} ;
154
- } , [ isOpen , menuRef , toggleRef , onOpenChange , onOpenChangeKeys ] ) ;
160
+ } , [
161
+ isOpen ,
162
+ menuRef ,
163
+ toggleRef ,
164
+ onOpenChange ,
165
+ onOpenChangeKeys ,
166
+ shouldPreventScrollOnItemFocus ,
167
+ shouldFocusFirstItemOnOpen ,
168
+ focusTimeoutDelay
169
+ ] ) ;
155
170
156
171
const scrollable = maxMenuHeight !== undefined || menuHeight !== undefined || isScrollable ;
157
172
@@ -161,7 +176,7 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
161
176
ref = { menuRef }
162
177
onSelect = { ( event , value ) => {
163
178
onSelect && onSelect ( event , value ) ;
164
- shouldFocusToggleOnSelect && toggleRef . current . focus ( ) ;
179
+ shouldFocusToggleOnSelect && toggleRef . current ? .focus ( ) ;
165
180
} }
166
181
isPlain = { isPlain }
167
182
isScrollable = { scrollable }
0 commit comments