1- import { autoUpdate , size , useClick , useDismiss , useFloating , useInteractions } from "@floating-ui/react" ;
2- import { createElement , ReactElement , useEffect , useLayoutEffect , useMemo , useRef , useState } from "react" ;
1+ import {
2+ FloatingFocusManager ,
3+ autoUpdate ,
4+ size ,
5+ useClick ,
6+ useDismiss ,
7+ useFloating ,
8+ useInteractions
9+ } from "@floating-ui/react" ;
10+ import { createElement , ReactElement , useEffect , useMemo , useState } from "react" ;
311import { flushSync } from "react-dom" ;
412import { GridColumn } from "../typings/GridColumn" ;
513import { FaEye } from "./icons/FaEye" ;
@@ -15,7 +23,6 @@ export function ColumnSelector(props: ColumnSelectorProps): ReactElement {
1523 const { visibleLength } = props ;
1624 const [ show , setShow ] = useState ( false ) ;
1725 const [ maxHeight , setMaxHeight ] = useState < number > ( 0 ) ;
18- const buttonRef = useRef < HTMLButtonElement > ( null ) ;
1926 const { refs, floatingStyles, context, update } = useFloating ( {
2027 open : show ,
2128 placement : "bottom-end" ,
@@ -49,20 +56,6 @@ export function ColumnSelector(props: ColumnSelectorProps): ReactElement {
4956 const firstHidableColumnIndex = useMemo ( ( ) => props . columns . findIndex ( c => c . canHide ) , [ props . columns ] ) ;
5057 const lastHidableColumnIndex = useMemo ( ( ) => props . columns . map ( c => c . canHide ) . lastIndexOf ( true ) , [ props . columns ] ) ;
5158
52- useLayoutEffect ( ( ) => {
53- if ( show ) {
54- // Focus the first visible column
55- setTimeout ( ( ) => {
56- ( refs . floating ?. current ?. querySelector ( "li" ) as HTMLElement ) ?. focus ( ) ;
57- } , 10 ) ;
58- } else {
59- // focus back to the button when closing
60- setTimeout ( ( ) => {
61- ( refs . reference ?. current as HTMLElement ) ?. focus ( ) ;
62- } , 10 ) ;
63- }
64- } , [ show ] ) ;
65-
6659 const optionsComponent = (
6760 < ul
6861 ref = { refs . setFloating }
@@ -102,7 +95,6 @@ export function ColumnSelector(props: ColumnSelectorProps): ReactElement {
10295 ) {
10396 e . preventDefault ( ) ;
10497 setShow ( false ) ;
105- buttonRef . current ?. focus ( ) ;
10698 }
10799 } }
108100 role = "menuitem"
@@ -147,7 +139,11 @@ export function ColumnSelector(props: ColumnSelectorProps): ReactElement {
147139 < FaEye />
148140 </ button >
149141 </ div >
150- { show && optionsComponent }
142+ { show && (
143+ < FloatingFocusManager context = { context } modal = { false } >
144+ { optionsComponent }
145+ </ FloatingFocusManager >
146+ ) }
151147 </ div >
152148 ) ;
153149}
0 commit comments