From 4caa98daac2e125da80f242b3ef73e969a58f449 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 26 Aug 2025 10:37:16 -0500 Subject: [PATCH] Dismiss toast when clicking action button --- src/components/Toast/Toast.tsx | 51 +++++++++++++++++++----- src/components/Toast/index.tsx | 30 ++++++++++---- src/components/Toast/index.web.tsx | 29 ++++++++++---- src/components/Toast/sonner/index.ts | 3 ++ src/components/Toast/sonner/index.web.ts | 3 ++ src/view/screens/Storybook/Toasts.tsx | 8 +++- 6 files changed, 96 insertions(+), 28 deletions(-) create mode 100644 src/components/Toast/sonner/index.ts create mode 100644 src/components/Toast/sonner/index.web.ts diff --git a/src/components/Toast/Toast.tsx b/src/components/Toast/Toast.tsx index 6b10a7ecb1..53d5e51158 100644 --- a/src/components/Toast/Toast.tsx +++ b/src/components/Toast/Toast.tsx @@ -1,5 +1,5 @@ import {createContext, useContext, useMemo} from 'react' -import {View} from 'react-native' +import {type GestureResponderEvent, View} from 'react-native' import {atoms as a, select, useAlf, useTheme} from '#/alf' import { @@ -12,10 +12,15 @@ import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/ico import {CircleInfo_Stroke2_Corner0_Rounded as ErrorIcon} from '#/components/icons/CircleInfo' import {type Props as SVGIconProps} from '#/components/icons/common' import {Warning_Stroke2_Corner0_Rounded as WarningIcon} from '#/components/icons/Warning' +import {dismiss} from '#/components/Toast/sonner' import {type ToastType} from '#/components/Toast/types' import {Text as BaseText} from '#/components/Typography' -type ContextType = { +type ToastConfigContextType = { + id: string +} + +type ToastThemeContextType = { type: ToastType } @@ -32,10 +37,29 @@ export const ICONS = { info: CircleInfo, } -const Context = createContext({ +const ToastConfigContext = createContext({ + id: '', +}) +ToastConfigContext.displayName = 'ToastConfigContext' + +export function ToastConfigProvider({ + children, + id, +}: { + children: React.ReactNode + id: string +}) { + return ( + ({id}), [id])}> + {children} + + ) +} + +const ToastThemeContext = createContext({ type: 'default', }) -Context.displayName = 'ToastContext' +ToastThemeContext.displayName = 'ToastThemeContext' export function Default({type = 'default', content}: ToastComponentProps) { return ( @@ -57,7 +81,7 @@ export function Outer({ const styles = useToastStyles({type}) return ( - ({type}), [type])}> + ({type}), [type])}> {children} - + ) } export function Icon({icon}: {icon?: React.ComponentType}) { - const {type} = useContext(Context) + const {type} = useContext(ToastThemeContext) const styles = useToastStyles({type}) const IconComponent = icon || ICONS[type] return } export function Text({children}: {children: React.ReactNode}) { - const {type} = useContext(Context) + const {type} = useContext(ToastThemeContext) const {textColor} = useToastStyles({type}) const {fontScaleCompensation} = useToastFontScaleCompensation() return ( @@ -123,7 +147,8 @@ export function Action( ) { const t = useTheme() const {fontScaleCompensation} = useToastFontScaleCompensation() - const {type} = useContext(Context) + const {type} = useContext(ToastThemeContext) + const {id} = useContext(ToastConfigContext) const styles = useMemo(() => { const base = { base: { @@ -178,9 +203,15 @@ export function Action( }[type] }, [t, type]) + const onPress = (e: GestureResponderEvent) => { + console.log('Toast Action pressed, dismissing toast', id) + dismiss(id) + props.onPress?.(e) + } + return ( -