File size: 2,515 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
78
79
80
81
82
83
84
85
86
87
import {
	__experimentalVStack as VStack,
	__experimentalHStack as HStack,
	Button,
	Card,
	CardBody,
	Icon,
} from '@wordpress/components';
import { info, published, error, closeSmall } from '@wordpress/icons';
import clsx from 'clsx';
import { forwardRef } from 'react';
import { caution } from './icons';
import type { NoticeVariant, NoticeProps } from './types';
import './style.scss';

const icons: { [ key in NoticeVariant ]: any } = {
	info,
	warning: caution,
	success: published,
	error,
};

/**
 * The Notice component is a visually non-obtrusive element that communicates the system status
 * and provides options and actions related to the message tone.
 *
 * This component is designed to sit inline with the page area and capture the user’s attention
 * to inform them about relevant information.
 *
 * ```jsx
 * import { Notice } from '@automattic/components';
 * import { ExternalLink } from '@wordpress/components';
 *
 * function MyComponent() {
 * 	return (
 * 		<Notice
 * 			title="Title"
 * 			actions={ <Button variant="primary">Label</Button> }
 * 		>
 * 			<>Hello, I’m a notice with an inline <ExternalLink href="#">link</ExternalLink></>
 * 		</Notice>
 * 	);
 * }
 * ```
 */
function UnforwardedNotice(
	{ variant = 'info', title, children, actions, density = 'low', onClose }: NoticeProps,
	ref: React.ForwardedRef< HTMLDivElement >
) {
	const hasLowDensity = density === 'low';

	return (
		<Card
			className={ clsx( 'dashboard-notice', `is-${ variant }`, `has-density-${ density }` ) }
			ref={ ref }
		>
			<CardBody className="dashboard-notice__body">
				<HStack spacing={ hasLowDensity ? 2 : 1 } justify="flex-start" alignment="flex-start">
					<Icon className="dashboard-notice__icon" icon={ icons[ variant ] } />
					<VStack className="dashboard-notice__content" spacing={ 3 }>
						<VStack className="dashboard-notice__heading" spacing={ 1 }>
							{ title && <span className="dashboard-notice__title">{ title }</span> }
							<span className="dashboard-notice__description">{ children }</span>
						</VStack>
						{ actions && (
							<HStack className="dashboard-notice__actions" spacing={ 3 } justify="flex-start">
								{ actions }
							</HStack>
						) }
					</VStack>
					{ !! onClose && (
						<Button
							className="dashboard-notice__close-button"
							icon={ closeSmall }
							onClick={ onClose }
						/>
					) }
				</HStack>
			</CardBody>
		</Card>
	);
}

export const Notice = forwardRef( UnforwardedNotice );

export default Notice;