1- import React , { useEffect , useState } from 'react' // eslint-disable-line
2- import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line
1+ import React , { useEffect } from 'react' // eslint-disable-line
2+ import { Toaster as SonnerToaster , toast } from 'sonner'
33
44import './toaster.css'
55
@@ -12,143 +12,51 @@ export interface ToasterProps {
1212}
1313
1414export const Toaster = ( props : ToasterProps ) => {
15- const [ state , setState ] = useState < {
16- message : string | JSX . Element
17- hide : boolean
18- hiding : boolean
19- timeOutId : any
20- timeOut : number
21- showModal : boolean
22- showFullBtn : boolean
23- } > ( {
24- message : '' ,
25- hide : true ,
26- hiding : false ,
27- timeOutId : null ,
28- timeOut : props . timeOut || 7000 ,
29- showModal : false ,
30- showFullBtn : false
31- } )
32-
3315 useEffect ( ( ) => {
3416 if ( props . message ) {
35- const timeOutId = setTimeout ( ( ) => {
36- setState ( ( prevState ) => {
37- return { ...prevState , hiding : true }
38- } )
39- } , state . timeOut )
40-
41- setState ( ( prevState ) => {
42- if ( typeof props . message === 'string' && props . message . length > 201 ) {
43- const shortTooltipText = props . message . substring ( 0 , 200 ) + '...'
44-
45- return {
46- ...prevState ,
47- hide : false ,
48- hiding : false ,
49- timeOutId,
50- message : shortTooltipText
17+ // Show toast using Sonner
18+ const duration = props . timeOut || 2000
19+
20+ if ( typeof props . message === 'string' ) {
21+ toast ( props . message , {
22+ duration,
23+ onDismiss : ( ) => {
24+ props . handleHide && props . handleHide ( )
25+ } ,
26+ onAutoClose : ( ) => {
27+ props . handleHide && props . handleHide ( )
5128 }
52- } else {
53- const shortTooltipText = props . message
54-
55- return {
56- ...prevState ,
57- hide : false ,
58- hiding : false ,
59- timeOutId,
60- message : shortTooltipText
29+ } )
30+ } else {
31+ // For JSX elements, use toast.custom
32+ toast . custom (
33+ ( ) => (
34+ < div className = "remixui_custom_toast" >
35+ { props . message }
36+ </ div >
37+ ) ,
38+ {
39+ duration,
40+ onDismiss : ( ) => {
41+ props . handleHide && props . handleHide ( )
42+ } ,
43+ onAutoClose : ( ) => {
44+ props . handleHide && props . handleHide ( )
45+ }
6146 }
62- }
63- } )
64- }
65- } , [ props . message , props . timestamp ] )
66-
67- useEffect ( ( ) => {
68- if ( state . hiding ) {
69- setTimeout ( ( ) => {
70- closeTheToaster ( )
71- } , 1800 )
72- }
73- } , [ state . hiding ] )
74-
75- const showFullMessage = ( ) => {
76- setState ( ( prevState ) => {
77- return { ...prevState , showModal : true }
78- } )
79- }
80-
81- const hideFullMessage = ( ) => {
82- //eslint-disable-line
83- setState ( ( prevState ) => {
84- return { ...prevState , showModal : false }
85- } )
86- }
87-
88- const closeTheToaster = ( ) => {
89- if ( state . timeOutId ) {
90- clearTimeout ( state . timeOutId )
91- }
92- props . handleHide && props . handleHide ( )
93- setState ( ( prevState ) => {
94- return {
95- ...prevState ,
96- message : '' ,
97- hide : true ,
98- hiding : false ,
99- timeOutId : null ,
100- showModal : false
47+ )
10148 }
102- } )
103- }
104-
105- const handleMouseEnter = ( ) => {
106- if ( state . timeOutId ) {
107- clearTimeout ( state . timeOutId )
10849 }
109- setState ( ( prevState ) => {
110- return { ...prevState , timeOutId : null }
111- } )
112- }
113-
114- const handleMouseLeave = ( ) => {
115- if ( ! state . timeOutId ) {
116- const timeOutId = setTimeout ( ( ) => {
117- setState ( ( prevState ) => {
118- return { ...prevState , hiding : true }
119- } )
120- } , state . timeOut )
121-
122- setState ( ( prevState ) => {
123- return { ...prevState , timeOutId }
124- } )
125- }
126- }
50+ } , [ props . message , props . timestamp ] )
12751
12852 return (
129- < >
130- < ModalDialog id = "toaster" message = { props . message } cancelLabel = "Close" cancelFn = { ( ) => { } } hide = { ! state . showModal } handleHide = { hideFullMessage } />
131- { ! state . hide && (
132- < div
133- data-shared = "tooltipPopup"
134- className = { `remixui_tooltip mb-4 alert alert-info p-2 ${ state . hiding ? 'remixui_animateTop' : 'remixui_animateBottom' } ` }
135- onMouseEnter = { handleMouseEnter }
136- onMouseLeave = { handleMouseLeave }
137- >
138- < span className = "px-2" >
139- { state . message }
140- { state . showFullBtn && (
141- < button className = "btn btn-secondary btn-sm mx-3" style = { { whiteSpace : 'nowrap' } } onClick = { showFullMessage } >
142- Show full message
143- </ button >
144- ) }
145- </ span >
146- < span style = { { alignSelf : 'baseline' } } >
147- < button data-id = "tooltipCloseButton" className = "fas fa-times btn-close p-0 mt-2" onClick = { closeTheToaster } > </ button >
148- </ span >
149- </ div >
150- ) }
151- </ >
53+ < SonnerToaster
54+ position = "bottom-center"
55+ offset = "25vh"
56+ toastOptions = { {
57+ className : 'remixui_sonner_toast' ,
58+ } }
59+ />
15260 )
15361}
15462
0 commit comments