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

Adding snackbar notices support to the new widgets page #16020

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion packages/edit-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/components": "file:../components",
"@wordpress/element": "file:../element",
"@wordpress/i18n": "file:../i18n"
"@wordpress/i18n": "file:../i18n",
"@wordpress/notices": "file:../notices"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-widgets/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { navigateRegions } from '@wordpress/components';
import Header from '../header';
import Sidebar from '../sidebar';
import WidgetAreas from '../widget-areas';
import Notices from '../notices';

function Layout( { blockEditorSettings } ) {
return (
<>
<Header />
<Sidebar />
<Notices />
<div
className="edit-widgets-layout__content"
role="region"
Expand Down
32 changes: 32 additions & 0 deletions packages/edit-widgets/src/components/notices/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* External dependencies
*/
import { filter } from 'lodash';

/**
* WordPress dependencies
*/
import { SnackbarList } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';

function Notices() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me wonder if we want to move the EditorNotices component from the editor package to the notices package as we'd have to rewrite such component for each consumer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I guess it would be good to avoid this code repetition and just expose the component as part of the notices package.

const { notices } = useSelect( ( select ) => {
return {
notices: select( 'core/notices' ).getNotices(),
};
} );
const snackbarNotices = filter( notices, {
type: 'snackbar',
} );
const { removeNotice } = useDispatch( 'core/notices' );

return (
<SnackbarList
notices={ snackbarNotices }
className="edit-widgets-notices__snackbar"
onRemove={ removeNotice }
/>
);
}

export default Notices;
8 changes: 8 additions & 0 deletions packages/edit-widgets/src/components/notices/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.edit-widgets-notices__snackbar {
position: fixed;
right: 0;
bottom: 20px;
padding-left: 16px;
padding-right: 16px;
}
@include editor-left(".edit-widgets-notices__snackbar");
1 change: 1 addition & 0 deletions packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import '@wordpress/notices';
import { render } from '@wordpress/element';
import { registerCoreBlocks } from '@wordpress/block-library';

Expand Down
13 changes: 13 additions & 0 deletions packages/edit-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { get, map } from 'lodash';
*/
import { parse, serialize } from '@wordpress/blocks';
import { dispatch, select } from '@wordpress/data-controls';
import { __ } from '@wordpress/i18n';

const WIDGET_AREAS_SAVE_NOTICE_ID = 'WIDGET_AREAS_SAVE_NOTICE_ID';

/**
* Yields an action object that setups the widget areas.
Expand Down Expand Up @@ -73,4 +76,14 @@ export function* saveWidgetAreas() {
}
);
}

yield dispatch(
'core/notices',
'createSuccessNotice',
__( 'Block areas saved succesfully.' ),
{
id: WIDGET_AREAS_SAVE_NOTICE_ID,
type: 'snackbar',
}
);
}
18 changes: 18 additions & 0 deletions packages/edit-widgets/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ describe( 'actions', () => {
},
} );

expect(
saveWidgetAreasGen.next()
).toEqual( {
done: false,
value: {
type: 'DISPATCH',
storeKey: 'core/notices',
actionName: 'createSuccessNotice',
args: [
'Block areas saved succesfully.',
{
id: 'WIDGET_AREAS_SAVE_NOTICE_ID',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick: Maybe the test can import WIDGET_AREAS_SAVE_NOTICE_ID as a constant from the actions file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not exported at the moment. I mean I could export it but for a test, maybe it's not that important.

type: 'snackbar',
},
],
},
} );

expect(
saveWidgetAreasGen.next()
).toEqual( {
Expand Down
1 change: 1 addition & 0 deletions packages/edit-widgets/src/style.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import "./components/header/style.scss";
@import "./components/layout/style.scss";
@import "./components/notices/style.scss";
@import "./components/sidebar/style.scss";
@import "./components/widget-area/style.scss";

Expand Down