@@ -33,22 +33,21 @@ const eventTypeMapping = {
33
33
touchend : 'onTouchEnd'
34
34
} ;
35
35
36
- const reactMajorVersion = parseInt ( React . version . split ( '.' ) [ 0 ] , 10 ) ;
37
- console . log ( { reactMajorVersion } ) ;
38
-
39
- function useForkRef < T = any > (
40
- ...refs : Array < Ref < T > | undefined >
41
- ) : RefCallback < T > {
42
- return ( node : T ) => {
36
+ const mergeRefs = < T extends any > (
37
+ refs : Array < Ref < T > | undefined | null >
38
+ ) : RefCallback < T > => {
39
+ return ( value ) => {
43
40
refs . forEach ( ( ref ) => {
44
41
if ( typeof ref === 'function' ) {
45
- ref ( node ) ;
46
- } else if ( ref != null && typeof ref === 'object' ) {
47
- ( ref as MutableRefObject < T | null > ) . current = node ;
42
+ ref ( value ) ;
43
+ } else if ( ref != null ) {
44
+ ( ref as MutableRefObject < T | null > ) . current = value ;
48
45
}
49
46
} ) ;
50
47
} ;
51
- }
48
+ } ;
49
+
50
+ const reactMajorVersion = parseInt ( React . version . split ( '.' ) [ 0 ] , 10 ) ;
52
51
53
52
const ClickAwayListener : FunctionComponent < Props > = ( {
54
53
children,
@@ -87,13 +86,17 @@ const ClickAwayListener: FunctionComponent<Props> = ({
87
86
}
88
87
} ;
89
88
90
- const combinedRef = useForkRef ( ( ref ) => {
91
- node . current = ref ;
92
- } , ( children as any ) . ref ) ;
89
+ let childRef : React . Ref < any > | null = null ;
93
90
94
- const handleReact19ChildRef = ( instance : HTMLElement | null ) => {
95
- node . current = instance ;
96
- } ;
91
+ // For React 19+, we get the ref via props.ref
92
+ if ( reactMajorVersion >= 19 ) {
93
+ childRef = children . props ?. ref || null ;
94
+ } else if ( 'ref' in children ) {
95
+ childRef = ( children as any ) . ref ;
96
+ }
97
+
98
+ // Create a combined ref handler
99
+ const combinedRef = mergeRefs ( [ node , childRef ] ) ;
97
100
98
101
useEffect ( ( ) => {
99
102
const nodeDocument = node . current ?. ownerDocument ?? document ;
@@ -129,7 +132,7 @@ const ClickAwayListener: FunctionComponent<Props> = ({
129
132
130
133
return React . Children . only (
131
134
cloneElement ( children as ReactElement < any > , {
132
- ref : reactMajorVersion >= 19 ? handleReact19ChildRef : combinedRef ,
135
+ ref : combinedRef ,
133
136
[ mappedFocusEvent ] : handleBubbledEvents ( mappedFocusEvent ) ,
134
137
[ mappedMouseEvent ] : handleBubbledEvents ( mappedMouseEvent ) ,
135
138
[ mappedTouchEvent ] : handleBubbledEvents ( mappedTouchEvent )
0 commit comments