Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/components/MessageBox.tsx
7461 views
1
import * as React from 'react';
2
import tw, { TwStyle } from 'twin.macro';
3
import styled from 'styled-components/macro';
4
5
export type FlashMessageType = 'success' | 'info' | 'warning' | 'error';
6
7
interface Props {
8
title?: string;
9
children: string;
10
type?: FlashMessageType;
11
}
12
13
const styling = (type?: FlashMessageType): TwStyle | string => {
14
switch (type) {
15
case 'error':
16
return tw`bg-red-600 border-red-800`;
17
case 'info':
18
return tw`bg-primary-600 border-primary-800`;
19
case 'success':
20
return tw`bg-green-600 border-green-800`;
21
case 'warning':
22
return tw`bg-yellow-600 border-yellow-800`;
23
default:
24
return '';
25
}
26
};
27
28
const getBackground = (type?: FlashMessageType): TwStyle | string => {
29
switch (type) {
30
case 'error':
31
return tw`bg-red-500`;
32
case 'info':
33
return tw`bg-primary-500`;
34
case 'success':
35
return tw`bg-green-500`;
36
case 'warning':
37
return tw`bg-yellow-500`;
38
default:
39
return '';
40
}
41
};
42
43
const Container = styled.div<{ $type?: FlashMessageType }>`
44
${tw`p-2 border items-center leading-normal rounded flex w-full text-sm text-white`};
45
${(props) => styling(props.$type)};
46
`;
47
Container.displayName = 'MessageBox.Container';
48
49
const MessageBox = ({ title, children, type }: Props) => (
50
<Container css={tw`lg:inline-flex`} $type={type} role={'alert'}>
51
{title && (
52
<span
53
className={'title'}
54
css={[
55
tw`flex rounded-full uppercase px-2 py-1 text-xs font-bold mr-3 leading-none`,
56
getBackground(type),
57
]}
58
>
59
{title}
60
</span>
61
)}
62
<span css={tw`mr-2 text-left flex-auto`}>{children}</span>
63
</Container>
64
);
65
MessageBox.displayName = 'MessageBox';
66
67
export default MessageBox;
68
69