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

Commit

Permalink
feat(drawer): add innerRef prop (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasecdb authored and Matt Goo committed Apr 30, 2019
1 parent 5f55983 commit a8c64b5
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 77 deletions.
139 changes: 108 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@material/chips": "^1.0.0",
"@material/dialog": "^0.43.0",
"@material/dom": "^0.41.0",
"@material/drawer": "^0.41.0",
"@material/drawer": "^1.0.1",
"@material/fab": "^0.41.0",
"@material/floating-label": "^0.41.0",
"@material/icon-button": "^0.41.0",
Expand Down Expand Up @@ -147,8 +147,8 @@
"react-dom": "^16.4.2",
"react-router-dom": "^4.3.1",
"remap-istanbul": "^0.12.0",
"rimraf": "^2.6.3",
"resemblejs": "^3.0.1",
"rimraf": "^2.6.3",
"sass-loader": "^6.0.7",
"testdouble": "^3.6.0",
"ts-loader": "^3.5.0",
Expand Down
18 changes: 9 additions & 9 deletions packages/drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import {
MDCDismissibleDrawerFoundation,
MDCModalDrawerFoundation,
util,
// @ts-ignore no .d.ts file
} from '@material/drawer/dist/mdc.drawer';
} from '@material/drawer';
// @ts-ignore no .d.ts file
import {MDCListFoundation} from '@material/list/dist/mdc.list';
import DrawerHeader from './Header';
Expand Down Expand Up @@ -62,7 +61,7 @@ const isRefObject = function(ref: DrawerProps['innerRef']): ref is React.RefObje

class Drawer extends React.Component<DrawerProps, DrawerState> {
previousFocus: HTMLElement | null = null;
foundation: MDCDismissibleDrawerFoundation | MDCModalDrawerFoundation;
foundation?: MDCDismissibleDrawerFoundation | MDCModalDrawerFoundation;
focusTrap?: FocusTrap;
drawerElement: React.RefObject<HTMLDivElement> = React.createRef();

Expand Down Expand Up @@ -113,7 +112,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
if (changedToModal || changedToDismissible) {
this.initFoundation();
}
if (open !== prevProps.open) {
if (open !== prevProps.open && this.foundation) {
open ? this.foundation.open() : this.foundation.close();
}
}
Expand All @@ -124,7 +123,7 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
}

private initializeFocusTrap = () => {
this.focusTrap = util.createFocusTrapInstance(this.drawerElement.current);
this.focusTrap = util.createFocusTrapInstance(this.drawerElement.current!);
};

get classes() {
Expand Down Expand Up @@ -191,13 +190,13 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
handleKeyDown = (evt: React.KeyboardEvent<HTMLElement>) => {
this.props.onKeyDown!(evt);
if (!this.foundation) return;
this.foundation.handleKeydown(evt);
this.foundation.handleKeydown(evt.nativeEvent);
};

handleTransitionEnd = (evt: React.TransitionEvent<HTMLElement>) => {
this.props.onTransitionEnd!(evt);
if (!this.foundation) return;
this.foundation.handleTransitionEnd(evt);
this.foundation.handleTransitionEnd(evt.nativeEvent);
};

attachRef = (node: HTMLElement) => {
Expand Down Expand Up @@ -230,9 +229,9 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
children,
className,
innerRef,
modal,
/* eslint-enable no-unused-vars */
tag: Tag,
modal,
...otherProps
} = this.props;

Expand All @@ -253,11 +252,12 @@ class Drawer extends React.Component<DrawerProps, DrawerState> {
</React.Fragment>
);
}

renderScrim() {
return (
<div
className='mdc-drawer-scrim'
onClick={() => this.foundation.handleScrimClick()}
onClick={() => (this.foundation as MDCModalDrawerFoundation).handleScrimClick()}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/drawer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"url": "https://github.com/material-components/material-components-web-react.git"
},
"dependencies": {
"@material/drawer": "^0.41.0",
"@material/drawer": "^1.0.1",
"@material/list": "^0.41.0",
"classnames": "^2.2.6",
"focus-trap": "^4.0.2",
Expand Down
Loading

0 comments on commit a8c64b5

Please sign in to comment.