woocommerce/plugins/woocommerce-blocks/assets/js/base/components/notice-banner/index.tsx

100 lines
2.9 KiB
TypeScript
Raw Normal View History

Feature Branch: Updated Shopper Notices (https://github.com/woocommerce/woocommerce-blocks/pull/8659) * Notice banner component * Snackbar support * Switch to new components * Finish snackbar implementation * Summary notice * Styling issues * Fix text wrap in shipping calculator * Storybook entries * Docs and tests for NoticeBanner * Framer motion to avoid components dependency * Snackbar list stories * Docs for snackbar list * Update assets/js/base/components/notice-banner/README.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/base/components/notice-banner/README.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/base/components/notice-banner/README.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/base/components/notice-banner/README.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update assets/js/base/components/notice-banner/README.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Types/docblocks * Docs * Update notice type * Use NoticeBannerProps for type of noticeProps * Raw html to fix notice encoding * getClassNameFromStatus is unused * Update position text * Clarify notice text * Fix hover style in whisper TT3 theme * remove div styles * Add new templates for legacy buyer notices in WooCommerce core (https://github.com/woocommerce/woocommerce-blocks/pull/8732) * Add templates for legacy core notices * Update src/Domain/Services/Notices.php Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com> * Remove debugging code * DRY get_notices_template * Simplify error template * Fix padding * Only include new notices if using block cart/checkout --------- Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com> * Fix view box tag * Hover and focus styles * Styling when notices added via ajax * Remove margin change * Implement react-transition-group instead of framer (https://github.com/woocommerce/woocommerce-blocks/pull/8920) * Add screenshots to docs --------- Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
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 {
// The displayed message of a notice. Also used as the spoken message for assistive technology, unless `spokenMessage` is provided as an alternative message.
children: React.ReactNode;
// Additional class name to give to the notice.
className?: string | undefined;
// Determines whether the notice can be dismissed by the user.
isDismissible?: boolean | undefined;
// Function called when dismissing the notice.
onRemove?: ( () => void ) | undefined;
// Determines the level of politeness for the notice for assistive technology.
politeness?: 'polite' | 'assertive' | undefined;
// Optionally provided to change the spoken message for assistive technology.
spokenMessage?: string | React.ReactNode | undefined;
// Status determines the color of the notice and the icon.
status: 'success' | 'error' | 'info' | 'warning' | 'default';
// Optional summary text shown above notice content, used when several notices are listed together.
summary?: string | undefined;
}
/**
* NoticeBanner: An informational UI displayed near the top of the store pages.
*
* Notices are informational UI displayed near the top of store pages. WooCommerce blocks, themes, and plugins all use
* notices to indicate the result of an action, or to draw the users attention to necessary information.
*/
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',
'woo-gutenberg-products-block'
) }
onClick={ dismiss }
showTooltip={ false }
/>
) }
</div>
);
};
export default NoticeBanner;