Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/docs/pages/components/toast.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,28 @@

### Watch promises

<Preview name="toast-promise" />
<Preview name="toast-promise" />

## Functions

The `toast` object exposes the following methods to dispatch toasts imperatively. Each method accepts a `message` argument of type `string | JSX.Element` and returns a toast `id`.

| Method | Description |
| --- | --- |
| `toast.informational(message)` | Dispatches an informational toast |
| `toast.success(message)` | Dispatches a success toast |
| `toast.critical(message)` | Dispatches a critical toast |
| `toast.warning(message)` | Dispatches a warning toast |
| `toast.loading(message)` | Dispatches a loading toast with a spinner |
| `toast.promise(promise, messages)` | Tracks a promise and dispatches loading, success, or error toasts accordingly. The `messages` argument is an object with `loading`, `success`, and `error` keys, each accepting `string \| JSX.Element` |
| `toast.dismiss(toastId?)` | Dismisses a toast by id, or all toasts if no id is provided |

> **Note:** A [`ToastStack`](/components/toast/toast-stack) must be rendered in your application for toasts to appear.

## Related components

<ComponentSummaryGrid>
<ComponentSummary name="toast-stack" />
<ComponentSummary name="alert" />
<ComponentSummary name="modal" />
</ComponentSummaryGrid>
7 changes: 7 additions & 0 deletions packages/shoreline/src/components/toast/toast-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ import type { ToastVariant } from './toast-types'
* toast.informational('Message!')
*/
export const toast = {
/** Dispatches an informational toast */
informational(message: ToastMessage) {
return toastFactory(message, getOptions('informational'))
},
/** Dispatches a success toast */
success(message: ToastMessage) {
return toastFactory.success(message, getOptions('success'))
},
/** Dispatches a critical toast */
critical(message: ToastMessage) {
return toastFactory.error(message, getOptions('critical'))
},
/** Dispatches a warning toast */
warning(message: ToastMessage) {
return toastFactory(message, getOptions('warning'))
},
/** Dispatches a loading toast with a spinner */
loading(message: ToastMessage) {
return toastFactory.loading(message, getOptions('informational'))
},
/** Tracks a promise and dispatches loading, success, or error toasts accordingly */
promise(promise: Promise<any>, messages: ToastPromiseMessages) {
return toastFactory.promise(promise, messages)
},
/** Dismisses a toast by id, or all toasts if no id is provided */
dismiss: toastFactory.dismiss,
}

Expand Down