Users who have used the block copy button are no stranger to the Snackbar, which informs the user of the results of the application’s execution. This article will give an example of how to use Snackbar to meet the user’s need for custom interaction.
API#
Function | Description |
---|---|
show(body: String, duration: number = 2000) |
body : the body of the message, duration : the duration of the message, in milliseconds. |
Export#
Thanks to assets/main/js/custom.ts
, we can customize the JavaScript, here we export the whole Snackbar as a global variable:
import Snackbar from 'js/snackbar';
// Show a message via JavaScript.
Snackbar.show('a message with 3s duration from pure JavaScript', 3000)
// Export the Snackbar as a global variable, so that you can send a message from a HTML.
// Such as: <button onclick="Snackbar.show('message from a button')">Snackbar</button>.
const _global = (window || global ) as any
_global.Snackbar = Snackbar
Usage#
Then we can call it up in HTML or JavaScript:
<button onclick="Snackbar.show('message from a button')">Snackbar</button>
See Hooks for how to inject custom HTML.
Comments