|
| 1 | +import clsx from 'clsx'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import type { Status } from '../../util/variant-types'; |
| 5 | +import { ButtonV2 as Button } from '../Button'; |
| 6 | +import Icon, { type IconName } from '../Icon'; |
| 7 | +import Text from '../Text'; |
| 8 | + |
| 9 | +import styles from './Toast-v2.module.css'; |
| 10 | + |
| 11 | +/** |
| 12 | + * TODO-AH: |
| 13 | + * - which status is default? |
| 14 | + * - default / min / max widths? |
| 15 | + */ |
| 16 | + |
| 17 | +export type ToastProps = { |
| 18 | + // Component API |
| 19 | + /** |
| 20 | + * Additional class names that can be appended to the component, passed in for styling. |
| 21 | + */ |
| 22 | + className?: string; |
| 23 | + /** |
| 24 | + * Callback when notification is dismissed. When passed in, renders banner with a close icon in the top right. |
| 25 | + */ |
| 26 | + onDismiss?: () => void; |
| 27 | + // Design API |
| 28 | + /** |
| 29 | + * Keyword to characterize the state of the notification |
| 30 | + */ |
| 31 | + status?: Extract<Status, 'favorable' | 'critical'>; |
| 32 | + /** |
| 33 | + * The title/heading of the notification |
| 34 | + */ |
| 35 | + title: string; |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Map statuses to existing icon names |
| 40 | + * TODO-AH: de-dupe this with the function in InlineNotification |
| 41 | + * |
| 42 | + * @param status component status |
| 43 | + * @returns the matching icon name |
| 44 | + */ |
| 45 | +function getIconNameFromStatus(status: Status): IconName { |
| 46 | + const map: Record<Status, IconName> = { |
| 47 | + informational: 'info', |
| 48 | + critical: 'dangerous', |
| 49 | + warning: 'warning', |
| 50 | + favorable: 'check-circle', |
| 51 | + }; |
| 52 | + return map[status]; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * `import {Toast} from "@chanzuckerberg/eds";` |
| 57 | + * |
| 58 | + * Toasts display brief, temporary notifications. They're meant to be noticed without disrupting a user's experience or requiring an action to be taken. |
| 59 | + */ |
| 60 | +export const Toast = ({ |
| 61 | + className, |
| 62 | + onDismiss, |
| 63 | + status = 'favorable', |
| 64 | + title, |
| 65 | + ...other |
| 66 | +}: ToastProps) => { |
| 67 | + const componentClassName = clsx( |
| 68 | + styles['toast'], |
| 69 | + status && styles[`toast--status-${status}`], |
| 70 | + className, |
| 71 | + ); |
| 72 | + return ( |
| 73 | + <div className={componentClassName} {...other}> |
| 74 | + <Icon |
| 75 | + className={styles['toast__icon']} |
| 76 | + name={getIconNameFromStatus(status)} |
| 77 | + purpose="decorative" |
| 78 | + size="1.875rem" |
| 79 | + /> |
| 80 | + <div className={styles['toast__body']}> |
| 81 | + <Text as="span" className={styles['toast__text']} preset="title-md"> |
| 82 | + {title} |
| 83 | + </Text> |
| 84 | + </div> |
| 85 | + {onDismiss && ( |
| 86 | + <Button |
| 87 | + aria-label="close" |
| 88 | + context="default" |
| 89 | + icon="close" |
| 90 | + iconLayout="icon-only" |
| 91 | + onClick={onDismiss} |
| 92 | + rank="tertiary" |
| 93 | + > |
| 94 | + Close |
| 95 | + </Button> |
| 96 | + )} |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
0 commit comments