Skip to content

Commit cd22fc4

Browse files
authored
Use wp snackbar on the settings page (#136)
1 parent 8df17d4 commit cd22fc4

File tree

13 files changed

+72
-80
lines changed

13 files changed

+72
-80
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ npm run build
164164
* License: MIT License
165165
* Source: <https://github.com/microsoft/monaco-editor>
166166

167-
### react-notifications-component
168-
169-
* License: MIT License
170-
* Source: <https://github.com/teodosii/react-notifications-component>
171-
172167
### emmet-monaco-es
173168

174169
* License: MIT License

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"@wordpress/icons": "^10.20.0",
2929
"emmet-monaco-es": "^5.5.0",
3030
"monaco-editor": "^0.52.2",
31-
"react-notifications-component": "^4.0.1",
3231
"webfontloader": "^1.6.28"
3332
},
3433
"devDependencies": {

readme.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ And supports the classic editor, the theme/plugin editor, import/export editor s
3535
License: MIT License
3636
Source: https://github.com/microsoft/monaco-editor
3737

38-
= react-notifications-component =
39-
License: MIT License
40-
Source: https://github.com/teodosii/react-notifications-component
41-
4238
= emmet-monaco-es =
4339
License: MIT License
4440
Source: https://github.com/troy351/emmet-monaco-es
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { SnackbarList } from '@wordpress/components';
5+
import { useSelect, useDispatch } from '@wordpress/data';
6+
import { store as noticesStore } from '@wordpress/notices';
7+
8+
// Last three notices. Slices from the tail end of the list.
9+
const MAX_VISIBLE_NOTICES = -3;
10+
11+
export default function Snackbars() {
12+
const notices = useSelect( ( select ) => select( noticesStore ).getNotices(), [] );
13+
const { removeNotice } = useDispatch( noticesStore );
14+
const filteredNotices = notices.slice( MAX_VISIBLE_NOTICES );
15+
16+
return (
17+
<SnackbarList
18+
notices={ filteredNotices }
19+
className="chbe-admin-snackbars"
20+
onRemove={ removeNotice }
21+
/>
22+
);
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@use "../../../wp-base-styles.scss" as wpBaseStyles;
2+
3+
.chbe-admin-snackbars {
4+
right: wpBaseStyles.$grid-unit-20;
5+
bottom: wpBaseStyles.$grid-unit-20;
6+
position: fixed;
7+
8+
.components-snackbar-list__notice-container {
9+
display: flex;
10+
justify-content: flex-end;
11+
}
12+
}

src/admin/editor-config/components/editor-preview/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
*/
44
import { __, sprintf } from '@wordpress/i18n';
55
import { useContext } from '@wordpress/element';
6+
import { useDispatch } from '@wordpress/data';
7+
import { store as noticesStore } from '@wordpress/notices';
68

79
/**
810
* Internal dependencies
911
*/
1012
import { AdminContext } from '../../../index';
11-
import { addNotification } from '../../../../lib/helper';
1213
import MonacoEditor from '../../../../components/monaco-editor';
1314

1415
export default function EditorPreview( { isEditorDisabled, setFontWeights } ) {
1516
const { code, setCode, editorSettings, editorOptions, setEditorOptions } =
1617
useContext( AdminContext );
18+
const { createErrorNotice } = useDispatch( noticesStore );
1719

1820
const onFontLoad = ( { isSuccess, font } ) => {
1921
if ( ! isSuccess ) {
20-
addNotification(
22+
createErrorNotice(
2123
sprintf(
2224
/* translators: %s is replaced with the font family name. */
2325
__( 'Failed to load the font. (%s)', 'custom-html-block-extension' ),
2426
font.label
25-
),
26-
'danger',
27-
5000
27+
)
2828
);
2929
setFontWeights( [ 300, 400, 500, 600, 700 ] );
3030
setEditorOptions( {

src/admin/editor-config/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import {
1111
__experimentalHStack as HStack,
1212
__experimentalVStack as VStack,
1313
} from '@wordpress/components';
14+
import { useDispatch } from '@wordpress/data';
15+
import { store as noticesStore } from '@wordpress/notices';
1416

1517
/**
1618
* Internal dependencies
1719
*/
1820
import { AdminContext } from '../index';
19-
import { addNotification } from '../../lib/helper';
2021
import EditorPreview from './components/editor-preview';
2122
import Filter from './components/filter';
2223
import Controls from './components/controls';
@@ -42,6 +43,7 @@ export default function EditorConfig() {
4243
const [ editorMode, setEditorMode ] = useState( 'basic' );
4344
const [ searchQuery, setSearchQuery ] = useState( '' );
4445
const [ fontWeights, setFontWeights ] = useState( [ 300 ] );
46+
const { createNotice, createSuccessNotice } = useDispatch( noticesStore );
4547

4648
// Update editor config.
4749
const onUpdateOptions = () => {
@@ -56,7 +58,9 @@ export default function EditorConfig() {
5658
},
5759
} ).then( ( response ) => {
5860
setTimeout( () => {
59-
addNotification( response.message, response.success ? 'success' : 'danger' );
61+
createNotice( response.success ? 'success' : 'error', response.message, {
62+
type: 'snackbar',
63+
} );
6064
setIsWaiting( false );
6165
}, 600 );
6266
} );
@@ -75,10 +79,9 @@ export default function EditorConfig() {
7579
setEditorOptions( response.editorOptions );
7680

7781
setTimeout( () => {
78-
addNotification(
79-
__( 'Settings have been reset.', 'custom-html-block-extension' ),
80-
'success'
81-
);
82+
createSuccessNotice( __( 'Settings have been reset.', 'custom-html-block-extension' ), {
83+
type: 'snackbar',
84+
} );
8285
setIsWaiting( false );
8386
}, 600 );
8487
} );

src/admin/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* External dependencies
33
*/
4-
import { ReactNotifications } from 'react-notifications-component';
54
import clsx from 'clsx';
65

76
/**
@@ -16,6 +15,7 @@ import domReady from '@wordpress/dom-ready';
1615
* Internal dependencies
1716
*/
1817
import './style.scss';
18+
import Snackbars from './components/snackbars';
1919
import Header from './components/header';
2020
import EditorConfig from './editor-config';
2121
import Tools from './tools';
@@ -66,7 +66,7 @@ function Admin() {
6666

6767
return (
6868
<div className={ clsx( 'chbe-admin', { 'is-waiting': isWaiting } ) }>
69-
<ReactNotifications />
69+
<Snackbars />
7070
{ isWaiting && (
7171
<div className="chbe-admin__loading">
7272
<Spinner />

src/admin/options/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { __ } from '@wordpress/i18n';
55
import apiFetch from '@wordpress/api-fetch';
66
import { createContext, useContext } from '@wordpress/element';
77
import { Button, Flex, __experimentalVStack as VStack } from '@wordpress/components';
8+
import { useDispatch } from '@wordpress/data';
9+
import { store as noticesStore } from '@wordpress/notices';
810

911
/**
1012
* Internal dependencies
1113
*/
1214
import { AdminContext } from '../index';
13-
import { addNotification } from '../../lib/helper';
1415
import PermissionEditor from './components/permission-editor';
1516
import PermissionUserRole from './components/permission-user-role';
1617

@@ -21,6 +22,7 @@ export const OptionsContext = createContext();
2122

2223
export default function Options() {
2324
const { isWaiting, options, setIsWaiting } = useContext( AdminContext );
25+
const { createNotice } = useDispatch( noticesStore );
2426

2527
// Update editor config.
2628
const onUpdateOptions = () => {
@@ -32,7 +34,9 @@ export default function Options() {
3234
data: { options },
3335
} ).then( ( response ) => {
3436
setTimeout( () => {
35-
addNotification( response.message, response.success ? 'success' : 'danger' );
37+
createNotice( response.success ? 'success' : 'error', response.message, {
38+
type: 'snackbar',
39+
} );
3640
setIsWaiting( false );
3741
}, 600 );
3842
} );

0 commit comments

Comments
 (0)