@vendure-io/ui 3.0.0-next.3 → 3.0.0-next.5

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