@@ -36,6 +36,9 @@ type CarouselSlideProps = {
3636function CarouselSlide ( { slide, offset } : CarouselSlideProps ) {
3737 const containerRef = React . useRef < HTMLDivElement > ( null ) ;
3838
39+ const pointerDownData = React . useRef < { x : number ; y : number ; time : number } | null > ( null ) ;
40+ const isRealClickRef = React . useRef < boolean > ( false ) ;
41+
3942 const { currentIndex } = useLightboxState ( ) ;
4043 const { slideRect, close, focus } = useController ( ) ;
4144 const {
@@ -55,6 +58,30 @@ function CarouselSlide({ slide, offset }: CarouselSlideProps) {
5558 }
5659 } , [ offscreen , focus , getOwnerDocument ] ) ;
5760
61+ const handlePointerDown = ( event : React . PointerEvent < HTMLDivElement > ) => {
62+ pointerDownData . current = {
63+ x : event . clientX ,
64+ y : event . clientY ,
65+ time : Date . now ( ) ,
66+ } ;
67+ isRealClickRef . current = false ;
68+ } ;
69+
70+ const handlePointerUp = ( event : React . PointerEvent < HTMLDivElement > ) => {
71+ const down = pointerDownData . current ;
72+ if ( ! down ) return ;
73+ const dx = event . clientX - down . x ;
74+ const dy = event . clientY - down . y ;
75+ const dt = Date . now ( ) - down . time ;
76+ const distance = Math . sqrt ( dx * dx + dy * dy ) ;
77+ if ( distance < 5 && dt < 300 ) {
78+ isRealClickRef . current = true ;
79+ } else {
80+ isRealClickRef . current = false ;
81+ }
82+ pointerDownData . current = null ;
83+ } ;
84+
5885 const renderSlide = ( ) => {
5986 let rendered = render . slide ?.( { slide, offset, rect : slideRect } ) ;
6087
@@ -84,17 +111,19 @@ function CarouselSlide({ slide, offset }: CarouselSlideProps) {
84111 const handleBackdropClick : React . MouseEventHandler = ( event ) => {
85112 const container = containerRef . current ;
86113 const target = event . target instanceof HTMLElement ? event . target : undefined ;
114+
87115 if (
88116 closeOnBackdropClick &&
117+ isRealClickRef . current &&
89118 target &&
90119 container &&
91120 ( target === container ||
92- // detect Zoom and Video wrappers
93121 ( Array . from ( container . children ) . find ( ( x ) => x === target ) &&
94122 target . classList . contains ( cssClass ( CLASS_SLIDE_WRAPPER ) ) ) )
95123 ) {
96124 close ( ) ;
97125 }
126+ isRealClickRef . current = false ;
98127 } ;
99128
100129 return (
@@ -107,6 +136,8 @@ function CarouselSlide({ slide, offset }: CarouselSlideProps) {
107136 cssClass ( CLASS_FLEX_CENTER ) ,
108137 ) }
109138 { ...makeInertWhen ( offscreen ) }
139+ onPointerDown = { handlePointerDown }
140+ onPointerUp = { handlePointerUp }
110141 onClick = { handleBackdropClick }
111142 style = { style }
112143 role = "region"
0 commit comments