Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 782d9da

Browse files
lucasecdbMatt Goo
authored andcommitted
feat(drawer): add innerRef prop (#749)
1 parent 23fe283 commit 782d9da

File tree

6 files changed

+210
-78
lines changed

6 files changed

+210
-78
lines changed

package-lock.json

Lines changed: 108 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@material/chips": "^1.0.0",
6868
"@material/dialog": "^0.43.0",
6969
"@material/dom": "^0.41.0",
70-
"@material/drawer": "^0.41.0",
70+
"@material/drawer": "^1.0.1",
7171
"@material/fab": "^0.41.0",
7272
"@material/floating-label": "^0.41.0",
7373
"@material/icon-button": "^0.41.0",
@@ -147,8 +147,8 @@
147147
"react-dom": "^16.4.2",
148148
"react-router-dom": "^4.3.1",
149149
"remap-istanbul": "^0.12.0",
150-
"rimraf": "^2.6.3",
151150
"resemblejs": "^3.0.1",
151+
"rimraf": "^2.6.3",
152152
"sass-loader": "^6.0.7",
153153
"testdouble": "^3.6.0",
154154
"ts-loader": "^3.5.0",

packages/drawer/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ modal | Boolean | Indicates that the drawer is of type modal.
419419
dismissible | Boolean | Indicates that the drawer is of type dismissible.
420420
tag | String | Customizes the drawer tag type (default to `<aside>`).
421421
open | boolean | If true, opens drawer. If false, closes drawer.
422+
innerRef | RefObject | Root drawer element ref.
422423

423424
## Sass Mixins
424425

packages/drawer/index.tsx

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import {
2626
MDCDismissibleDrawerFoundation,
2727
MDCModalDrawerFoundation,
2828
util,
29-
// @ts-ignore no .d.ts file
30-
} from '@material/drawer/dist/mdc.drawer';
29+
} from '@material/drawer';
3130
// @ts-ignore no .d.ts file
3231
import {MDCListFoundation} from '@material/list/dist/mdc.list';
3332
import DrawerHeader from './Header';
@@ -39,6 +38,8 @@ import {FocusTrap} from 'focus-trap';
3938

4039
const {cssClasses: listCssClasses} = MDCListFoundation;
4140

41+
type RefCallback<T> = (node: T) => void;
42+
4243
export interface DrawerProps extends React.HTMLProps<HTMLElement>{
4344
className?: string;
4445
open?: boolean;
@@ -47,15 +48,20 @@ export interface DrawerProps extends React.HTMLProps<HTMLElement>{
4748
tag?: string;
4849
dismissible?: boolean;
4950
modal?: boolean;
51+
innerRef?: RefCallback<HTMLElement> | React.RefObject<HTMLElement>;
5052
};
5153

5254
interface DrawerState {
5355
classList: Set<string>;
5456
};
5557

58+
const isRefObject = function(ref: DrawerProps['innerRef']): ref is React.RefObject<HTMLElement> {
59+
return typeof ref !== 'function';
60+
};
61+
5662
class Drawer extends React.Component<DrawerProps, DrawerState> {
5763
previousFocus: HTMLElement | null = null;
58-
foundation: MDCDismissibleDrawerFoundation | MDCModalDrawerFoundation;
64+
foundation?: MDCDismissibleDrawerFoundation | MDCModalDrawerFoundation;
5965
focusTrap?: FocusTrap;
6066
drawerElement: React.RefObject<HTMLDivElement> = React.createRef();
6167

@@ -106,7 +112,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
106112
if (changedToModal || changedToDismissible) {
107113
this.initFoundation();
108114
}
109-
if (open !== prevProps.open) {
115+
if (open !== prevProps.open && this.foundation) {
110116
open ? this.foundation.open() : this.foundation.close();
111117
}
112118
}
@@ -117,7 +123,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
117123
}
118124

119125
private initializeFocusTrap = () => {
120-
this.focusTrap = util.createFocusTrapInstance(this.drawerElement.current);
126+
this.focusTrap = util.createFocusTrapInstance(this.drawerElement.current!);
121127
};
122128

123129
get classes() {
@@ -184,15 +190,34 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
184190
handleKeyDown = (evt: React.KeyboardEvent<HTMLElement>) => {
185191
this.props.onKeyDown!(evt);
186192
if (!this.foundation) return;
187-
this.foundation.handleKeydown(evt);
193+
this.foundation.handleKeydown(evt.nativeEvent);
188194
};
189195

190196
handleTransitionEnd = (evt: React.TransitionEvent<HTMLElement>) => {
191197
this.props.onTransitionEnd!(evt);
192198
if (!this.foundation) return;
193-
this.foundation.handleTransitionEnd(evt);
199+
this.foundation.handleTransitionEnd(evt.nativeEvent);
194200
};
195201

202+
attachRef = (node: HTMLElement) => {
203+
const {innerRef} = this.props;
204+
205+
// https://github.com/facebook/react/issues/13029#issuecomment-410002316
206+
// @ts-ignore this is acceptable according to the comment above
207+
this.drawerElement.current = node;
208+
209+
if (!innerRef) {
210+
return;
211+
}
212+
213+
if (isRefObject(innerRef)) {
214+
// @ts-ignore same as above
215+
innerRef.current = node;
216+
} else {
217+
innerRef(node);
218+
}
219+
}
220+
196221
render() {
197222
const {
198223
/* eslint-disable no-unused-vars */
@@ -203,9 +228,10 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
203228
dismissible,
204229
children,
205230
className,
231+
innerRef,
232+
modal,
206233
/* eslint-enable no-unused-vars */
207234
tag: Tag,
208-
modal,
209235
...otherProps
210236
} = this.props;
211237

@@ -215,7 +241,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
215241
// @ts-ignore */}
216242
<Tag
217243
className={this.classes}
218-
ref={this.drawerElement}
244+
ref={this.attachRef}
219245
onKeyDown={this.handleKeyDown}
220246
onTransitionEnd={this.handleTransitionEnd}
221247
{...otherProps}
@@ -226,11 +252,12 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
226252
</React.Fragment>
227253
);
228254
}
255+
229256
renderScrim() {
230257
return (
231258
<div
232259
className='mdc-drawer-scrim'
233-
onClick={() => this.foundation.handleScrimClick()}
260+
onClick={() => (this.foundation as MDCModalDrawerFoundation).handleScrimClick()}
234261
/>
235262
);
236263
}

packages/drawer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"url": "https://github.com/material-components/material-components-web-react.git"
1818
},
1919
"dependencies": {
20-
"@material/drawer": "^0.41.0",
20+
"@material/drawer": "^1.0.1",
2121
"@material/list": "^0.41.0",
2222
"classnames": "^2.2.6",
2323
"focus-trap": "^3.0.0",

0 commit comments

Comments
 (0)