diff --git a/src/components/Toast/Toast.tsx b/src/components/Toast/Toast.tsx index 53d5e51158..4a3c93d34b 100644 --- a/src/components/Toast/Toast.tsx +++ b/src/components/Toast/Toast.tsx @@ -16,19 +16,6 @@ import {dismiss} from '#/components/Toast/sonner' import {type ToastType} from '#/components/Toast/types' import {Text as BaseText} from '#/components/Typography' -type ToastConfigContextType = { - id: string -} - -type ToastThemeContextType = { - type: ToastType -} - -export type ToastComponentProps = { - type?: ToastType - content: string -} - export const ICONS = { default: CircleCheck, success: CircleCheck, @@ -37,81 +24,67 @@ export const ICONS = { info: CircleInfo, } -const ToastConfigContext = createContext({ +const ToastConfigContext = createContext<{ + id: string + type: ToastType +}>({ id: '', + type: 'default', }) ToastConfigContext.displayName = 'ToastConfigContext' export function ToastConfigProvider({ children, id, + type, }: { children: React.ReactNode id: string + type: ToastType }) { return ( - ({id}), [id])}> + ({id, type}), [id, type])}> {children} ) } -const ToastThemeContext = createContext({ - type: 'default', -}) -ToastThemeContext.displayName = 'ToastThemeContext' - -export function Default({type = 'default', content}: ToastComponentProps) { - return ( - - - {content} - - ) -} - -export function Outer({ - children, - type = 'default', -}: { - children: React.ReactNode - type?: ToastType -}) { +export function Outer({children}: {children: React.ReactNode}) { const t = useTheme() + const {type} = useContext(ToastConfigContext) const styles = useToastStyles({type}) return ( - ({type}), [type])}> - - {children} - - + + {children} + ) } export function Icon({icon}: {icon?: React.ComponentType}) { - const {type} = useContext(ToastThemeContext) + const {type} = useContext(ToastConfigContext) const styles = useToastStyles({type}) const IconComponent = icon || ICONS[type] return } export function Text({children}: {children: React.ReactNode}) { - const {type} = useContext(ToastThemeContext) + const {type} = useContext(ToastConfigContext) const {textColor} = useToastStyles({type}) const {fontScaleCompensation} = useToastFontScaleCompensation() return ( @@ -147,7 +120,7 @@ export function Action( ) { const t = useTheme() const {fontScaleCompensation} = useToastFontScaleCompensation() - const {type} = useContext(ToastThemeContext) + const {type} = useContext(ToastConfigContext) const {id} = useContext(ToastConfigContext) const styles = useMemo(() => { const base = { diff --git a/src/components/Toast/index.tsx b/src/components/Toast/index.tsx index 0d1c661d2d..d70a8ad164 100644 --- a/src/components/Toast/index.tsx +++ b/src/components/Toast/index.tsx @@ -6,15 +6,15 @@ import {toast as sonner, Toaster} from 'sonner-native' import {atoms as a} from '#/alf' import {DURATION} from '#/components/Toast/const' import { - Default as DefaultToast, + Icon as ToastIcon, Outer as BaseOuter, - type ToastComponentProps, + Text as ToastText, ToastConfigProvider, } from '#/components/Toast/Toast' import {type BaseToastOptions} from '#/components/Toast/types' export {DURATION} from '#/components/Toast/const' -export {Action, Icon, Text} from '#/components/Toast/Toast' +export {Action, Icon, Text, ToastConfigProvider} from '#/components/Toast/Toast' export {type ToastType} from '#/components/Toast/types' /** @@ -25,27 +25,10 @@ export function ToastOutlet() { return } -/** - * The toast UI component - */ -export function Default({type, content}: ToastComponentProps) { +export function Outer({children}: {children: React.ReactNode}) { return ( - - - ) -} - -export function Outer({ - children, - type = 'default', -}: { - children: React.ReactNode - type?: ToastComponentProps['type'] -}) { - return ( - - {children} + {children} ) } @@ -60,14 +43,17 @@ export const api = sonner */ export function show( content: React.ReactNode, - {type, ...options}: BaseToastOptions = {}, + {type = 'default', ...options}: BaseToastOptions = {}, ) { const id = nanoid() if (typeof content === 'string') { sonner.custom( - - + + + + {content} + , { ...options, @@ -77,7 +63,9 @@ export function show( ) } else if (React.isValidElement(content)) { sonner.custom( - {content}, + + {content} + , { ...options, id, diff --git a/src/components/Toast/index.web.tsx b/src/components/Toast/index.web.tsx index c4db20dcaa..8b2028db98 100644 --- a/src/components/Toast/index.web.tsx +++ b/src/components/Toast/index.web.tsx @@ -5,7 +5,9 @@ import {toast as sonner, Toaster} from 'sonner' import {atoms as a} from '#/alf' import {DURATION} from '#/components/Toast/const' import { - Default as DefaultToast, + Icon as ToastIcon, + Outer as ToastOuter, + Text as ToastText, ToastConfigProvider, } from '#/components/Toast/Toast' import {type BaseToastOptions} from '#/components/Toast/types' @@ -39,14 +41,17 @@ export const api = sonner */ export function show( content: React.ReactNode, - {type, ...options}: BaseToastOptions = {}, + {type = 'default', ...options}: BaseToastOptions = {}, ) { const id = nanoid() if (typeof content === 'string') { sonner( - - + + + + {content} + , { ...options, @@ -56,12 +61,17 @@ export function show( }, ) } else if (React.isValidElement(content)) { - sonner({content}, { - ...options, - unstyled: true, // required on web - id, - duration: options?.duration ?? DURATION, - }) + sonner( + + {content} + , + { + ...options, + unstyled: true, // required on web + id, + duration: options?.duration ?? DURATION, + }, + ) } else { throw new Error( `Toast can be a string or a React element, got ${typeof content}`, diff --git a/src/view/screens/Storybook/Toasts.tsx b/src/view/screens/Storybook/Toasts.tsx index 319f88e215..91fe0d970c 100644 --- a/src/view/screens/Storybook/Toasts.tsx +++ b/src/view/screens/Storybook/Toasts.tsx @@ -4,12 +4,28 @@ import {show as deprecatedShow} from '#/view/com/util/Toast' import {atoms as a} from '#/alf' import {Globe_Stroke2_Corner0_Rounded as GlobeIcon} from '#/components/icons/Globe' import * as Toast from '#/components/Toast' -import {Default} from '#/components/Toast/Toast' import {H1} from '#/components/Typography' -function ToastWithAction({type = 'default'}: {type?: Toast.ToastType}) { +function DefaultToast({ + content, + type = 'default', +}: { + content: string + type?: Toast.ToastType +}) { return ( - + + + + {content} + + + ) +} + +function ToastWithAction() { + return ( + This toast has an action button + This is a longer message to test how the toast handles multiple lines of @@ -44,33 +60,25 @@ export function Toasts() {

Toast Examples

- - Toast.show()}> - - - Toast.show()}> - - - Toast.show()}> - - - Toast.show()}> - - - - + Toast.show(, {type: 'success'})}> + + + Toast.show(, {type: 'error'})}> + + + Toast.show()}> + + Toast.show(`Hey I'm a toast!`)}> - + - + - + - + - + - + - + -