2023-04-05 12:43:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Icon, close } from '@wordpress/icons';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
import { getDefaultPoliteness, getStatusIcon } from './utils';
|
|
|
|
import Button from '../button';
|
|
|
|
import { useSpokenMessage } from '../../hooks';
|
|
|
|
|
|
|
|
export interface NoticeBannerProps {
|
|
|
|
children: React.ReactNode;
|
2023-11-02 13:01:42 +00:00
|
|
|
className?: string;
|
|
|
|
isDismissible?: boolean;
|
|
|
|
onRemove?: () => void;
|
|
|
|
politeness?: 'polite' | 'assertive';
|
|
|
|
spokenMessage?: string | React.ReactNode;
|
2023-04-05 12:43:03 +00:00
|
|
|
status: 'success' | 'error' | 'info' | 'warning' | 'default';
|
2023-11-02 13:01:42 +00:00
|
|
|
summary?: string;
|
2023-04-05 12:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-02 13:01:42 +00:00
|
|
|
* NoticeBanner component.
|
2023-04-05 12:43:03 +00:00
|
|
|
*
|
2023-11-02 13:01:42 +00:00
|
|
|
* An informational UI displayed near the top of the store pages.
|
2023-04-05 12:43:03 +00:00
|
|
|
*/
|
|
|
|
const NoticeBanner = ( {
|
|
|
|
className,
|
|
|
|
status = 'default',
|
|
|
|
children,
|
|
|
|
spokenMessage = children,
|
|
|
|
onRemove = () => void 0,
|
|
|
|
isDismissible = true,
|
|
|
|
politeness = getDefaultPoliteness( status ),
|
|
|
|
summary,
|
|
|
|
}: NoticeBannerProps ) => {
|
|
|
|
useSpokenMessage( spokenMessage, politeness );
|
|
|
|
|
|
|
|
const dismiss = ( event: React.SyntheticEvent ) => {
|
|
|
|
if (
|
|
|
|
typeof event?.preventDefault === 'function' &&
|
|
|
|
event.preventDefault
|
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
onRemove();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
className,
|
|
|
|
'wc-block-components-notice-banner',
|
|
|
|
'is-' + status,
|
|
|
|
{
|
|
|
|
'is-dismissible': isDismissible,
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<Icon icon={ getStatusIcon( status ) } />
|
|
|
|
<div className="wc-block-components-notice-banner__content">
|
|
|
|
{ summary && (
|
|
|
|
<p className="wc-block-components-notice-banner__summary">
|
|
|
|
{ summary }
|
|
|
|
</p>
|
|
|
|
) }
|
|
|
|
{ children }
|
|
|
|
</div>
|
|
|
|
{ !! isDismissible && (
|
|
|
|
<Button
|
|
|
|
className="wc-block-components-notice-banner__dismiss"
|
|
|
|
icon={ close }
|
|
|
|
label={ __(
|
|
|
|
'Dismiss this notice',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-04-05 12:43:03 +00:00
|
|
|
) }
|
|
|
|
onClick={ dismiss }
|
|
|
|
showTooltip={ false }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NoticeBanner;
|