|
| 1 | +/** |
| 2 | + * Copyright (c) 2021 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { useState } from "react"; |
| 8 | +import { ReactComponent as Exclamation } from "../images/exclamation.svg"; |
| 9 | +import { ReactComponent as Exclamation2 } from "../images/exclamation2.svg"; |
| 10 | +import { ReactComponent as InfoSvg } from "../images/info.svg"; |
| 11 | +import { ReactComponent as XSvg } from "../images/x.svg"; |
| 12 | + |
| 13 | +export type AlertType = |
| 14 | + // Yellow |
| 15 | + | "warning" |
| 16 | + // Gray alert |
| 17 | + | "info" |
| 18 | + // Red |
| 19 | + | "error" |
| 20 | + // Blue |
| 21 | + | "message"; |
| 22 | + |
| 23 | +export interface AlertProps { |
| 24 | + className?: string; |
| 25 | + type?: AlertType; |
| 26 | + // Without background color, default false |
| 27 | + light?: boolean; |
| 28 | + closable?: boolean; |
| 29 | + showIcon?: boolean; |
| 30 | + icon?: React.ReactNode; |
| 31 | + children?: React.ReactNode; |
| 32 | +} |
| 33 | + |
| 34 | +interface AlertInfo { |
| 35 | + bgCls: string; |
| 36 | + txtCls: string; |
| 37 | + icon: React.ReactNode; |
| 38 | + iconColor?: string; |
| 39 | +} |
| 40 | + |
| 41 | +const infoMap: Record<AlertType, AlertInfo> = { |
| 42 | + warning: { |
| 43 | + bgCls: "bg-yellow-100 dark:bg-yellow-700", |
| 44 | + txtCls: "text-yellow-600 dark:text-yellow-50", |
| 45 | + icon: <Exclamation2 className="w-4 h-4"></Exclamation2>, |
| 46 | + iconColor: "text-yellow-400 dark:text-yellow-900", |
| 47 | + }, |
| 48 | + info: { |
| 49 | + bgCls: "bg-gray-100 dark:bg-gray-700", |
| 50 | + txtCls: "text-gray-500 dark:text-gray-300", |
| 51 | + icon: <InfoSvg className="w-4 h-4"></InfoSvg>, |
| 52 | + iconColor: "text-gray-400", |
| 53 | + }, |
| 54 | + message: { |
| 55 | + bgCls: "bg-blue-50 dark:bg-blue-700", |
| 56 | + txtCls: "text-blue-800 dark:text-blue-100", |
| 57 | + icon: <InfoSvg className="w-4 h-4"></InfoSvg>, |
| 58 | + iconColor: "text-blue-400", |
| 59 | + }, |
| 60 | + error: { |
| 61 | + bgCls: "bg-red-50 dark:bg-red-800 dark:bg-opacity-50", |
| 62 | + txtCls: "text-red-600 dark:text-red-200", |
| 63 | + icon: <Exclamation className="w-4 h-4"></Exclamation>, |
| 64 | + iconColor: "text-red-400", |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +export default function Alert(props: AlertProps) { |
| 69 | + const [visible, setVisible] = useState(true); |
| 70 | + if (!visible) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + const type: AlertType = props.type ?? "info"; |
| 74 | + const info = infoMap[type]; |
| 75 | + const showIcon = props.showIcon ?? true; |
| 76 | + const light = props.light ?? false; |
| 77 | + return ( |
| 78 | + <div className={`flex relative rounded p-4 ${info.txtCls} ${props.className || ""} ${light ? "" : info.bgCls}`}> |
| 79 | + {showIcon && <span className={`mt-1 mr-4 h-4 w-4 ${info.iconColor}`}>{props.icon ?? info.icon}</span>} |
| 80 | + <span className="flex-1 text-left">{props.children}</span> |
| 81 | + {props.closable && ( |
| 82 | + <XSvg onClick={() => setVisible(false)} className="mt-1 ml-4 w-3 h-3 cursor-pointer"></XSvg> |
| 83 | + )} |
| 84 | + </div> |
| 85 | + ); |
| 86 | +} |
0 commit comments