Path: blob/1.0-develop/resources/scripts/components/MessageBox.tsx
7461 views
import * as React from 'react';1import tw, { TwStyle } from 'twin.macro';2import styled from 'styled-components/macro';34export type FlashMessageType = 'success' | 'info' | 'warning' | 'error';56interface Props {7title?: string;8children: string;9type?: FlashMessageType;10}1112const styling = (type?: FlashMessageType): TwStyle | string => {13switch (type) {14case 'error':15return tw`bg-red-600 border-red-800`;16case 'info':17return tw`bg-primary-600 border-primary-800`;18case 'success':19return tw`bg-green-600 border-green-800`;20case 'warning':21return tw`bg-yellow-600 border-yellow-800`;22default:23return '';24}25};2627const getBackground = (type?: FlashMessageType): TwStyle | string => {28switch (type) {29case 'error':30return tw`bg-red-500`;31case 'info':32return tw`bg-primary-500`;33case 'success':34return tw`bg-green-500`;35case 'warning':36return tw`bg-yellow-500`;37default:38return '';39}40};4142const Container = styled.div<{ $type?: FlashMessageType }>`43${tw`p-2 border items-center leading-normal rounded flex w-full text-sm text-white`};44${(props) => styling(props.$type)};45`;46Container.displayName = 'MessageBox.Container';4748const MessageBox = ({ title, children, type }: Props) => (49<Container css={tw`lg:inline-flex`} $type={type} role={'alert'}>50{title && (51<span52className={'title'}53css={[54tw`flex rounded-full uppercase px-2 py-1 text-xs font-bold mr-3 leading-none`,55getBackground(type),56]}57>58{title}59</span>60)}61<span css={tw`mr-2 text-left flex-auto`}>{children}</span>62</Container>63);64MessageBox.displayName = 'MessageBox';6566export default MessageBox;676869