@@ -22,6 +22,7 @@ import { getNextActiveElement, isRTL } from '../../utils'
2222
2323import type { Alignments , Directions } from './types'
2424import { getPlacement } from './utils'
25+ import { CFocusTrap } from '../focus-trap'
2526
2627export interface CDropdownProps extends HTMLAttributes < HTMLDivElement | HTMLLIElement > {
2728 /**
@@ -288,7 +289,11 @@ export const CDropdown: PolymorphicRefForwardingComponent<'div', CDropdownProps>
288289 } , [ dropdownToggleElement , allowPopperUse , destroyPopper , onHide ] )
289290
290291 const handleKeydown = useCallback ( ( event : KeyboardEvent ) => {
291- if ( dropdownMenuRef . current && ( event . key === 'ArrowDown' || event . key === 'ArrowUp' ) ) {
292+ if ( ! dropdownMenuRef . current ) {
293+ return
294+ }
295+
296+ if ( event . key === 'ArrowDown' || event . key === 'ArrowUp' ) {
292297 event . preventDefault ( )
293298 const target = event . target as HTMLElement
294299 const items = [
@@ -404,26 +409,28 @@ export const CDropdown: PolymorphicRefForwardingComponent<'div', CDropdownProps>
404409
405410 return (
406411 < CDropdownContext . Provider value = { contextValues } >
407- { variant === 'input-group' ? (
408- < > { children } </ >
409- ) : (
410- < Component
411- className = { classNames (
412- variant === 'nav-item' ? 'nav-item dropdown' : variant ,
413- {
414- 'dropdown-center' : direction === 'center' ,
415- 'dropup dropup-center' : direction === 'dropup-center' ,
416- [ `${ direction } ` ] :
417- direction && direction !== 'center' && direction !== 'dropup-center' ,
418- } ,
419- className
420- ) }
421- { ...rest }
422- ref = { forkedRef }
423- >
424- { children }
425- </ Component >
426- ) }
412+ < CFocusTrap active = { portal && _visible } additionalContainer = { dropdownMenuRef } restoreFocus >
413+ { variant === 'input-group' ? (
414+ < > { children } </ >
415+ ) : (
416+ < Component
417+ className = { classNames (
418+ variant === 'nav-item' ? 'nav-item dropdown' : variant ,
419+ {
420+ 'dropdown-center' : direction === 'center' ,
421+ 'dropup dropup-center' : direction === 'dropup-center' ,
422+ [ `${ direction } ` ] :
423+ direction && direction !== 'center' && direction !== 'dropup-center' ,
424+ } ,
425+ className
426+ ) }
427+ { ...rest }
428+ ref = { forkedRef }
429+ >
430+ { children }
431+ </ Component >
432+ ) }
433+ </ CFocusTrap >
427434 </ CDropdownContext . Provider >
428435 )
429436 }
0 commit comments