Skip to content

Commit 46e0ebe

Browse files
committed
Add material dialog
1 parent 3011d23 commit 46e0ebe

File tree

5 files changed

+290
-4
lines changed

5 files changed

+290
-4
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@material/checkbox": "5.1.0",
3434
"@material/circular-progress": "7.0.0",
3535
"@material/data-table": "5.1.0",
36+
"@material/dialog": "10.0.0",
3637
"@material/drawer": "5.1.0",
3738
"@material/floating-label": "5.1.0",
3839
"@material/form-field": "5.1.0",

plugin/assets/css/src/settings/base.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@
7979
font-weight: 500;
8080
margin-left: 10px;
8181
}
82+
83+
.mdc-dialog .mdc-dialog__title {
84+
margin-top: 0;
85+
}
8286
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* External dependencies
19+
*/
20+
import classNames from 'classnames';
21+
import { MDCDialog } from '@material/dialog';
22+
import '@material/dialog/dist/mdc.dialog.css';
23+
24+
/**
25+
* WordPress dependencies
26+
*/
27+
import { withInstanceId } from '@wordpress/compose';
28+
import { useEffect, useRef } from '@wordpress/element';
29+
30+
const Dialog = withInstanceId(
31+
( { open = false, title, content, actions, instanceId } ) => {
32+
const ref = useRef();
33+
34+
useEffect( () => {
35+
if ( ! ref.current ) {
36+
return;
37+
}
38+
39+
const dialog = new MDCDialog( ref.current );
40+
41+
return () => {
42+
dialog.destroy();
43+
};
44+
}, [] );
45+
46+
return (
47+
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
48+
<div
49+
className={ classNames( 'mdc-dialog', { 'mdc-dialog--open': open } ) }
50+
ref={ ref }
51+
>
52+
<div className="mdc-dialog__container">
53+
<div
54+
className="mdc-dialog__surface"
55+
role="alertdialog"
56+
aria-modal="true"
57+
aria-labelledby={ `dialog-title-${ instanceId }` }
58+
aria-describedby={ `dialog-content-${ instanceId }` }
59+
>
60+
{ title && (
61+
<h2
62+
className="mdc-dialog__title"
63+
id={ `dialog-title-${ instanceId }` }
64+
>
65+
{ title }
66+
</h2>
67+
) }
68+
69+
{ content && (
70+
<div
71+
className="mdc-dialog__content"
72+
id={ `dialog-content-${ instanceId }` }
73+
>
74+
{ content }
75+
</div>
76+
) }
77+
78+
{ actions && (
79+
<div className="mdc-dialog__actions">{ actions }</div>
80+
) }
81+
</div>
82+
</div>
83+
<div className="mdc-dialog__scrim"></div>
84+
</div>
85+
);
86+
}
87+
);
88+
89+
export default Dialog;

plugin/assets/src/settings/components/integrations/api.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ import SettingsContext from '../../context';
2828
import { ACTIONS, KEY_PLACEHOLDER } from '../../constants';
2929
import Button from '../../../wizard/components/navigation/button';
3030
import { setApiKey } from '../../utils';
31+
import Dialog from '../../../common/components/dialog';
3132

3233
const Api = () => {
3334
const { state, dispatch } = useContext( SettingsContext );
3435
const [ api, setApi ] = useState( null );
3536
const [ isLoading, setIsLoading ] = useState( false );
37+
const [ confirm, setConfirm ] = useState( false );
3638
const isApiOk = 'ok' === state.apiStatus;
3739

3840
const dispatchError = error =>
@@ -44,16 +46,17 @@ const Api = () => {
4446
const removeApiKey = () => {
4547
setIsLoading( true );
4648
setApi( '' );
47-
4849
setApiKey( api )
4950
.then( () => {
5051
setIsLoading( false );
5152
dispatch( { type: ACTIONS.REMOVE_API_KEY } );
53+
setConfirm( false );
5254
} )
5355
.catch( error => {
5456
dispatchError( error.message );
5557
console.error( error );
5658
setIsLoading( false );
59+
setConfirm( false );
5760
} );
5861
};
5962

@@ -86,6 +89,39 @@ const Api = () => {
8689

8790
return (
8891
<div className="material-settings__api mdc-layout-grid">
92+
<Dialog
93+
open={ confirm }
94+
title={ __( 'Remove Google API Key?', 'material-design' ) }
95+
content={ __(
96+
'This action is permanent and cannot be undone',
97+
'material-design'
98+
) }
99+
actions={
100+
<>
101+
<button
102+
type="button"
103+
className="mdc-button mdc-dialog__button"
104+
onClick={ () => setConfirm( false ) }
105+
>
106+
<div className="mdc-button__ripple"></div>
107+
<span className="mdc-button__label">
108+
{ __( 'Cancel', 'material-design' ) }
109+
</span>
110+
</button>
111+
<button
112+
type="button"
113+
className="mdc-button mdc-dialog__button"
114+
onClick={ removeApiKey }
115+
>
116+
<div className="mdc-button__ripple"></div>
117+
<span className="mdc-button__label">
118+
{ __( 'Remove', 'material-design' ) }
119+
</span>
120+
</button>
121+
</>
122+
}
123+
/>
124+
89125
<div className="mdc-layout-grid__inner">
90126
{ ! isApiOk && (
91127
<TextControl
@@ -122,7 +158,7 @@ const Api = () => {
122158
<Button
123159
text={ __( 'Remove', 'material-design' ) }
124160
leadingIcon="delete"
125-
onClick={ removeApiKey }
161+
onClick={ () => setConfirm( true ) }
126162
loading={ isLoading }
127163
/>
128164
) }

0 commit comments

Comments
 (0)