@@ -4,7 +4,25 @@ import type { RippleProps } from './types';
4
4
import MDBBtn from '../../components/Button/Button' ;
5
5
import MDBRippleWave from './RippleWave/RippleWave' ;
6
6
7
- const MDBRipple : React . FC < RippleProps > =
7
+ const useCombinedRefs = ( ...refs : any ) => {
8
+ const targetRef = React . useRef ( )
9
+
10
+ React . useEffect ( ( ) => {
11
+ refs . forEach ( ( ref : any ) => {
12
+ if ( ! ref ) return
13
+
14
+ if ( typeof ref === 'function' ) {
15
+ ref ( targetRef . current )
16
+ } else {
17
+ ref . current = targetRef . current
18
+ }
19
+ } )
20
+ } , [ refs ] )
21
+
22
+ return targetRef
23
+ }
24
+
25
+ const MDBRipple : React . FC < RippleProps > = React . forwardRef < HTMLAllCollection , RippleProps > (
8
26
(
9
27
{
10
28
className,
@@ -16,12 +34,13 @@ const MDBRipple: React.FC<RippleProps> =
16
34
rippleColor,
17
35
children,
18
36
onClick,
19
- rippleRef,
20
37
...props
21
- }
38
+ } ,
39
+ ref
22
40
) => {
23
- const rippleEl = useRef ( null ) ;
24
- const rippleReference = rippleRef ? rippleRef : rippleEl ;
41
+ const rippleRef = useRef ( null ) ;
42
+ const combinedRef = useCombinedRefs ( ref , rippleRef ) ;
43
+
25
44
const GRADIENT =
26
45
'rgba({{color}}, 0.2) 0, rgba({{color}}, 0.3) 40%, rgba({{color}}, 0.4) 50%, rgba({{color}}, 0.5) 60%, rgba({{color}}, 0) 70%' ;
27
46
@@ -161,7 +180,9 @@ const MDBRipple: React.FC<RippleProps> =
161
180
} ;
162
181
163
182
const getStyles = ( e : any ) => {
164
- const itemRect = rippleReference . current . getBoundingClientRect ( ) ;
183
+ // eslint-disable-next-line
184
+ // @ts -ignore
185
+ const itemRect = combinedRef . current ?. getBoundingClientRect ( ) ;
165
186
166
187
const offsetX = e . clientX - itemRect . left ;
167
188
const offsetY = e . clientY - itemRect . top ;
@@ -221,14 +242,15 @@ const MDBRipple: React.FC<RippleProps> =
221
242
} , [ rippleDuration , rippleStyles ] ) ;
222
243
223
244
return (
224
- < Tag className = { classes } onClick = { ( e : any ) => handleClick ( e ) } ref = { rippleReference } { ...props } >
245
+ < Tag className = { classes } onClick = { ( e : any ) => handleClick ( e ) } ref = { combinedRef } { ...props } >
225
246
{ children }
226
247
{ rippleStyles . map ( ( item , i ) => (
227
248
< MDBRippleWave key = { i } style = { item } > </ MDBRippleWave >
228
249
) ) }
229
250
</ Tag >
230
251
) ;
231
- } ;
252
+ }
253
+ ) ;
232
254
233
255
MDBRipple . defaultProps = { rippleTag : 'div' , rippleDuration : 500 , rippleRadius : 0 , rippleColor : 'dark' } ;
234
256
0 commit comments