import cx from 'clsx'; import React, { cloneElement, isValidElement } from 'react'; import { useToast } from '../hooks/useToast'; import { ToastProps } from '../types'; import { Default, isFn, renderContent } from '../utils'; import { CloseButton } from './CloseButton'; import { ProgressBar } from './ProgressBar'; import { getIcon } from './Icons'; import { NoTransition } from './Transitions'; export const Toast: React.FC = props => { const { isRunning, preventExitTransition, toastRef, eventHandlers, playToast } = useToast(props); const { closeButton, children, autoClose, onClick, type, hideProgressBar, closeToast, transition, position, className, style, progressClassName, updateId, role, progress, rtl, toastId, deleteToast, isIn, isLoading, closeOnClick, theme, ariaLabel } = props; const defaultClassName = cx( `${Default.CSS_NAMESPACE}__toast`, `${Default.CSS_NAMESPACE}__toast-theme--${theme}`, `${Default.CSS_NAMESPACE}__toast--${type}`, { [`${Default.CSS_NAMESPACE}__toast--rtl`]: rtl }, { [`${Default.CSS_NAMESPACE}__toast--close-on-click`]: closeOnClick } ); const cssClasses = isFn(className) ? className({ rtl, position, type, defaultClassName }) : cx(defaultClassName, className); const icon = getIcon(props); const Transition = transition || NoTransition; const isProgressControlled = !!progress || !autoClose; const closeButtonProps = { closeToast, type, theme }; let Close: React.ReactNode = null; if (closeButton === false) { // hide } else if (isFn(closeButton)) { Close = closeButton(closeButtonProps); } else if (isValidElement(closeButton)) { Close = cloneElement(closeButton, closeButtonProps); } else { Close = CloseButton(closeButtonProps); } return (
{icon != null && (
{icon}
)} {renderContent(children, props, !isRunning)} {Close} {!props.customProgressBar && ( )}
); };