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
Add shared Alert and Thumbnail components to dashboard-ui
Damian Legawiecef8c453959a3
diff --git a/packages/dashboard-core/src/components/resource-picker-sheet.tsx b/packages/dashboard-core/src/components/resource-picker-sheet.tsxindex bf035cdac5..0e9db6dfd9 100644--- a/packages/dashboard-core/src/components/resource-picker-sheet.tsx+++ b/packages/dashboard-core/src/components/resource-picker-sheet.tsx@@ -1,7 +1,4 @@import {- Avatar,- AvatarFallback,- AvatarImage,Button,Checkbox,InputGroup,@@ -13,9 +10,10 @@ import {SheetFooter,SheetHeader,SheetTitle,+ Thumbnail,} from '@spree/dashboard-ui'import { useQuery } from '@tanstack/react-query'-import { ImageIcon, Loader2Icon, SearchIcon, XIcon } from 'lucide-react'+import { Loader2Icon, SearchIcon, XIcon } from 'lucide-react'import { useDeferredValue, useMemo, useState } from 'react'import { useTranslation } from 'react-i18next'@@ -188,16 +186,7 @@ export function ResourcePickerSheet<T extends PickerOption>({disabled={inList}className="pointer-events-none"/>- {getOptionImageUrl && (- <Avatar className="size-9 rounded-md">- {imageUrl ? (- <AvatarImage src={imageUrl} alt="" className="object-cover" />- ) : null}- <AvatarFallback className="rounded-md bg-muted">- <ImageIcon className="size-4 text-muted-foreground" />- </AvatarFallback>- </Avatar>- )}+ {getOptionImageUrl && <Thumbnail src={imageUrl} size="sm" />}<span className="min-w-0 flex-1"><span className="block truncate text-sm">{getOptionLabel(option)}</span>{subtitle && (diff --git a/packages/dashboard-ui/src/index.ts b/packages/dashboard-ui/src/index.tsindex ed3f795051..ee42de3528 100644--- a/packages/dashboard-ui/src/index.ts+++ b/packages/dashboard-ui/src/index.ts@@ -62,6 +62,7 @@ export * from './spree/theme-toggle'// ---------------------------------------------------------------------------// UI primitives (shadcn) — see ./ui/*// ---------------------------------------------------------------------------+export * from './ui/alert'export * from './ui/attachment'export * from './ui/avatar'export * from './ui/badge'@@ -98,4 +99,5 @@ export * from './ui/slot'export * from './ui/sonner'export * from './ui/switch'export * from './ui/textarea'+export * from './ui/thumbnail'export * from './ui/tooltip'diff --git a/packages/dashboard-ui/src/ui/alert.tsx b/packages/dashboard-ui/src/ui/alert.tsxnew file mode 100644index 0000000000..2da6c7d838--- /dev/null+++ b/packages/dashboard-ui/src/ui/alert.tsx@@ -0,0 +1,79 @@+import { cva, type VariantProps } from 'class-variance-authority'+import * as React from 'react'++import { cn } from '../lib/utils'++const alertVariants = cva(+ "group/alert relative grid w-full gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",+ {+ variants: {+ variant: {+ default: 'bg-card text-card-foreground',+ destructive:+ 'bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current',+ // Custom colours, per the shadcn recipe: a tinted fill with matching+ // text rather than the card background, so an advisory note reads as+ // information instead of as a card of its own. Mirrors the `success`+ // recipe in badge.tsx, including the dark-mode text bump for legibility.+ info: 'border-blue-500/20 bg-blue-500/10 text-blue-800 *:data-[slot=alert-description]:text-blue-800/90 dark:text-blue-300 dark:*:data-[slot=alert-description]:text-blue-300/90',+ // Amber rather than the `--color-warning` token, which is yellow-500 —+ // a fill colour too light to read as body text. Amber is what the rest+ // of the dashboard already uses to mark a warning.+ warning:+ 'border-amber-500/20 bg-amber-500/10 text-amber-800 *:data-[slot=alert-description]:text-amber-800/90 dark:text-amber-300 dark:*:data-[slot=alert-description]:text-amber-300/90',+ },+ },+ defaultVariants: {+ variant: 'default',+ },+ },+)++function Alert({+ className,+ variant,+ ...props+}: React.ComponentProps<'div'> & VariantProps<typeof alertVariants>) {+ return (+ <div+ data-slot="alert"+ role="alert"+ className={cn(alertVariants({ variant }), className)}+ {...props}+ />+ )+}++function AlertTitle({ className, ...props }: React.ComponentProps<'div'>) {+ return (+ <div+ data-slot="alert-title"+ className={cn(+ 'font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground',+ className,+ )}+ {...props}+ />+ )+}++function AlertDescription({ className, ...props }: React.ComponentProps<'div'>) {+ return (+ <div+ data-slot="alert-description"+ className={cn(+ 'text-sm text-balance text-muted-foreground md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4',+ className,+ )}+ {...props}+ />+ )+}++function AlertAction({ className, ...props }: React.ComponentProps<'div'>) {+ return (+ <div data-slot="alert-action" className={cn('absolute top-2 right-2', className)} {...props} />+ )+}++export { Alert, AlertAction, AlertDescription, AlertTitle }diff --git a/packages/dashboard-ui/src/ui/thumbnail.tsx b/packages/dashboard-ui/src/ui/thumbnail.tsxnew file mode 100644index 0000000000..cb7bf34c3b--- /dev/null+++ b/packages/dashboard-ui/src/ui/thumbnail.tsx@@ -0,0 +1,81 @@+import { cva, type VariantProps } from 'class-variance-authority'+import { ImageIcon } from 'lucide-react'+import type * as React from 'react'++import { cn } from '../lib/utils'++const thumbnailVariants = cva(+ // The wrapper always renders, so a record without an image keeps the same+ // footprint as one with it and rows never reflow. `shrink-0` matters in the+ // flex rows these sit in — without it a long product name squashes the image.+ 'flex shrink-0 items-center justify-center overflow-hidden border bg-muted text-muted-foreground [&_svg]:pointer-events-none [&_svg:not([class*=size-])]:size-4',+ {+ variants: {+ size: {+ xs: 'size-5 [&_svg:not([class*=size-])]:size-3',+ sm: 'size-8',+ md: 'size-10',+ lg: 'size-12 [&_svg:not([class*=size-])]:size-5',+ },+ shape: {+ rounded: 'rounded-md',+ square: 'rounded-none',+ circle: 'rounded-full',+ },+ },+ defaultVariants: {+ size: 'md',+ shape: 'rounded',+ },+ },+)++export interface ThumbnailProps+ extends Omit<React.ComponentProps<'div'>, 'children'>,+ VariantProps<typeof thumbnailVariants> {+ /** Image to show. When absent or failed, the fallback renders instead. */+ src?: string | null+ /**+ * Describes the image for screen readers. Leave unset when a visible label+ * sits beside the thumbnail — the image is decorative then, and repeating+ * the name makes screen readers announce it twice.+ */+ alt?: string+ /** Overrides the placeholder shown when there is no image. */+ fallback?: React.ReactNode+ /** Defer loading until the thumbnail is near the viewport. */+ loading?: 'eager' | 'lazy'+}++/**+ * A product or record image at a fixed size, falling back to a placeholder.+ *+ * Images are cropped to fill the box (`object-cover`), which suits photography.+ * Logos and artwork that must not be cropped want `Attachment` instead.+ */+export function Thumbnail({+ src,+ alt = '',+ fallback,+ size,+ shape,+ className,+ loading,+ ...props+}: ThumbnailProps) {+ return (+ <div+ data-slot="thumbnail"+ className={cn(thumbnailVariants({ size, shape }), className)}+ {...props}+ >+ {src ? (+ <img src={src} alt={alt} loading={loading} className="size-full object-cover" />+ ) : (+ (fallback ?? <ImageIcon />)+ )}+ </div>+ )+}++export { thumbnailVariants }diff --git a/packages/dashboard/src/components/spree/orders/fulfillment-fulfill-form.tsx b/packages/dashboard/src/components/spree/orders/fulfillment-fulfill-form.tsxindex bfb5af2e60..6679a7aedf 100644--- a/packages/dashboard/src/components/spree/orders/fulfillment-fulfill-form.tsx+++ b/packages/dashboard/src/components/spree/orders/fulfillment-fulfill-form.tsx@@ -1,8 +1,18 @@import { zodResolver } from '@hookform/resolvers/zod'import type { Fulfillment, Order } from '@spree/admin-sdk'import { mapSpreeErrorsToForm } from '@spree/dashboard-core'-import { Button, Checkbox, Field, FieldLabel, Input } from '@spree/dashboard-ui'-import { PackageIcon } from 'lucide-react'+import {+ Alert,+ AlertDescription,+ Button,+ Checkbox,+ cn,+ Field,+ FieldLabel,+ Input,+ Thumbnail,+} from '@spree/dashboard-ui'+import { PackageIcon, TriangleAlertIcon } from 'lucide-react'import { useEffect, useRef } from 'react'import { Controller, type UseFormReturn, useForm } from 'react-hook-form'import { useTranslation } from 'react-i18next'@@ -43,7 +53,7 @@ function ItemQuantityRow({const checkboxId = `${idPrefix}-item-${index}`return (- <div className="flex items-center gap-3 py-3">+ <div className={cn('flex items-center gap-3 p-3', selected && 'bg-accent')}><Controllercontrol={form.control}name={`items.${index}.selected`}@@ -65,26 +75,13 @@ function ItemQuantityRow({)}/>- {row.thumbnailUrl ? (- <img- src={row.thumbnailUrl}- alt={row.name}- className="size-12 shrink-0 rounded-lg border object-cover"- />- ) : (- <div className="flex size-12 shrink-0 items-center justify-center rounded-lg border bg-muted">- <PackageIcon className="size-5 text-muted-foreground" />- </div>- )}+ <Thumbnail src={row.thumbnailUrl} fallback={<PackageIcon />} /><div className="min-w-0 flex-1"><div className="truncate text-sm font-medium">{row.name}</div>{row.optionsText && (<div className="truncate text-xs text-muted-foreground">{row.optionsText}</div>)}- {row.displayPrice && (- <div className="text-xs text-muted-foreground">{row.displayPrice}</div>- )}</div><Controller@@ -211,7 +208,7 @@ export function FulfillmentFulfillForm({}return (- <form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col gap-4">+ <form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col">{errors.root?.message && (<p className="text-sm text-destructive" role="alert">{errors.root.message}@@ -235,7 +232,7 @@ export function FulfillmentFulfillForm({</div>)}- <Field>+ <Field className="p-3 border-t"><FieldLabel htmlFor={`${idPrefix}-tracking`}>{t('admin.orders.fulfill.tracking_label')}</FieldLabel>@@ -252,7 +249,7 @@ export function FulfillmentFulfillForm({render={({ field }) => (<labelhtmlFor={`${idPrefix}-notify`}- className="flex cursor-pointer items-center gap-2 text-sm"+ className="flex cursor-pointer items-center gap-2 text-sm p-3"><Checkboxid={`${idPrefix}-notify`}@@ -265,17 +262,22 @@ export function FulfillmentFulfillForm({/>{!shipsEverything && totalSelected > 0 && (- <p className="text-xs text-muted-foreground">{t('admin.orders.fulfill.partial_hint')}</p>+ <div className="px-3 pb-3">+ <Alert variant="warning">+ <TriangleAlertIcon />+ <AlertDescription>{t('admin.orders.fulfill.partial_hint')}</AlertDescription>+ </Alert>+ </div>)}- <div className="flex items-center justify-end gap-2">+ <div className="flex items-center justify-end gap-2 p-3 border-t"><span className="mr-auto text-sm text-muted-foreground">{t('admin.orders.fulfill.units_selected')}: {totalSelected} / {totalAvailable}</span>- <Button type="button" variant="outline" onClick={onDone}>+ <Button type="button" size="sm" variant="outline" onClick={onDone}>{t('admin.actions.cancel')}</Button>- <Button type="submit" disabled={totalSelected === 0 || fulfill.isPending}>+ <Button type="submit" size="sm" disabled={totalSelected === 0 || fulfill.isPending}>{fulfill.isPending ? t('admin.actions.saving') : t('admin.orders.fulfill.submit')}</Button></div>diff --git a/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx b/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsxindex 1edcd5b683..00fce690fc 100644--- a/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx+++ b/packages/dashboard/src/components/spree/orders/fulfillment-item-list.tsx@@ -1,3 +1,4 @@+import { Thumbnail } from '@spree/dashboard-ui'import { PackageIcon } from 'lucide-react'import type { FulfillmentItemRow } from '../../../lib/fulfillment-items'@@ -8,18 +9,8 @@ import type { FulfillmentItemRow } from '../../../lib/fulfillment-items'*/function ItemRow({ row }: { row: FulfillmentItemRow }) {return (- <div className="flex items-center gap-3 py-3">- {row.thumbnailUrl ? (- <img- src={row.thumbnailUrl}- alt={row.name}- className="size-10 shrink-0 rounded-lg border object-cover"- />- ) : (- <div className="flex size-10 shrink-0 items-center justify-center rounded-lg border bg-muted">- <PackageIcon className="size-4 text-muted-foreground" />- </div>- )}+ <div className="flex items-center gap-3 p-3">+ <Thumbnail src={row.thumbnailUrl} fallback={<PackageIcon />} /><div className="min-w-0 flex-1"><div className="truncate text-sm font-medium">{row.name}</div>diff --git a/packages/dashboard/src/components/spree/product-membership-card.tsx b/packages/dashboard/src/components/spree/product-membership-card.tsxindex 3e5d156828..a03e9f24fc 100644--- a/packages/dashboard/src/components/spree/product-membership-card.tsx+++ b/packages/dashboard/src/components/spree/product-membership-card.tsx@@ -37,10 +37,11 @@ import {TableHead,TableHeader,TableRow,+ Thumbnail,useConfirm,} from '@spree/dashboard-ui'import { Link } from '@tanstack/react-router'-import { ImageIcon, PlusIcon, SearchIcon, Trash2Icon, XIcon } from 'lucide-react'+import { PlusIcon, SearchIcon, Trash2Icon, XIcon } from 'lucide-react'import { useEffect, useMemo, useState } from 'react'import { useTranslation } from 'react-i18next'@@ -415,13 +416,7 @@ function ProductRow({params={{ storeId, productId: product.id }}className="flex items-center gap-3 hover:underline">- <span className="flex size-9 shrink-0 items-center justify-center overflow-hidden rounded border border-border bg-muted">- {product.thumbnail_url ? (- <img src={product.thumbnail_url} alt="" className="size-full object-cover" />- ) : (- <ImageIcon className="size-4 text-muted-foreground" />- )}- </span>+ <Thumbnail src={product.thumbnail_url} size="sm" /><span className="truncate text-sm">{product.name}</span></Link></TableCell>diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/customers/$customerId.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/customers/$customerId.tsxindex c32b4af753..f0de6017a0 100644--- a/packages/dashboard/src/routes/_authenticated/$storeId/customers/$customerId.tsx+++ b/packages/dashboard/src/routes/_authenticated/$storeId/customers/$customerId.tsx@@ -56,6 +56,7 @@ import {SheetTitle,StatusBadge,Textarea,+ Thumbnail,useConfirm,} from '@spree/dashboard-ui'import { createFileRoute, Link, useNavigate } from '@tanstack/react-router'@@ -650,9 +651,7 @@ function LastOrderCard({ order }: { order: Order }) {</div>{order.items?.slice(0, 5).map((item) => (<div key={item.id} className="border-t flex items-center gap-3 px-6 py-3 text-sm">- {item.thumbnail_url && (- <img src={item.thumbnail_url} alt="" className="size-10 rounded object-cover" />- )}+ <Thumbnail src={item.thumbnail_url} /><div className="flex-1 min-w-0"><div className="truncate">{item.name}</div>{item.options_text && (diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/index.tsxindex 7a4b9c2d06..067b3818d9 100644--- a/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx+++ b/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx@@ -13,6 +13,7 @@ import {type DateRange,DateRangePicker,Skeleton,+ Thumbnail,} from '@spree/dashboard-ui'import { useQuery } from '@tanstack/react-query'import { createFileRoute, Link } from '@tanstack/react-router'@@ -188,17 +189,7 @@ function TopProducts({ products }: { products: DashboardAnalytics['top_products'})}className="flex items-center gap-3 hover:underline">- {product.image_url ? (- <img- src={product.image_url}- alt={product.name}- className="size-10 rounded-md border object-cover"- />- ) : (- <div className="flex size-10 items-center justify-center rounded-md border bg-muted">- <PackageIcon className="size-4 text-muted-foreground" />- </div>- )}+ <Thumbnail src={product.image_url} fallback={<PackageIcon />} /><span className="font-medium">{product.name}</span></Link></td>diff --git a/packages/dashboard/src/search/default.tsx b/packages/dashboard/src/search/default.tsxindex 995d5b7e63..5d0edfda67 100644--- a/packages/dashboard/src/search/default.tsx+++ b/packages/dashboard/src/search/default.tsx@@ -152,7 +152,13 @@ searchRegistry.add(}),)-/** A result row's thumbnail, falling back to the resource's own icon. */+/**+ * A result row's thumbnail, falling back to the resource's own icon.+ *+ * Deliberately not `Thumbnail`: search results without an image show a bare+ * glyph rather than an empty framed box, so a category or a page does not look+ * like a product whose photo failed to load.+ */function IconOrThumbnail({ thumbnailUrl, icon }: { thumbnailUrl: string | null; icon: ReactNode }) {if (!thumbnailUrl) return iconreturn (diff --git a/packages/dashboard/src/tables/products.tsx b/packages/dashboard/src/tables/products.tsxindex 50a1757961..2c5ddf586a 100644--- a/packages/dashboard/src/tables/products.tsx+++ b/packages/dashboard/src/tables/products.tsx@@ -1,6 +1,6 @@import type { Channel } from '@spree/admin-sdk'import { defineTable, formatPrice, Subject } from '@spree/dashboard-core'-import { StatusBadge, TagList } from '@spree/dashboard-ui'+import { StatusBadge, TagList, Thumbnail } from '@spree/dashboard-ui'import { Link } from '@tanstack/react-router'import i18n from 'i18next'import { PackageIcon } from 'lucide-react'@@ -28,17 +28,7 @@ defineTable('products', {params={{ productId: product.id }}className="flex items-center gap-3 no-underline">- <div className="flex size-10 shrink-0 items-center justify-center rounded-lg border border-border bg-muted overflow-hidden">- {product.thumbnail_url ? (- <img- src={product.thumbnail_url}- alt={product.name}- className="size-full object-cover"- />- ) : (- <PackageIcon className="size-4 text-muted-foreground" />- )}- </div>+ <Thumbnail src={product.thumbnail_url} fallback={<PackageIcon />} /><div className="min-w-0"><div className="truncate font-medium text-foreground">{product.name}</div></div>