File tree 5 files changed +87
-1
lines changed
5 files changed +87
-1
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,29 @@ import { ZView } from 'react-native-z-view';
69
69
/>;
70
70
```
71
71
72
+
73
+ ### Dismiss toast on tap
74
+
75
+ Use the ` ToastWrapper ` prop to wrap the Toast component with a custom component. This is useful when you want to customize the behavior of the toast, for example add a dismiss on tap instead of the the close icon.
76
+
77
+ ``` tsx
78
+ import { Pressable } from " react-native"
79
+
80
+ function Wrapper({toastId , children }){
81
+ function onPress(){
82
+ toast .dismiss (toastId )
83
+ }
84
+ return <Pressable onPress = { onPress } >{ children } </Pressable >
85
+ }
86
+
87
+ <Toaster
88
+ ToastWrapper = { Wrapper }
89
+ toastOptions = { {
90
+ style: { backgroundColor: ' red' },
91
+ }}
92
+ />;
93
+ ```
94
+
72
95
## API Reference
73
96
74
97
| Property | Description | Default |
@@ -85,5 +108,6 @@ import { ZView } from 'react-native-z-view';
85
108
| pauseWhenPageIsHidden | Pauses toast timers when the app enters background. | ` {} ` |
86
109
| ` swipeToDismissDirection ` | Swipe direction to dismiss (` left ` , ` up ` ). | ` up ` |
87
110
| ToasterOverlayWrapper | Custom component to wrap the Toaster. | ` div ` |
111
+ | ToastWrapper | Custom component to wrap the Toast. | ` div ` |
88
112
| autoWiggleOnUpdate | Adds a wiggle animation on toast update. ` never ` , ` toast-change ` , ` always ` | ` never ` |
89
113
| richColors | Makes error and success state more colorful | ` false ` |
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';
5
5
import { Toaster } from 'sonner-native' ;
6
6
import '../global.css' ;
7
7
import Navigator from './navigation' ;
8
+ // import { ToastWrapper } from './ToastWrapper';
8
9
9
10
const App : React . FC = ( ) => {
10
11
return (
@@ -29,6 +30,7 @@ const App: React.FC = () => {
29
30
paddingHorizontal : 20 ,
30
31
} ,
31
32
} }
33
+ // ToastWrapper={ToastWrapper}
32
34
pauseWhenPageIsHidden
33
35
/>
34
36
</ GestureHandlerRootView >
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import { Pressable , View } from 'react-native' ;
3
+ import { toast } from 'sonner-native' ;
4
+ import '../global.css' ;
5
+
6
+ export const ToastWrapper : React . ComponentType <
7
+ React . ComponentProps < typeof View > & {
8
+ children : React . ReactNode ;
9
+ toastId : string | number ;
10
+ }
11
+ > = ( { toastId, style, ...props } ) => {
12
+ return (
13
+ < Pressable
14
+ style = { [ style , { backgroundColor : 'red' } ] }
15
+ onPress = { ( ) => toast . dismiss ( toastId ) }
16
+ { ...props }
17
+ />
18
+ ) ;
19
+ } ;
Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ export const ToasterUI: React.FC<
104
104
setToasts,
105
105
toastsCounter,
106
106
toastRefs,
107
+ ToastWrapper,
107
108
...props
108
109
} ) => {
109
110
addToastHandler = React . useCallback (
@@ -325,6 +326,23 @@ export const ToasterUI: React.FC<
325
326
< ToastContext . Provider value = { value } >
326
327
< Positioner position = { position } >
327
328
{ positionedNonDynamicToasts . map ( ( positionedToast ) => {
329
+ if ( ToastWrapper ) {
330
+ return (
331
+ < ToastWrapper
332
+ key = { positionedToast . id }
333
+ toastId = { positionedToast . id }
334
+ style = { { width : '100%' } }
335
+ >
336
+ < Toast
337
+ { ...positionedToast }
338
+ onDismiss = { onDismiss }
339
+ onAutoClose = { onAutoClose }
340
+ ref = { toastRefs . current [ positionedToast . id ] }
341
+ { ...props }
342
+ />
343
+ </ ToastWrapper >
344
+ ) ;
345
+ }
328
346
return (
329
347
< Toast
330
348
key = { positionedToast . id }
@@ -343,6 +361,23 @@ export const ToasterUI: React.FC<
343
361
}
344
362
>
345
363
{ positionedDynamicToasts . map ( ( positionedToast ) => {
364
+ if ( ToastWrapper ) {
365
+ return (
366
+ < ToastWrapper
367
+ key = { positionedToast . id }
368
+ toastId = { positionedToast . id }
369
+ style = { { width : '100%' } }
370
+ >
371
+ < Toast
372
+ key = { positionedToast . id }
373
+ { ...positionedToast }
374
+ onDismiss = { onDismiss }
375
+ onAutoClose = { onAutoClose }
376
+ { ...props }
377
+ />
378
+ </ ToastWrapper >
379
+ ) ;
380
+ }
346
381
return (
347
382
< Toast
348
383
key = { positionedToast . id }
Original file line number Diff line number Diff line change 1
1
import type React from 'react' ;
2
- import type { TextStyle , ViewStyle } from 'react-native' ;
2
+ import type { TextStyle , ViewProps , ViewStyle } from 'react-native' ;
3
3
4
4
type StyleProps = {
5
5
unstyled ?: boolean ;
@@ -125,6 +125,12 @@ export type ToasterProps = {
125
125
swipeToDismissDirection ?: ToastSwipeDirection ;
126
126
pauseWhenPageIsHidden ?: boolean ;
127
127
ToasterOverlayWrapper ?: React . ComponentType < { children : React . ReactNode } > ;
128
+ ToastWrapper ?: React . ComponentType <
129
+ ViewProps & {
130
+ children : React . ReactNode ;
131
+ toastId : string | number ;
132
+ }
133
+ > ;
128
134
} ;
129
135
130
136
export type AddToastContextHandler = (
You can’t perform that action at this time.
0 commit comments