Toast for Electron app.
Toast is a concise, non-modal notification method that is used to briefly display information on the user interface without interrupting the user's current operation. It is widely used in mobile operating systems such as Android and iOS to provide quick feedback and important prompt information. Through toast notifications, developers can improve user experience and effectively communicate application status changes.
npm i @electron-uikit/core @electron-uikit/toast
import { toast } from '@electron-uikit/toast/renderer'
toast.text('foo')
toast.loading('loading')
-
Exposes the UI Kit APIs for components. See @electron-uikit/core guide for more details.
You can expose it in the specified preload script:
import { exposeUIKit } from '@electron-uikit/core/preload' exposeUIKit()
Or, you can expose it globally in the main process for all renderer processes:
import { useUIKit } from '@electron-uikit/core/main' useUIKit()
Note
If you are using @electron-toolkit/preload to expose Electron APIs, there is no need to use this module, because core
is also an export of it.
-
Register a listener in the renderer process, so that you can use it in the main process.
import { toast } from '@electron-uikit/toast/renderer' toast.config({ supportMain: true })
-
Use the notification in the main process.
import { BrowserWindow } from 'electron' import { Toast } from '@electron-uikit/toast' const win = new BrowserWindow() const toast = new Toast(win) toast.text('foo') toast.loading('loading')
Note
To use Toast in the main process, you need to create a Toast instance of the specified window.
Configure toast defaults or customize toast. Can only be used in the renderer process.
-
options:
object
- container:
HTMLElement
(optional) - Container element of Toast. Default todocument.body
. - duration:
number
(optional) - Display duration in millisecond. If set to0
, it will not turn off automatically. Default to2000
. - customClass:
string
(optional) - Custom CSS class name for toast. - bottom:
number
(optional) - Toast position to the bottom. Default to50
. - maxWidth:
number
(optional) - The maximum width of toast. Default to320
. - color:
string
(optional) - Toast background color. - textColor:
string
(optional) - Toast text color. - fontSize:
number
(optional) - Toast font size. Default to14
. - iconSize:
number
(optional) - Toast icon size. Default to20
. - supportMain:
boolean
(optional) - Support Electron main process. Default tofalse
.
- container:
Show text. The default duration is 2000
ms.
Show loading. The default duration is 0, which means it is always displayed and can be turned off by calling its return value function.
import { toast } from '@electron-uikit/toast/renderer'
const reply = toast.loading('Loading')
setTimeout(() => {
reply.success('Successful')
// reply.fail('Failed')
// reply.dismiss()
}, 3000)
- Customize using CSS classes
.toast {
--toast-bottom: 50px;
--toast-z-index: 5001;
--toast-color: #48484e;
--toast-text-color: #ffffffd1;
--toast-font-size: 14px;
--toast-font-family: -apple-system, BlinkMacSystemFont, Ubuntu, 'Segoe UI';
--toast-icon-size: 20px;
--toast-max-width: 320px;
}
toast.config({
customClass: 'toast'
})
- Customize using
config
API
toast.config({
bottom: 200,
maxWidth: 280
})