import { ReactNode } from 'react'; import styled from '@emotion/styled'; import { css } from '@emotion/react'; import InfoIcon from 'components/Icon/icons/alert/info'; import SuccessIcon from 'components/Icon/icons/alert/success'; import WarningIcon from 'components/Icon/icons/alert/warning'; import TipIcon from 'components/Icon/icons/alert/bulb'; import CloseIcon from 'components/Icon/icons/content/close'; import noop from 'utils/noop'; import { white, treePoppy, pictonBlue, valencia, mountainMeadow, nobel, } from 'styles/colors'; const IconContainer = styled.div` ${({ theme: { rtl } }) => rtl ? css` margin: 0 0 0 0.5em; ` : css` margin: 0 0.5em 0 0; `} `; const ContentAction = styled.div` margin-top: 0.86em; `; const Content = styled.div` display: inline-flex; flex: 1; flex-flow: column; line-height: 1.428em; letter-spacing: 143%; word-break: break-word; & span { display: inline-block; margin: 2px; } & span:not(:last-of-type) { font-weight: bold; } `; const CloseButton = styled.button` appearance: none; padding: 0; border: none; ${({ theme: { rtl } }) => rtl ? css` margin: 0 1em 0 0; ` : css` margin: 0 0 0 1em; `} background-color: transparent; cursor: pointer; outline: 0; `; const Container = styled.div< Pick >` display: flex; align-items: flex-start; padding: 1.1em; color: ${white}; ${({ type }) => { switch (type) { case 'warning': return css` background: ${treePoppy.main}; `; case 'error': return css` background: ${valencia.main}; `; case 'success': return css` background: ${mountainMeadow.main}; `; case 'tip': return css` background: ${nobel['900']}; `; case 'info': default: return css` background: ${pictonBlue.main}; `; } }}; ${({ variant }) => variant === 'toast' && css` width: 21em; `}; ${({ theme: { rtl } }) => rtl && css` direction: rtl; `}; ${({ onDismiss, dismissOnClick }) => onDismiss !== noop && dismissOnClick && css` cursor: pointer; `} `; const icon = { warning: WarningIcon, error: WarningIcon, success: SuccessIcon, info: InfoIcon, tip: TipIcon, }; interface Props { /** Type of Alert component */ type?: 'warning' | 'error' | 'success' | 'info' | 'tip'; /** Variant of Alert component, controlling the size of the Alert */ variant?: 'default' | 'toast'; /** Title of the Alert message */ message?: string; /** Description of the Alert message */ description?: string; /** Callback function, to be executed when clicked close button. No close button if no * function passed */ onDismiss?: () => void; /** Another way to dismiss the Alert message. Replaces close button if true */ dismissOnClick?: boolean; /** @ignore */ children?: ReactNode; /** Rest Props */ [rest: string]: any; } /** * Alert component is used to give feedback to the user action or state changes. */ const Alert = ({ type = 'info', variant = 'default', message = '', description = '', onDismiss = noop, dismissOnClick = false, children = null, ...rest }: Props) => { const Icon = icon[type]; return ( {message && {message}} {description && {description}} {children && {children}} {onDismiss !== noop && !dismissOnClick && ( )} ); }; export default Alert;