Store UI State
Hooks that wire a store: toast queue, add to bag with fly animation, favourite toggle, product card actions and saved favourite ids.
npx shadcn@latest add https://ui.develop-better-solutions.com/r/commerce-ui-state.jsonUses:commerce-cart
useToasts keeps the three newest messages. useFavoriteIds persists ids.
Atlas Canvas Tote
Souk Linen Shirt
Draa Wool Scarf
Usage
commerce-ui-state-example.tsx
"use client";
import { useToasts, useFavoriteIds } from "@dbs/ui";
import { StoreScope } from "./_store";
import { Toasts } from "@dbs/ui";
const ITEMS = ["Atlas Canvas Tote", "Souk Linen Shirt", "Draa Wool Scarf"];
function Demo() {
const { toasts, toast, dismissToast } = useToasts();
const fav = useFavoriteIds("catalog-demo-favorite-ids");
return (
<div className="flex flex-col gap-3 text-sm">
<p className="text-ink-muted">useToasts keeps the three newest messages. useFavoriteIds persists ids.</p>
{ITEMS.map((name) => (
<div key={name} className="flex items-center justify-between rounded-xl border border-line px-4 py-3">
<span>{name}</span>
<span className="flex gap-2">
<button
type="button"
onClick={() => toast("cart", "Added to cart", name)}
className="rounded-full border border-line px-3 py-1 text-xs"
>
Add
</button>
<button
type="button"
aria-pressed={fav.has(name)}
onClick={() => toast("favorite", fav.toggle(name) ? "Saved" : "Removed", name)}
className="rounded-full border border-line px-3 py-1 text-xs aria-pressed:bg-ink aria-pressed:text-canvas"
>
Favourite
</button>
</span>
</div>
))}
<Toasts toasts={toasts} onDismiss={dismissToast} />
</div>
);
}
export default function CommerceUiStateExample() {
return (
<StoreScope minHeight={380}>
<Demo />
</StoreScope>
);
}