@vendure-io/ui 2.1.0 → 3.0.0-next.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.
Files changed (118) hide show
  1. package/package.json +19 -5
  2. package/src/components/atoms/accordion.tsx +21 -6
  3. package/src/components/atoms/alert-dialog.tsx +31 -6
  4. package/src/components/atoms/alert.tsx +73 -13
  5. package/src/components/atoms/avatar.tsx +1 -1
  6. package/src/components/atoms/badge.tsx +7 -2
  7. package/src/components/atoms/breadcrumb.tsx +137 -4
  8. package/src/components/atoms/button-group.tsx +30 -6
  9. package/src/components/atoms/button.tsx +128 -18
  10. package/src/components/atoms/calendar.tsx +6 -6
  11. package/src/components/atoms/card.tsx +130 -16
  12. package/src/components/atoms/carousel.tsx +16 -9
  13. package/src/components/atoms/chart.tsx +87 -1
  14. package/src/components/atoms/checkbox.tsx +85 -7
  15. package/src/components/atoms/collapsible.tsx +2 -1
  16. package/src/components/atoms/combobox.tsx +147 -21
  17. package/src/components/atoms/command.tsx +414 -12
  18. package/src/components/atoms/context-menu.tsx +28 -23
  19. package/src/components/atoms/dialog.tsx +151 -29
  20. package/src/components/atoms/direction.tsx +23 -2
  21. package/src/components/atoms/drawer.tsx +15 -10
  22. package/src/components/atoms/dropdown-menu.tsx +43 -28
  23. package/src/components/atoms/empty.tsx +4 -0
  24. package/src/components/atoms/field.tsx +41 -15
  25. package/src/components/atoms/hover-card.tsx +61 -8
  26. package/src/components/atoms/input-group.tsx +17 -9
  27. package/src/components/atoms/input-otp.tsx +6 -6
  28. package/src/components/atoms/input.tsx +7 -1
  29. package/src/components/atoms/item.tsx +3 -3
  30. package/src/components/atoms/kbd.tsx +32 -3
  31. package/src/components/atoms/label-swap.tsx +82 -0
  32. package/src/components/atoms/label.tsx +1 -1
  33. package/src/components/atoms/menubar.tsx +28 -26
  34. package/src/components/atoms/native-select.tsx +3 -2
  35. package/src/components/atoms/navigation-menu.tsx +13 -8
  36. package/src/components/atoms/pagination.tsx +2 -2
  37. package/src/components/atoms/popover.tsx +23 -5
  38. package/src/components/atoms/progress.tsx +46 -33
  39. package/src/components/atoms/radio-group.tsx +9 -4
  40. package/src/components/atoms/resizable.tsx +1 -1
  41. package/src/components/atoms/scroll-area.tsx +2 -2
  42. package/src/components/atoms/select.tsx +258 -19
  43. package/src/components/atoms/sheet.tsx +59 -23
  44. package/src/components/atoms/sidebar.tsx +333 -23
  45. package/src/components/atoms/skeleton.tsx +1 -1
  46. package/src/components/atoms/slider.tsx +1 -1
  47. package/src/components/atoms/sonner.tsx +128 -38
  48. package/src/components/atoms/spinner.tsx +1 -1
  49. package/src/components/atoms/switch.tsx +10 -5
  50. package/src/components/atoms/table.tsx +147 -7
  51. package/src/components/atoms/tabs.tsx +3 -3
  52. package/src/components/atoms/textarea.tsx +3 -1
  53. package/src/components/atoms/toggle-group.tsx +4 -2
  54. package/src/components/atoms/toggle.tsx +28 -6
  55. package/src/components/atoms/tooltip.tsx +85 -15
  56. package/src/components/molecules/anonymized-token.tsx +24 -8
  57. package/src/components/molecules/app-shell.tsx +53 -20
  58. package/src/components/molecules/arrow-link.tsx +40 -0
  59. package/src/components/molecules/chip.tsx +2 -2
  60. package/src/components/molecules/code-block.tsx +54 -31
  61. package/src/components/molecules/collapsible-section.tsx +129 -0
  62. package/src/components/molecules/confirm-dialog.tsx +113 -9
  63. package/src/components/molecules/copyable-text.tsx +75 -11
  64. package/src/components/molecules/data-table/data-table-bulk-actions.tsx +32 -4
  65. package/src/components/molecules/data-table/data-table-column-header.tsx +20 -3
  66. package/src/components/molecules/data-table/data-table-filters.tsx +1 -1
  67. package/src/components/molecules/data-table/data-table-helpers.tsx +68 -1
  68. package/src/components/molecules/data-table/data-table-selection-storage.tsx +115 -0
  69. package/src/components/molecules/data-table/data-table-types.tsx +138 -4
  70. package/src/components/molecules/data-table/data-table-view-options.tsx +2 -2
  71. package/src/components/molecules/data-table/data-table.tsx +916 -84
  72. package/src/components/molecules/data-table/table-pagination.tsx +2 -2
  73. package/src/components/molecules/date-picker.tsx +13 -7
  74. package/src/components/molecules/date-range-picker.tsx +13 -6
  75. package/src/components/molecules/date-time-picker.tsx +13 -7
  76. package/src/components/molecules/date-time.tsx +8 -1
  77. package/src/components/molecules/direction-link.tsx +130 -0
  78. package/src/components/molecules/elapsed-time.tsx +223 -0
  79. package/src/components/molecules/file-dropzone.tsx +2 -2
  80. package/src/components/molecules/id-chip.tsx +169 -32
  81. package/src/components/molecules/inline-code.tsx +4 -0
  82. package/src/components/molecules/issued-secret.tsx +318 -0
  83. package/src/components/molecules/list-row.tsx +349 -0
  84. package/src/components/molecules/live-indicator.tsx +83 -0
  85. package/src/components/molecules/page-header.tsx +117 -5
  86. package/src/components/molecules/password-input.tsx +2 -2
  87. package/src/components/molecules/relative-time.tsx +85 -56
  88. package/src/components/molecules/search-trigger.tsx +127 -0
  89. package/src/components/molecules/secret-value.tsx +26 -9
  90. package/src/components/molecules/skip-link.tsx +1 -1
  91. package/src/components/molecules/stat-card.tsx +4 -4
  92. package/src/components/molecules/state-views/empty-state.tsx +35 -17
  93. package/src/components/molecules/state-views/error-state.tsx +32 -14
  94. package/src/components/molecules/status-badge.tsx +415 -49
  95. package/src/components/molecules/time-tooltip.tsx +140 -0
  96. package/src/components/molecules/timeline.tsx +246 -0
  97. package/src/components/molecules/visibility-icon.tsx +89 -0
  98. package/src/hooks/use-active-label-size.ts +32 -0
  99. package/src/hooks/use-pinned-height.ts +55 -0
  100. package/src/hooks/use-scrolled.ts +27 -0
  101. package/src/lib/content-inset.ts +17 -0
  102. package/src/lib/control-name.ts +55 -0
  103. package/src/lib/dialog-padding.ts +48 -0
  104. package/src/lib/field-surface.ts +31 -0
  105. package/src/lib/format-elapsed.ts +67 -0
  106. package/src/lib/format-relative.ts +29 -1
  107. package/src/lib/highlight.ts +3 -3
  108. package/src/lib/menu-item.tsx +72 -0
  109. package/src/lib/outline-edge.ts +21 -0
  110. package/src/lib/popup-align.ts +67 -0
  111. package/src/lib/popup-direction.ts +22 -0
  112. package/src/lib/popup-motion.ts +19 -0
  113. package/src/lib/state-dictionary.ts +42 -6
  114. package/src/lib/syntax-colors.ts +8 -0
  115. package/src/lib/tint-recipes.ts +68 -0
  116. package/src/lib/tone-icons.ts +24 -0
  117. package/src/lib/trigger-slot.ts +16 -0
  118. package/src/lib/utils.ts +104 -1
@@ -151,7 +151,7 @@ function TablePagination({
151
151
  <div className="flex items-center gap-2">
152
152
  <span className="text-muted-foreground text-sm">{pageSizeLabel}</span>
153
153
  <Select value={pageSize} onValueChange={(value) => onPageSizeChange(Number(value))}>
154
- <SelectTrigger size="sm" aria-label={pageSizeLabel} className="w-fit">
154
+ <SelectTrigger aria-label={pageSizeLabel} className="w-fit">
155
155
  <SelectValue />
156
156
  </SelectTrigger>
157
157
  <SelectContent>
@@ -219,7 +219,7 @@ function NavButton({
219
219
  }) {
220
220
  const shared = {
221
221
  variant: 'outline' as const,
222
- size: 'icon-sm' as const,
222
+ size: 'icon' as const,
223
223
  'data-slot': slot,
224
224
  };
225
225
 
@@ -9,6 +9,7 @@ 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';
12
13
  import { cn } from '@vendure-io/ui/lib/utils';
13
14
  import { CalendarIcon, XIcon } from 'lucide-react';
14
15
  import * as React from 'react';
@@ -50,6 +51,9 @@ function DatePicker({
50
51
  ...props
51
52
  }: DatePickerProps) {
52
53
  const [open, setOpen] = React.useState(false);
54
+ // The calendar popup is a dialog, named by its trigger.
55
+ const generatedId = React.useId();
56
+ const triggerId = id ?? generatedId;
53
57
  const { locale } = useFormatSettings();
54
58
  const selected = parseCalendarDate(value);
55
59
  const minDate = parseCalendarDate(min);
@@ -70,23 +74,25 @@ function DatePicker({
70
74
  <PopoverTrigger
71
75
  render={
72
76
  <Button
73
- id={id}
77
+ id={triggerId}
74
78
  type="button"
75
- variant="outline"
79
+ variant="ghost"
76
80
  disabled={disabled}
77
81
  aria-invalid={invalid || undefined}
78
- // Pickers sit in forms next to Inputs, so mirror the Input/Select
79
- // trigger look (transparent bg) instead of the outline button fill.
80
- className="min-w-0 flex-1 justify-start border-input bg-transparent font-normal hover:bg-transparent aria-expanded:bg-transparent"
82
+ // Pickers sit in forms next to Inputs, so they take the field look.
83
+ className={fieldButton}
81
84
  />
82
85
  }
83
86
  >
84
87
  <CalendarIcon />
85
- <span className={cn('truncate', !selected && 'text-muted-foreground')}>
88
+ <span
89
+ data-placeholder={selected ? undefined : ''}
90
+ className={cn('truncate', !selected && 'text-faint-foreground')}
91
+ >
86
92
  {selected ? formatDateLabel(selected, locale) : placeholder}
87
93
  </span>
88
94
  </PopoverTrigger>
89
- <PopoverContent className="w-auto p-0" align="start">
95
+ <PopoverContent className="w-auto p-0" align="start" aria-labelledby={triggerId}>
90
96
  <Calendar
91
97
  {...calendarProps}
92
98
  mode="single"
@@ -9,6 +9,7 @@ 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';
12
13
  import { cn } from '@vendure-io/ui/lib/utils';
13
14
  import { CalendarRangeIcon, XIcon } from 'lucide-react';
14
15
  import * as React from 'react';
@@ -60,6 +61,9 @@ function DateRangePicker({
60
61
  ...props
61
62
  }: DateRangePickerProps) {
62
63
  const [open, setOpen] = React.useState(false);
64
+ // The calendar popup is a dialog, named by its trigger.
65
+ const generatedId = React.useId();
66
+ const triggerId = generatedId;
63
67
  const { locale } = useFormatSettings();
64
68
  const selected: DateRange = {
65
69
  from: parseCalendarDate(value?.from),
@@ -88,22 +92,25 @@ function DateRangePicker({
88
92
  <PopoverTrigger
89
93
  render={
90
94
  <Button
95
+ id={triggerId}
91
96
  type="button"
92
- variant="outline"
97
+ variant="ghost"
93
98
  disabled={disabled}
94
99
  aria-invalid={invalid || undefined}
95
- // Pickers sit in forms next to Inputs, so mirror the Input/Select
96
- // trigger look (transparent bg) instead of the outline button fill.
97
- className="min-w-0 flex-1 justify-start border-input bg-transparent font-normal hover:bg-transparent aria-expanded:bg-transparent"
100
+ // Pickers sit in forms next to Inputs, so they take the field look.
101
+ className={fieldButton}
98
102
  />
99
103
  }
100
104
  >
101
105
  <CalendarRangeIcon />
102
- <span className={cn('truncate', !hasValue && 'text-muted-foreground')}>
106
+ <span
107
+ data-placeholder={hasValue ? undefined : ''}
108
+ className={cn('truncate', !hasValue && 'text-faint-foreground')}
109
+ >
103
110
  {selected.from ? formatDateRangeLabel(selected.from, selected.to, locale) : placeholder}
104
111
  </span>
105
112
  </PopoverTrigger>
106
- <PopoverContent className="w-auto p-0" align="start">
113
+ <PopoverContent className="w-auto p-0" align="start" aria-labelledby={triggerId}>
107
114
  <div className={cn('flex', presets.length > 0 && 'divide-x')}>
108
115
  {presets.length > 0 ? (
109
116
  <div className="flex w-40 flex-col gap-1 p-2">
@@ -6,6 +6,7 @@ import { Input } from '@vendure-io/ui/components/atoms/input';
6
6
  import { Popover, PopoverContent, PopoverTrigger } from '@vendure-io/ui/components/atoms/popover';
7
7
  import { useFormatSettings } from '@vendure-io/ui/components/molecules/format-provider';
8
8
  import { formatDateLabel, parseInstant } from '@vendure-io/ui/lib/date-value';
9
+ import { fieldButton } from '@vendure-io/ui/lib/field-surface';
9
10
  import { cn } from '@vendure-io/ui/lib/utils';
10
11
  import { CalendarClockIcon, XIcon } from 'lucide-react';
11
12
  import * as React from 'react';
@@ -43,6 +44,9 @@ function DateTimePicker({
43
44
  ...props
44
45
  }: DateTimePickerProps) {
45
46
  const [open, setOpen] = React.useState(false);
47
+ // The calendar popup is a dialog, named by its trigger.
48
+ const generatedId = React.useId();
49
+ const triggerId = id ?? generatedId;
46
50
  const { locale } = useFormatSettings();
47
51
  const selected = parseInstant(value);
48
52
  const timeValue = selected
@@ -79,23 +83,25 @@ function DateTimePicker({
79
83
  <PopoverTrigger
80
84
  render={
81
85
  <Button
82
- id={id}
86
+ id={triggerId}
83
87
  type="button"
84
- variant="outline"
88
+ variant="ghost"
85
89
  disabled={disabled}
86
90
  aria-invalid={invalid || undefined}
87
- // Pickers sit in forms next to Inputs, so mirror the Input/Select
88
- // trigger look (transparent bg) instead of the outline button fill.
89
- className="min-w-0 flex-1 justify-start border-input bg-transparent font-normal hover:bg-transparent aria-expanded:bg-transparent"
91
+ // Pickers sit in forms next to Inputs, so they take the field look.
92
+ className={fieldButton}
90
93
  />
91
94
  }
92
95
  >
93
96
  <CalendarClockIcon />
94
- <span className={cn('truncate', !selected && 'text-muted-foreground')}>
97
+ <span
98
+ data-placeholder={selected ? undefined : ''}
99
+ className={cn('truncate', !selected && 'text-faint-foreground')}
100
+ >
95
101
  {selected ? formatDateLabel(selected, locale) : placeholder}
96
102
  </span>
97
103
  </PopoverTrigger>
98
- <PopoverContent className="w-auto p-0" align="start">
104
+ <PopoverContent className="w-auto p-0" align="start" aria-labelledby={triggerId}>
99
105
  <Calendar
100
106
  {...calendarProps}
101
107
  mode="single"
@@ -1,6 +1,7 @@
1
1
  'use client';
2
2
 
3
3
  import { useFormatSettings } from '@vendure-io/ui/components/molecules/format-provider';
4
+ import { cn } from '@vendure-io/ui/lib/utils';
4
5
  import type { ComponentProps, ReactNode } from 'react';
5
6
 
6
7
  function toDate(value: string | number | Date | null | undefined): Date | null {
@@ -39,6 +40,7 @@ function DateTime({
39
40
  timeZone,
40
41
  formatOptions,
41
42
  fallback = '—',
43
+ className,
42
44
  ...props
43
45
  }: DateTimeProps) {
44
46
  const settings = useFormatSettings();
@@ -64,7 +66,12 @@ function DateTime({
64
66
  }
65
67
 
66
68
  return (
67
- <time data-slot="date-time" dateTime={date.toISOString()} {...props}>
69
+ <time
70
+ data-slot="date-time"
71
+ dateTime={date.toISOString()}
72
+ className={cn('tabular-nums', className)}
73
+ {...props}
74
+ >
68
75
  {formatted}
69
76
  </time>
70
77
  );
@@ -0,0 +1,130 @@
1
+ import { mergeProps } from '@base-ui/react/merge-props';
2
+ import { useRender } from '@base-ui/react/use-render';
3
+ import { cn } from '@vendure-io/ui/lib/utils';
4
+ import { ArrowUpRight, ChevronLeft, ChevronRight } from 'lucide-react';
5
+ import type * as React from 'react';
6
+
7
+ // Internal: ArrowLink (forward, external) and PageHeaderBack (back) render
8
+ // this, and PageHeaderFactLink renders ExternalArrow. The package exports map
9
+ // this file to null.
10
+ //
11
+ // The v3 look: 13px medium text, and a 15px chevron at 45%: trailing forward,
12
+ // leading back. A straight direction takes a chevron; an arrow is for a
13
+ // diagonal only, such as a link that leaves the app. On hover or keyboard
14
+ // focus the text reaches full foreground and the chevron full strength, and
15
+ // the chevron moves 2px the way the link goes. Colour eases in `quick`
16
+ // (120ms) on `soft`; the chevron fades in `moderate` on `soft` and moves in
17
+ // `normal` on the strong ease-out. With reduced motion only the colour and
18
+ // opacity change. In a right-to-left layout the chevron points and moves the
19
+ // other way.
20
+ //
21
+ // A link that opens another site (`external`) ends with a 14px up-right arrow
22
+ // at 40% instead of a chevron. On hover or keyboard focus the arrow goes to
23
+ // 85% and moves 1.5px out along its diagonal: it fades in `moderate` on `soft`
24
+ // and moves in `external-arrow` (220ms) on the strong ease-out. In a
25
+ // right-to-left layout it points up-left and moves that way. With reduced
26
+ // motion it only fades.
27
+ //
28
+ // Every direction uses the `group/arrow-link` group, the public hook for a card
29
+ // or row that opens the same page (see ArrowLink).
30
+
31
+ type Direction = 'forward' | 'back' | 'external';
32
+
33
+ const nudge: Record<Exclude<Direction, 'external'>, string> = {
34
+ forward:
35
+ 'group-hover/arrow-link:translate-x-0.5 group-focus-visible/arrow-link:translate-x-0.5 rtl:group-hover/arrow-link:-translate-x-0.5 rtl:group-focus-visible/arrow-link:-translate-x-0.5',
36
+ back: 'group-hover/arrow-link:-translate-x-0.5 group-focus-visible/arrow-link:-translate-x-0.5 rtl:group-hover/arrow-link:translate-x-0.5 rtl:group-focus-visible/arrow-link:translate-x-0.5',
37
+ };
38
+
39
+ // The class names stay literal so Tailwind can scan them.
40
+ const EXTERNAL_ARROW_CLASS = [
41
+ 'size-3.5 shrink-0 opacity-40 rtl:-scale-x-100',
42
+ '[transition:opacity_var(--transition-duration-moderate)_var(--ease-soft),translate_var(--transition-duration-external-arrow)_var(--ease-out)]',
43
+ 'group-hover/arrow-link:opacity-85 group-focus-visible/arrow-link:opacity-85',
44
+ 'group-hover/arrow-link:translate-x-[--spacing(0.375)] group-hover/arrow-link:-translate-y-[--spacing(0.375)] group-focus-visible/arrow-link:translate-x-[--spacing(0.375)] group-focus-visible/arrow-link:-translate-y-[--spacing(0.375)]',
45
+ 'rtl:group-hover/arrow-link:-translate-x-[--spacing(0.375)] rtl:group-focus-visible/arrow-link:-translate-x-[--spacing(0.375)]',
46
+ 'motion-reduce:translate-none!',
47
+ ].join(' ');
48
+
49
+ /**
50
+ * The diagonal arrow of a link that opens another site. Decorative. Its link
51
+ * takes `group/arrow-link`, which drives the hover.
52
+ */
53
+ function ExternalArrow({ className, ...props }: React.ComponentProps<typeof ArrowUpRight>) {
54
+ return (
55
+ <ArrowUpRight
56
+ aria-hidden="true"
57
+ strokeWidth={1.75}
58
+ className={cn(EXTERNAL_ARROW_CLASS, className)}
59
+ {...props}
60
+ />
61
+ );
62
+ }
63
+
64
+ type DirectionLinkProps = useRender.ComponentProps<'a'> & {
65
+ direction: Direction;
66
+ /** The link's `data-slot`. */
67
+ slot: string;
68
+ /** The icon's `data-slot`. */
69
+ iconSlot: string;
70
+ };
71
+
72
+ function DirectionLink({
73
+ direction,
74
+ slot,
75
+ iconSlot,
76
+ className,
77
+ render,
78
+ children,
79
+ ...props
80
+ }: DirectionLinkProps) {
81
+ const Icon = direction === 'back' ? ChevronLeft : ChevronRight;
82
+ const icon =
83
+ direction === 'external' ? (
84
+ <ExternalArrow data-slot={iconSlot} />
85
+ ) : (
86
+ <Icon
87
+ aria-hidden="true"
88
+ data-slot={iconSlot}
89
+ className={cn(
90
+ 'size-3.75 shrink-0 opacity-45 rtl:-scale-x-100',
91
+ '[transition:opacity_var(--transition-duration-moderate)_var(--ease-soft),translate_var(--transition-duration-normal)_var(--ease-out)]',
92
+ 'group-hover/arrow-link:opacity-100 group-focus-visible/arrow-link:opacity-100',
93
+ nudge[direction],
94
+ 'motion-reduce:translate-none!',
95
+ )}
96
+ />
97
+ );
98
+ return useRender({
99
+ defaultTagName: 'a',
100
+ render,
101
+ props: mergeProps<'a'>(
102
+ {
103
+ className: cn(
104
+ 'group/arrow-link inline-flex cursor-pointer items-center gap-1.5 text-sm font-medium text-foreground-secondary',
105
+ 'transition-colors duration-quick ease-soft',
106
+ 'hover:text-foreground focus-visible:text-foreground group-hover/arrow-link:text-foreground',
107
+ 'rounded-md outline-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
108
+ className,
109
+ ),
110
+ children:
111
+ direction === 'back' ? (
112
+ <>
113
+ {icon}
114
+ {children}
115
+ </>
116
+ ) : (
117
+ <>
118
+ {children}
119
+ {icon}
120
+ </>
121
+ ),
122
+ },
123
+ props,
124
+ ),
125
+ state: { slot },
126
+ });
127
+ }
128
+
129
+ export { DirectionLink, ExternalArrow };
130
+ export type { Direction };
@@ -0,0 +1,223 @@
1
+ 'use client';
2
+
3
+ import { useFormatSettings } from '@vendure-io/ui/components/molecules/format-provider';
4
+ import { formatElapsed, isoDuration } from '@vendure-io/ui/lib/format-elapsed';
5
+ import { cn } from '@vendure-io/ui/lib/utils';
6
+ import {
7
+ type ComponentProps,
8
+ type ReactNode,
9
+ useCallback,
10
+ useEffect,
11
+ useId,
12
+ useRef,
13
+ useState,
14
+ useSyncExternalStore,
15
+ } from 'react';
16
+ // Relative: `time-tooltip` is internal.
17
+ import { TimeSpan, TimeTooltip, toTime, useReadersClock } from './time-tooltip.tsx';
18
+
19
+ // One shared clock for every timer on the page: one interval, whole seconds.
20
+ // It runs only while at least one visible timer listens.
21
+ const listeners = new Set<() => void>();
22
+ let clockNow = 0;
23
+ let clockTimer: ReturnType<typeof setInterval> | undefined;
24
+
25
+ function wholeSecond() {
26
+ return Math.floor(Date.now() / 1000) * 1000;
27
+ }
28
+
29
+ function subscribeClock(listener: () => void) {
30
+ listeners.add(listener);
31
+ if (clockTimer === undefined) {
32
+ clockNow = wholeSecond();
33
+ clockTimer = setInterval(() => {
34
+ clockNow = wholeSecond();
35
+ for (const notify of listeners) notify();
36
+ }, 1000);
37
+ }
38
+ return () => {
39
+ listeners.delete(listener);
40
+ if (listeners.size === 0 && clockTimer !== undefined) {
41
+ clearInterval(clockTimer);
42
+ clockTimer = undefined;
43
+ }
44
+ };
45
+ }
46
+
47
+ function readClock() {
48
+ // Without a running clock, read the time now (to the second, so two reads
49
+ // in one render agree).
50
+ return clockTimer === undefined ? wholeSecond() : clockNow;
51
+ }
52
+
53
+ // The server has no reader's clock: it renders the fallback, and so does the
54
+ // first client render during hydration. The count starts right after.
55
+ function readServerClock(): number | null {
56
+ return null;
57
+ }
58
+
59
+ function noSubscription() {
60
+ return () => {};
61
+ }
62
+
63
+ // True while the element is on screen and the tab is visible. Starts true, so
64
+ // a timer counts from its first render; the observer turns it off when the
65
+ // element is off screen.
66
+ function useOnScreen(element: React.RefObject<HTMLElement | null>) {
67
+ const [onScreen, setOnScreen] = useState(true);
68
+ const [tabVisible, setTabVisible] = useState(true);
69
+
70
+ useEffect(() => {
71
+ const node = element.current;
72
+ if (!node || typeof IntersectionObserver === 'undefined') return;
73
+ const observer = new IntersectionObserver((entries) => {
74
+ const entry = entries[entries.length - 1];
75
+ if (entry) setOnScreen(entry.isIntersecting);
76
+ });
77
+ observer.observe(node);
78
+ return () => observer.disconnect();
79
+ }, [element]);
80
+
81
+ useEffect(() => {
82
+ const update = () => setTabVisible(document.visibilityState !== 'hidden');
83
+ update();
84
+ document.addEventListener('visibilitychange', update);
85
+ return () => document.removeEventListener('visibilitychange', update);
86
+ }, []);
87
+
88
+ return onScreen && tabVisible;
89
+ }
90
+
91
+ interface ElapsedTimeProps extends Omit<ComponentProps<'time'>, 'dateTime' | 'children'> {
92
+ /** When the count started: a Date, an ISO string or epoch milliseconds. */
93
+ since: string | number | Date | null | undefined;
94
+ /**
95
+ * When the count stopped. Leave it out for a live count, which ticks every
96
+ * second. With `until`, the text is fixed.
97
+ */
98
+ until?: string | number | Date | null;
99
+ /**
100
+ * Rendered for nullish or invalid input, and on the server and the first
101
+ * client render of a server-rendered page (the server has no reader's
102
+ * clock or locale). An app that renders only in the browser shows the count
103
+ * from the first paint.
104
+ * Default `'—'`.
105
+ */
106
+ fallback?: ReactNode;
107
+ /**
108
+ * Formats the elapsed milliseconds. Default `formatElapsed` in `locale`:
109
+ * `1m 15s` in English.
110
+ */
111
+ format?: (ms: number) => string;
112
+ /**
113
+ * A tooltip on hover and focus. Default `false`. `true` shows when the
114
+ * count started in the reader's locale (with `until`, the range and how
115
+ * long it took). Pass content to show that instead. The tooltip content is
116
+ * the accessible description too.
117
+ */
118
+ tooltip?: boolean | ReactNode;
119
+ /**
120
+ * Make the time a tab stop so keyboard users can open the tooltip. Default
121
+ * `false`, and `true` under a status label (a StatusBadge `detail`). Never
122
+ * inside interactive content such as a link or a button. Needs `tooltip`.
123
+ */
124
+ focusable?: boolean;
125
+ /** The locale of the count and the tooltip times. Default: the FormatProvider's. */
126
+ locale?: string;
127
+ }
128
+
129
+ /**
130
+ * How long something has run, counting up every second: `31s`, `1m 15s`,
131
+ * `1h 05m` in English, in the reader's locale from the FormatProvider. Use it for a state that is happening now (Building, Deploying) or
132
+ * waiting (Queued).
133
+ *
134
+ * - The digits are tabular, so the text does not shift while it counts.
135
+ * - It stops ticking while it is off screen or the tab is hidden, and catches
136
+ * up when it shows again. All timers on a page share one interval.
137
+ * - The server and the first client render show `fallback`, also with
138
+ * `until`, so hydration matches; the count shows right after. In an app
139
+ * that renders only in the browser it shows from the first paint.
140
+ * - It renders a `<time>` with the ISO duration (`PT1M15S`). Screen readers
141
+ * do not announce each tick.
142
+ * - With `tooltip`, under a status label (a StatusBadge `detail`), the time is
143
+ * a tab stop and the tooltip opens beside the whole status block.
144
+ */
145
+ function ElapsedTime({
146
+ since,
147
+ until,
148
+ fallback = '—',
149
+ format,
150
+ tooltip = false,
151
+ focusable,
152
+ locale,
153
+ className,
154
+ ...props
155
+ }: ElapsedTimeProps) {
156
+ const ref = useRef<HTMLTimeElement>(null);
157
+ const descriptionId = useId();
158
+ const settings = useFormatSettings();
159
+ const start = toTime(since);
160
+ const end = toTime(until);
161
+ const live = start !== null && end === null;
162
+ const active = useOnScreen(ref) && live;
163
+ const subscribe = useCallback(
164
+ (listener: () => void) => (active ? subscribeClock(listener) : noSubscription()),
165
+ [active],
166
+ );
167
+ const now = useSyncExternalStore(subscribe, readClock, readServerClock);
168
+ // A fixed count (`until`) does not need the clock, but its text and its
169
+ // description depend on the reader's locale: the server renders the
170
+ // fallback for it too, the same as RelativeTime.
171
+ const readersClock = useReadersClock();
172
+
173
+ const reference = end ?? now;
174
+ const elapsed = start !== null && reference !== null ? reference - start : null;
175
+
176
+ const timeProps = {
177
+ 'data-slot': 'elapsed-time',
178
+ dateTime: elapsed === null ? undefined : isoDuration(elapsed),
179
+ className: cn('tabular-nums', className),
180
+ ...props,
181
+ };
182
+ const resolvedLocale = locale ?? settings.locale;
183
+ const text =
184
+ elapsed === null || !readersClock
185
+ ? fallback
186
+ : format
187
+ ? format(elapsed)
188
+ : formatElapsed(elapsed, { locale: resolvedLocale });
189
+
190
+ if (tooltip === false || tooltip === null || start === null) {
191
+ return (
192
+ <time ref={ref} {...timeProps}>
193
+ {text}
194
+ </time>
195
+ );
196
+ }
197
+
198
+ const content =
199
+ typeof tooltip !== 'boolean' ? (
200
+ tooltip
201
+ ) : end === null ? (
202
+ new Date(start).toLocaleString(resolvedLocale, { timeZone: settings.timeZone })
203
+ ) : (
204
+ <TimeSpan start={start} end={end} locale={resolvedLocale} timeZone={settings.timeZone} />
205
+ );
206
+ const describedBy = [props['aria-describedby'], descriptionId].filter(Boolean).join(' ');
207
+
208
+ return (
209
+ <TimeTooltip
210
+ content={content}
211
+ focusable={focusable}
212
+ render={<time ref={ref} {...timeProps} aria-describedby={describedBy} />}
213
+ >
214
+ {text}
215
+ {/* A hidden child, so the component renders one element; aria-describedby still resolves it. */}
216
+ <span id={descriptionId} hidden>
217
+ {readersClock ? content : null}
218
+ </span>
219
+ </TimeTooltip>
220
+ );
221
+ }
222
+
223
+ export { ElapsedTime, type ElapsedTimeProps };
@@ -203,8 +203,8 @@ function FileDropzone({
203
203
  void addFiles(Array.from(event.dataTransfer.files));
204
204
  }}
205
205
  className={cn(
206
- 'border-border text-muted-foreground hover:border-foreground/40 flex min-h-32 cursor-pointer flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed px-6 py-8 text-center transition-[border-color,background-color,color] duration-(--transition-duration-fast) ease-(--ease-out)',
207
- 'peer-focus-visible:border-ring peer-focus-visible:ring-3 peer-focus-visible:ring-ring/50',
206
+ 'border-border text-muted-foreground hover:border-foreground/40 flex min-h-32 cursor-pointer flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed px-6 py-8 text-center transition-[border-color,background-color,color] duration-fast ease-out',
207
+ 'peer-focus-visible:outline-solid peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-ring',
208
208
  'data-dragging:border-foreground data-dragging:bg-accent data-dragging:text-accent-foreground',
209
209
  'data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-disabled:opacity-50',
210
210
  )}