@@ -12,11 +12,12 @@ import {
12
12
getSlideKey ,
13
13
hasSlides ,
14
14
isImageSlide ,
15
+ makeInertWhen ,
15
16
parseLengthPercentage ,
16
17
} from "../utils.js" ;
17
18
import { ImageSlide } from "../components/index.js" ;
18
19
import { useController } from "./Controller/index.js" ;
19
- import { useLightboxProps , useLightboxState } from "../contexts/index.js" ;
20
+ import { useDocumentContext , useLightboxProps , useLightboxState } from "../contexts/index.js" ;
20
21
import { CLASS_FLEX_CENTER , CLASS_SLIDE_WRAPPER , MODULE_CAROUSEL } from "../consts.js" ;
21
22
22
23
function cssPrefix ( value ?: string ) {
@@ -36,14 +37,23 @@ function CarouselSlide({ slide, offset }: CarouselSlideProps) {
36
37
const containerRef = React . useRef < HTMLDivElement | null > ( null ) ;
37
38
38
39
const { currentIndex } = useLightboxState ( ) ;
39
- const { slideRect, close } = useController ( ) ;
40
+ const { slideRect, close, focus } = useController ( ) ;
40
41
const {
41
42
render,
42
43
carousel : { imageFit, imageProps } ,
43
44
on : { click : onClick } ,
44
45
controller : { closeOnBackdropClick } ,
45
46
styles : { slide : style } ,
46
47
} = useLightboxProps ( ) ;
48
+ const { getOwnerDocument } = useDocumentContext ( ) ;
49
+
50
+ const offscreen = offset !== 0 ;
51
+
52
+ React . useEffect ( ( ) => {
53
+ if ( offscreen && containerRef . current ?. contains ( getOwnerDocument ( ) . activeElement ) ) {
54
+ focus ( ) ;
55
+ }
56
+ } , [ offscreen , focus , getOwnerDocument ] ) ;
47
57
48
58
const renderSlide = ( ) => {
49
59
let rendered = render . slide ?.( { slide, offset, rect : slideRect } ) ;
@@ -57,7 +67,7 @@ function CarouselSlide({ slide, offset }: CarouselSlideProps) {
57
67
rect = { slideRect }
58
68
imageFit = { imageFit }
59
69
imageProps = { imageProps }
60
- onClick = { offset === 0 ? ( ) => onClick ?.( { index : currentIndex } ) : undefined }
70
+ onClick = { ! offscreen ? ( ) => onClick ?.( { index : currentIndex } ) : undefined }
61
71
/>
62
72
) ;
63
73
}
@@ -93,10 +103,10 @@ function CarouselSlide({ slide, offset }: CarouselSlideProps) {
93
103
ref = { containerRef }
94
104
className = { clsx (
95
105
cssClass ( cssSlidePrefix ( ) ) ,
96
- offset === 0 && cssClass ( cssSlidePrefix ( "current" ) ) ,
106
+ ! offscreen && cssClass ( cssSlidePrefix ( "current" ) ) ,
97
107
cssClass ( CLASS_FLEX_CENTER ) ,
98
108
) }
99
- aria-hidden = { offset !== 0 }
109
+ { ... makeInertWhen ( offscreen ) }
100
110
onClick = { handleBackdropClick }
101
111
style = { style }
102
112
>
0 commit comments