diff --git a/src/components/Toast/Toast.stories.tsx b/src/components/Toast/Toast.stories.tsx index 09882008..934612dd 100644 --- a/src/components/Toast/Toast.stories.tsx +++ b/src/components/Toast/Toast.stories.tsx @@ -1,4 +1,4 @@ -import { Toast, toast } from './Toast'; +import { Toast, ToastContent, toast } from './Toast'; import { Button } from './../Button/Button'; export default { title: 'Components/Toast' }; @@ -14,11 +14,32 @@ export const _Toast = { Create ERROR Toast
-
- +
+ +
+ + + ), }; diff --git a/src/components/Toast/Toast.test.tsx b/src/components/Toast/Toast.test.tsx index 4a00f6e3..551373f1 100644 --- a/src/components/Toast/Toast.test.tsx +++ b/src/components/Toast/Toast.test.tsx @@ -4,13 +4,7 @@ import { Toast } from './Toast'; describe('Components | Toast', () => { test('it should render', () => { - render( - , - ); + render(); let input = screen.getByTestId('toast'); diff --git a/src/components/Toast/Toast.tsx b/src/components/Toast/Toast.tsx index 2a6aa18e..675fcdda 100644 --- a/src/components/Toast/Toast.tsx +++ b/src/components/Toast/Toast.tsx @@ -2,107 +2,114 @@ // @ts-ignore import React from 'react'; -import { useEffect, useState } from 'react'; -import { toast as _toast, Toaster } from 'react-hot-toast'; +import { toast as _toast, Toaster, Toast as ToastType } from 'react-hot-toast'; import { ComponentPropsWithoutRef } from 'react'; -import { FiX, FiCheckCircle, FiAlertCircle } from 'react-icons/fi'; +import { FiX, FiCheckCircle, FiAlertCircle, FiInfo } from 'react-icons/fi'; import { Button } from '../Button'; -import { useLocalStorage } from '../../util/useLocalStorage'; import { Transition } from '@headlessui/react'; +import { Spinner } from '../Spinner'; -export interface ToastProps extends ComponentPropsWithoutRef<'div'> { - storageKey: string; - error?: string; - success?: string; - theme?: string; +export interface ToastProps { durationMs?: number; } const defaultDurationMs = 10000; -function Error(props: ToastProps) { - const { error } = props; +export const toast = _toast; + +export function Toast(props: ToastProps) { + const { durationMs } = props; return ( - <> -
+ - -
-
{error}
- + {(t) => } + + ); } -function Success(props: ToastProps) { - const { success } = props; - - return ( - <> -
- -
-
- {success} -
- - ); +interface ToastContentProps extends ComponentPropsWithoutRef<'div'> { + t: ToastType; } -export const toast = _toast; +export function ToastContent(props: ToastContentProps) { + const { t, ...rest } = props; -export function Toast(props: ToastProps) { - const { storageKey, error, theme: _theme, ...rest } = props; + const toastIcons = { + success: , + error: , + loading: , + blank: , + custom: t.icon ?? , + }; + + const toastTypeToThemeState = { + success: 's-success', + error: 's-error', + loading: 'f-secondary', + blank: 'f-secondary', + custom: 'f-secondary', + }; - const [storedTheme] = useLocalStorage(storageKey, 'theme-dark'); - const [theme, setTheme] = useState(storedTheme); - const isError = Boolean(error); + const toastTypeToBGThemeState = { + success: 's-success', + error: 's-error', + loading: 'info', + blank: 'info', + custom: 'info', + }; - useEffect(() => { - setTheme(_theme ?? storedTheme); - }, [_theme]); + const state = toastTypeToThemeState[t.type]; + + let content; + if (typeof t.message === 'string') { + content = t.message; + } else { + if (!t.message) return; + content = (t.message as CallableFunction)().props.children; + } return ( -
- +
- {(t) => ( - -
- {t.type === 'error' || isError ? ( - - ) : ( - - )} - -
-
- )} - -
+
+ {toastIcons[t.type]} +
+
+ {content} +
+ +
+ ); }