@vendure-io/ui 3.0.0-next.4 → 3.0.0-next.6

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.
Files changed (89) hide show
  1. package/package.json +12 -11
  2. package/src/components/atoms/alert-dialog.tsx +1 -4
  3. package/src/components/atoms/alert.tsx +59 -35
  4. package/src/components/atoms/badge.tsx +2 -2
  5. package/src/components/atoms/breadcrumb.tsx +154 -59
  6. package/src/components/atoms/button-group.tsx +1 -1
  7. package/src/components/atoms/button.tsx +2 -2
  8. package/src/components/atoms/calendar.tsx +2 -2
  9. package/src/components/atoms/card.tsx +15 -2
  10. package/src/components/atoms/carousel.tsx +33 -7
  11. package/src/components/atoms/chart.tsx +3 -3
  12. package/src/components/atoms/checkbox.tsx +1 -1
  13. package/src/components/atoms/combobox.tsx +60 -36
  14. package/src/components/atoms/command.tsx +17 -7
  15. package/src/components/atoms/context-menu.tsx +5 -5
  16. package/src/components/atoms/dialog.tsx +30 -14
  17. package/src/components/atoms/dropdown-menu.tsx +1 -1
  18. package/src/components/atoms/hover-card.tsx +1 -1
  19. package/src/components/atoms/input-group.tsx +30 -12
  20. package/src/components/atoms/input-otp.tsx +2 -2
  21. package/src/components/atoms/input.tsx +13 -3
  22. package/src/components/atoms/menubar.tsx +2 -2
  23. package/src/components/atoms/native-select.tsx +1 -1
  24. package/src/components/atoms/navigation-menu.tsx +3 -3
  25. package/src/components/atoms/pagination.tsx +52 -15
  26. package/src/components/atoms/popover.tsx +1 -1
  27. package/src/components/atoms/select.tsx +3 -3
  28. package/src/components/atoms/sheet.tsx +35 -15
  29. package/src/components/atoms/sidebar.tsx +224 -36
  30. package/src/components/atoms/skeleton.tsx +6 -3
  31. package/src/components/atoms/spinner.tsx +7 -2
  32. package/src/components/atoms/tabs.tsx +6 -2
  33. package/src/components/atoms/textarea.tsx +1 -1
  34. package/src/components/atoms/toggle.tsx +1 -1
  35. package/src/components/molecules/action-needed.tsx +483 -0
  36. package/src/components/molecules/anonymized-token.tsx +70 -25
  37. package/src/components/molecules/app-shell.tsx +14 -5
  38. package/src/components/molecules/checklist/checklist.tsx +47 -17
  39. package/src/components/molecules/chip.tsx +37 -19
  40. package/src/components/molecules/choice-card.tsx +117 -9
  41. package/src/components/molecules/code-block/package-manager-storage.ts +50 -0
  42. package/src/components/molecules/code-block/process-code.ts +1 -1
  43. package/src/components/molecules/code-block.tsx +58 -47
  44. package/src/components/molecules/combobox-free-text.tsx +75 -20
  45. package/src/components/molecules/confirm-dialog.tsx +17 -5
  46. package/src/components/molecules/copyable-text.tsx +10 -8
  47. package/src/components/molecules/data-table/data-table-filters.tsx +36 -6
  48. package/src/components/molecules/data-table/data-table-helpers.tsx +109 -3
  49. package/src/components/molecules/data-table/data-table-row-count-storage.tsx +88 -0
  50. package/src/components/molecules/data-table/data-table-selection-storage.tsx +33 -15
  51. package/src/components/molecules/data-table/data-table-types.tsx +156 -29
  52. package/src/components/molecules/data-table/data-table-view-options.tsx +14 -4
  53. package/src/components/molecules/data-table/data-table.tsx +156 -75
  54. package/src/components/molecules/data-table/table-pagination.tsx +142 -38
  55. package/src/components/molecules/date-picker.tsx +58 -25
  56. package/src/components/molecules/date-range-picker.tsx +59 -24
  57. package/src/components/molecules/date-time-picker.tsx +63 -25
  58. package/src/components/molecules/date-time.tsx +38 -3
  59. package/src/components/molecules/elapsed-time.tsx +61 -12
  60. package/src/components/molecules/file-dropzone.tsx +151 -40
  61. package/src/components/molecules/format-provider.tsx +14 -4
  62. package/src/components/molecules/id-chip-chrome.tsx +114 -0
  63. package/src/components/molecules/id-chip.tsx +13 -115
  64. package/src/components/molecules/issued-secret.tsx +35 -25
  65. package/src/components/molecules/live-indicator.tsx +1 -1
  66. package/src/components/molecules/money.tsx +44 -3
  67. package/src/components/molecules/multi-select.tsx +62 -16
  68. package/src/components/molecules/page-header.tsx +6 -2
  69. package/src/components/molecules/password-input.tsx +11 -10
  70. package/src/components/molecules/relative-time.tsx +88 -16
  71. package/src/components/molecules/search-trigger.tsx +5 -3
  72. package/src/components/molecules/secret-value.tsx +35 -19
  73. package/src/components/molecules/skip-link.tsx +11 -9
  74. package/src/components/molecules/stat-card.tsx +145 -33
  75. package/src/components/molecules/state-views/empty-state.tsx +16 -12
  76. package/src/components/molecules/state-views/error-state.tsx +36 -20
  77. package/src/components/molecules/state-views/loading-state.tsx +18 -6
  78. package/src/components/molecules/state-views/skeleton-parts.tsx +6 -4
  79. package/src/components/molecules/status-badge.tsx +11 -35
  80. package/src/components/molecules/time-tooltip.tsx +55 -1
  81. package/src/hooks/use-copy.ts +3 -1
  82. package/src/lib/deprecated.ts +71 -0
  83. package/src/lib/field-surface.ts +7 -7
  84. package/src/lib/format-messages.ts +368 -0
  85. package/src/lib/highlight.ts +1 -1
  86. package/src/lib/menu-item.tsx +1 -1
  87. package/src/lib/tint-recipes.ts +10 -5
  88. package/src/lib/utils.ts +19 -1
  89. package/src/lib/dialog-padding.ts +0 -48
@@ -9,9 +9,13 @@ import {
9
9
  SelectTrigger,
10
10
  SelectValue,
11
11
  } from '@vendure-io/ui/components/atoms/select';
12
+ import { SkeletonLine } from '@vendure-io/ui/components/molecules/state-views/skeleton-parts';
12
13
  import { cn } from '@vendure-io/ui/lib/utils';
13
14
  import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react';
14
15
  import type * as React from 'react';
16
+ // Relative: `data-table-helpers` is closed in the package exports.
17
+ import { fillMessage, useMessages } from '../../../lib/format-messages.ts';
18
+ import { PENDING_CLASS } from './data-table-helpers.tsx';
15
19
 
16
20
  // The list-page pagination footer: range text on one side, optional page-size
17
21
  // selector and prev/next controls on the other. TanStack-free and
@@ -21,6 +25,16 @@ import type * as React from 'react';
21
25
  // page-size selector appears only with `onPageSizeChange`; prev/next appear only
22
26
  // with `onPageChange` or `getPageHref`. There are no feature flags and no
23
27
  // disabled placeholder UI for capabilities the consumer didn't opt into.
28
+ //
29
+ // One line at every width: the range and the page-size label never wrap, and
30
+ // in a footer narrower than 28rem the label is visually hidden (it stays the
31
+ // select's accessible name), so the footer height does not depend on the
32
+ // range text.
33
+ //
34
+ // Loading: an unknown `totalItems` (the first page is still loading) shows a
35
+ // skeleton where the range goes and disables prev/next. `isLoading` with a
36
+ // known total (a later page, sort or filter is loading) keeps the range and
37
+ // marks it and prev/next as pending: see `PENDING_CLASS`.
24
38
 
25
39
  /** max(1, …) so an empty result still reports a single page, never zero pages. */
26
40
  function getPageCount(totalItems: number, pageSize: number): number {
@@ -49,17 +63,21 @@ function getRange(
49
63
  return { start, end };
50
64
  }
51
65
 
52
- /** Default range string: "0 of 0" when empty, else "start–end of total" (en dash). */
53
- function defaultFormatRange({
54
- start,
55
- end,
56
- total,
57
- }: {
58
- start: number;
59
- end: number;
60
- total: number;
61
- }): string {
62
- return total <= 0 ? '0 of 0' : `${start}–${end} of ${total}`;
66
+ // Group digits in the default range text ("1–10 of 123,456"), in the language
67
+ // of its English words, so a server render and the browser agree.
68
+ const rangeNumber = new Intl.NumberFormat('en');
69
+
70
+ /**
71
+ * Default range string from `messages.tablePagination`: `rangeEmpty` ("0 of 0")
72
+ * when empty, else `range` ("start–end of total", en dash).
73
+ */
74
+ function defaultFormatRange(
75
+ { start, end, total }: { start: number; end: number; total: number },
76
+ messages: { range: string; rangeEmpty: string },
77
+ ): string {
78
+ if (total <= 0) return messages.rangeEmpty;
79
+ const n = (value: number) => rangeNumber.format(value);
80
+ return fillMessage(messages.range, { start: n(start), end: n(end), total: n(total) });
63
81
  }
64
82
 
65
83
  interface TablePaginationProps extends React.ComponentProps<'div'> {
@@ -72,8 +90,20 @@ interface TablePaginationProps extends React.ComponentProps<'div'> {
72
90
  page: number;
73
91
  /** Items per page — also scales the range math. */
74
92
  pageSize: number;
75
- /** Total items across all pages, used for the range text and last-page detection. */
76
- totalItems: number;
93
+ /**
94
+ * Total items across all pages, used for the range text and last-page
95
+ * detection. `null` or `undefined` means not known yet (the first page is
96
+ * loading): the range shows a skeleton of the same height and prev/next are
97
+ * disabled. Do not pass `0` while loading: `0` renders "0 of 0".
98
+ */
99
+ totalItems: number | null | undefined;
100
+ /**
101
+ * The page, page size, sort or filter just changed and its rows are loading.
102
+ * The range text dims after a short delay (a fast read shows nothing), and
103
+ * prev/next ignore presses until it ends, so a second click does not skip a
104
+ * page. They keep focus and stay in the tab order (`aria-disabled`).
105
+ */
106
+ isLoading?: boolean;
77
107
  /**
78
108
  * Wires button-mode prev/next: renders `<button>`s that call this with the
79
109
  * target page. Ignored for navigation rendering when `getPageHref` is set.
@@ -96,17 +126,30 @@ interface TablePaginationProps extends React.ComponentProps<'div'> {
96
126
  getPageHref?: (page: number) => string;
97
127
  /**
98
128
  * Override the range text. Receives the clamped 1-based range and total, and
99
- * defaults to "start–end of total" ("0 of 0" when empty). A function, not a
100
- * word-swap prop, so it survives non-English word order.
129
+ * defaults to `messages.tablePagination.range`, "{start}–{end} of {total}"
130
+ * (`rangeEmpty`, "0 of 0", when empty). A function, not a word-swap prop, so
131
+ * it survives non-English word order.
101
132
  */
102
133
  formatRange?: (range: { start: number; end: number; total: number }) => string;
103
- /** Accessible name for the prev/next navigation landmark. */
134
+ /**
135
+ * Accessible name for the prev/next navigation landmark.
136
+ * @default `messages.tablePagination.nav`, "Pagination"
137
+ */
104
138
  navLabel?: string;
105
- /** Accessible name for the previous-page control. */
139
+ /**
140
+ * Accessible name for the previous-page control.
141
+ * @default `messages.tablePagination.previous`, "Go to previous page"
142
+ */
106
143
  previousLabel?: string;
107
- /** Accessible name for the next-page control. */
144
+ /**
145
+ * Accessible name for the next-page control.
146
+ * @default `messages.tablePagination.next`, "Go to next page"
147
+ */
108
148
  nextLabel?: string;
109
- /** Visible + accessible name for the page-size selector. */
149
+ /**
150
+ * Visible + accessible name for the page-size selector.
151
+ * @default `messages.tablePagination.pageSize`, "Items per page"
152
+ */
110
153
  pageSizeLabel?: string;
111
154
  }
112
155
 
@@ -114,44 +157,76 @@ function TablePagination({
114
157
  page,
115
158
  pageSize,
116
159
  totalItems,
160
+ isLoading = false,
117
161
  onPageChange,
118
162
  onPageSizeChange,
119
163
  pageSizeOptions = [10, 25, 50, 100],
120
164
  getPageHref,
121
- formatRange = defaultFormatRange,
122
- navLabel = 'Pagination',
123
- previousLabel = 'Go to previous page',
124
- nextLabel = 'Go to next page',
125
- pageSizeLabel = 'Items per page',
165
+ formatRange,
166
+ navLabel,
167
+ previousLabel,
168
+ nextLabel,
169
+ pageSizeLabel,
126
170
  className,
127
171
  ...props
128
172
  }: TablePaginationProps) {
129
- const pageCount = getPageCount(totalItems, pageSize);
173
+ const t = useMessages('tablePagination', {
174
+ nav: navLabel,
175
+ previous: previousLabel,
176
+ next: nextLabel,
177
+ pageSize: pageSizeLabel,
178
+ });
179
+ const totalKnown = totalItems != null;
180
+ const pageCount = getPageCount(totalItems ?? 0, pageSize);
130
181
  const current = clampPage(page, pageCount);
131
- const { start, end } = getRange(page, pageSize, totalItems);
132
-
133
- const rangeText = formatRange({ start, end, total: totalItems });
182
+ const { start, end } = getRange(page, pageSize, totalItems ?? 0);
134
183
 
135
184
  const anchorMode = Boolean(getPageHref);
136
185
  const showNav = anchorMode || Boolean(onPageChange);
137
- const isFirst = current <= 1;
138
- const isLast = current >= pageCount;
186
+ // With the total unknown there is no page to go to yet.
187
+ const isFirst = !totalKnown || current <= 1;
188
+ const isLast = !totalKnown || current >= pageCount;
189
+ const pending = isLoading && totalKnown;
139
190
 
140
191
  return (
141
192
  <div
142
193
  data-slot="table-pagination"
143
- className={cn('flex items-center justify-between gap-4', className)}
194
+ className={cn(
195
+ '@container/table-pagination flex items-center justify-between gap-4',
196
+ className,
197
+ )}
144
198
  {...props}
145
199
  >
146
- <span className="text-muted-foreground text-sm tabular-nums">{rangeText}</span>
200
+ <div
201
+ data-slot="table-pagination-range"
202
+ data-pending={pending ? '' : undefined}
203
+ className={cn(
204
+ 'text-muted-foreground text-sm whitespace-nowrap tabular-nums',
205
+ PENDING_CLASS,
206
+ )}
207
+ >
208
+ {totalKnown ? (
209
+ (formatRange ?? ((range) => defaultFormatRange(range, t)))({
210
+ start,
211
+ end,
212
+ total: totalItems,
213
+ })
214
+ ) : (
215
+ // One line box tall, so the footer keeps the height it has with the
216
+ // range text. The text is not known, so there is nothing to announce.
217
+ <SkeletonLine className="w-24" />
218
+ )}
219
+ </div>
147
220
 
148
221
  {(onPageSizeChange || showNav) && (
149
222
  <div className="flex items-center gap-4">
150
223
  {onPageSizeChange && (
151
224
  <div className="flex items-center gap-2">
152
- <span className="text-muted-foreground text-sm">{pageSizeLabel}</span>
225
+ <span className="text-muted-foreground text-sm whitespace-nowrap @max-md/table-pagination:sr-only">
226
+ {t.pageSize}
227
+ </span>
153
228
  <Select value={pageSize} onValueChange={(value) => onPageSizeChange(Number(value))}>
154
- <SelectTrigger aria-label={pageSizeLabel} className="w-fit">
229
+ <SelectTrigger aria-label={t.pageSize} className="w-fit">
155
230
  <SelectValue />
156
231
  </SelectTrigger>
157
232
  <SelectContent>
@@ -171,12 +246,17 @@ function TablePagination({
171
246
  // Only the prev/next group is a navigation landmark — not the range
172
247
  // text or size selector. Mirrors the shadcn pagination atom's
173
248
  // role="navigation" + aria-label.
174
- <nav aria-label={navLabel} className="flex items-center gap-1">
249
+ <nav
250
+ aria-label={t.nav}
251
+ data-pending={pending ? '' : undefined}
252
+ className={cn('flex items-center gap-1', PENDING_CLASS)}
253
+ >
175
254
  <NavButton
176
255
  slot="table-pagination-previous"
177
256
  anchor={anchorMode}
178
257
  disabled={isFirst}
179
- label={previousLabel}
258
+ pending={pending}
259
+ label={t.previous}
180
260
  href={anchorMode && !isFirst ? getPageHref?.(current - 1) : undefined}
181
261
  onClick={onPageChange ? () => onPageChange(current - 1) : undefined}
182
262
  >
@@ -186,7 +266,8 @@ function TablePagination({
186
266
  slot="table-pagination-next"
187
267
  anchor={anchorMode}
188
268
  disabled={isLast}
189
- label={nextLabel}
269
+ pending={pending}
270
+ label={t.next}
190
271
  href={anchorMode && !isLast ? getPageHref?.(current + 1) : undefined}
191
272
  onClick={onPageChange ? () => onPageChange(current + 1) : undefined}
192
273
  >
@@ -206,6 +287,7 @@ function NavButton({
206
287
  href,
207
288
  onClick,
208
289
  disabled,
290
+ pending,
209
291
  label,
210
292
  children,
211
293
  }: {
@@ -214,6 +296,11 @@ function NavButton({
214
296
  href?: string;
215
297
  onClick?: () => void;
216
298
  disabled: boolean;
299
+ /**
300
+ * The next page is loading: presses do nothing, but the control keeps
301
+ * focus, its tab stop and its look (the nav dims as a whole, after a delay).
302
+ */
303
+ pending: boolean;
217
304
  label: string;
218
305
  children: React.ReactNode;
219
306
  }) {
@@ -222,6 +309,15 @@ function NavButton({
222
309
  size: 'icon' as const,
223
310
  'data-slot': slot,
224
311
  };
312
+ // `aria-disabled:opacity-100`: the nav dims as a whole after a delay (see
313
+ // PENDING_CLASS), so the button does not dim at once on every fast read.
314
+ const pendingProps = pending
315
+ ? {
316
+ 'aria-disabled': true,
317
+ className:
318
+ 'aria-disabled:opacity-100 aria-disabled:shadow-control aria-disabled:cursor-progress',
319
+ }
320
+ : {};
225
321
 
226
322
  if (anchor) {
227
323
  // A disabled edge is a non-link (span) so it isn't focusable or followable.
@@ -242,8 +338,10 @@ function NavButton({
242
338
  return (
243
339
  <Button
244
340
  {...shared}
341
+ {...pendingProps}
245
342
  aria-label={label}
246
343
  nativeButton={false}
344
+ onClick={pending ? (event) => event.preventDefault() : undefined}
247
345
  render={
248
346
  <a href={href} aria-label={label}>
249
347
  {children}
@@ -254,7 +352,13 @@ function NavButton({
254
352
  }
255
353
 
256
354
  return (
257
- <Button {...shared} aria-label={label} disabled={disabled} onClick={onClick}>
355
+ <Button
356
+ {...shared}
357
+ {...pendingProps}
358
+ aria-label={label}
359
+ disabled={disabled}
360
+ onClick={pending ? undefined : onClick}
361
+ >
258
362
  {children}
259
363
  </Button>
260
364
  );
@@ -9,10 +9,11 @@ import {
9
9
  formatDateLabel,
10
10
  parseCalendarDate,
11
11
  } from '@vendure-io/ui/lib/date-value';
12
- import { fieldButton } from '@vendure-io/ui/lib/field-surface';
13
12
  import { cn } from '@vendure-io/ui/lib/utils';
14
13
  import { CalendarIcon, XIcon } from 'lucide-react';
15
14
  import * as React from 'react';
15
+ import { fieldButton } from '../../lib/field-surface.ts';
16
+ import { useMessages } from '../../lib/format-messages.ts';
16
17
 
17
18
  type CalendarProps = Omit<
18
19
  React.ComponentProps<typeof Calendar>,
@@ -20,41 +21,65 @@ type CalendarProps = Omit<
20
21
  >;
21
22
 
22
23
  interface DatePickerProps extends Omit<React.ComponentProps<'div'>, 'defaultValue' | 'onChange'> {
23
- /** Local calendar date (`YYYY-MM-DD`), never an instant. */
24
+ /**
25
+ * Local calendar date (`YYYY-MM-DD`), never an instant. Pass `value` to
26
+ * control the picker. Leave the prop out and it keeps its own state.
27
+ */
24
28
  value?: string;
29
+ /** Initial date when `value` is not passed. */
30
+ defaultValue?: string;
25
31
  onValueChange?: (value: string | undefined) => void;
26
32
  min?: string;
27
33
  max?: string;
34
+ /** @default `messages.datePicker.placeholder`, "Pick a date" */
28
35
  placeholder?: React.ReactNode;
29
36
  clearable?: boolean;
30
37
  disabled?: boolean;
31
38
  invalid?: boolean;
39
+ /** `sm` is 28px tall, for dense rows, like `Input size="sm"`. */
40
+ size?: 'sm' | 'default';
32
41
  name?: string;
33
42
  id?: string;
34
43
  calendarProps?: CalendarProps;
44
+ /** Accessible name of the clear button. @default `messages.datePicker.clear`, "Clear date" */
45
+ clearLabel?: string;
35
46
  }
36
47
 
37
48
  /** Date-only picker whose value cannot shift when serialized across time zones. */
38
- function DatePicker({
39
- value,
40
- onValueChange,
41
- min,
42
- max,
43
- placeholder = 'Pick a date',
44
- clearable = true,
45
- disabled,
46
- invalid,
47
- name,
48
- id,
49
- calendarProps,
50
- className,
51
- ...props
52
- }: DatePickerProps) {
49
+ function DatePicker(allProps: DatePickerProps) {
50
+ const {
51
+ value: valueProp,
52
+ defaultValue,
53
+ onValueChange,
54
+ min,
55
+ max,
56
+ placeholder,
57
+ clearable = true,
58
+ disabled,
59
+ invalid,
60
+ size = 'default',
61
+ name,
62
+ id,
63
+ calendarProps,
64
+ clearLabel,
65
+ className,
66
+ ...props
67
+ } = allProps;
68
+ // Controlled when the `value` prop is passed, even as `undefined` (no value).
69
+ const controlled = 'value' in allProps;
53
70
  const [open, setOpen] = React.useState(false);
71
+ const [own, setOwn] = React.useState(defaultValue);
72
+ const value = controlled ? valueProp : own;
73
+ function commit(next: string | undefined) {
74
+ if (!controlled) setOwn(next);
75
+ onValueChange?.(next);
76
+ }
54
77
  // The calendar popup is a dialog, named by its trigger.
55
78
  const generatedId = React.useId();
56
79
  const triggerId = id ?? generatedId;
57
80
  const { locale } = useFormatSettings();
81
+ const t = useMessages('datePicker', { clear: clearLabel });
82
+ const placeholderText = placeholder !== undefined ? placeholder : t.placeholder;
58
83
  const selected = parseCalendarDate(value);
59
84
  const minDate = parseCalendarDate(min);
60
85
  const maxDate = parseCalendarDate(max);
@@ -66,7 +91,9 @@ function DatePicker({
66
91
  return (
67
92
  <div
68
93
  data-slot="date-picker"
69
- className={cn('flex min-w-0 items-center gap-1', className)}
94
+ data-size={size}
95
+ // max-w-62 is the calendar: 7 cells of 2rem plus p-3. Pass max-w-none to fill the column.
96
+ className={cn('flex min-w-0 max-w-62 items-center gap-1', className)}
70
97
  {...props}
71
98
  >
72
99
  {name ? <input type="hidden" name={name} value={value ?? ''} /> : null}
@@ -80,7 +107,7 @@ function DatePicker({
80
107
  disabled={disabled}
81
108
  aria-invalid={invalid || undefined}
82
109
  // Pickers sit in forms next to Inputs, so they take the field look.
83
- className={fieldButton}
110
+ className={cn(fieldButton, size === 'sm' && 'h-7')}
84
111
  />
85
112
  }
86
113
  >
@@ -89,18 +116,24 @@ function DatePicker({
89
116
  data-placeholder={selected ? undefined : ''}
90
117
  className={cn('truncate', !selected && 'text-faint-foreground')}
91
118
  >
92
- {selected ? formatDateLabel(selected, locale) : placeholder}
119
+ {selected ? formatDateLabel(selected, locale) : placeholderText}
93
120
  </span>
94
121
  </PopoverTrigger>
95
- <PopoverContent className="w-auto p-0" align="start" aria-labelledby={triggerId}>
122
+ <PopoverContent
123
+ className="w-auto min-w-(--anchor-width) p-0"
124
+ align="start"
125
+ aria-labelledby={triggerId}
126
+ >
96
127
  <Calendar
97
128
  {...calendarProps}
129
+ // In a popup wider than the grid, the month sits centred with even padding.
130
+ className={cn('mx-auto', calendarProps?.className)}
98
131
  mode="single"
99
132
  selected={selected}
100
133
  defaultMonth={selected ?? minDate}
101
134
  disabled={disabledMatchers}
102
135
  onSelect={(date) => {
103
- onValueChange?.(date ? formatCalendarDate(date) : undefined);
136
+ commit(date ? formatCalendarDate(date) : undefined);
104
137
  if (date) setOpen(false);
105
138
  }}
106
139
  />
@@ -110,10 +143,10 @@ function DatePicker({
110
143
  <Button
111
144
  type="button"
112
145
  variant="ghost"
113
- size="icon-sm"
146
+ size={size === 'sm' ? 'icon-xs' : 'icon-sm'}
114
147
  disabled={disabled}
115
- aria-label="Clear date"
116
- onClick={() => onValueChange?.(undefined)}
148
+ aria-label={t.clear}
149
+ onClick={() => commit(undefined)}
117
150
  >
118
151
  <XIcon />
119
152
  </Button>
@@ -9,11 +9,12 @@ import {
9
9
  formatDateRangeLabel,
10
10
  parseCalendarDate,
11
11
  } from '@vendure-io/ui/lib/date-value';
12
- import { fieldButton } from '@vendure-io/ui/lib/field-surface';
13
12
  import { cn } from '@vendure-io/ui/lib/utils';
14
13
  import { CalendarRangeIcon, XIcon } from 'lucide-react';
15
14
  import * as React from 'react';
16
15
  import type { DateRange } from 'react-day-picker';
16
+ import { fieldButton } from '../../lib/field-surface.ts';
17
+ import { useMessages } from '../../lib/format-messages.ts';
17
18
 
18
19
  type CalendarProps = Omit<
19
20
  React.ComponentProps<typeof Calendar>,
@@ -33,38 +34,62 @@ interface DateRangePreset {
33
34
 
34
35
  interface DateRangePickerProps
35
36
  extends Omit<React.ComponentProps<'div'>, 'defaultValue' | 'onChange'> {
36
- /** Local calendar-date boundaries (`YYYY-MM-DD`), inclusive. */
37
+ /**
38
+ * Local calendar-date boundaries (`YYYY-MM-DD`), inclusive. Pass `value` to
39
+ * control the picker. Leave the prop out and it keeps its own state.
40
+ */
37
41
  value?: DateRangeValue;
42
+ /** Initial range when `value` is not passed. */
43
+ defaultValue?: DateRangeValue;
38
44
  onValueChange?: (value: DateRangeValue | undefined) => void;
45
+ /** @default `messages.dateRangePicker.placeholder`, "Pick a date range" */
39
46
  placeholder?: React.ReactNode;
40
47
  clearable?: boolean;
41
48
  disabled?: boolean;
42
49
  invalid?: boolean;
50
+ /** `sm` is 28px tall, for dense rows, like `Input size="sm"`. */
51
+ size?: 'sm' | 'default';
43
52
  presets?: readonly DateRangePreset[];
44
53
  calendarProps?: CalendarProps;
54
+ /** Accessible name of the clear button. @default `messages.dateRangePicker.clear`, "Clear date range" */
55
+ clearLabel?: string;
45
56
  }
46
57
 
47
58
  function resolvePreset(value: DateRangePreset['value']): DateRangeValue {
48
59
  return typeof value === 'function' ? value() : value;
49
60
  }
50
61
 
51
- function DateRangePicker({
52
- value,
53
- onValueChange,
54
- placeholder = 'Pick a date range',
55
- clearable = true,
56
- disabled,
57
- invalid,
58
- presets = [],
59
- calendarProps,
60
- className,
61
- ...props
62
- }: DateRangePickerProps) {
62
+ function DateRangePicker(allProps: DateRangePickerProps) {
63
+ const {
64
+ value: valueProp,
65
+ defaultValue,
66
+ onValueChange,
67
+ placeholder,
68
+ clearable = true,
69
+ disabled,
70
+ invalid,
71
+ size = 'default',
72
+ presets = [],
73
+ calendarProps,
74
+ clearLabel,
75
+ className,
76
+ ...props
77
+ } = allProps;
78
+ // Controlled when the `value` prop is passed, even as `undefined` (no value).
79
+ const controlled = 'value' in allProps;
63
80
  const [open, setOpen] = React.useState(false);
81
+ const [own, setOwn] = React.useState(defaultValue);
82
+ const value = controlled ? valueProp : own;
83
+ function commit(next: DateRangeValue | undefined) {
84
+ if (!controlled) setOwn(next);
85
+ onValueChange?.(next);
86
+ }
64
87
  // The calendar popup is a dialog, named by its trigger.
65
88
  const generatedId = React.useId();
66
89
  const triggerId = generatedId;
67
90
  const { locale } = useFormatSettings();
91
+ const t = useMessages('dateRangePicker', { clear: clearLabel });
92
+ const placeholderText = placeholder !== undefined ? placeholder : t.placeholder;
68
93
  const selected: DateRange = {
69
94
  from: parseCalendarDate(value?.from),
70
95
  to: parseCalendarDate(value?.to),
@@ -73,10 +98,10 @@ function DateRangePicker({
73
98
 
74
99
  function commitRange(range: DateRange | undefined) {
75
100
  if (!range?.from) {
76
- onValueChange?.(undefined);
101
+ commit(undefined);
77
102
  return;
78
103
  }
79
- onValueChange?.({
104
+ commit({
80
105
  from: formatCalendarDate(range.from),
81
106
  to: range.to ? formatCalendarDate(range.to) : undefined,
82
107
  });
@@ -85,7 +110,9 @@ function DateRangePicker({
85
110
  return (
86
111
  <div
87
112
  data-slot="date-range-picker"
88
- className={cn('flex min-w-0 items-center gap-1', className)}
113
+ data-size={size}
114
+ // max-w-72 is the calendar (62) plus room for the longest range label. Pass max-w-none to fill the column.
115
+ className={cn('flex min-w-0 max-w-72 items-center gap-1', className)}
89
116
  {...props}
90
117
  >
91
118
  <Popover open={open} onOpenChange={setOpen}>
@@ -98,7 +125,7 @@ function DateRangePicker({
98
125
  disabled={disabled}
99
126
  aria-invalid={invalid || undefined}
100
127
  // Pickers sit in forms next to Inputs, so they take the field look.
101
- className={fieldButton}
128
+ className={cn(fieldButton, size === 'sm' && 'h-7')}
102
129
  />
103
130
  }
104
131
  >
@@ -107,10 +134,16 @@ function DateRangePicker({
107
134
  data-placeholder={hasValue ? undefined : ''}
108
135
  className={cn('truncate', !hasValue && 'text-faint-foreground')}
109
136
  >
110
- {selected.from ? formatDateRangeLabel(selected.from, selected.to, locale) : placeholder}
137
+ {selected.from
138
+ ? formatDateRangeLabel(selected.from, selected.to, locale)
139
+ : placeholderText}
111
140
  </span>
112
141
  </PopoverTrigger>
113
- <PopoverContent className="w-auto p-0" align="start" aria-labelledby={triggerId}>
142
+ <PopoverContent
143
+ className="w-auto min-w-(--anchor-width) p-0"
144
+ align="start"
145
+ aria-labelledby={triggerId}
146
+ >
114
147
  <div className={cn('flex', presets.length > 0 && 'divide-x')}>
115
148
  {presets.length > 0 ? (
116
149
  <div className="flex w-40 flex-col gap-1 p-2">
@@ -122,7 +155,7 @@ function DateRangePicker({
122
155
  size="sm"
123
156
  className="justify-start font-normal"
124
157
  onClick={() => {
125
- onValueChange?.(resolvePreset(preset.value));
158
+ commit(resolvePreset(preset.value));
126
159
  setOpen(false);
127
160
  }}
128
161
  >
@@ -133,6 +166,8 @@ function DateRangePicker({
133
166
  ) : null}
134
167
  <Calendar
135
168
  {...calendarProps}
169
+ // In a popup wider than the grid, the month sits centred with even padding.
170
+ className={cn('mx-auto', calendarProps?.className)}
136
171
  mode="range"
137
172
  selected={selected}
138
173
  defaultMonth={selected.from}
@@ -145,10 +180,10 @@ function DateRangePicker({
145
180
  <Button
146
181
  type="button"
147
182
  variant="ghost"
148
- size="icon-sm"
183
+ size={size === 'sm' ? 'icon-xs' : 'icon-sm'}
149
184
  disabled={disabled}
150
- aria-label="Clear date range"
151
- onClick={() => onValueChange?.(undefined)}
185
+ aria-label={t.clear}
186
+ onClick={() => commit(undefined)}
152
187
  >
153
188
  <XIcon />
154
189
  </Button>