DDBS UI
commerce

Price & Storage

Price formatter factory (whole amounts, comma grouping, currency suffix such as 1,290 MAD), round2, formatCount and safe local or session storage helpers.

npx shadcn@latest add https://ui.develop-better-solutions.com/r/commerce-money.json
Fullscreen
format(n)
1,290.50 MAD
round2(n)
1290.5
formatCount(n)
1.3K

Usage

commerce-money-example.tsx
"use client";

import { useState } from "react";
import { createPriceFormatter, formatCount, round2 } from "@dbs/ui";

const CURRENCIES = ["MAD", "EUR", "USD"] as const;

export default function CommerceMoneyExample() {
  const [amount, setAmount] = useState("1290.5");
  const [currency, setCurrency] = useState<(typeof CURRENCIES)[number]>("MAD");
  const format = createPriceFormatter(currency);
  const n = Number(amount) || 0;

  return (
    <div className="flex w-full max-w-md flex-col gap-4 text-sm">
      <label className="flex flex-col gap-1.5">
        <span className="text-ink-muted">Amount</span>
        <input
          value={amount}
          onChange={(e) => setAmount(e.target.value)}
          inputMode="decimal"
          className="h-11 rounded-lg border border-line bg-canvas px-4"
        />
      </label>
      <div className="flex gap-2" role="group" aria-label="Currency">
        {CURRENCIES.map((c) => (
          <button
            key={c}
            type="button"
            onClick={() => setCurrency(c)}
            aria-pressed={c === currency}
            className="h-9 rounded-full border border-line px-4 aria-pressed:bg-ink aria-pressed:text-canvas"
          >
            {c}
          </button>
        ))}
      </div>
      <dl className="grid grid-cols-[auto_1fr] gap-x-6 gap-y-2 rounded-xl border border-line bg-surface p-4">
        <dt className="text-ink-subtle">format(n)</dt>
        <dd className="font-medium">{format(n)}</dd>
        <dt className="text-ink-subtle">round2(n)</dt>
        <dd className="font-medium">{round2(n)}</dd>
        <dt className="text-ink-subtle">formatCount(n)</dt>
        <dd className="font-medium">{formatCount(Math.round(n))}</dd>
      </dl>
    </div>
  );
}