Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import type { BuiltInStatus } from '@spree/core'
import { cva, type VariantProps } from 'class-variance-authority'
import type * as React from 'react'
import { useTranslation } from 'react-i18next'
import { cn } from '../lib/utils'
import { Slot } from './slot'
const badgeVariants = cva(
// No `border-transparent` here: it and a variant's `border-*` have the same
// specificity, so the one Tailwind emits last wins regardless of the order
// they appear in the class list — which silently erased the outline
// variant's border. Each variant states its own border colour instead.
'group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-sm border px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-[color,background-color,border-color,box-shadow] duration-100 ease-out focus-visible:border-ring focus-visible:shadow-[0_0_0_3px_color-mix(in_srgb,var(--ring)_15%,transparent)] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&>svg]:pointer-events-none [&>svg]:size-3!',
{
variants: {
variant: {
default: 'border-transparent bg-primary text-primary-foreground [a]:hover:bg-primary/80',
secondary:
'border-transparent bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80',
// The four status variants below share one recipe: a tint from the
// status ramp, text from the same ramp's reading step, and a border
// that firms the shape up on busy surfaces. Both themes get real
// colours rather than one hue at reduced opacity — an opacity tint
// takes on whatever sits behind it, which turns a badge muddy over a
// striped table row and washes it out entirely on a dark card. Linked
// badges darken to the border step on hover, which is the next stop up
// the same ramp rather than a second colour.
destructive:
'border-danger-border bg-danger-bg text-danger focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-danger-border',
success: 'border-success-border bg-success-bg text-success [a]:hover:bg-success-border',
info: 'border-info-border bg-info-bg text-info [a]:hover:bg-info-border',
warning: 'border-warning-border bg-warning-bg text-warning [a]:hover:bg-warning-border',
outline:
'border-border bg-card/50 text-foreground/75 [a]:hover:bg-accent [a]:hover:text-muted-foreground',
ghost:
'border-transparent hover:bg-accent hover:text-muted-foreground dark:hover:bg-accent/50',
link: 'border-transparent text-primary underline-offset-4 hover:underline',
},
},
defaultVariants: {
variant: 'default',
},
},
)
function Badge({
className,
variant = 'default',
asChild = false,
...props
}: React.ComponentProps<'span'> & VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
const Comp = asChild ? Slot : 'span'
return (
<Comp
data-slot="badge"
data-variant={variant}
className={cn(badgeVariants({ variant }), className)}
{...props}
/>
)
}
/**
* Semantic tone of a status, independent of how it is drawn. `StatusBadge`
* renders it as a coloured dot, `Badge` as a tinted pill — same vocabulary,
* two presentations.
*/
type StatusTone = 'success' | 'warning' | 'destructive' | 'info' | 'neutral'
/**
* Maps an order/payment/product status code to its semantic tone.
*
* Kept as one table because the same codes surface in tables, cards, page
* headers and filter menus — a local copy in any one of those is how two
* surfaces end up disagreeing about what "pending" looks like. Extend it
* rather than branching on status at a call site.
*/
const statusToneMap: Record<string, StatusTone> = {
active: 'success',
// Staff accounts: locked after too many failed sign-ins, deactivated by an admin.
locked: 'warning',
deactivated: 'neutral',
authorized: 'info',
complete: 'success',
completed: 'success',
placed: 'success',
paid: 'success',
fulfilled: 'success',
delivered: 'success',
unfulfilled: 'warning',
proposed: 'warning',
published: 'success',
approved: 'success',
verified: 'success',
unverified: 'destructive',
unavailable: 'neutral',
unsupported: 'neutral',
revoked: 'destructive',
expired: 'destructive',
ready: 'warning',
available: 'success',
// Stock states, as the Inventory page's filter names them: sellable is
// good, nothing to sell is the problem, and the two middle states are units
// that exist but cannot be sold today — held by a checkout, or still on the
// road.
in_stock: 'success',
out_of_stock: 'destructive',
with_reserved: 'warning',
with_incoming: 'info',
draft: 'neutral',
pending: 'warning',
processing: 'warning',
inactive: 'neutral',
confirm: 'neutral',
resumed: 'neutral',
partial: 'info',
// What a carrier reports about a consignment in flight. `pending` and
// `delivered` are already above — they mean the same thing here.
pre_transit: 'info',
in_transit: 'info',
out_for_delivery: 'info',
available_for_pickup: 'warning',
return_to_sender: 'destructive',
failure: 'destructive',
unknown: 'neutral',
archived: 'neutral',
returned: 'success',
backorder: 'warning',
// Order payment status: amber while money is still owed either way, blue
// for a hold or a part refund, red once the authorisation is gone.
none: 'neutral',
partially_paid: 'warning',
partially_refunded: 'info',
overcharged: 'warning',
voided: 'destructive',
balance_due: 'warning',
credit_owed: 'warning',
canceled: 'destructive',
suspended: 'destructive',
invited: 'info',
onboarding: 'warning',
incomplete: 'neutral',
ready_for_review: 'warning',
failed: 'destructive',
void: 'destructive',
error: 'destructive',
rejected: 'destructive',
used_up: 'destructive',
// Returns, exchanges and claims: amber while the merchant still owes an
// action, green once settled, muted when it went nowhere.
requested: 'warning',
open: 'warning',
received: 'warning',
refunded: 'success',
resolved: 'success',
denied: 'destructive',
// Inventory operations. `received` is deliberately absent: on a return it
// means the merchant still owes a refund, on a transfer or purchase order it
// means the job is done — one code, two tones, so those two callers pass
// `tone` explicitly.
ready_to_ship: 'warning',
ordered: 'info',
partially_received: 'info',
// More arrived than was ordered: done, but worth a look.
over_received: 'warning',
// Gift cards.
partially_redeemed: 'info',
redeemed: 'neutral',
// Price lists — `scheduled` is live-but-not-yet, which is the same shape as
// any other "waiting on the clock" state.
scheduled: 'info',
// The rest of the API's own statuses, on the same rule as above.
accepted: 'success',
waived: 'neutral',
withdrawn: 'neutral',
awaiting_confirmation: 'warning',
lapsed: 'neutral',
mapping: 'warning',
completed_mapping: 'info',
unresolved: 'destructive',
on_hand: 'success',
backordered: 'warning',
shipped: 'success',
checkout: 'neutral',
invalid: 'destructive',
purchased: 'success',
refund_requested: 'warning',
// Boolean filter values. A yes/no pair is a state like any other — "in
// stock: no" is the row an operator is looking for, and reading it in the
// same green/red the rest of the app uses saves them parsing the word.
true: 'success',
false: 'destructive',
// Maintenance task runs: amber while a worker holds or is about to release
// the run, blue while it waits for one.
enqueued: 'info',
running: 'warning',
interrupted: 'info',
cancelling: 'warning',
cancelled: 'destructive',
succeeded: 'success',
errored: 'destructive',
} satisfies Record<BuiltInStatus, StatusTone> & Record<string, StatusTone>
// Dot fills, by tone. These read the same `--status-*` ramps the tinted Badge
// variants use, but take the *reading* step rather than the fill step: a 6px
// dot needs the saturated end of the ramp to register at all, where a pill can
// rely on area.
const dotToneClasses: Record<StatusTone, string> = {
success: 'bg-success',
warning: 'bg-warning',
destructive: 'bg-danger',
info: 'bg-info',
neutral: 'bg-muted-foreground/50',
}
/**
* Bare status dot. Decorative by construction — it carries no accessible name,
* so it must always sit beside text that says the same thing. Use it to align
* a status with its label in a menu or list; use `StatusBadge` when you want
* the dot and its label together.
*/
function StatusDot({
status,
tone: toneOverride,
className,
}: {
status: string
tone?: StatusTone
className?: string
}) {
const tone = toneOverride ?? statusToneMap[status] ?? 'neutral'
return (
<span
aria-hidden
className={cn('inline-block size-2 shrink-0 rounded-full', dotToneClasses[tone], className)}
/>
)
}
/**
* Status indicator: a coloured dot beside the label, rather than a tinted pill.
*
* A table column of pills reads as a column of coloured blocks — the eye lands
* on the fills instead of the words, and a row where three statuses sit side by
* side becomes a bar chart of nothing. A dot puts the colour where it belongs:
* a small mark that the label does the talking for. Colour is never the only
* channel, since the label is always rendered.
*
* Stays headless: pass a translated `label` from the app layer; without one it
* humanizes the code itself (`balance_due` → `balance due`) as a best-effort
* fallback.
*
* `tone` overrides the shared map, for the rare code that means different
* things in two domains — a `received` return still owes a refund, a
* `received` stock transfer is finished. Reach for it only then: everything
* else belongs in `statusToneMap`, or two surfaces will disagree.
*/
function StatusBadge({
status,
label,
tone,
className,
}: {
status: string
label?: string
tone?: StatusTone
className?: string
}) {
return (
<span
data-slot="status-badge"
data-status={status}
className={cn(
'inline-flex w-fit shrink-0 items-center gap-1.5 whitespace-nowrap text-sm capitalize',
className,
)}
>
<StatusDot status={status} tone={tone} />
{label ?? status.replace(/_/g, ' ')}
</span>
)
}
/**
* Boolean-state badge — mirrors the Rails admin's `active_badge` helper.
*
* Renders a `success` (check + label) badge when `active`, an `outline`
* badge with the inactive label otherwise. Use it for "Yes/No" or
* "Enabled/Disabled" cells in tables and detail rows.
*
* @example Default Yes/No
* <ActiveBadge active={user.confirmed} />
*
* @example Custom labels
* <ActiveBadge active={sl.pickup_enabled} activeLabel="Enabled" inactiveLabel="Disabled" />
*
* @example Hide inactive (renders a muted dash instead — matches the
* pre-existing `<Badge> : <span>—</span>` pattern in stock-locations).
* <ActiveBadge active={sl.pickup_enabled} activeLabel="Enabled" dashWhenInactive />
*/
function ActiveBadge({
active,
activeLabel,
inactiveLabel,
dashWhenInactive = false,
className,
}: {
active: boolean | null | undefined
activeLabel?: string
inactiveLabel?: string
dashWhenInactive?: boolean
className?: string
}) {
const { t } = useTranslation()
const resolvedActiveLabel = activeLabel ?? t('admin.common.yes')
const resolvedInactiveLabel = inactiveLabel ?? t('admin.common.no')
if (dashWhenInactive && !active) {
return <span className={cn('text-muted-foreground', className)}>—</span>
}
// Boolean states read through the same map as every other status, so an
// "Active / Inactive" column and a "Paid / Failed" one agree on what green
// and red mean. The tick the active case used to carry is redundant beside
// a dot that already says the same thing.
return (
<StatusBadge
status={active ? 'true' : 'false'}
label={active ? resolvedActiveLabel : resolvedInactiveLabel}
className={className}
/>
)
}
export type { StatusTone }
export { ActiveBadge, Badge, badgeVariants, StatusBadge, StatusDot, statusToneMap }