Skip to main content

toast()

Create and display a toast notification.
toast(message: Message, options?: ToastOptions): string
message
Message
required
The message to display. Can be a string, JSX element, or a function that receives the toast object and returns a renderable value.
type Message = ValueOrFunction<Renderable, Toast>
type Renderable = JSX.Element | string | null
options
ToastOptions
Optional configuration for the toast.
return
string
The unique ID of the created toast.

Example

import { toast } from 'solid-toast';

// Simple string message
const id = toast('Hello World');

// With options
toast('Custom toast', {
  duration: 5000,
  position: 'top-center',
  style: {
    background: '#333',
    color: '#fff',
  },
});

// With JSX
toast(<div><strong>Bold</strong> message</div>);

// Dynamic message based on toast state
toast((t) => `Toast ${t.visible ? 'visible' : 'hidden'}`);

toast.success()

Create and display a success toast with a checkmark icon.
toast.success(message: Message, options?: ToastOptions): string
message
Message
required
The success message to display.
options
ToastOptions
Optional configuration. Same as toast() options.
return
string
The unique ID of the created toast.

Example

toast.success('Successfully saved!');

toast.error()

Create and display an error toast with an error icon.
toast.error(message: Message, options?: ToastOptions): string
message
Message
required
The error message to display.
options
ToastOptions
Optional configuration. Same as toast() options.
return
string
The unique ID of the created toast.

Example

toast.error('Something went wrong!');

toast.loading()

Create and display a loading toast with a spinner icon.
toast.loading(message: Message, options?: ToastOptions): string
message
Message
required
The loading message to display.
options
ToastOptions
Optional configuration. Same as toast() options.
return
string
The unique ID of the created toast.

Example

const id = toast.loading('Saving...');

// Later, update or dismiss it
toast.success('Saved!', { id });

toast.custom()

Create and display a custom toast without any default icon.
toast.custom(message: Message, options?: ToastOptions): string
message
Message
required
The custom message to display.
options
ToastOptions
Optional configuration. Same as toast() options.
return
string
The unique ID of the created toast.

Example

toast.custom(
  <div class="custom-toast">
    <MyCustomIcon />
    <span>Custom notification</span>
  </div>
);

toast.promise()

Automatically handles promise states with different toast messages for loading, success, and error states.
toast.promise<T>(
  promise: Promise<T>,
  msgs: {
    loading: Renderable;
    success: ValueOrFunction<Renderable, T>;
    error: ValueOrFunction<Renderable, any>;
  },
  opts?: DefaultToastOptions
): Promise<T>
promise
Promise<T>
required
The promise to track.
msgs
object
required
Messages for each state.
opts
DefaultToastOptions
Optional configuration to apply to all toast states.
return
Promise<T>
Returns the original promise.

Example

const saveData = async () => {
  const response = await fetch('/api/save', { method: 'POST' });
  return response.json();
};

toast.promise(saveData(), {
  loading: 'Saving...',
  success: 'Saved successfully!',
  error: 'Failed to save.',
});

// With dynamic messages
toast.promise(fetchUser(id), {
  loading: 'Loading user...',
  success: (user) => `Welcome, ${user.name}!`,
  error: (err) => `Error: ${err.message}`,
});

toast.dismiss()

Dismiss one or all toasts.
toast.dismiss(toastId?: string): void
toastId
string
The ID of the toast to dismiss. If omitted, all toasts will be dismissed.

Example

// Dismiss a specific toast
const id = toast.loading('Please wait...');
toast.dismiss(id);

// Dismiss all toasts
toast.dismiss();

toast.remove()

Immediately remove one or all toasts from the DOM.
toast.remove(toastId?: string): void
toastId
string
The ID of the toast to remove. If omitted, all toasts will be removed.

Example

// Remove a specific toast
const id = toast('Hello');
toast.remove(id);

// Remove all toasts
toast.remove();
The difference between dismiss() and remove() is that dismiss() triggers the exit animation before removal, while remove() immediately removes the toast from the DOM.

Build docs developers (and LLMs) love