No results found

Toast

A notification system that displays brief messages to users with automatic dismissal options.

Features

  • Automatic dismissal with configurable duration
  • Pause-on-hover functionality to prevent dismissal
  • Fixed positioning that persists during page scroll
  • Accessible title and description with proper ARIA relationships
  • Manual close functionality with keyboard support
  • Composable structure allowing flexible content
  • Built on Popover primitives for robust positioning
  • Support for controlled state with signal binding
  • onChange callback for state synchronization
  • Built-in hover detection for timer management

Anatomy

PartDescription
<Toast.Root>Root component that manages the toast state, positioning, and auto-dismiss behavior
<Toast.Title>Accessible title element that provides the main heading for the toast message
<Toast.Description>Description element that provides additional context or details for the toast
<Toast.Close>Button element that allows users to manually dismiss the toast

Examples

Basic Usage

Default Toast

The Toast component provides a way to show temporary notifications to users. It automatically dismisses after a set duration and can be manually closed.

In this example, we're using the basic Toast structure with:

Custom Duration

You can customize how long the toast appears before automatically dismissing.

This example sets a custom duration of 2 seconds using the duration prop on <Toast.Root>. The default duration is 5 seconds.

Signal Binding

Toast state can be bound to a signal for external state management.

Is open: false

The bind:open prop creates a two-way binding with the provided signal, allowing you to track the toast's open state from outside the component.

Change Callback

You can respond to toast state changes with the onChange$ callback.

onChange called: 0

The onChange$ callback is triggered whenever the toast opens or closes, allowing you to perform side effects or track usage.

Component State

Using Component State

The Toast component provides several ways to control its state through props and callbacks.

Controlling Open State

As shown in the Basic Usage example above, you can control the toast's visibility using the bind:open prop with a signal:

const isOpen = useSignal(false);
<Toast.Root bind:open={isOpen}>
  <Toast.Title>Toast Title</Toast.Title>
  <Toast.Description>Toast Description</Toast.Description>
  <Toast.Close>Close</Toast.Close>
</Toast.Root>

This creates a two-way binding between your application state and the toast's open state. When the toast opens or closes, the bound signal will update accordingly.

Auto-Dismissal Duration

As shown in the Custom Duration example above, you can control how long the toast appears before automatically dismissing:

<Toast.Root bind:open={isOpen} duration={2000}>
  <!-- Toast content -->
</Toast.Root>

The duration prop accepts a value in milliseconds. The default duration is 5000ms (5 seconds). Setting duration to 0 will disable auto-dismissal.

State Interactions

Responding to State Changes

As shown in the Change Callback example above, you can use the onChange$ prop to respond to toast state changes:

const handleChange$ = $((open: boolean) => {
  // Perform actions when toast opens or closes
  console.log(`Toast is now ${open ? 'open' : 'closed'}`);
});
<Toast.Root bind:open={isOpen} onChange$={handleChange$}>
  <!-- Toast content -->
</Toast.Root>

The onChange$ callback receives the current open state as a boolean parameter. This is useful for tracking usage, logging, or triggering additional actions when the toast opens or closes.

Pause on Hover

The Toast component automatically pauses its auto-dismissal timer when the user hovers over it. This behavior is built-in and requires no additional configuration. When the user moves their cursor away from the toast, the timer resumes with the remaining time. This feature ensures that users have enough time to read and interact with the toast content, especially for longer messages or when the toast contains interactive elements like buttons or links.

Configuration

Core Configuration

Duration Settings

The Toast component automatically dismisses after a set duration. You can customize this duration using the duration prop on <Toast.Root>.

<Toast.Root duration={3000}>
  {/* Toast content */}
</Toast.Root>

The duration prop accepts a value in milliseconds. The default duration is 5000ms (5 seconds). Setting duration to 0 will disable auto-dismissal, requiring the user to manually close the toast. As shown in the Custom Duration example above, you can set a custom duration of 2000ms (2 seconds) to make the toast dismiss more quickly.

Advanced Configuration

Pause on Hover Behavior

The Toast component automatically pauses its auto-dismissal timer when the user hovers over it. This behavior is built-in and requires no additional configuration. When the user moves their cursor away from the toast, the timer resumes with the remaining time. This feature ensures that users have enough time to read and interact with the toast content, especially for longer messages or when the toast contains interactive elements like buttons or links.

State Change Callback

For advanced use cases, you can track toast state changes using the onChange$ callback:

const handleChange$ = $((open: boolean) => {
  // Perform actions when toast opens or closes
  console.log(`Toast is now ${open ? 'open' : 'closed'}`);
});
<Toast.Root onChange$={handleChange$}>
  {/* Toast content */}
</Toast.Root>

As shown in the Change Callback example above, the onChange$ callback receives the current open state as a boolean parameter. This is useful for tracking usage, logging, or triggering additional actions when the toast opens or closes.

API Reference

Toast Root

Inherits from: <div />

Props

PropTypeDefaultDescription
onChange$(open: boolean) => void-
durationnumber5000

Data Attributes

AttributeDescription
data-qds-toast-rootPresent on the root element
data-openIndicates whether the toast is currently open
data-closedIndicates whether the toast is currently closed

Toast Title

Inherits from: <span />

Data Attributes

AttributeDescription
data-qds-toast-titlePresent on the title element

Toast Description

Inherits from: <div />

Data Attributes

AttributeDescription
data-qds-toast-descriptionPresent on the description element

Toast Close

Inherits from: <button />

Data Attributes

AttributeDescription
data-qds-toast-closePresent on the close button element