@@ -33,6 +33,7 @@ export function usePieSectorFocus({
3333 const sectorFocusRef = useRef ( - 1 ) ;
3434 const lastSectorRef = useRef ( - 1 ) ;
3535 const spaceHeldRef = useRef ( false ) ;
36+ const rafIdRef = useRef ( 0 ) ;
3637 const [ inSectorMode , setInSectorMode ] = useState ( false ) ;
3738 const getSectorLabelRef = useRef ( getSectorLabel ) ;
3839 // Keep ref in sync so focusSector always uses the latest callback without re-creating the memoized function.
@@ -49,6 +50,10 @@ export function usePieSectorFocus({
4950 setInSectorMode ( false ) ;
5051 } , [ dataLength ] ) ;
5152
53+ useEffect ( ( ) => {
54+ return ( ) => cancelAnimationFrame ( rafIdRef . current ) ;
55+ } , [ ] ) ;
56+
5257 const focusSector = useCallback (
5358 ( index : number ) => {
5459 const pieGroup = chartRef . current ?. querySelector < SVGGElement > ( '.recharts-pie' ) ;
@@ -94,6 +99,7 @@ export function usePieSectorFocus({
9499 . querySelectorAll < SVGGElement > ( '.recharts-pie-sector[tabindex]' )
95100 . forEach ( ( s ) => s . removeAttribute ( 'tabindex' ) ) ;
96101 }
102+ spaceHeldRef . current = false ;
97103 sectorFocusRef . current = - 1 ;
98104 setInSectorMode ( false ) ;
99105 } , [ chartRef ] ) ;
@@ -178,7 +184,7 @@ export function usePieSectorFocus({
178184 // Defer cleanup — blur fires before layout effects, so the new focus target may not be settled yet.
179185 if ( ! e . currentTarget . contains ( e . relatedTarget as Node ) ) {
180186 const container = e . currentTarget as HTMLElement ;
181- requestAnimationFrame ( ( ) => {
187+ rafIdRef . current = requestAnimationFrame ( ( ) => {
182188 if ( ! container . contains ( document . activeElement ) ) {
183189 lastSectorRef . current = sectorFocusRef . current ;
184190 exitSectorMode ( ) ;
0 commit comments