File size: 1,832 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Icon, warning, info, check, closeSmall } from '@wordpress/icons';
import clsx from 'clsx';
import React from 'react';
import { alert } from '../icons';
import './style.scss';

type NoticeBannerProps = {
	/** The severity of the alert. */
	level: 'error' | 'warning' | 'info' | 'success';

	/** The title of the NoticeBanner */
	title?: string;

	/** A list of action elements to show across the bottom */
	actions?: React.ReactNode[];

	/** Hide close button */
	hideCloseButton?: boolean;

	/** Method to call when the close button is clicked */
	onClose?: () => void;

	/** Children to be rendered inside the alert. */
	children: React.ReactNode;
};

const getIconByLevel = ( level: NoticeBannerProps[ 'level' ] ) => {
	switch ( level ) {
		case 'error':
			return alert;
		case 'warning':
			return info;
		case 'info':
			return info;
		case 'success':
			return check;
		default:
			return warning;
	}
};

export default function NoticeBanner( {
	level = 'info',
	title,
	children,
	actions,
	hideCloseButton = false,
	onClose,
}: NoticeBannerProps ) {
	return (
		<div className={ clsx( 'notice-banner', `is-${ level }` ) }>
			<div className="notice-banner__icon-wrapper">
				<Icon icon={ getIconByLevel( level ) } className="notice-banner__icon" />
			</div>

			<div className="notice-banner__main-content">
				{ title ? <div className="notice-banner__title">{ title }</div> : null }
				{ children }

				{ actions && actions.length > 0 && (
					<div className="notice-banner__action-bar">
						{ actions.map( ( action, index ) => (
							<div key={ index }>{ action }</div>
						) ) }
					</div>
				) }
			</div>

			{ ! hideCloseButton && (
				<button aria-label="close" className="notice-banner__close-button" onClick={ onClose }>
					<Icon icon={ closeSmall } />
				</button>
			) }
		</div>
	);
}