@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
@@ -1,9 +1,11 @@
1
1
  'use client';
2
2
 
3
3
  import { Button } from '@vendure-io/ui/components/atoms/button';
4
+ import { useFormatSettings } from '@vendure-io/ui/components/molecules/format-provider';
4
5
  import { cn } from '@vendure-io/ui/lib/utils';
5
6
  import { FileIcon, UploadCloudIcon, XIcon } from 'lucide-react';
6
7
  import * as React from 'react';
8
+ import { defaultMessages, useMessages } from '../../lib/format-messages.ts';
7
9
 
8
10
  type FileValidationResult = string | readonly string[] | null | undefined;
9
11
  type FileValidator = (file: File) => FileValidationResult | Promise<FileValidationResult>;
@@ -13,8 +15,40 @@ interface FileRejection {
13
15
  messages: string[];
14
16
  }
15
17
 
16
- interface FileDropzoneProps extends Omit<React.ComponentProps<'div'>, 'onChange'> {
18
+ /**
19
+ * The texts of a FileDropzone. Each one is optional and wins over
20
+ * `FormatProvider`'s `messages.fileDropzone`, which wins over English.
21
+ * Texts with a value in them are functions here (and `{name}`, `{size}`,
22
+ * `{max}` placeholders in `messages`).
23
+ */
24
+ interface FileDropzoneLabels {
25
+ /** @default `messages.fileDropzone.typeNotAccepted`, "This file type is not accepted." */
26
+ typeNotAccepted?: string;
27
+ /** Receives the largest allowed size, already formatted. @default `messages.fileDropzone.fileTooLarge`, "File size must be {size} or smaller." */
28
+ fileTooLarge?: (size: string) => string;
29
+ /** Receives the file limit. @default `messages.fileDropzone.tooManyFiles` ("You can select up to {max} files.") and `tooManyFilesOne` for a limit of one */
30
+ tooManyFiles?: (max: number) => string;
31
+ /** Shown when `validateFile` throws. @default `messages.fileDropzone.validationFailed`, "This file could not be validated." */
32
+ validationFailed?: string;
33
+ /** Replaces the drop prompt while `validateFile` runs. @default `messages.fileDropzone.checking`, "Checking files…" */
34
+ checking?: string;
35
+ /** Placed before the `accept` list. @default `messages.fileDropzone.accepted`, "Accepted:" */
36
+ accepted?: string;
37
+ /** Accessible name of a file's remove button. Receives the file name. @default `messages.fileDropzone.remove`, "Remove {name}" */
38
+ remove?: (name: string) => string;
39
+ /** @default `messages.fileDropzone.byte`, "B" */
40
+ byte?: string;
41
+ /** @default `messages.fileDropzone.kilobyte`, "KB" */
42
+ kilobyte?: string;
43
+ /** @default `messages.fileDropzone.megabyte`, "MB" */
44
+ megabyte?: string;
45
+ }
46
+
47
+ interface FileDropzoneProps extends Omit<React.ComponentProps<'div'>, 'defaultValue' | 'onChange'> {
48
+ /** Selected files. Pass `value` to control the dropzone. Leave the prop out and it keeps its own state. */
17
49
  value?: readonly File[];
50
+ /** Initial files when `value` is not passed. */
51
+ defaultValue?: readonly File[];
18
52
  onValueChange?: (files: File[]) => void;
19
53
  onRejected?: (rejections: FileRejection[]) => void;
20
54
  accept?: string;
@@ -26,11 +60,16 @@ interface FileDropzoneProps extends Omit<React.ComponentProps<'div'>, 'onChange'
26
60
  required?: boolean;
27
61
  name?: string;
28
62
  id?: string;
63
+ /** @default `messages.fileDropzone.label`, "Upload files" */
29
64
  label?: React.ReactNode;
30
65
  description?: React.ReactNode;
66
+ /** @default `messages.fileDropzone.empty`, "Drag and drop files here, or choose files" */
31
67
  emptyLabel?: React.ReactNode;
68
+ /** @default `messages.fileDropzone.drag`, "Drop files to add them" */
32
69
  dragLabel?: React.ReactNode;
33
70
  renderFile?: (file: File, remove: () => void) => React.ReactNode;
71
+ /** The component's own texts: rejection messages, status text, remove button and size units. */
72
+ labels?: FileDropzoneLabels;
34
73
  }
35
74
 
36
75
  function fileMatchesAccept(file: File, accept: string | undefined): boolean {
@@ -46,10 +85,36 @@ function fileMatchesAccept(file: File, accept: string | undefined): boolean {
46
85
  });
47
86
  }
48
87
 
49
- function formatFileSize(bytes: number): string {
50
- if (bytes < 1024) return `${bytes} B`;
51
- if (bytes < 1024 ** 2) return `${Math.round(bytes / 1024)} KB`;
52
- return `${(bytes / 1024 ** 2).toFixed(1)} MB`;
88
+ interface FormatFileSizeOptions {
89
+ /** BCP 47 locale of the number. Default: the runtime locale. */
90
+ locale?: string;
91
+ /** Unit texts. Default: `B`, `KB` and `MB`. */
92
+ units?: { byte?: string; kilobyte?: string; megabyte?: string };
93
+ }
94
+
95
+ /**
96
+ * Whole bytes below 1 KB, whole KB below 1 MB, MB with one decimal above. The
97
+ * number follows `locale`; the unit texts are English unless `units` is passed.
98
+ */
99
+ function formatFileSize(bytes: number, { locale, units }: FormatFileSizeOptions = {}): string {
100
+ const defaults = defaultMessages.fileDropzone;
101
+ const format = (value: number, fractionDigits: number) =>
102
+ new Intl.NumberFormat(locale, {
103
+ minimumFractionDigits: fractionDigits,
104
+ maximumFractionDigits: fractionDigits,
105
+ useGrouping: false,
106
+ }).format(value);
107
+ if (bytes < 1024) return `${format(bytes, 0)} ${units?.byte ?? defaults.byte}`;
108
+ if (bytes < 1024 ** 2)
109
+ return `${format(bytes / 1024, 0)} ${units?.kilobyte ?? defaults.kilobyte}`;
110
+ return `${format(bytes / 1024 ** 2, 1)} ${units?.megabyte ?? defaults.megabyte}`;
111
+ }
112
+
113
+ /** Replaces each `{key}` in a message with its value. */
114
+ function fillMessage(template: string, values: Record<string, string | number>): string {
115
+ return template.replace(/\{(\w+)\}/g, (match, key: string) =>
116
+ key in values ? String(values[key]) : match,
117
+ );
53
118
  }
54
119
 
55
120
  function normalizeValidationResult(result: FileValidationResult): string[] {
@@ -57,32 +122,60 @@ function normalizeValidationResult(result: FileValidationResult): string[] {
57
122
  return typeof result === 'string' ? [result] : [...result];
58
123
  }
59
124
 
125
+ const NO_FILES: readonly File[] = [];
126
+
60
127
  /**
61
- * Controlled, transport-agnostic file input. It owns accessible picking,
128
+ * Transport-agnostic file input, controlled with `value` or uncontrolled with
129
+ * `defaultValue`. It owns accessible picking,
62
130
  * drag-and-drop, limits, validation, errors, and removal; consumers own upload
63
131
  * persistence and can replace the selected-file rendering with `renderFile`.
64
132
  */
65
- function FileDropzone({
66
- value = [],
67
- onValueChange,
68
- onRejected,
69
- accept,
70
- multiple = false,
71
- maxFiles = multiple ? Number.POSITIVE_INFINITY : 1,
72
- maxSize,
73
- validateFile,
74
- disabled,
75
- required,
76
- name,
77
- id: providedId,
78
- label = 'Upload files',
79
- description,
80
- emptyLabel = 'Drag and drop files here, or choose files',
81
- dragLabel = 'Drop files to add them',
82
- renderFile,
83
- className,
84
- ...props
85
- }: FileDropzoneProps) {
133
+ function FileDropzone(allProps: FileDropzoneProps) {
134
+ const {
135
+ value: valueProp,
136
+ defaultValue,
137
+ onValueChange,
138
+ onRejected,
139
+ accept,
140
+ multiple = false,
141
+ maxFiles = multiple ? Number.POSITIVE_INFINITY : 1,
142
+ maxSize,
143
+ validateFile,
144
+ disabled,
145
+ required,
146
+ name,
147
+ id: providedId,
148
+ label,
149
+ description,
150
+ emptyLabel,
151
+ dragLabel,
152
+ renderFile,
153
+ labels,
154
+ className,
155
+ ...props
156
+ } = allProps;
157
+ // Controlled when the `value` prop is passed, even as `undefined` (no value).
158
+ const controlled = 'value' in allProps;
159
+ const [own, setOwn] = React.useState(defaultValue ?? NO_FILES);
160
+ const value = controlled ? (valueProp ?? NO_FILES) : own;
161
+ function commit(files: File[]) {
162
+ if (!controlled) setOwn(files);
163
+ onValueChange?.(files);
164
+ }
165
+ const { locale } = useFormatSettings();
166
+ const t = useMessages('fileDropzone', {
167
+ typeNotAccepted: labels?.typeNotAccepted,
168
+ validationFailed: labels?.validationFailed,
169
+ checking: labels?.checking,
170
+ accepted: labels?.accepted,
171
+ byte: labels?.byte,
172
+ kilobyte: labels?.kilobyte,
173
+ megabyte: labels?.megabyte,
174
+ });
175
+ const sizeOptions = {
176
+ locale,
177
+ units: { byte: t.byte, kilobyte: t.kilobyte, megabyte: t.megabyte },
178
+ };
86
179
  const generatedId = React.useId();
87
180
  const id = providedId ?? generatedId;
88
181
  const descriptionId = `${id}-description`;
@@ -105,15 +198,16 @@ function FileDropzone({
105
198
 
106
199
  for (const file of incoming.slice(0, remaining)) {
107
200
  const messages: string[] = [];
108
- if (!fileMatchesAccept(file, accept)) messages.push('This file type is not accepted.');
201
+ if (!fileMatchesAccept(file, accept)) messages.push(t.typeNotAccepted);
109
202
  if (maxSize !== undefined && file.size > maxSize) {
110
- messages.push(`File size must be ${formatFileSize(maxSize)} or smaller.`);
203
+ const size = formatFileSize(maxSize, sizeOptions);
204
+ messages.push(labels?.fileTooLarge?.(size) ?? fillMessage(t.fileTooLarge, { size }));
111
205
  }
112
206
  if (validateFile) {
113
207
  try {
114
208
  messages.push(...normalizeValidationResult(await validateFile(file)));
115
209
  } catch {
116
- messages.push('This file could not be validated.');
210
+ messages.push(t.validationFailed);
117
211
  }
118
212
  }
119
213
  if (messages.length > 0) rejected.push({ file, messages });
@@ -124,7 +218,10 @@ function FileDropzone({
124
218
  for (const file of incoming.slice(remaining)) {
125
219
  rejected.push({
126
220
  file,
127
- messages: [`You can select up to ${maxFiles} ${maxFiles === 1 ? 'file' : 'files'}.`],
221
+ messages: [
222
+ labels?.tooManyFiles?.(maxFiles) ??
223
+ fillMessage(maxFiles === 1 ? t.tooManyFilesOne : t.tooManyFiles, { max: maxFiles }),
224
+ ],
128
225
  });
129
226
  }
130
227
  }
@@ -134,12 +231,12 @@ function FileDropzone({
134
231
  setValidating(false);
135
232
  if (rejected.length > 0) onRejected?.(rejected);
136
233
  if (accepted.length > 0) {
137
- onValueChange?.(multiple ? [...value, ...accepted] : accepted.slice(0, 1));
234
+ commit(multiple ? [...value, ...accepted] : accepted.slice(0, 1));
138
235
  }
139
236
  }
140
237
 
141
238
  function removeFile(index: number) {
142
- onValueChange?.(value.filter((_, fileIndex) => fileIndex !== index));
239
+ commit(value.filter((_, fileIndex) => fileIndex !== index));
143
240
  setRejections([]);
144
241
  }
145
242
 
@@ -147,8 +244,8 @@ function FileDropzone({
147
244
  <div data-slot="file-dropzone" className={cn('flex flex-col gap-3', className)} {...props}>
148
245
  <div>
149
246
  <label htmlFor={id} className="text-sm font-medium">
150
- {label}
151
- {required ? <span className="text-destructive"> *</span> : null}
247
+ {label !== undefined ? label : t.label}
248
+ {required ? <span className="text-destructive-text"> *</span> : null}
152
249
  </label>
153
250
  {description ? (
154
251
  <p id={descriptionId} className="text-muted-foreground mt-1 text-sm">
@@ -211,9 +308,17 @@ function FileDropzone({
211
308
  >
212
309
  <UploadCloudIcon className="size-7" />
213
310
  <span className="text-foreground text-sm font-medium">
214
- {validating ? 'Checking files…' : dragging ? dragLabel : emptyLabel}
311
+ {validating
312
+ ? t.checking
313
+ : dragging
314
+ ? dragLabel !== undefined
315
+ ? dragLabel
316
+ : t.drag
317
+ : emptyLabel !== undefined
318
+ ? emptyLabel
319
+ : t.empty}
215
320
  </span>
216
- {accept ? <span className="text-xs">Accepted: {accept}</span> : null}
321
+ {accept ? <span className="text-xs">{`${t.accepted} ${accept}`}</span> : null}
217
322
  </label>
218
323
 
219
324
  {value.length > 0 ? (
@@ -229,14 +334,18 @@ function FileDropzone({
229
334
  <FileIcon className="text-muted-foreground size-4 shrink-0" />
230
335
  <div className="min-w-0 flex-1">
231
336
  <p className="truncate text-sm font-medium">{file.name}</p>
232
- <p className="text-muted-foreground text-xs">{formatFileSize(file.size)}</p>
337
+ <p className="text-muted-foreground text-xs">
338
+ {formatFileSize(file.size, sizeOptions)}
339
+ </p>
233
340
  </div>
234
341
  <Button
235
342
  type="button"
236
343
  variant="ghost"
237
344
  size="icon-xs"
238
345
  disabled={disabled}
239
- aria-label={`Remove ${file.name}`}
346
+ aria-label={
347
+ labels?.remove?.(file.name) ?? fillMessage(t.remove, { name: file.name })
348
+ }
240
349
  onClick={() => removeFile(index)}
241
350
  >
242
351
  <XIcon />
@@ -249,7 +358,7 @@ function FileDropzone({
249
358
  ) : null}
250
359
 
251
360
  {rejections.length > 0 ? (
252
- <div id={errorId} role="alert" className="text-destructive text-sm">
361
+ <div id={errorId} role="alert" className="text-destructive-text text-sm">
253
362
  {rejections.map(({ file, messages }, index) => (
254
363
  <p key={`${file.name}-${file.size}-${index}`}>
255
364
  <span className="font-medium">{file.name}:</span> {messages.join(' ')}
@@ -265,7 +374,9 @@ export {
265
374
  FileDropzone,
266
375
  fileMatchesAccept,
267
376
  formatFileSize,
377
+ type FileDropzoneLabels,
268
378
  type FileDropzoneProps,
269
379
  type FileRejection,
380
+ type FormatFileSizeOptions,
270
381
  type FileValidator,
271
382
  };
@@ -2,13 +2,15 @@
2
2
 
3
3
  import { createContext, type ReactNode, useContext, useMemo } from 'react';
4
4
  import type { Locale } from 'react-day-picker';
5
+ import type { FormatMessages } from '../../lib/format-messages.ts';
5
6
 
6
7
  // The design system cannot depend on any app's i18n. Formatters resolve their
7
8
  // locale/currency in a fixed order — explicit prop → this context → `undefined`
8
9
  // (a valid Intl argument that falls back to the runtime default locale) — so a
9
10
  // bare `<Money>` or `<DateTime>` works with zero setup, and an app that mounts
10
11
  // `FormatProvider` (e.g. from its own locale + server config) gets consistent
11
- // output everywhere without threading props.
12
+ // output everywhere without threading props. Texts follow the same order
13
+ // through `messages` (see `lib/format-messages.ts`).
12
14
  interface FormatContextValue {
13
15
  /** BCP 47 locale tag, e.g. `en-GB`. */
14
16
  locale?: string;
@@ -30,6 +32,13 @@ interface FormatContextValue {
30
32
  * `undefined` → US English. A `Calendar`'s own `locale` prop wins.
31
33
  */
32
34
  calendarLocale?: Locale;
35
+ /**
36
+ * The library's texts in the active language, nested by component, for
37
+ * example `{ passwordInput: { showPassword: 'Passwort anzeigen' } }`. Every
38
+ * text is optional: a missing one stays English. A component's own label
39
+ * prop wins over this.
40
+ */
41
+ messages?: FormatMessages;
33
42
  }
34
43
 
35
44
  const FormatContext = createContext<FormatContextValue>({});
@@ -41,10 +50,11 @@ function FormatProvider({
41
50
  currency,
42
51
  currencyPrecision,
43
52
  calendarLocale,
53
+ messages,
44
54
  }: FormatContextValue & { children: ReactNode }) {
45
55
  const value = useMemo(
46
- () => ({ locale, timeZone, currency, currencyPrecision, calendarLocale }),
47
- [locale, timeZone, currency, currencyPrecision, calendarLocale],
56
+ () => ({ locale, timeZone, currency, currencyPrecision, calendarLocale, messages }),
57
+ [locale, timeZone, currency, currencyPrecision, calendarLocale, messages],
48
58
  );
49
59
  return <FormatContext.Provider value={value}>{children}</FormatContext.Provider>;
50
60
  }
@@ -53,4 +63,4 @@ function useFormatSettings(): FormatContextValue {
53
63
  return useContext(FormatContext);
54
64
  }
55
65
 
56
- export { FormatProvider, useFormatSettings, type FormatContextValue };
66
+ export { FormatProvider, useFormatSettings, type FormatContextValue, type FormatMessages };
@@ -0,0 +1,114 @@
1
+ 'use client';
2
+
3
+ import {
4
+ Tooltip,
5
+ TooltipContent,
6
+ TooltipLabelSwap,
7
+ TooltipTrigger,
8
+ } from '@vendure-io/ui/components/atoms/tooltip';
9
+ import type * as React from 'react';
10
+ import { contentInsetPull } from '../../lib/content-inset.ts';
11
+ import { cn } from '../../lib/utils.ts';
12
+
13
+ // Internal: closed in `package.json` exports. Only the identifier-chip family
14
+ // imports it, by a relative path.
15
+
16
+ /**
17
+ * Shared chrome for the identifier-chip family (`IdChip`, `AnonymizedToken`,
18
+ * `SecretValue`): a borderless `bg-muted`, monospace `text-xs` chip, 24px
19
+ * tall, sized to its content. Its `--content-inset` (the 8px start padding)
20
+ * lets a DataTable cell line the value up with the column header. Kept in
21
+ * one place so the chips can't drift
22
+ * apart: only their *contents* differ (a plain value, a blurred
23
+ * reveal-on-hover secret, or a masked secret loaded on click).
24
+ *
25
+ * A chip that copies as a whole (see `chipCopyButtonProps`) gets the hover
26
+ * fill and the press feedback here, keyed on that button, so a chip with
27
+ * several controls (`SecretValue`) keeps a still surface.
28
+ */
29
+ function chipChromeClass(copyable: boolean, className?: string): string {
30
+ return cn(
31
+ 'relative inline-flex min-h-6 w-fit items-center gap-1.5 rounded-lg bg-muted py-0.5 ps-2 [--content-inset:calc(var(--spacing)*2)] font-mono text-xs text-foreground-secondary',
32
+ 'transition-[background-color,color,scale] duration-quick ease-soft',
33
+ contentInsetPull,
34
+ 'has-data-chip-copy:cursor-pointer has-data-chip-copy:hover:bg-accent has-data-chip-copy:hover:text-foreground motion-safe:has-data-chip-copy:active:scale-[0.97]',
35
+ // The chip is the copy target, so the focus ring goes around the chip, not the 13px button.
36
+ 'has-[[data-chip-copy]:focus-visible]:outline-2 has-[[data-chip-copy]:focus-visible]:outline-offset-2 has-[[data-chip-copy]:focus-visible]:outline-ring has-[[data-chip-copy]:focus-visible]:outline-solid',
37
+ copyable ? 'pe-1.5' : 'pe-2',
38
+ className,
39
+ );
40
+ }
41
+
42
+ /**
43
+ * The look of an icon button inside an identifier chip: exactly the 13px
44
+ * icon, in the chip's text colour, with no fill on hover. The icon shows at
45
+ * 55%, quieter than the value beside it; a spinner (`role=status`) and the
46
+ * copied check keep full strength. The chip's 6px end padding is the visible
47
+ * inset. `IdChip`, `AnonymizedToken` and `SecretValue` all build on it, so
48
+ * their buttons cannot drift apart.
49
+ */
50
+ const chipIconButtonClass = cn(
51
+ "size-3.25 [&_svg:not([class*='size-'])]:size-3.25",
52
+ 'text-inherit hover:bg-transparent dark:hover:bg-transparent',
53
+ '[&>svg:not([role=status])]:opacity-55',
54
+ '[&_[data-slot=copy-state-icon]:not([data-copied])>svg:first-child]:opacity-55',
55
+ );
56
+
57
+ /**
58
+ * Props for a `CopyButton` that turns its whole chip into the copy target. Its
59
+ * `::after` stretches over the chip (the chip is `relative`), so a click
60
+ * anywhere on the chip copies, while the button stays the only tab stop and
61
+ * keeps its accessible name. The value text stays plain, readable text.
62
+ *
63
+ * It has the `chipIconButtonClass` look. Its focus ring moves to the chip
64
+ * (see `chipChromeClass`), and it keeps the chip's text colour on hover: the
65
+ * whole chip brightens instead.
66
+ *
67
+ * The button never scales on press (the chip does). Any `scale` other than
68
+ * `none` makes the button the containing block of its `::after`, which then
69
+ * shrinks to the 13px icon between mousedown and mouseup. A mouseup over the
70
+ * value text then lands outside the button, and the click goes to the chip
71
+ * instead of the button, so nothing is copied.
72
+ */
73
+ const chipCopyButtonProps = {
74
+ 'data-chip-copy': '',
75
+ className: cn(
76
+ chipIconButtonClass,
77
+ 'after:absolute after:inset-0 focus-visible:outline-none hover:text-inherit',
78
+ 'active:scale-none motion-reduce:active:scale-none',
79
+ ),
80
+ } as const;
81
+
82
+ /**
83
+ * Tooltip text of a whole-chip copy target: "Copy" until the copy succeeds,
84
+ * then "Copied", swapped in one motion by `TooltipLabelSwap`. Nothing else:
85
+ * the chip shows the start and end of its value, and a click copies all of it.
86
+ *
87
+ * The tooltip closes as soon as the pointer leaves the chip. In a list it can
88
+ * open below the chip and cover the chip in the next row; a tooltip that stays
89
+ * open while the pointer moves onto it would block that chip. Nothing in it
90
+ * needs the pointer.
91
+ */
92
+ function ChipCopyTooltip({
93
+ chip,
94
+ copied,
95
+ copyLabel,
96
+ copiedLabel,
97
+ }: {
98
+ chip: React.ReactElement;
99
+ copied: boolean;
100
+ copyLabel: string;
101
+ copiedLabel: string;
102
+ }) {
103
+ return (
104
+ <Tooltip disableHoverablePopup>
105
+ {/* The click that copies must not close the tooltip, or "Copied" is never seen. */}
106
+ <TooltipTrigger render={chip} closeOnClick={false} />
107
+ <TooltipContent>
108
+ <TooltipLabelSwap labels={[copyLabel, copiedLabel]} active={copied ? 1 : 0} />
109
+ </TooltipContent>
110
+ </Tooltip>
111
+ );
112
+ }
113
+
114
+ export { ChipCopyTooltip, chipChromeClass, chipCopyButtonProps, chipIconButtonClass };
@@ -1,116 +1,13 @@
1
1
  'use client';
2
2
 
3
- import {
4
- Tooltip,
5
- TooltipContent,
6
- TooltipLabelSwap,
7
- TooltipTrigger,
8
- } from '@vendure-io/ui/components/atoms/tooltip';
3
+ import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
9
4
  import { CopyButton, useCopyAction } from '@vendure-io/ui/components/molecules/copyable-text';
10
- import { contentInsetPull } from '@vendure-io/ui/lib/content-inset';
11
5
  import { cn } from '@vendure-io/ui/lib/utils';
12
- import type * as React from 'react';
6
+ import { useMessages } from '../../lib/format-messages.ts';
7
+ import { ChipCopyTooltip, chipChromeClass, chipCopyButtonProps } from './id-chip-chrome.tsx';
13
8
 
14
9
  type TruncateMode = 'middle' | 'start' | 'none';
15
10
 
16
- /**
17
- * Shared chrome for the identifier-chip family (`IdChip`, `AnonymizedToken`,
18
- * `SecretValue`): a borderless `bg-muted`, monospace `text-xs` chip, 24px
19
- * tall, sized to its content. Its `--content-inset` (the 8px start padding)
20
- * lets a DataTable cell line the value up with the column header. Kept in
21
- * one place so the chips can't drift
22
- * apart: only their *contents* differ (a plain value, a blurred
23
- * reveal-on-hover secret, or a masked secret loaded on click).
24
- *
25
- * A chip that copies as a whole (see `chipCopyButtonProps`) gets the hover
26
- * fill and the press feedback here, keyed on that button, so a chip with
27
- * several controls (`SecretValue`) keeps a still surface.
28
- */
29
- function chipChromeClass(copyable: boolean, className?: string): string {
30
- return cn(
31
- 'relative inline-flex min-h-6 w-fit items-center gap-1.5 rounded-lg bg-muted py-0.5 ps-2 [--content-inset:calc(var(--spacing)*2)] font-mono text-xs text-foreground-secondary',
32
- 'transition-[background-color,color,scale] duration-quick ease-soft',
33
- contentInsetPull,
34
- 'has-data-chip-copy:cursor-pointer has-data-chip-copy:hover:bg-accent has-data-chip-copy:hover:text-foreground motion-safe:has-data-chip-copy:active:scale-[0.97]',
35
- // The chip is the copy target, so the focus ring goes around the chip, not the 13px button.
36
- 'has-[[data-chip-copy]:focus-visible]:outline-2 has-[[data-chip-copy]:focus-visible]:outline-offset-2 has-[[data-chip-copy]:focus-visible]:outline-ring has-[[data-chip-copy]:focus-visible]:outline-solid',
37
- copyable ? 'pe-1.5' : 'pe-2',
38
- className,
39
- );
40
- }
41
-
42
- /**
43
- * The look of an icon button inside an identifier chip: exactly the 13px
44
- * icon, in the chip's text colour, with no fill on hover. The icon shows at
45
- * 55%, quieter than the value beside it; a spinner (`role=status`) and the
46
- * copied check keep full strength. The chip's 6px end padding is the visible
47
- * inset. `IdChip`, `AnonymizedToken` and `SecretValue` all build on it, so
48
- * their buttons cannot drift apart.
49
- */
50
- const chipIconButtonClass = cn(
51
- "size-3.25 [&_svg:not([class*='size-'])]:size-3.25",
52
- 'text-inherit hover:bg-transparent dark:hover:bg-transparent',
53
- '[&>svg:not([role=status])]:opacity-55',
54
- '[&_[data-slot=copy-state-icon]:not([data-copied])>svg:first-child]:opacity-55',
55
- );
56
-
57
- /**
58
- * Props for a `CopyButton` that turns its whole chip into the copy target. Its
59
- * `::after` stretches over the chip (the chip is `relative`), so a click
60
- * anywhere on the chip copies, while the button stays the only tab stop and
61
- * keeps its accessible name. The value text stays plain, readable text.
62
- *
63
- * It has the `chipIconButtonClass` look. Its focus ring moves to the chip
64
- * (see `chipChromeClass`), and it keeps the chip's text colour on hover: the
65
- * whole chip brightens instead.
66
- *
67
- * The button never scales on press (the chip does). Any `scale` other than
68
- * `none` makes the button the containing block of its `::after`, which then
69
- * shrinks to the 13px icon between mousedown and mouseup. A mouseup over the
70
- * value text then lands outside the button, and the click goes to the chip
71
- * instead of the button, so nothing is copied.
72
- */
73
- const chipCopyButtonProps = {
74
- 'data-chip-copy': '',
75
- className: cn(
76
- chipIconButtonClass,
77
- 'after:absolute after:inset-0 focus-visible:outline-none hover:text-inherit',
78
- 'active:scale-none motion-reduce:active:scale-none',
79
- ),
80
- } as const;
81
-
82
- /**
83
- * Tooltip text of a whole-chip copy target: "Copy" until the copy succeeds,
84
- * then "Copied", swapped in one motion by `TooltipLabelSwap`. Nothing else:
85
- * the chip shows the start and end of its value, and a click copies all of it.
86
- *
87
- * The tooltip closes as soon as the pointer leaves the chip. In a list it can
88
- * open below the chip and cover the chip in the next row; a tooltip that stays
89
- * open while the pointer moves onto it would block that chip. Nothing in it
90
- * needs the pointer.
91
- */
92
- function ChipCopyTooltip({
93
- chip,
94
- copied,
95
- copyLabel,
96
- copiedLabel,
97
- }: {
98
- chip: React.ReactElement;
99
- copied: boolean;
100
- copyLabel: string;
101
- copiedLabel: string;
102
- }) {
103
- return (
104
- <Tooltip disableHoverablePopup>
105
- {/* The click that copies must not close the tooltip, or "Copied" is never seen. */}
106
- <TooltipTrigger render={chip} closeOnClick={false} />
107
- <TooltipContent>
108
- <TooltipLabelSwap labels={[copyLabel, copiedLabel]} active={copied ? 1 : 0} />
109
- </TooltipContent>
110
- </Tooltip>
111
- );
112
- }
113
-
114
11
  // Middle truncation renders 13 chars (8 + ellipsis + 4), so anything at or
115
12
  // below that length would lose information without saving space — skip it.
116
13
  const TRUNCATE_THRESHOLD = 13;
@@ -162,10 +59,10 @@ interface IdChipProps {
162
59
  * The tooltip text before copying, e.g. "Copy commit". Keep it short and
163
60
  * leave the value out: the copy button's accessible name adds the shown
164
61
  * text itself ("Copy commit 9f2c4e1"), so each button in a list has its own
165
- * name. @default "Copy"
62
+ * name. @default `messages.idChip.copy`, "Copy"
166
63
  */
167
64
  copyLabel?: string;
168
- /** The tooltip text and the copy button's accessible name while the copied state is active. @default "Copied" */
65
+ /** The tooltip text and the copy button's accessible name while the copied state is active. @default `messages.idChip.copied`, "Copied" */
169
66
  copiedLabel?: string;
170
67
  /** How long the check-mark feedback stays visible, in ms. @default 2000 */
171
68
  timeout?: number;
@@ -186,11 +83,12 @@ function IdChip({
186
83
  className,
187
84
  onCopied,
188
85
  onCopyError,
189
- copyLabel = 'Copy',
190
- copiedLabel = 'Copied',
86
+ copyLabel,
87
+ copiedLabel,
191
88
  timeout,
192
89
  }: IdChipProps) {
193
90
  const action = useCopyAction({ timeout, onCopied, onCopyError });
91
+ const t = useMessages('idChip', { copy: copyLabel, copied: copiedLabel });
194
92
  const text = display ?? truncateId(value, truncate);
195
93
  const shortened = text !== value;
196
94
  const chip = (
@@ -207,8 +105,8 @@ function IdChip({
207
105
  <CopyButton
208
106
  value={value}
209
107
  copyAction={action}
210
- copyLabel={`${copyLabel} ${text}`}
211
- copiedLabel={copiedLabel}
108
+ copyLabel={`${t.copy} ${text}`}
109
+ copiedLabel={t.copied}
212
110
  {...chipCopyButtonProps}
213
111
  />
214
112
  ) : null}
@@ -222,8 +120,8 @@ function IdChip({
222
120
  <ChipCopyTooltip
223
121
  chip={chip}
224
122
  copied={action.copied}
225
- copyLabel={copyLabel}
226
- copiedLabel={copiedLabel}
123
+ copyLabel={t.copy}
124
+ copiedLabel={t.copied}
227
125
  />
228
126
  );
229
127
  }
@@ -242,5 +140,5 @@ function IdChip({
242
140
  );
243
141
  }
244
142
 
245
- export { ChipCopyTooltip, chipChromeClass, chipCopyButtonProps, chipIconButtonClass, IdChip };
143
+ export { IdChip };
246
144
  export type { IdChipProps };