@@ -29,6 +29,8 @@ const ToasterContext = createContext<ToasterContextValue | null>(null);
2929export function ToastProvider ( { insets, children } : ToastProviderProps ) {
3030 const [ toasts , dispatch ] = useReducer ( toastReducer , [ ] ) ;
3131
32+ const isToastVisible = toasts . length > 0 ;
33+
3234 const heights = useSharedValue < Record < string , number > > ( { } ) ;
3335
3436 const total = useSharedValue < number > ( 0 ) ;
@@ -38,57 +40,58 @@ export function ToastProvider({ insets, children }: ToastProviderProps) {
3840 /**
3941 * Show a toast
4042 */
41- const show = useCallback ( ( options : ToastShowOptions ) : string => {
42- const id = options . id ?? `toast-${ Date . now ( ) } -${ idCounter . current ++ } ` ;
43-
44- dispatch ( {
45- type : 'SHOW' ,
46- payload : {
47- id,
48- component : options . component ,
49- } ,
50- } ) ;
51-
52- total . set ( ( value ) => value + 1 ) ;
43+ const show = useCallback (
44+ ( options : ToastShowOptions ) : string => {
45+ const id = options . id ?? `toast-${ Date . now ( ) } -${ idCounter . current ++ } ` ;
5346
54- return id ;
55- } , [ ] ) ;
56-
57- /**
58- * Hide one or more toasts
59- */
60- const hide = useCallback ( ( ids ?: string | string [ ] ) => {
61- if ( ids === undefined ) {
62- // Hide all toasts
63- dispatch ( { type : 'HIDE_ALL' } ) ;
64- heights . set ( { } ) ;
65- total . set ( 0 ) ;
66- } else {
67- // Hide specific toast(s)
68- const idsArray = Array . isArray ( ids ) ? ids : [ ids ] ;
69- const idsToRemove = idsArray ;
7047 dispatch ( {
71- type : 'HIDE' ,
72- payload : { ids : idsArray } ,
48+ type : 'SHOW' ,
49+ payload : {
50+ id,
51+ component : options . component ,
52+ } ,
7353 } ) ;
7454
75- heights . modify ( < T extends Record < string , number > > ( value : T ) : T => {
76- 'worklet' ;
77- const result = { ...value } ;
78- for ( const id of idsToRemove ) {
79- delete result [ id ] ;
80- }
81- return result ;
82- } ) ;
55+ total . set ( ( value ) => value + 1 ) ;
8356
84- total . set ( ( value ) => value - 1 ) ;
85- }
86- } , [ ] ) ;
57+ return id ;
58+ } ,
59+ [ total ]
60+ ) ;
8761
8862 /**
89- * Whether any toast is currently visible
63+ * Hide one or more toasts
9064 */
91- const isToastVisible = toasts . length > 0 ;
65+ const hide = useCallback (
66+ ( ids ?: string | string [ ] ) => {
67+ if ( ids === undefined ) {
68+ // Hide all toasts
69+ dispatch ( { type : 'HIDE_ALL' } ) ;
70+ heights . set ( { } ) ;
71+ total . set ( 0 ) ;
72+ } else {
73+ // Hide specific toast(s)
74+ const idsArray = Array . isArray ( ids ) ? ids : [ ids ] ;
75+ const idsToRemove = idsArray ;
76+ dispatch ( {
77+ type : 'HIDE' ,
78+ payload : { ids : idsArray } ,
79+ } ) ;
80+
81+ heights . modify ( < T extends Record < string , number > > ( value : T ) : T => {
82+ 'worklet' ;
83+ const result = { ...value } ;
84+ for ( const id of idsToRemove ) {
85+ delete result [ id ] ;
86+ }
87+ return result ;
88+ } ) ;
89+
90+ total . set ( ( value ) => value - 1 ) ;
91+ }
92+ } ,
93+ [ total , heights ]
94+ ) ;
9295
9396 const contextValue = useMemo < ToasterContextValue > (
9497 ( ) => ( {
0 commit comments