@vendure-io/ui 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vendure-io/ui",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "React component library for Vendure, built on shadcn/ui and Tailwind v4",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "homepage": "https://github.com/vendurehq/design/tree/main/packages/ui",
@@ -3,8 +3,13 @@ import { cva, type VariantProps } from "class-variance-authority"
3
3
 
4
4
  import { cn } from "@vendure-io/ui/lib/utils"
5
5
 
6
+ // Alert is an inline-size container so AlertAction can move under the text when
7
+ // the alert is narrow. A container cannot size to its content: an Alert inside a
8
+ // flex row needs a width (it is w-full by default), not w-fit.
9
+ // The grid has no column gap: the icon carries its own margin, so the empty
10
+ // action column of a narrow alert takes no space.
6
11
  const alertVariants = cva(
7
- "grid gap-0.5 rounded-lg border px-4 py-3 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.5 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4 w-full relative group/alert",
12
+ "@container/alert grid grid-cols-[minmax(0,1fr)] gap-y-0.5 rounded-lg border px-4 py-3 text-left text-sm has-[>svg]:grid-cols-[auto_minmax(0,1fr)] has-[>[data-slot=alert-action]]:grid-cols-[minmax(0,1fr)_auto] has-[>svg]:has-[>[data-slot=alert-action]]:grid-cols-[auto_minmax(0,1fr)_auto] *:[svg]:row-span-2 *:[svg]:me-2.5 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4 w-full relative group/alert",
8
13
  {
9
14
  variants: {
10
15
  variant: {
@@ -43,7 +48,7 @@ function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
43
48
  <div
44
49
  data-slot="alert-title"
45
50
  className={cn(
46
- "[&_a]:hover:text-foreground font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3",
51
+ "[&_a]:hover:text-foreground col-start-1 font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3",
47
52
  className
48
53
  )}
49
54
  {...props}
@@ -59,7 +64,7 @@ function AlertDescription({
59
64
  <div
60
65
  data-slot="alert-description"
61
66
  className={cn(
62
- "text-muted-foreground [&_a]:hover:text-foreground text-sm text-balance md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_p:not(:last-child)]:mb-4",
67
+ "text-muted-foreground [&_a]:hover:text-foreground col-start-1 text-sm text-balance wrap-anywhere group-has-[>svg]/alert:col-start-2 md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_p:not(:last-child)]:mb-4",
63
68
  className
64
69
  )}
65
70
  {...props}
@@ -67,11 +72,19 @@ function AlertDescription({
67
72
  )
68
73
  }
69
74
 
75
+ // From a 28rem-wide alert up, the action takes the last grid column on the first
76
+ // row, so the text wraps before it. The action box is one text line high and
77
+ // centres its content, so a button lines up with the first line of the text and
78
+ // overflows into the alert padding instead of making the alert taller. In a
79
+ // narrower alert the action moves to its own row under the text.
70
80
  function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
71
81
  return (
72
82
  <div
73
83
  data-slot="alert-action"
74
- className={cn("absolute top-2.5 right-3", className)}
84
+ className={cn(
85
+ "col-end-[-1] flex flex-wrap content-center items-center gap-2 @max-md/alert:col-start-1 @max-md/alert:mt-2 @max-md/alert:group-has-[>svg]/alert:col-start-2 @md/alert:row-start-1 @md/alert:ms-3 @md/alert:h-lh @md/alert:self-start",
86
+ className
87
+ )}
75
88
  {...props}
76
89
  />
77
90
  )
@@ -169,6 +169,23 @@ export interface DataTableProps<TData> {
169
169
 
170
170
  isLoading?: boolean; // skeleton rows only while `rows` is empty; never blanks populated rows
171
171
  skeletonRowCount?: number; // default 10
172
+ /**
173
+ * Per-column loading cells, keyed by column id, for cells that are not one
174
+ * text line tall (a title over a muted sub-line, an avatar, a button), so the
175
+ * loading rows keep the loaded row height and the table does not jump when
176
+ * data arrives. Stack `DataTableSkeletonLine`s for multi-line cells.
177
+ *
178
+ * Keys are TanStack column ids: the column's `id`, else its `accessorKey`
179
+ * with `.` replaced by `_` (`customer.name` → `customer_name`). Unknown keys
180
+ * warn in development.
181
+ *
182
+ * A missing or `undefined` entry renders the default: one
183
+ * `DataTableSkeletonLine`, as tall as one text line, so one-line columns
184
+ * (text, Money) need no entry. `null` renders an empty cell. The
185
+ * generated actions cell (key `actions`) defaults to an icon-button-sized
186
+ * block and can be overridden; the generated select cell is always empty.
187
+ */
188
+ skeletonCells?: Record<string, React.ReactNode>;
172
189
 
173
190
  // capability configs — omit → capability + its UI vanish
174
191
  pagination?: DataTablePagination;
@@ -61,6 +61,29 @@ import * as React from 'react';
61
61
  // Relative: `data-table-helpers` is closed in the package exports.
62
62
  import { buildDisplayColumns, getSelectedRowIds, resolveSlot } from './data-table-helpers.tsx';
63
63
 
64
+ // Id of the generated row-actions column. Public: it is the `skeletonCells` key
65
+ // for the actions cell.
66
+ const ACTIONS_COLUMN_ID = 'actions';
67
+
68
+ /**
69
+ * One text line of loading placeholder: a box exactly one line box tall (`1lh`
70
+ * of the inherited font) with a bar centred in it, so a skeleton line is as tall
71
+ * as the text line it stands in for. Size the line with a text utility
72
+ * (`text-xs` for a muted sub-line) and its width with `w-*`. DataTable uses it as
73
+ * the default loading cell; stack several for multi-line `skeletonCells`.
74
+ */
75
+ function DataTableSkeletonLine({ className, ...props }: React.ComponentProps<'div'>) {
76
+ return (
77
+ <div
78
+ data-slot="data-table-skeleton-line"
79
+ className={cn('flex h-[1lh] w-full items-center', className)}
80
+ {...props}
81
+ >
82
+ <Skeleton className="h-[0.8lh] w-full" />
83
+ </div>
84
+ );
85
+ }
86
+
64
87
  // The composition root. Owns the single `useReactTable` instance and the
65
88
  // controlled/uncontrolled bridge, then lays out the card-framed table anatomy:
66
89
  // a CardHeader band (header → controls → chips → bulk overlay), a CardTable
@@ -126,6 +149,7 @@ function DataTable<TData>({
126
149
  getRowId,
127
150
  isLoading,
128
151
  skeletonRowCount = 10,
152
+ skeletonCells,
129
153
  pagination,
130
154
  sorting,
131
155
  filters,
@@ -235,7 +259,7 @@ function DataTable<TData>({
235
259
  // biome-ignore lint/suspicious/noExplicitAny: matches TanStack's ColumnDef value-type convention
236
260
  const actionsColumn: ColumnDef<TData, any> | undefined = rowActions
237
261
  ? {
238
- id: 'actions',
262
+ id: ACTIONS_COLUMN_ID,
239
263
  enableSorting: false,
240
264
  enableHiding: false,
241
265
  header: () => <span className="sr-only">{l.rowActions}</span>,
@@ -332,20 +356,65 @@ function DataTable<TData>({
332
356
  rowSelection != null
333
357
  ? table.getVisibleLeafColumns().find((column) => column.id !== selectColumnId)?.id
334
358
  : undefined;
359
+ // Which generated display column an id belongs to, if any. Consumer columns
360
+ // can't collide: buildDisplayColumns only injects these when configured.
361
+ const generatedColumn = (id: string): 'select' | 'actions' | undefined => {
362
+ if (rowSelection != null && id === selectColumnId) return 'select';
363
+ if (rowActions != null && id === ACTIONS_COLUMN_ID) return 'actions';
364
+ return undefined;
365
+ };
335
366
  const columnClass = (id: string): string | undefined => {
367
+ const generated = generatedColumn(id);
336
368
  // `pl-0!`: the select cell is the first child, so CardTable's edge-cell
337
369
  // padding would otherwise widen the zero-width cell and push the whole
338
370
  // gutter arrangement out; important because CardTable's descendant rule
339
371
  // out-specifies a plain utility on the cell.
340
- if (id === selectColumnId) return 'relative w-0 p-0 pl-0!';
372
+ if (generated === 'select') return 'relative w-0 p-0 pl-0!';
341
373
  if (id === leadingColumnId) return 'pl-8';
342
374
  // Actions cells drop vertical padding so a row-action button (taller than a
343
375
  // text line) sits inside the natural row height rather than inflating every
344
376
  // row; `align-middle` on the cell keeps it centred.
345
- if (id === 'actions') return 'py-0';
377
+ if (generated === 'actions') return 'py-0';
346
378
  return undefined;
347
379
  };
348
380
 
381
+ // Loading cells follow the real column layout (same ids, same cell classes),
382
+ // so a consumer's `skeletonCells` entry can match its loaded cell's height.
383
+ const skeletonCell = (id: string): React.ReactNode => {
384
+ const generated = generatedColumn(id);
385
+ // The checkbox is hidden at rest and the cell is zero-width, so there is
386
+ // nothing to stand in for — and no room for an override.
387
+ if (generated === 'select') return null;
388
+ // `null` is a deliberate empty cell; only a missing/undefined entry falls back.
389
+ const custom = skeletonCells?.[id];
390
+ if (custom !== undefined) return custom;
391
+ if (generated === 'actions') return <Skeleton className="size-8" />;
392
+ return <DataTableSkeletonLine />;
393
+ };
394
+
395
+ const warnedUnknownSkeletonCells = React.useRef(false);
396
+ if (
397
+ process.env.NODE_ENV !== 'production' &&
398
+ skeletonCells != null &&
399
+ !warnedUnknownSkeletonCells.current
400
+ ) {
401
+ const knownIds = new Set(
402
+ table
403
+ .getAllLeafColumns()
404
+ .map((column) => column.id)
405
+ .filter((id) => generatedColumn(id) !== 'select'),
406
+ );
407
+ const unknownKeys = Object.keys(skeletonCells).filter((key) => !knownIds.has(key));
408
+ if (unknownKeys.length > 0) {
409
+ warnedUnknownSkeletonCells.current = true;
410
+ console.warn(
411
+ `[DataTable] skeletonCells keys match no column: ${unknownKeys.join(', ')}. ` +
412
+ "Keys are column ids: `id`, else `accessorKey` with '.' replaced by '_' " +
413
+ "(`customer.name` → `customer_name`). The select column can't be overridden.",
414
+ );
415
+ }
416
+ }
417
+
349
418
  const toolbarNode = toolbar != null ? resolveSlot(toolbar, table) : undefined;
350
419
  const showViewOptions = columnVisibility != null && columnVisibility.showViewOptions !== false;
351
420
  const hasFilterMenu = Boolean(
@@ -461,11 +530,17 @@ function DataTable<TData>({
461
530
  {isLoading && rows.length === 0 ? (
462
531
  Array.from({ length: skeletonRowCount }).map((_, rowIndex) => (
463
532
  <TableRow key={`skeleton-${rowIndex}`}>
464
- {Array.from({ length: Math.max(columnCount, 1) }).map((__, cellIndex) => (
465
- <TableCell key={`skeleton-cell-${cellIndex}`}>
466
- <Skeleton className="h-4 w-full" />
533
+ {columnCount === 0 ? (
534
+ <TableCell>
535
+ <DataTableSkeletonLine />
467
536
  </TableCell>
468
- ))}
537
+ ) : (
538
+ table.getVisibleLeafColumns().map((column) => (
539
+ <TableCell key={column.id} className={columnClass(column.id)}>
540
+ {skeletonCell(column.id)}
541
+ </TableCell>
542
+ ))
543
+ )}
469
544
  </TableRow>
470
545
  ))
471
546
  ) : bodyRows.length === 0 ? (
@@ -569,7 +644,7 @@ function DataTable<TData>({
569
644
  );
570
645
  }
571
646
 
572
- export { DataTable };
647
+ export { DataTable, DataTableSkeletonLine };
573
648
 
574
649
  // Re-export the consumer-facing TanStack types so columns can be authored from
575
650
  // the same import as `DataTable` — no direct `@tanstack/react-table` dependency