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
Use shadcn Base UI Progress and Attachment in the import flow
Damian Legawiec93ae8ba0372b
diff --git a/docs/plans/5.6-admin-spa-csv-import.md b/docs/plans/5.6-admin-spa-csv-import.mdindex 938aa74795..52cb581e37 100644--- a/docs/plans/5.6-admin-spa-csv-import.md+++ b/docs/plans/5.6-admin-spa-csv-import.md@@ -311,7 +311,7 @@ readonly imports = {**`ImportButton` (`packages/dashboard-core/src/components/import-button.tsx`)** — sits beside `export-button.tsx`, same toolbar slot. Props: `{ type: ImportType; subject: SubjectName; onCreated: (imp: AdminImport) => void }` — dashboard-core must not know app routes or dialogs, so what happens after create is the app's callback (the index pages set the `?import=` search param, which opens the wizard dialog). The button wraps itself in `<Can I="create" a={subject}>` (per-context gating — see Key Decisions) and opens a **Sheet** containing:-- CSV dropzone → `useDirectUpload`. **Force `content_type: 'text/csv'` in the presign for `.csv` files** — Windows browsers report `.csv` as `application/vnd.ms-excel` and the model validation rejects it.+- CSV dropzone → `useDirectUpload`. **Force `content_type: 'text/csv'` in the presign for `.csv` files** — Windows browsers report `.csv` as `application/vnd.ms-excel` and the model validation rejects it. The picked file renders as an `Attachment` (name, size, uploading state, remove) — `Attachment` and `Progress` were ported into `dashboard-ui` from the shadcn **Base UI** registry for this feature (`cn-*` preset classes flattened to plain utilities, matching the package's pre-CSS-layer style); the wizard's processing bar is the `Progress` primitive with `value={null}` as the indeterminate "preparing" state. Migrating `ImageUploadField` to `Attachment` is a separate follow-up — it's a shipped shared component with its own controlled contract.- Delimiter select (`,` `;` `|` tab — the model's `preferred_delimiter` preference).- "Download template" link → authed fetch of `GET /imports/template?type=…`, Blob-URL download (same helper approach as `use-export.ts`).- Submit → `imports.create({ type, attachment: signedId, preferred_delimiter })` → `onCreated`.diff --git a/packages/dashboard-core/src/components/import-button.tsx b/packages/dashboard-core/src/components/import-button.tsxindex c2e867912e..b9121067fc 100644--- a/packages/dashboard-core/src/components/import-button.tsx+++ b/packages/dashboard-core/src/components/import-button.tsx@@ -1,7 +1,13 @@import type { Import, ImportType } from '@spree/admin-sdk'import {+ Attachment,+ AttachmentAction,+ AttachmentActions,+ AttachmentContent,+ AttachmentDescription,+ AttachmentMedia,+ AttachmentTitle,Button,- cn,Field,FieldLabel,Select,@@ -16,7 +22,7 @@ import {SheetHeader,SheetTitle,} from '@spree/dashboard-ui'-import { DownloadIcon, FileSpreadsheetIcon, UploadIcon } from 'lucide-react'+import { DownloadIcon, FileSpreadsheetIcon, UploadIcon, XIcon } from 'lucide-react'import { useState } from 'react'import { useTranslation } from 'react-i18next'import { toast } from 'sonner'@@ -33,6 +39,12 @@ const DELIMITERS = [type Delimiter = (typeof DELIMITERS)[number]['value']+function formatFileSize(bytes: number): string {+ if (bytes < 1024) return `${bytes} B`+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`+}+interface ImportButtonProps {/** Which dataset to import. Server validates against `Spree::Import.available_types`. */type: ImportType@@ -132,10 +144,7 @@ export function ImportButton({ type, subject, onCreated, label }: ImportButtonPr{/* A <label> wrapping the file input: clicking anywhere opens thepicker natively, no JS forwarding needed. */}<label- className={cn(- 'flex cursor-pointer flex-col items-center justify-center gap-2 rounded-md border border-border border-dashed bg-muted/40 px-4 py-10 text-center transition-colors hover:bg-muted',- file && 'border-solid',- )}+ className="flex cursor-pointer flex-col items-center justify-center gap-2 rounded-md border border-border border-dashed bg-muted/40 px-4 py-10 text-center transition-colors hover:bg-muted"onDragOver={(e) => e.preventDefault()}onDrop={(e) => {e.preventDefault()@@ -144,13 +153,9 @@ export function ImportButton({ type, subject, onCreated, label }: ImportButtonPr}}><FileSpreadsheetIcon className="size-6 text-muted-foreground" />- {file ? (- <span className="font-medium text-sm">{file.name}</span>- ) : (- <span className="text-muted-foreground text-sm">- {t('admin.components.import_button.drop_label')}- </span>- )}+ <span className="text-muted-foreground text-sm">+ {t('admin.components.import_button.drop_label')}+ </span><span className="mt-1 inline-flex h-8 items-center rounded-md border border-border bg-background px-3 font-medium text-sm shadow-xs">{t('admin.components.import_button.browse')}</span>@@ -166,6 +171,31 @@ export function ImportButton({ type, subject, onCreated, label }: ImportButtonPr/></label>+ {file && (+ <Attachment className="w-full" state={createImport.isPending ? 'uploading' : 'done'}>+ <AttachmentMedia>+ <FileSpreadsheetIcon />+ </AttachmentMedia>+ <AttachmentContent>+ <AttachmentTitle>{file.name}</AttachmentTitle>+ <AttachmentDescription>+ {createImport.isPending+ ? t('admin.components.import_button.uploading')+ : formatFileSize(file.size)}+ </AttachmentDescription>+ </AttachmentContent>+ <AttachmentActions>+ <AttachmentAction+ aria-label={t('admin.actions.remove')}+ onClick={() => setFile(null)}+ disabled={createImport.isPending}+ >+ <XIcon />+ </AttachmentAction>+ </AttachmentActions>+ </Attachment>+ )}+<Field><FieldLabel>{t('admin.components.import_button.delimiter_label')}</FieldLabel><Selectdiff --git a/packages/dashboard-ui/src/index.ts b/packages/dashboard-ui/src/index.tsindex 117d42a19f..b0181677b7 100644--- a/packages/dashboard-ui/src/index.ts+++ b/packages/dashboard-ui/src/index.ts@@ -60,6 +60,7 @@ export * from './spree/theme-toggle'// ---------------------------------------------------------------------------// UI primitives (shadcn) — see ./ui/*// ---------------------------------------------------------------------------+export * from './ui/attachment'export * from './ui/avatar'export * from './ui/badge'export * from './ui/breadcrumb'@@ -83,6 +84,7 @@ export * from './ui/input-group'export * from './ui/label'export * from './ui/pagination'export * from './ui/popover'+export * from './ui/progress'export * from './ui/radio-group'export * from './ui/rich-text-editor'export * from './ui/select'diff --git a/packages/dashboard-ui/src/spree/row-actions.tsx b/packages/dashboard-ui/src/spree/row-actions.tsxindex 09d97a6d75..bc74af1ae9 100644--- a/packages/dashboard-ui/src/spree/row-actions.tsx+++ b/packages/dashboard-ui/src/spree/row-actions.tsx@@ -1,4 +1,4 @@-import { CopyIcon, EllipsisVerticalIcon, PencilIcon, Trash2Icon } from 'lucide-react'+import { CopyIcon, EllipsisVerticalIcon, EyeIcon, PencilIcon, Trash2Icon } from 'lucide-react'import type { ReactNode } from 'react'import { useTranslation } from 'react-i18next'import { Button } from '../ui/button'@@ -38,6 +38,7 @@ const BUILTIN_ICONS: Record<string, ReactNode> = {edit: <PencilIcon className="size-4" />,clone: <CopyIcon className="size-4" />,delete: <Trash2Icon className="size-4" />,+ view: <EyeIcon className="size-4" />,}/**diff --git a/packages/dashboard-ui/src/styles.css b/packages/dashboard-ui/src/styles.cssindex e90c41724e..5b3e843794 100644--- a/packages/dashboard-ui/src/styles.css+++ b/packages/dashboard-ui/src/styles.css@@ -161,10 +161,10 @@--color-background: var(--background);/* Spree status colors — for badges & alerts */- --color-success: var(--color-green-900);- --color-warning: var(--color-yellow-900);- --color-info: var(--color-blue-900);- --color-danger: var(--color-red-600);+ --color-success: var(--color-green-500);+ --color-warning: var(--color-yellow-500);+ --color-info: var(--color-blue-500);+ --color-danger: var(--color-red-500);}@utility focus-ring {diff --git a/packages/dashboard-ui/src/ui/attachment.tsx b/packages/dashboard-ui/src/ui/attachment.tsxnew file mode 100644index 0000000000..a024919c8e--- /dev/null+++ b/packages/dashboard-ui/src/ui/attachment.tsx@@ -0,0 +1,185 @@+'use client'++import { mergeProps } from '@base-ui/react/merge-props'+import { useRender } from '@base-ui/react/use-render'+import { cva, type VariantProps } from 'class-variance-authority'+import type * as React from 'react'++import { cn } from '../lib/utils'+import { Button } from './button'++// Ported from the shadcn Base UI `attachment` registry component, with the+// `cn-*` preset classes flattened into plain utilities (this package predates+// the shadcn CSS layer).+const attachmentVariants = cva(+ 'group/attachment relative flex max-w-full min-w-0 shrink-0 flex-wrap rounded-lg border bg-card text-card-foreground transition-colors has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed',+ {+ variants: {+ size: {+ default: 'gap-2.5 p-2 pr-2.5',+ sm: 'gap-2 p-1.5 pr-2',+ xs: 'gap-1.5 p-1 pr-1.5',+ },+ orientation: {+ horizontal: 'items-center',+ vertical: 'flex-col',+ },+ },+ },+)++function Attachment({+ className,+ state = 'done',+ size = 'default',+ orientation = 'horizontal',+ ...props+}: React.ComponentProps<'div'> &+ VariantProps<typeof attachmentVariants> & {+ state?: 'idle' | 'uploading' | 'processing' | 'error' | 'done'+ }) {+ return (+ <div+ data-slot="attachment"+ data-state={state}+ data-size={size}+ data-orientation={orientation}+ className={cn(attachmentVariants({ size, orientation }), className)}+ {...props}+ />+ )+}++const attachmentMediaVariants = cva(+ 'relative flex aspect-square size-9 shrink-0 items-center justify-center overflow-hidden rounded-md group-data-[state=error]/attachment:bg-destructive/10 group-data-[state=error]/attachment:text-destructive [&_svg]:pointer-events-none [&_svg:not([class*=size-])]:size-4',+ {+ variants: {+ variant: {+ icon: 'bg-muted text-muted-foreground',+ image: '*:[img]:aspect-square *:[img]:w-full *:[img]:object-cover',+ },+ },+ defaultVariants: {+ variant: 'icon',+ },+ },+)++function AttachmentMedia({+ className,+ variant = 'icon',+ ...props+}: React.ComponentProps<'div'> & VariantProps<typeof attachmentMediaVariants>) {+ return (+ <div+ data-slot="attachment-media"+ data-variant={variant}+ className={cn(attachmentMediaVariants({ variant }), className)}+ {...props}+ />+ )+}++function AttachmentContent({ className, ...props }: React.ComponentProps<'div'>) {+ return (+ <div+ data-slot="attachment-content"+ className={cn('max-w-full min-w-0 flex-1', className)}+ {...props}+ />+ )+}++function AttachmentTitle({ className, ...props }: React.ComponentProps<'span'>) {+ return (+ <span+ data-slot="attachment-title"+ className={cn('block max-w-full min-w-0 truncate font-medium text-sm', className)}+ {...props}+ />+ )+}++function AttachmentDescription({ className, ...props }: React.ComponentProps<'span'>) {+ return (+ <span+ data-slot="attachment-description"+ className={cn(+ 'block max-w-full min-w-0 truncate text-muted-foreground text-xs group-data-[state=error]/attachment:text-destructive/80',+ className,+ )}+ {...props}+ />+ )+}++function AttachmentActions({ className, ...props }: React.ComponentProps<'div'>) {+ return (+ <div+ data-slot="attachment-actions"+ className={cn('flex shrink-0 items-center gap-1', className)}+ {...props}+ />+ )+}++function AttachmentAction({+ className,+ variant,+ size = 'icon-xs',+ ...props+}: React.ComponentProps<typeof Button>) {+ return (+ <Button+ data-slot="attachment-action"+ variant={variant ?? 'ghost'}+ size={size}+ className={className}+ {...props}+ />+ )+}++function AttachmentTrigger({+ className,+ render,+ type,+ ...props+}: useRender.ComponentProps<'button'>) {+ return useRender({+ defaultTagName: 'button',+ props: mergeProps<'button'>(+ {+ type: render ? type : (type ?? 'button'),+ className: cn('absolute inset-0 z-10 outline-none', className),+ },+ props,+ ),+ render,+ state: {+ slot: 'attachment-trigger',+ },+ })+}++function AttachmentGroup({ className, ...props }: React.ComponentProps<'div'>) {+ return (+ <div+ data-slot="attachment-group"+ className={cn('flex min-w-0 gap-2 overflow-x-auto', className)}+ {...props}+ />+ )+}++export {+ Attachment,+ AttachmentAction,+ AttachmentActions,+ AttachmentContent,+ AttachmentDescription,+ AttachmentGroup,+ AttachmentMedia,+ AttachmentTitle,+ AttachmentTrigger,+}diff --git a/packages/dashboard-ui/src/ui/progress.tsx b/packages/dashboard-ui/src/ui/progress.tsxnew file mode 100644index 0000000000..c6732fd515--- /dev/null+++ b/packages/dashboard-ui/src/ui/progress.tsx@@ -0,0 +1,73 @@+'use client'++import { Progress as ProgressPrimitive } from '@base-ui/react/progress'++import { cn } from '../lib/utils'++/**+ * Base UI progress bar. `value={null}` renders the indeterminate state+ * (Base UI stamps `data-indeterminate` on every part). Compose+ * `ProgressLabel`/`ProgressValue` as children when a caption is needed —+ * the track always renders last.+ */+function Progress({ className, children, value, ...props }: ProgressPrimitive.Root.Props) {+ return (+ <ProgressPrimitive.Root+ value={value}+ data-slot="progress"+ className={cn('flex w-full flex-wrap items-center gap-2', className)}+ {...props}+ >+ {children}+ <ProgressTrack>+ <ProgressIndicator />+ </ProgressTrack>+ </ProgressPrimitive.Root>+ )+}++function ProgressTrack({ className, ...props }: ProgressPrimitive.Track.Props) {+ return (+ <ProgressPrimitive.Track+ data-slot="progress-track"+ className={cn('relative h-2 w-full overflow-hidden rounded-full bg-primary/20', className)}+ {...props}+ />+ )+}++function ProgressIndicator({ className, ...props }: ProgressPrimitive.Indicator.Props) {+ return (+ <ProgressPrimitive.Indicator+ data-slot="progress-indicator"+ className={cn(+ 'h-full rounded-full bg-primary transition-all',+ 'data-[indeterminate]:w-1/3 data-[indeterminate]:animate-pulse',+ className,+ )}+ {...props}+ />+ )+}++function ProgressLabel({ className, ...props }: ProgressPrimitive.Label.Props) {+ return (+ <ProgressPrimitive.Label+ data-slot="progress-label"+ className={cn('font-medium text-sm', className)}+ {...props}+ />+ )+}++function ProgressValue({ className, ...props }: ProgressPrimitive.Value.Props) {+ return (+ <ProgressPrimitive.Value+ data-slot="progress-value"+ className={cn('ml-auto text-muted-foreground text-sm', className)}+ {...props}+ />+ )+}++export { Progress, ProgressIndicator, ProgressLabel, ProgressTrack, ProgressValue }diff --git a/packages/dashboard/src/components/spree/imports/import-wizard-dialog.tsx b/packages/dashboard/src/components/spree/imports/import-wizard-dialog.tsxindex ab12b16bb7..27c849e411 100644--- a/packages/dashboard/src/components/spree/imports/import-wizard-dialog.tsx+++ b/packages/dashboard/src/components/spree/imports/import-wizard-dialog.tsx@@ -12,6 +12,7 @@ import {DialogContent,DialogHeader,DialogTitle,+ Progress,Select,SelectContent,SelectItem,@@ -106,7 +107,7 @@ function ImportWizard({ importId, onClose }: { importId: string; onClose: () =></DialogHeader><DialogBody className="min-h-0 flex-1 overflow-y-auto p-4">- <div className="mx-auto flex w-full max-w-4xl flex-col gap-4">+ <div className="mx-auto flex w-full flex-col gap-4">{isLoading || !imp ? (<><Skeleton className="h-6 w-64" />@@ -329,16 +330,8 @@ function ProcessingCard({ imp }: { imp: Import }) {<CardTitle>{t('admin.imports.processing.title')}</CardTitle></CardHeader><CardContent className="flex flex-col gap-3">- <div className="h-2 w-full overflow-hidden rounded-full bg-muted">- {preparing ? (- <div className="h-full w-1/3 animate-pulse rounded-full bg-primary" />- ) : (- <div- className="h-full rounded-full bg-primary transition-all"- style={{ width: `${percent}%` }}- />- )}- </div>+ {/* value={null} = Base UI's indeterminate state while rows are created */}+ <Progress value={preparing ? null : percent} /><p className="text-sm">{preparing ? (@@ -456,10 +449,10 @@ function FailedRowsCard({ imp }: { imp: Import }) {<CardHeader><CardTitle>{t('admin.imports.failed_rows.title')}</CardTitle></CardHeader>- <CardContent className="flex flex-col gap-3">+ <CardContent className="flex flex-col gap-3 p-0"><div className="overflow-x-auto"><Table>- <TableHeader>+ <TableHeader className="border-b"><TableRow><TableHead className="w-16">{t('admin.imports.failed_rows.row_number')}</TableHead><TableHead>{t('admin.imports.failed_rows.error')}</TableHead>