🚀
ShipClojure
  • README
  • Development
    • Getting Started
    • REPL Workflow
    • AI Development with ShipClojure
    • Getting Updates
    • Formatting code
    • ShipClojure Guiding Principles
  • Backend
    • Migrations
    • Secrets
    • Routing
    • ShipClojure Blog
    • Email
  • Frontend
    • UIx + re-frame
    • HTTP Requests with Re-frame
    • Frontend Navigation with Re-frame
    • Toast Notifications
    • Icons
  • Server Side Rendering
    • Static/Landing pages
  • Auth
    • How Auth works
    • Oauth2 providers
  • Deployment
    • Deployment
  • Decisions
    • 001 - Cookie Sessions
    • 002 - Single Page Application Architecture
    • 003 - Re-frame instead of Refx
    • 003 - Move from cookie sessions to JWT Access + refresh tokens
Powered by GitBook
On this page
  • Table of Contents
  • Overview
  • Toast Positions
  • Usage with Re-Frame
  • Toast Data Structure
  • Toast Types
  • Customizing Toasts
  • Custom Timeout
  • Clearing Toasts
  • Remove a Single Toast
  • Clear All Toasts at a Position
  • Clear All Toasts
  • Setting Up the Toast Container
  • Reference
  • Toast Examples
  • Implementation Details
  1. Frontend

Toast Notifications

PreviousFrontend Navigation with Re-frameNextIcons

Last updated 1 month ago

Toast notifications provide a way to display temporary, non-blocking notifications to users. They are especially useful for providing feedback after user actions like form submissions, API calls, or system events.

Table of Contents

Overview

The toast system in this application uses for state management and for styling. Toasts can be displayed at different positions on the screen and with different statuses (info, success, warning, error).

The main components of the toast system are:

  • toast-container: A component that displays all active toasts

  • Re-frame events for managing toasts

  • Re-frame subscriptions for accessing toast data

Toast Positions

Toasts can be positioned in 9 different locations on the screen:

#{[:top :start] [:top :center] [:top :end]
  [:middle :start] [:middle :center] [:middle :end]
  [:bottom :start] [:bottom :center] [:bottom :end]}

The default position is [:bottom :center].

Usage with Re-Frame

To display a toast notification, dispatch one of the toast events with the appropriate data:

;; Basic info toast
(rf/dispatch [:toast/info {:message "Operation completed successfully"}])

;; Success toast with custom position
(rf/dispatch [:toast/success 
              {:message "Item saved successfully" 
               :vertical :top 
               :horizontal :center}])

;; Warning toast
(rf/dispatch [:toast/warning {:message "Please check your input"}])

;; Error toast (stays longer by default)
(rf/dispatch [:toast/error {:message "Failed to connect to server"}])

Toast Data Structure

When dispatching a toast event, you can provide the following parameters:

Parameter
Type
Default
Description

:message

String

(required)

The message to display in the toast

:vertical

Keyword

:bottom

Vertical position (:top, :middle, :bottom)

:horizontal

Keyword

:center

Horizontal position (:start, :center, :end)

:timeout

Number (ms)

5000 (7000 for errors)

Time in milliseconds before toast auto-dismisses

:id

String/UUID

(auto-generated)

Unique identifier for the toast

Toast Types

There are four built-in toast types, each with its own color styling:

  1. Info (:toast/info): For general information and neutral messages

  2. Success (:toast/success): For confirming successful operations

  3. Warning (:toast/warning): For non-critical warnings

  4. Error (:toast/error): For error messages (stays visible longer)

Customizing Toasts

Custom Timeout

You can customize how long a toast appears by setting the :timeout parameter:

(rf/dispatch [:toast/info 
              {:message "This will disappear quickly" 
               :timeout 2000}])

Clearing Toasts

Remove a Single Toast

To remove a single toast programmatically, you need its ID and position:

(rf/dispatch [:toast/remove {:id "toast-id" :position [:top :center]}])

Clear All Toasts at a Position

(rf/dispatch [:toast/clear-position [:top :center]])

Clear All Toasts

(rf/dispatch [:toast/clear-all])

Setting Up the Toast Container

To display toasts in your application, you need to include the toast container component in your app's root component:

(ns your-app.core
  (:require
   [re-frame.core :as rf]
   [saas.ui.toasts :as toasts]
   [uix.core :refer [$ defui]]))

(defui main-app []
  ($ :<>
     ;; Your app content
     ($ your-app-component)
     
     ;; Toast container (should be at the root level)
     ($ toasts/toast-container)))

Reference

Toast Examples

For visual examples of different toast layouts and positions, refer to the toast scenes in the Portfolio:

The portfolio includes examples of:

  • Basic toast with alert

  • Toasts in all 9 positions

  • Interactive examples with buttons that trigger real toasts

Implementation Details

The toast system is implemented in:

: Contains the re-frame events, subscriptions, and UI components

: Contains the base toast component

Toast Scenes
saas.ui.toasts
saas.common.ui.core
re-frame
daisyUI
Overview
Toast Positions
Usage with Re-Frame
Toast Types
Customizing Toasts
Clearing Toasts
Reference