-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try using snackbar notices instead of prominent ones for saving/failu…
…re notices (#15594)
- Loading branch information
1 parent
45cd52d
commit 4956082
Showing
29 changed files
with
379 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Snackbar | ||
|
||
Use Snackbars to communicate low priority, non-interruptive messages to the user. | ||
|
||
## Table of contents | ||
|
||
1. [Design guidelines](#design-guidelines) | ||
2. [Development guidelines](#development-guidelines) | ||
3. [Related components](#related-components) | ||
|
||
## Design guidelines | ||
|
||
A Snackbar displays a succinct message that is cleared out after a small delay. It can also offer the user options, like viewing a published post but these options should also be available elsewhere in the UI. | ||
|
||
## Development guidelines | ||
|
||
### Usage | ||
|
||
To display a plain snackbar, pass the message as a `children` prop: | ||
|
||
```jsx | ||
const MySnackbarNotice = () => ( | ||
<Snackbar> | ||
Post published successfully. | ||
</Snackbar> | ||
); | ||
``` | ||
|
||
For more complex markup, you can pass any JSX element: | ||
|
||
```jsx | ||
const MySnackbarNotice = () => ( | ||
<Snackbar> | ||
<p>An error occurred: <code>{ errorDetails }</code>.</p> | ||
</Snackbar> | ||
); | ||
``` | ||
|
||
#### Props | ||
|
||
The following props are used to control the display of the component. | ||
|
||
* `onRemove`: function called when dismissing the notice. | ||
* `actions`: (array) an array of action objects. Each member object should contain a `label` and either a `url` link string or `onClick` callback function. A `className` property can be used to add custom classes to the button styles. | ||
|
||
## Related components | ||
|
||
- To create a prominent message that requires a higher-level of attention, use a Notice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { noop } from 'lodash'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Button } from '../'; | ||
|
||
const NOTICE_TIMEOUT = 10000; | ||
|
||
function Snackbar( { | ||
className, | ||
children, | ||
actions = [], | ||
onRemove = noop, | ||
} ) { | ||
useEffect( () => { | ||
// This rule doesn't account yet for React Hooks | ||
// eslint-disable-next-line @wordpress/react-no-unsafe-timeout | ||
const timeoutHandle = setTimeout( () => { | ||
onRemove(); | ||
}, NOTICE_TIMEOUT ); | ||
|
||
return () => clearTimeout( timeoutHandle ); | ||
}, [] ); | ||
|
||
const classes = classnames( className, 'components-snackbar' ); | ||
|
||
return ( | ||
<div | ||
className={ classes } | ||
onClick={ onRemove } | ||
tabIndex="0" | ||
role="button" | ||
onKeyPress={ onRemove } | ||
label={ __( 'Dismiss this notice' ) } | ||
> | ||
<div className="components-snackbar__content"> | ||
{ children } | ||
{ actions.map( | ||
( | ||
{ | ||
className: buttonCustomClasses, | ||
label, | ||
onClick, | ||
url, | ||
}, | ||
index | ||
) => { | ||
return ( | ||
<Button | ||
key={ index } | ||
href={ url } | ||
isTertiary | ||
onClick={ ( event ) => { | ||
event.stopPropagation(); | ||
if ( onClick ) { | ||
onClick( event ); | ||
} | ||
} } | ||
className={ classnames( | ||
'components-snackbar__action', | ||
buttonCustomClasses | ||
) } | ||
> | ||
{ label } | ||
</Button> | ||
); | ||
} | ||
|
||
) } | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default Snackbar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { omit, noop } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Snackbar from './'; | ||
|
||
/** | ||
* Renders a list of notices. | ||
* | ||
* @param {Object} $0 Props passed to the component. | ||
* @param {Array} $0.notices Array of notices to render. | ||
* @param {Function} $0.onRemove Function called when a notice should be removed / dismissed. | ||
* @param {Object} $0.className Name of the class used by the component. | ||
* @param {Object} $0.children Array of children to be rendered inside the notice list. | ||
* @return {Object} The rendered notices list. | ||
*/ | ||
function SnackbarList( { notices, className, children, onRemove = noop } ) { | ||
className = classnames( 'components-snackbar-list', className ); | ||
const removeNotice = ( id ) => () => onRemove( id ); | ||
|
||
return ( | ||
<div className={ className }> | ||
{ children } | ||
{ notices.map( ( notice ) => ( | ||
<Snackbar | ||
{ ...omit( notice, [ 'content' ] ) } | ||
key={ notice.id } | ||
onRemove={ removeNotice( notice.id ) } | ||
> | ||
{ notice.content } | ||
</Snackbar> | ||
) ) } | ||
</div> | ||
); | ||
} | ||
|
||
export default SnackbarList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
.components-snackbar { | ||
font-family: $default-font; | ||
font-size: $default-font-size; | ||
background-color: $dark-gray-700; | ||
border-radius: $radius-round-rectangle; | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3); | ||
color: $white; | ||
padding: 16px 24px; | ||
width: 100%; | ||
max-width: 600px; | ||
margin: 8px 0 0; | ||
|
||
@include break-small() { | ||
width: fit-content; | ||
} | ||
|
||
&:hover { | ||
background-color: $dark-gray-900; | ||
} | ||
|
||
&:focus { | ||
background-color: $dark-gray-900; | ||
box-shadow: | ||
0 0 0 1px $white, | ||
0 0 0 3px $blue-medium-focus; | ||
} | ||
} | ||
|
||
.components-snackbar__action.components-button { | ||
margin-left: 32px; | ||
color: $white; | ||
height: auto; | ||
flex-shrink: 0; | ||
line-height: $default-line-height; | ||
|
||
&:not(:disabled):not([aria-disabled="true"]):not(.is-default) { | ||
text-decoration: underline; | ||
|
||
&:hover { | ||
color: $white; | ||
text-decoration: none; | ||
} | ||
} | ||
} | ||
|
||
.components-snackbar__content { | ||
display: flex; | ||
align-items: baseline; | ||
justify-content: space-between; | ||
line-height: $default-line-height; | ||
} | ||
|
||
.components-snackbar-list { | ||
position: absolute; | ||
z-index: z-index(".components-snackbar-list"); | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.