@@ -71,6 +71,10 @@ export interface DropdownProps extends MenuProps, OUIAProps {
7171 maxMenuHeight ?: string ;
7272 /** @beta Flag indicating the first menu item should be focused after opening the dropdown. */
7373 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 ;
7478}
7579
7680const DropdownBase : React . FunctionComponent < DropdownProps > = ( {
@@ -92,6 +96,8 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
9296 menuHeight,
9397 maxMenuHeight,
9498 shouldFocusFirstItemOnOpen = false ,
99+ shouldPreventScrollOnItemFocus = true ,
100+ focusTimeoutDelay = 0 ,
95101 ...props
96102} : DropdownProps ) => {
97103 const localMenuRef = React . useRef < HTMLDivElement > ( ) ;
@@ -126,8 +132,8 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
126132 const firstElement = menuRef ?. current ?. querySelector (
127133 'li button:not(:disabled),li input:not(:disabled),li a:not([aria-disabled="true"])'
128134 ) ;
129- firstElement && ( firstElement as HTMLElement ) . focus ( ) ;
130- } , 10 ) ;
135+ firstElement && ( firstElement as HTMLElement ) . focus ( { preventScroll : shouldPreventScrollOnItemFocus } ) ;
136+ } , focusTimeoutDelay ) ;
131137 }
132138
133139 // If the event is not on the toggle and onOpenChange callback is provided, close the menu
@@ -145,7 +151,16 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
145151 window . removeEventListener ( 'keydown' , handleMenuKeys ) ;
146152 window . removeEventListener ( 'click' , handleClick ) ;
147153 } ;
148- } , [ isOpen , menuRef , toggleRef , onOpenChange , onOpenChangeKeys ] ) ;
154+ } , [
155+ isOpen ,
156+ menuRef ,
157+ toggleRef ,
158+ onOpenChange ,
159+ onOpenChangeKeys ,
160+ shouldPreventScrollOnItemFocus ,
161+ shouldFocusFirstItemOnOpen ,
162+ focusTimeoutDelay
163+ ] ) ;
149164
150165 const scrollable = maxMenuHeight !== undefined || menuHeight !== undefined || isScrollable ;
151166
@@ -155,7 +170,7 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
155170 ref = { menuRef }
156171 onSelect = { ( event , value ) => {
157172 onSelect && onSelect ( event , value ) ;
158- shouldFocusToggleOnSelect && toggleRef . current . focus ( ) ;
173+ shouldFocusToggleOnSelect && toggleRef . current ? .focus ( ) ;
159174 } }
160175 isPlain = { isPlain }
161176 isScrollable = { scrollable }
0 commit comments