Skip to content

Commit 2ae85c7

Browse files
author
ci-bot
committed
timeout and toastid
1 parent 48432e2 commit 2ae85c7

File tree

10 files changed

+83
-17
lines changed

10 files changed

+83
-17
lines changed

apps/remix-ide/src/app/plugins/notification.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@ import { LibraryProfile, MethodApi, StatusEvents } from '@remixproject/plugin-ut
33
import { AppModal } from '@remix-ui/app'
44
import { AlertModal } from '@remix-ui/app'
55
import { dispatchModalInterface } from '@remix-ui/app'
6+
import { Toaster, toast } from '@remix-ui/toaster'
67

78
interface INotificationApi {
89
events: StatusEvents
910
methods: {
1011
modal: (args: AppModal) => void
1112
alert: (args: AlertModal) => void
1213
toast: (message: string) => void
14+
hideToaster: (id: number) => void
1315
}
1416
}
1517

1618
const profile: LibraryProfile<INotificationApi> = {
1719
name: 'notification',
1820
displayName: 'Notification',
1921
description: 'Displays notifications',
20-
methods: ['modal', 'alert', 'toast']
22+
methods: ['modal', 'alert', 'toast', 'hideToaster']
2123
}
2224

2325
export class NotificationPlugin extends Plugin implements MethodApi<INotificationApi> {
2426
dispatcher: dispatchModalInterface
27+
toastId: number
2528
constructor() {
2629
super(profile)
30+
this.toastId = 0
2731
}
2832

2933
setDispatcher(dispatcher: dispatchModalInterface) {
@@ -38,7 +42,12 @@ export class NotificationPlugin extends Plugin implements MethodApi<INotificatio
3842
return this.dispatcher.alert(args)
3943
}
4044

41-
async toast(message: string | JSX.Element) {
42-
this.dispatcher.toast(message)
45+
async toast(message: string | JSX.Element, timeout?: number) {
46+
this.toastId++
47+
return this.dispatcher.toast(message, timeout, this.toastId)
48+
}
49+
50+
async hideToaster(id: number) {
51+
toast.dismiss(id)
4352
}
4453
}

libs/remix-ui/app/src/lib/remix-app/actions/modals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const enum modalActionTypes {
2222
type ModalPayload = {
2323
[modalActionTypes.setModal]: AppModal
2424
[modalActionTypes.handleHideModal]: any
25-
[modalActionTypes.setToast]: { message: string | JSX.Element, timestamp: number }
25+
[modalActionTypes.setToast]: { message: string | JSX.Element, timestamp: number, timeout?: number }
2626
[modalActionTypes.handleToaster]: any,
2727
[modalActionTypes.processQueue]: any
2828
}

libs/remix-ui/app/src/lib/remix-app/components/modals/dialogs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const AppDialogs = () => {
1010
return (
1111
<>
1212
<ModalWrapper {...focusModal} handleHide={handleHideModal}></ModalWrapper>
13-
<Toaster message={focusToaster.message} timestamp={focusToaster.timestamp} handleHide={handleToaster} />
13+
<Toaster message={focusToaster.message} id={focusToaster.toastId} timeout={focusToaster.timeout} timestamp={focusToaster.timestamp} handleHide={handleToaster} />
1414
</>
1515
)
1616
}

libs/remix-ui/app/src/lib/remix-app/context/context.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ export const platformContext = React.createContext<appPlatformTypes>(null)
2424

2525
export interface dispatchModalInterface {
2626
modal: (data: AppModal) => void
27-
toast: (message: string | JSX.Element) => void
27+
toast: (message: string | JSX.Element, timeout?: number, toastId?: number) => void
2828
alert: (data: AlertModal) => void
2929
handleHideModal: () => void
3030
handleToaster: () => void
3131
}
3232

3333
export const dispatchModalContext = React.createContext<dispatchModalInterface>({
3434
modal: (data: AppModal) => {},
35-
toast: (message: string | JSX.Element) => {},
35+
toast: (message: string | JSX.Element, timeout?: number, toastId?: number) => {},
3636
alert: (data: AlertModal) => {},
3737
handleHideModal: () => {},
3838
handleToaster: () => {}

libs/remix-ui/app/src/lib/remix-app/context/provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export const ModalProvider = ({ children = [], reducer = modalReducer, initialSt
7070
})
7171
}
7272

73-
const toast = (message: string | JSX.Element) => {
73+
const toast = (message: string | JSX.Element, timeout?: number) => {
7474
dispatch({
7575
type: modalActionTypes.setToast,
76-
payload: { message, timestamp: Date.now() }
76+
payload: { message, timestamp: Date.now(), timeout }
7777
})
7878
}
7979

libs/remix-ui/app/src/lib/remix-app/interface/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export interface AlertModal {
3838

3939
export interface ModalState {
4040
modals: AppModal[],
41-
toasters: {message: (string | JSX.Element), timestamp: number }[],
41+
toasters: {message: (string | JSX.Element), timestamp: number, timeout?: number, toastId?: number }[],
4242
focusModal: AppModal,
43-
focusToaster: {message: (string | JSX.Element), timestamp: number }
43+
focusToaster: {message: (string | JSX.Element), timestamp: number, timeout?: number, toastId?: number }
4444
}
4545

4646
export interface forceChoiceModal {

libs/remix-ui/app/src/lib/remix-app/state/modals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export const ModalInitialState: ModalState = {
1515
cancelFn: () => { },
1616
showCancelIcon: false
1717
},
18-
focusToaster: { message: '', timestamp: 0 }
18+
focusToaster: { message: '', timestamp: 0, timeout: 2000 }
1919
}

libs/remix-ui/toaster/src/lib/toaster.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,45 @@
4242
border: 0px solid var(--bs-secondary, #6c757d) !important;
4343
font-weight: 700 !important;
4444
}
45+
46+
/* Close button styling */
47+
[data-sonner-toast] [data-close-button] {
48+
position: absolute;
49+
top: 12px;
50+
right: 12px;
51+
background: transparent !important;
52+
border: none;
53+
color: var(--bs-info-text-emphasis, #212529) !important;
54+
opacity: 0.5;
55+
cursor: pointer;
56+
padding: 4px;
57+
display: flex;
58+
align-items: center;
59+
justify-content: center;
60+
border-radius: 4px;
61+
transition: all 0.2s ease;
62+
width: 24px;
63+
height: 24px;
64+
}
65+
66+
[data-sonner-toast] [data-close-button]:hover {
67+
opacity: 1;
68+
background: rgba(0, 0, 0, 0.05) !important;
69+
transform: scale(1.1);
70+
}
71+
72+
[data-sonner-toast] [data-close-button] svg {
73+
width: 16px;
74+
height: 16px;
75+
}
76+
77+
/* Dark theme close button */
78+
[data-sonner-toast][data-theme="dark"] [data-close-button],
79+
.dark [data-sonner-toast] [data-close-button] {
80+
color: var(--bs-light, #f8f9fa) !important;
81+
}
82+
83+
[data-sonner-toast][data-theme="dark"] [data-close-button]:hover,
84+
.dark [data-sonner-toast] [data-close-button]:hover {
85+
background: rgba(255, 255, 255, 0.1) !important;
86+
}

libs/remix-ui/toaster/src/lib/toaster.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@ import {Toaster as SonnerToaster, toast} from 'sonner'
33

44
import './toaster.css'
55

6+
// Export toast so callers can use toast.dismiss(id)
7+
export {toast}
8+
69
/* eslint-disable-next-line */
710
export interface ToasterProps {
811
message: string | JSX.Element
9-
timeOut?: number
12+
timeout?: number
1013
handleHide?: () => void
1114
timestamp?: number
15+
id?: string | number
16+
onToastCreated?: (toastId: string | number) => void
1217
}
1318

1419
export const Toaster = (props: ToasterProps) => {
1520
useEffect(() => {
1621
if (props.message) {
1722
// Show toast using Sonner
18-
const duration = props.timeOut || 120000
23+
const duration = props.timeout || 2000
1924
const showCloseButton = duration > 5000
2025

26+
let toastId: string | number
27+
2128
if (typeof props.message === 'string') {
22-
toast(props.message, {
29+
toastId = toast(props.message, {
30+
id: props.id,
2331
unstyled: true,
2432
duration,
2533
closeButton: showCloseButton,
@@ -32,13 +40,14 @@ export const Toaster = (props: ToasterProps) => {
3240
})
3341
} else {
3442
// For JSX elements, use toast.custom
35-
toast.custom(
43+
toastId = toast.custom(
3644
() => (
3745
<div className="remixui_sonner_toast alert alert-info bg-light">
3846
{props.message}
3947
</div>
4048
),
4149
{
50+
id: props.id,
4251
duration,
4352
closeButton: showCloseButton,
4453
onDismiss: () => {
@@ -50,6 +59,11 @@ export const Toaster = (props: ToasterProps) => {
5059
}
5160
)
5261
}
62+
63+
// Call the callback with the toast ID so caller can dismiss it later
64+
if (props.onToastCreated) {
65+
props.onToastCreated(toastId)
66+
}
5367
}
5468
}, [props.message, props.timestamp])
5569

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,5 +425,6 @@
425425
"@ethereumjs/util": "^10.0.0",
426426
"@ethereumjs/vm": "^10.0.0",
427427
"@ethereumjs/binarytree": "^10.0.0"
428-
}
428+
},
429+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
429430
}

0 commit comments

Comments
 (0)