@@ -39,6 +39,8 @@ import {FocusTrap} from 'focus-trap';
39
39
40
40
const { cssClasses : listCssClasses } = MDCListFoundation ;
41
41
42
+ type RefCallback < T > = ( node : T ) => void ;
43
+
42
44
export interface DrawerProps extends React . HTMLProps < HTMLElement > {
43
45
className ?: string ;
44
46
open ?: boolean ;
@@ -47,12 +49,17 @@ export interface DrawerProps extends React.HTMLProps<HTMLElement>{
47
49
tag ?: string ;
48
50
dismissible ?: boolean ;
49
51
modal ?: boolean ;
52
+ innerRef ?: RefCallback < HTMLElement > | React . RefObject < HTMLElement > ;
50
53
} ;
51
54
52
55
interface DrawerState {
53
56
classList : Set < string > ;
54
57
} ;
55
58
59
+ const isRefObject = function ( ref : DrawerProps [ 'innerRef' ] ) : ref is React . RefObject < HTMLElement > {
60
+ return typeof ref !== 'function' ;
61
+ } ;
62
+
56
63
class Drawer extends React . Component < DrawerProps , DrawerState > {
57
64
previousFocus : HTMLElement | null = null ;
58
65
foundation : MDCDismissibleDrawerFoundation | MDCModalDrawerFoundation ;
@@ -193,6 +200,25 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
193
200
this . foundation . handleTransitionEnd ( evt ) ;
194
201
} ;
195
202
203
+ attachRef = ( node : HTMLElement ) => {
204
+ const { innerRef} = this . props ;
205
+
206
+ // https://github.com/facebook/react/issues/13029#issuecomment-410002316
207
+ // @ts -ignore this is acceptable according to the comment above
208
+ this . drawerElement . current = node ;
209
+
210
+ if ( ! innerRef ) {
211
+ return ;
212
+ }
213
+
214
+ if ( isRefObject ( innerRef ) ) {
215
+ // @ts -ignore same as above
216
+ innerRef . current = node ;
217
+ } else {
218
+ innerRef ( node ) ;
219
+ }
220
+ }
221
+
196
222
render ( ) {
197
223
const {
198
224
/* eslint-disable no-unused-vars */
@@ -203,6 +229,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
203
229
dismissible,
204
230
children,
205
231
className,
232
+ innerRef,
206
233
/* eslint-enable no-unused-vars */
207
234
tag : Tag ,
208
235
modal,
@@ -215,7 +242,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
215
242
// @ts -ignore */ }
216
243
< Tag
217
244
className = { this . classes }
218
- ref = { this . drawerElement }
245
+ ref = { this . attachRef }
219
246
onKeyDown = { this . handleKeyDown }
220
247
onTransitionEnd = { this . handleTransitionEnd }
221
248
{ ...otherProps }
0 commit comments