| 1 |
import { __ } from '@wordpress/i18n' |
| 2 |
import classnames from 'classnames' |
| 3 |
import React from 'react' |
| 4 |
import type { HTMLAttributes, ReactNode } from 'react' |
| 5 |
|
| 6 |
export type NoticeType = 'info' | 'warning' | 'error' | 'success' |
| 7 |
|
| 8 |
export interface NoticeProps extends Omit<HTMLAttributes<HTMLDivElement>, 'className'> { |
| 9 |
className?: classnames.Argument |
| 10 |
children?: ReactNode |
| 11 |
type?: NoticeType |
| 12 |
} |
| 13 |
|
| 14 |
const isAssertive = (type?: NoticeType): boolean => 'error' === type || 'warning' === type |
| 15 |
|
| 16 |
export const Notice: React.FC<NoticeProps> = ({ className, type, children, ...props }) => |
| 17 |
<div |
| 18 |
className={classnames('notice', 'code-snippets-notice', { [`notice-${type}`]: type }, className)} |
| 19 |
role={isAssertive(type) ? 'alert' : 'status'} |
| 20 |
aria-live={isAssertive(type) ? 'assertive' : 'polite'} |
| 21 |
{...props} |
| 22 |
> |
| 23 |
{children} |
| 24 |
</div> |
| 25 |
|
| 26 |
export interface DismissibleNoticeProps extends NoticeProps { |
| 27 |
onDismiss: VoidFunction |
| 28 |
} |
| 29 |
|
| 30 |
export const DismissibleNotice: React.FC<DismissibleNoticeProps> = ({ className, onDismiss, children, ...noticeProps }) => |
| 31 |
<Notice className={classnames('is-dismissible', className)} {...noticeProps}> |
| 32 |
{children} |
| 33 |
|
| 34 |
<button type="button" className="notice-dismiss" onClick={event => { |
| 35 |
event.preventDefault() |
| 36 |
onDismiss() |
| 37 |
}}> |
| 38 |
<span className="screen-reader-text">{__('Dismiss notice.', 'code-snippets')}</span> |
| 39 |
</button> |
| 40 |
</Notice> |
| 41 |
|