Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Dialog): use MDC Foundation with Dialogs #177

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 46 additions & 39 deletions src/Dialog/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// @flow
import * as React from 'react';
import { MDCDialog } from '@material/dialog/dist/mdc.dialog';
import { MDCDialog, util } from '@material/dialog/dist/mdc.dialog';

import Button from '../Button';
import { simpleTag, withMDC, noop } from '../Base';
import type { SimpleTagPropsT } from '../Base';
import { MDCRipple } from '@material/ripple/dist/mdc.ripple';

import { withFoundation } from '../Base/MDCFoundation';


export const DialogRoot = simpleTag({
displayName: 'DialogRoot',
Expand Down Expand Up @@ -111,48 +115,51 @@ type DialogPropsT = {
onClose: (evt: Event) => mixed
};

export const Dialog = withMDC({
mdcConstructor: MDCDialog,
mdcElementRef: true,
mdcEvents: {
'MDCDialog:accept': (evt, props) => {
props.onAccept(evt);
props.onClose(evt);
},
'MDCDialog:cancel': (evt, props) => {
props.onCancel(evt);
props.onClose(evt);
}
},
defaultProps: {
open: false,
onAccept: noop,
onCancel: noop,
onClose: noop
},
onUpdate: (props, nextProps, api) => {
if (api && api.open !== !!nextProps.open) {
nextProps.open ? api.show() : api.close();
export class Dialog extends withFoundation({
constructor: MDCDialog,
refs: [
'root_'
],
adapter: {}
})<DialogPropsT, {}> {
static displayName = 'Dialog';

componentDidMount(){
super.componentDidMount()

this.footerBtnRipples_ = [];
const footerBtns = this.root_.querySelectorAll('.mdc-dialog__footer__button');
for (let i = 0, footerBtn; footerBtn = footerBtns[i]; i++) {
this.footerBtnRipples_.push(new MDCRipple(footerBtn));
}

this.focusTrap_ = util.createFocusTrapInstance(this.dialogSurface_, this.acceptButton_);
}
})(
class extends React.Component<DialogPropsT> {
static displayName = 'Dialog';

render() {
const {
open,
onAccept,
onCancel,
onClose,
mdcElementRef,
...rest
} = this.props;

return <DialogRoot elementRef={mdcElementRef} {...rest} />;

componentWillReceiveProps(newProps){
super.componentWillReceiveProps(newProps);

if(newProps.open){
this.show();
}
}
);

render() {
const {
open,
onAccept,
onCancel,
onClose,
mdcElementRef,
...rest
} = this.props;

const { root_ } = this.foundationRefs;

return <DialogRoot elementRef={root_} {...rest} />;
}
}


type SimpleDialogPropsT = {
/** A title for the default Dialog template. */
Expand Down