-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAlert.jsx
137 lines (122 loc) · 2.77 KB
/
Alert.jsx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { useState } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import Button from '../Button';
import Icon from '../Icon';
import {
components,
spacing,
colors,
baseFontSize as defaultBaseFontSize,
breakpoints,
} from '../shared/theme';
const Content = styled.div`
align-items: start;
display: flex;
& > span {
flex: 1;
}
`;
const AlertIcon = styled(Icon)`
width: ${defaultBaseFontSize * 1.5}px;
`;
const CloseButton = styled(Button.Icon).attrs({
icon: 'close',
})`
height: auto;
opacity: 0.8;
padding: 0;
transition: opacity 0.4s ease;
width: auto;
&:hover {
background: none;
opacity: 1;
}
`;
CloseButton.displayName = 'CloseButton';
const Wrapper = styled.div`
border-radius: 8px;
box-sizing: border-box;
${({
skin,
theme: {
baseFontSize,
spacing: { small, medium },
components: {
alert: {
skins: {
[skin]: { background, icon, text },
},
},
},
},
}) => `
font-size: ${baseFontSize}px;
background-color: ${background};
border: 1.5px solid ${icon};
color: ${text};
padding: ${small}px ${medium}px;
${Content} ${AlertIcon} {
color: ${icon};
margin-right: ${medium}px;
}
${Content} > ${CloseButton} {
color: ${icon};
margin: 0 0 0 ${medium}px !important;
min-height: 0;
opacity: 1;
}
`}
`;
const Alert = ({
icon = null,
skin = 'neutral',
children,
theme = {
colors,
baseFontSize: defaultBaseFontSize,
spacing,
breakpoints,
components: {
alert: components.alert,
},
},
onClose = undefined,
...rest
}) => {
const [show, setShow] = useState(true);
const handleClose = () => {
setShow(false);
onClose();
};
return (
show && (
<Wrapper theme={theme} skin={skin} {...rest} role="alert">
<Content>
{icon && <AlertIcon name={icon} />}
{children && <span>{children}</span>}
{onClose && <CloseButton theme={theme} onClick={handleClose} />}
</Content>
</Wrapper>
)
);
};
Alert.propTypes = {
/** At least one children is required for Alert component properly works */
children: PropTypes.node.isRequired,
/** Icon name. The full catalogue can be found
* [here](/?path=/docs/foundation-icons--page) */
icon: PropTypes.string,
/** You must pass a callback that is called when close button is clicked */
onClose: PropTypes.func,
skin: PropTypes.oneOf(['primary', 'success', 'error', 'neutral', 'warning']),
theme: PropTypes.shape({
baseFontSize: PropTypes.number,
colors: PropTypes.object,
spacing: PropTypes.object,
components: PropTypes.shape({
alert: PropTypes.object,
}),
}),
};
export default Alert;