toast
The toast function basically emits toasts to the screen. When called, it returns the an id, which is auto-generated or manually passed in when calling the function.
import { toast } from "toastice";
// id = 1
const id = toast("Hello World", { id: 1 });
// id = unique auto-generated string value (ex: "V1StGXR8_Z5jdHi6B-myT")
const id = toast("Hello World");
Arguments
The toast function takes in two arguments:
content: string|number|RenderCustomToastice
A string or a number value that will be displayed in the toast when the function is called.
OR
A render function that returns a React Component, all params provided in the settings will be passed to the render function in addition to other props:
close: function when called, dismisses the toastIcon: theIconcomponent displayed according to thetypefield insettingsprogress: dynamically updated progress value (from100to0), that is updated on every tick for the duration provided in theautoClosesetting
settings: ToasticeProps
A configuration object, for default values see here
id:
string | numberUnique identifier for a toast, only one toast with the same
idcan be emitted at a time.delay:
numberThe delay period after which the toast will show up on the screen.
onClose:
() => voidA callback that is invoked just before the toast is dismissed, either manually or automatically.
onOpen:
(params: ToasticePropsWithContent) => voidA callback that is invoked just after the toast is displayed.
onProgress:
(progress: number) => voidA function that is continuously ran for the duration of the countdown, it gives the caller access to the dynamically updated progress value (from
100to0), that is updated on every tick for the duration provided in theautoClosesetting.import { toast } from "toastice";
const id = toast("Hello World", {
autoClose: 3000,
onProgress: (progress) => {
log(progress); // 100 -> 0
},
});type:
"default"|"success"|"warning"|"error"|"info"The type of the toast, it provides a visual cue as to the nature of the message.
theme:
"filled"|"outlined"|"glass"The theme of the toast.
animation:
"pop"|"fade"|"slide"The type of animation used to animate the toast in and out of the screen.
autoClose:
false|numberThe period after which a toast should automatically close.
closeOnClick:
booleanIf
truethe toast is clickable anywhere on its surface, a click causes the toast to close.pauseOnHover:
booleanIf
truethe user can pause the countdown of the autoClose period.position:
"top-left"|"top-center"|"top-right"|"bottom-left"|"bottom-center"|"bottom-right"THe toasts list position on the screen.
showCloseButton:
booleanIf
truethe close button will be visible, this works well withcloseOnClick.showIcon:
booleanEach toast type, except
default, has its ownIcon, this flag controls whether it is visible or not.role:
stringDefines the ARIA role for the toast
elevated:
booleanIf
true, a shadow appears under the toast.
The toast function has two methods that expand the functionality of the function:
toast.dismiss
A function that takes in an optional id of an already emitted toast and removes it off the screen programmatically. If an id is not provided, then all the toasts will be dismissed.
import { useRef } from "react";
import { ToasticeContainer, toast } from "toastice";
import "toastice/toastice.css";
const App = () => {
const id = useRef();
const showToastice = () => {
id.current = toast("Hello World");
};
const dismissToastice = () => {
toast.dismiss(id.current);
};
const dismissAllToastices = () => {
toast.dismiss();
};
return (
<>
<button onClick={showToastice}>Show Toastice</button>
<button onClick={showToastice}>Close Toastice</button>
<button onClick={closeToastice}>Close All Toastices</button>
<ToasticeContainer />
</>
);
};
toast.update
A function that updates the params of a toast programmatically, usually used to alter the look and message of a toast in response to an action. It takes two arguments, the id of the toast to be updated, and the new settings that will be merged with the default ones.
import { useRef } from "react";
import { ToasticeContainer, toast } from "toastice";
import "toastice/toastice.css";
const App = () => {
const id = useRef();
const showToastice = () => {
id.current = toast("Signing In...", {
type: "default",
autoClose: false,
});
};
const updateToastice = (params) => {
toast.update(id.current, params);
};
const signIn = async () => {
try {
showToastice();
const result = await userSignIn(credentials);
updateToastice({
content: result,
type: "success",
autoClose: 5000,
});
} catch (err) {
updateToastice({
content: err,
type: "error",
autoClose: 5000,
});
}
};
return (
<>
<button onClick={signIn}>Sign In</button>
<ToasticeContainer />
</>
);
};