-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathSnackbar.js
More file actions
75 lines (71 loc) · 1.96 KB
/
Snackbar.js
File metadata and controls
75 lines (71 loc) · 1.96 KB
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
import PropTypes from 'prop-types';
import React from 'react';
import cx from 'classnames';
import { FormattedMessage } from 'react-intl';
import Icon from './Icon';
import { useTranslationsContext } from '../util/useTranslationsContext';
/**
* A generic snackbar notification.
* The caller is responsible for managing the `show` state transitions and
* for providing a `liveRegionMessage` for screen-reader announcements.
*/
const Snackbar = ({
show,
messageId,
defaultMessage,
liveRegionMessage,
onClose,
iconImg,
className,
}) => {
const intl = useTranslationsContext();
return (
<>
<div
className={cx('snackbar', className, {
hide: show === null,
show: show === true,
'slide-out': show === false,
})}
aria-hidden="true"
>
<Icon img={iconImg} omitViewBox />
<span className="snackbar-text">
<FormattedMessage id={messageId} defaultMessage={defaultMessage} />
</span>
<button
type="button"
className="close-button"
aria-label={intl.formatMessage({
id: 'close',
defaultMessage: 'Close notification',
})}
onClick={onClose}
tabIndex="-1"
>
<Icon id="close-icon" img="notification-close" omitViewBox />
</button>
</div>
<div className="sr-only" aria-live="polite" role="status">
{liveRegionMessage}
</div>
</>
);
};
Snackbar.propTypes = {
/** null = hidden, true = slide in, false = slide out */
show: PropTypes.oneOf([null, true, false]),
messageId: PropTypes.string.isRequired,
defaultMessage: PropTypes.string.isRequired,
liveRegionMessage: PropTypes.string,
onClose: PropTypes.func.isRequired,
iconImg: PropTypes.string,
className: PropTypes.string,
};
Snackbar.defaultProps = {
show: null,
liveRegionMessage: '',
iconImg: 'icon_checkmark-circled',
className: undefined,
};
export default Snackbar;