toast
Show short messages to the customer, such as success, error and loading notices.
Use toast to tell the customer something happened, such as "Added to your cart", without interrupting them. For content that needs a decision, use Modal.
Example
import { toast } from '@salla.sa/twilight-theme-engine/toast';
export async function addToCart(productId: number) {
return toast.promise(window.salla!.cart.addItem(productId), {
loading: 'Adding...',
success: 'Added to your cart',
error: 'Could not add this product',
});
}toast is a plain function, not a React hook, so you can call it anywhere in the browser. The example reads the SDK from window.salla, which exists once the page runs in the browser, so call addToCart from an event handler. useToast is exported too and is the same function.
API
toast(title: string, options?: ToastOptions): string | number;
toast.success | error | warning | info | loading(title, options?): string | number;
toast.dismiss(id?: string | number): void;
toast.update(id: string | number, options: { type?: ToastType; title?: string } & ToastOptions): void;
toast.promise<T>(promise: Promise<T>, options: ToastPromiseOptions<T>): Promise<T>;Each call returns the toast's id, which you can pass to toast.dismiss or toast.update. toast.update(id, { type, title }) replaces the toast in place, for example a loading toast that becomes a success. Leave out title to keep the current one. Passing { id } to a variant does the same: toast.success('Saved', { id }).
| Prop | Type | Description |
|---|---|---|
description? | string | Second line under the title. The rest of options goes to the toast library (duration, action and more). |
promise: loading | string | Message while pending. Required. |
promise: success | string | (data: T) => string | Message, or a function of the result. Required. |
promise: error | string | (error: Error) => string | Message, or a function of the error. Required. |
The Toaster
Toaster draws the toasts. The engine's TwilightProvider already mounts it, so a theme normally never renders one. Tune it through the provider instead:
| Prop | Type | Description |
|---|---|---|
toast? | boolean | On TwilightProvider: mount the Toaster. Default: true. |
toastMobilePosition? | ToastPosition | On TwilightProvider: position below 768px. Default: "bottom-center". |
addToCartToast? | boolean | On TwilightProvider: show the default add-to-cart toast. Default: true. |
The same last two options are props of Toaster itself (mobilePosition and addToCartToast).
Gotchas
- Toasts show in the browser, and a
Toastermust be mounted for anything to appear. Toasteralso shows the messages the Salla SDK raises, for example after adding to cart. Inside the Salla mobile app it hands those to the native app instead. Your owntoast()calls are always drawn by theToaster.- A page URL with
?success=messageor?danger=messageshows that message once as a toast, then removes it from the address bar. - On desktop toasts appear top-right, or top-left when the theme is RTL.
- If your theme shows its own add-to-cart feedback, set
addToCartToast={false}to avoid two messages.
Next
- React to cart events shows how to run code when the cart changes.
Modalfor dialogs.- Layout and components