@@ -39,6 +39,8 @@ import {FocusTrap} from 'focus-trap';
3939
4040const { cssClasses : listCssClasses } = MDCListFoundation ;
4141
42+ type RefCallback < T > = ( node : T ) => void ;
43+
4244export interface DrawerProps extends React . HTMLProps < HTMLElement > {
4345 className ?: string ;
4446 open ?: boolean ;
@@ -47,12 +49,17 @@ export interface DrawerProps extends React.HTMLProps<HTMLElement>{
4749 tag ?: string ;
4850 dismissible ?: boolean ;
4951 modal ?: boolean ;
52+ innerRef ?: RefCallback < HTMLElement > | React . RefObject < HTMLElement > ;
5053} ;
5154
5255interface DrawerState {
5356 classList : Set < string > ;
5457} ;
5558
59+ const isRefObject = function ( ref : DrawerProps [ 'innerRef' ] ) : ref is React . RefObject < HTMLElement > {
60+ return typeof ref !== 'function' ;
61+ } ;
62+
5663class Drawer extends React . Component < DrawerProps , DrawerState > {
5764 previousFocus : HTMLElement | null = null ;
5865 foundation : MDCDismissibleDrawerFoundation | MDCModalDrawerFoundation ;
@@ -193,6 +200,25 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
193200 this . foundation . handleTransitionEnd ( evt ) ;
194201 } ;
195202
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+
196222 render ( ) {
197223 const {
198224 /* eslint-disable no-unused-vars */
@@ -203,6 +229,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
203229 dismissible,
204230 children,
205231 className,
232+ innerRef,
206233 /* eslint-enable no-unused-vars */
207234 tag : Tag ,
208235 modal,
@@ -215,7 +242,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
215242 // @ts -ignore */ }
216243 < Tag
217244 className = { this . classes }
218- ref = { this . drawerElement }
245+ ref = { this . attachRef }
219246 onKeyDown = { this . handleKeyDown }
220247 onTransitionEnd = { this . handleTransitionEnd }
221248 { ...otherProps }
0 commit comments