@@ -19,7 +19,7 @@ import { render, Features, PropsForFeatures, forwardRefWithAs } from '../../util
19
19
import { useId } from '../../hooks/use-id'
20
20
import { match } from '../../utils/match'
21
21
import { Keys } from '../../components/keyboard'
22
- import { focusIn , Focus , sortByDomNode } from '../../utils/focus-management'
22
+ import { focusIn , Focus , sortByDomNode , FocusResult } from '../../utils/focus-management'
23
23
import { useIsoMorphicEffect } from '../../hooks/use-iso-morphic-effect'
24
24
import { useSyncRefs } from '../../hooks/use-sync-refs'
25
25
import { useResolveButtonType } from '../../hooks/use-resolve-button-type'
@@ -28,6 +28,7 @@ import { FocusSentinel } from '../../internal/focus-sentinel'
28
28
import { useEvent } from '../../hooks/use-event'
29
29
import { microTask } from '../../utils/micro-task'
30
30
import { Hidden } from '../../internal/hidden'
31
+ import { getOwnerDocument } from '../../utils/owner'
31
32
32
33
interface StateDefinition {
33
34
selectedIndex : number
@@ -322,6 +323,7 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
322
323
323
324
let { orientation, activation, selectedIndex, tabs, panels } = useData ( 'Tab' )
324
325
let actions = useActions ( 'Tab' )
326
+ let data = useData ( 'Tab' )
325
327
let SSRContext = useSSRTabsCounter ( 'Tab' )
326
328
327
329
let internalTabRef = useRef < HTMLElement | null > ( null )
@@ -336,6 +338,16 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
336
338
if ( myIndex === - 1 ) myIndex = mySSRIndex
337
339
let selected = myIndex === selectedIndex
338
340
341
+ let activateUsing = useEvent ( ( cb : ( ) => FocusResult ) => {
342
+ let result = cb ( )
343
+ if ( result === FocusResult . Success && activation === 'auto' ) {
344
+ let newTab = getOwnerDocument ( internalTabRef ) ?. activeElement
345
+ let idx = data . tabs . findIndex ( ( tab ) => tab . current === newTab )
346
+ if ( idx !== - 1 ) actions . change ( idx )
347
+ }
348
+ return result
349
+ } )
350
+
339
351
let handleKeyDown = useEvent ( ( event : ReactKeyboardEvent < HTMLElement > ) => {
340
352
let list = tabs . map ( ( tab ) => tab . current ) . filter ( Boolean ) as HTMLElement [ ]
341
353
@@ -353,38 +365,36 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
353
365
event . preventDefault ( )
354
366
event . stopPropagation ( )
355
367
356
- return focusIn ( list , Focus . First )
368
+ return activateUsing ( ( ) => focusIn ( list , Focus . First ) )
357
369
358
370
case Keys . End :
359
371
case Keys . PageDown :
360
372
event . preventDefault ( )
361
373
event . stopPropagation ( )
362
374
363
- return focusIn ( list , Focus . Last )
375
+ return activateUsing ( ( ) => focusIn ( list , Focus . Last ) )
364
376
}
365
377
366
- if (
367
- match ( orientation , {
378
+ let result = activateUsing ( ( ) => {
379
+ return match ( orientation , {
368
380
vertical ( ) {
369
381
if ( event . key === Keys . ArrowUp ) return focusIn ( list , Focus . Previous | Focus . WrapAround )
370
382
if ( event . key === Keys . ArrowDown ) return focusIn ( list , Focus . Next | Focus . WrapAround )
371
- return
383
+ return FocusResult . Error
372
384
} ,
373
385
horizontal ( ) {
374
386
if ( event . key === Keys . ArrowLeft ) return focusIn ( list , Focus . Previous | Focus . WrapAround )
375
387
if ( event . key === Keys . ArrowRight ) return focusIn ( list , Focus . Next | Focus . WrapAround )
376
- return
388
+ return FocusResult . Error
377
389
} ,
378
390
} )
379
- ) {
391
+ } )
392
+
393
+ if ( result === FocusResult . Success ) {
380
394
return event . preventDefault ( )
381
395
}
382
396
} )
383
397
384
- let handleFocus = useEvent ( ( ) => {
385
- internalTabRef . current ?. focus ( )
386
- } )
387
-
388
398
let ready = useRef ( false )
389
399
let handleSelection = useEvent ( ( ) => {
390
400
if ( ready . current ) return
@@ -411,7 +421,6 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
411
421
let ourProps = {
412
422
ref : tabRef ,
413
423
onKeyDown : handleKeyDown ,
414
- onFocus : activation === 'manual' ? handleFocus : handleSelection ,
415
424
onMouseDown : handleMouseDown ,
416
425
onClick : handleSelection ,
417
426
id,
0 commit comments