2020-11-09 07:17:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { noop } from 'lodash';
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2020-11-09 07:17:08 +00:00
|
|
|
import { speak } from '@wordpress/a11y';
|
2022-01-25 19:06:38 +00:00
|
|
|
import {
|
|
|
|
RawHTML,
|
|
|
|
useEffect,
|
|
|
|
forwardRef,
|
|
|
|
renderToString,
|
|
|
|
} from '@wordpress/element';
|
2020-11-09 07:17:08 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import warning from '@wordpress/warning';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
|
|
|
|
const NOTICE_TIMEOUT = 10000;
|
|
|
|
|
|
|
|
/** @typedef {import('@wordpress/element').WPElement} WPElement */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom hook which announces the message with the given politeness, if a
|
|
|
|
* valid message is provided.
|
|
|
|
*
|
|
|
|
* @param {string|WPElement} [message] Message to announce.
|
|
|
|
* @param {'polite'|'assertive'} politeness Politeness to announce.
|
|
|
|
*/
|
|
|
|
function useSpokenMessage( message, politeness ) {
|
|
|
|
const spokenMessage =
|
|
|
|
typeof message === 'string' ? message : renderToString( message );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( spokenMessage ) {
|
|
|
|
speak( spokenMessage, politeness );
|
|
|
|
}
|
|
|
|
}, [ spokenMessage, politeness ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
function Snackbar(
|
|
|
|
{
|
|
|
|
className,
|
|
|
|
children,
|
|
|
|
spokenMessage = children,
|
|
|
|
politeness = 'polite',
|
|
|
|
actions = [],
|
|
|
|
onRemove = noop,
|
2020-11-13 00:53:28 +00:00
|
|
|
icon = null,
|
2020-11-18 02:15:42 +00:00
|
|
|
explicitDismiss = false,
|
|
|
|
// onDismiss is a callback executed when the snackbar is dismissed.
|
|
|
|
// It is distinct from onRemove, which _looks_ like a callback but is
|
|
|
|
// actually the function to call to remove the snackbar from the UI.
|
|
|
|
onDismiss = null,
|
2022-01-25 19:06:38 +00:00
|
|
|
__unstableHTML = false,
|
2020-11-09 07:17:08 +00:00
|
|
|
},
|
|
|
|
ref
|
|
|
|
) {
|
2020-11-18 02:15:42 +00:00
|
|
|
onDismiss = onDismiss || noop;
|
|
|
|
|
|
|
|
function dismissMe( event ) {
|
|
|
|
if ( event && event.preventDefault ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
onDismiss();
|
|
|
|
onRemove();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onActionClick( event, onClick ) {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2020-11-20 00:59:49 +00:00
|
|
|
onRemove();
|
2020-11-18 02:15:42 +00:00
|
|
|
|
|
|
|
if ( onClick ) {
|
|
|
|
onClick( event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 07:17:08 +00:00
|
|
|
useSpokenMessage( spokenMessage, politeness );
|
2020-11-18 02:15:42 +00:00
|
|
|
|
|
|
|
// Only set up the timeout dismiss if we're not explicitly dismissing.
|
2020-11-09 07:17:08 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const timeoutHandle = setTimeout( () => {
|
2020-11-18 02:15:42 +00:00
|
|
|
if ( ! explicitDismiss ) {
|
|
|
|
onDismiss();
|
|
|
|
onRemove();
|
|
|
|
}
|
2020-11-09 07:17:08 +00:00
|
|
|
}, NOTICE_TIMEOUT );
|
|
|
|
|
|
|
|
return () => clearTimeout( timeoutHandle );
|
2020-11-19 21:51:47 +00:00
|
|
|
}, [ explicitDismiss, onDismiss, onRemove ] );
|
2020-11-09 07:17:08 +00:00
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const classes = clsx( className, 'components-snackbar', {
|
2020-11-18 02:15:42 +00:00
|
|
|
'components-snackbar-explicit-dismiss': !! explicitDismiss,
|
|
|
|
} );
|
2020-11-09 07:17:08 +00:00
|
|
|
if ( actions && actions.length > 1 ) {
|
|
|
|
// we need to inform developers that snackbar only accepts 1 action
|
|
|
|
warning(
|
|
|
|
'Snackbar can only have 1 action, use Notice if your message require many messages'
|
|
|
|
);
|
|
|
|
// return first element only while keeping it inside an array
|
|
|
|
actions = [ actions[ 0 ] ];
|
|
|
|
}
|
|
|
|
|
2024-05-31 03:49:36 +00:00
|
|
|
const snackbarContentClassnames = clsx( 'components-snackbar__content', {
|
|
|
|
'components-snackbar__content-with-icon': !! icon,
|
|
|
|
} );
|
2020-11-13 00:53:28 +00:00
|
|
|
|
2022-01-25 19:06:38 +00:00
|
|
|
if ( __unstableHTML === true ) {
|
|
|
|
children = <RawHTML>{ children }</RawHTML>;
|
|
|
|
}
|
|
|
|
|
2020-11-09 07:17:08 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
ref={ ref }
|
|
|
|
className={ classes }
|
2020-11-18 02:15:42 +00:00
|
|
|
onClick={ ! explicitDismiss ? dismissMe : noop }
|
2020-11-09 07:17:08 +00:00
|
|
|
tabIndex="0"
|
2020-11-18 02:15:42 +00:00
|
|
|
role={ ! explicitDismiss ? 'button' : '' }
|
|
|
|
onKeyPress={ ! explicitDismiss ? dismissMe : noop }
|
2022-07-08 10:37:30 +00:00
|
|
|
aria-label={
|
|
|
|
! explicitDismiss
|
|
|
|
? __( 'Dismiss this notice', 'woocommerce' )
|
|
|
|
: ''
|
|
|
|
}
|
2020-11-09 07:17:08 +00:00
|
|
|
>
|
2020-11-13 00:53:28 +00:00
|
|
|
<div className={ snackbarContentClassnames }>
|
|
|
|
{ icon && (
|
|
|
|
<div className="components-snackbar__icon">{ icon }</div>
|
|
|
|
) }
|
2020-11-09 07:17:08 +00:00
|
|
|
{ children }
|
|
|
|
{ actions.map( ( { label, onClick, url }, index ) => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={ index }
|
|
|
|
href={ url }
|
|
|
|
isTertiary
|
2020-11-18 02:15:42 +00:00
|
|
|
onClick={ ( event ) =>
|
|
|
|
onActionClick( event, onClick )
|
|
|
|
}
|
2020-11-09 07:17:08 +00:00
|
|
|
className="components-snackbar__action"
|
|
|
|
>
|
|
|
|
{ label }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
} ) }
|
2020-11-18 02:15:42 +00:00
|
|
|
{ explicitDismiss && (
|
|
|
|
<span
|
|
|
|
role="button"
|
|
|
|
aria-label="Dismiss this notice"
|
|
|
|
tabIndex="0"
|
|
|
|
className="components-snackbar__dismiss-button"
|
|
|
|
onClick={ dismissMe }
|
|
|
|
onKeyPress={ dismissMe }
|
|
|
|
>
|
|
|
|
✕
|
|
|
|
</span>
|
|
|
|
) }
|
2020-11-09 07:17:08 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default forwardRef( Snackbar );
|