DDBS UI
commerce

Store Toasts

A live region that stacks add-to-cart, favourite, info and success notifications and auto-dismisses them.

npx shadcn@latest add https://ui.develop-better-solutions.com/r/store-toasts.json
Dependencies:lucide-react
Fullscreen

Usage

store-toasts-example.tsx
"use client";

import { useState } from "react";
import { Toasts, type StoreToastKind, type StoreToastMessage } from "@dbs/ui";
import { StoreScope, triggerClass } from "./_store";

const KINDS: [StoreToastKind, string, string][] = [
  ["cart", "Added to cart", "Atlas Canvas Tote"],
  ["favorite", "Saved to favourites", "Souk Linen Shirt"],
  ["info", "Free shipping unlocked", "Spend 1,000 MAD or more"],
  ["success", "Signed in", "Welcome back, Amine"],
];

export default function StoreToastsExample() {
  const [toasts, setToasts] = useState<StoreToastMessage[]>([]);
  const [n, setN] = useState(0);
  const push = (i: number) => {
    const [kind, title, message] = KINDS[i % KINDS.length]!;
    setN(n + 1);
    setToasts((t) => [...t.slice(-2), { id: n + 1, kind, title, message }]);
  };
  return (
    <StoreScope minHeight={300}>
      <button type="button" className={triggerClass} onClick={() => push(n)}>
        Show a toast
      </button>
      <Toasts toasts={toasts} onDismiss={(id) => setToasts((t) => t.filter((x) => x.id !== id))} />
    </StoreScope>
  );
}