Toast Notification Hook (ui/use-toast.ts)

taxonomy · components/ui/use-toast.ts · typescript · MIT
ui, chat, fw:react

A React hook for managing toast notifications with add, update, dismiss, and remove functionality. Use it to show temporary feedback messages to users.

// Source: https://github.com/shadcn-ui/taxonomy | License: MIT | Copyright 2022 shadcn
// Provided via justclaw.it.com; keep this notice with the code.
// Inspired by react-hot-toast library
import * as React from "react"

import { ToastActionElement, type ToastProps } from "@/components/ui/toast"

const TOAST_LIMIT = 1
const TOAST_REMOVE_DELAY = 1000000

type ToasterToast = ToastProps & {
  id: string
  title?: React.ReactNode
  description?: React.ReactNode
  action?: ToastActionElement
}

const actionTypes = {
  ADD_TOAST: "ADD_TOAST",
  UPDATE_TOAST: "UPDATE_TOAST",
  DISMISS_TOAST: "DISMISS_TOAST",
  REMOVE_TOAST: "REMOVE_TOAST",
} as const

let count = 0

function genId() {
  count = (count + 1) % Number.MAX_VALUE
  return count.toString()
}

type ActionType = typeof actionTypes

ty

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key