@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,6 +9,7 @@ import { useFieldControlProps } from '@vendure-io/ui/components/atoms/field';
9
9
  import { cn } from '@vendure-io/ui/lib/utils';
10
10
  import { cva } from 'class-variance-authority';
11
11
  import { CheckIcon } from 'lucide-react';
12
+ import { motion, useReducedMotionConfig } from 'motion/react';
12
13
  import * as React from 'react';
13
14
 
14
15
  import { interactiveCardVariants } from './interactive-card.tsx';
@@ -25,13 +26,29 @@ import { interactiveCardVariants } from './interactive-card.tsx';
25
26
  // ring is an outline outside it, so both show together. The indicator (a radio
26
27
  // dot or a check box) shows the state in shape too, not only in colour.
27
28
  //
29
+ // In a single-choice group the selected edge is one element that moves: when
30
+ // the user picks another card, it glides from the old card to the new one
31
+ // (Motion `layoutId`, scoped to the group). See SelectedEdge for the timing.
32
+ //
28
33
  // The card's accessible name is its title, and its description is the
29
34
  // description plus the meta. Slots hold text and images only: a card is one
30
35
  // control, so a link or button inside it is not reachable as its own control.
31
36
 
32
37
  type ChoiceCardMode = 'single' | 'multiple';
33
38
 
34
- const ChoiceCardGroupContext = React.createContext<ChoiceCardMode>('single');
39
+ type ChoiceCardInput = 'pointer' | 'keyboard' | null;
40
+
41
+ interface ChoiceCardGroupState {
42
+ mode: ChoiceCardMode;
43
+ /** Single choice: the selected value, controlled or not. */
44
+ selected?: unknown;
45
+ /** Single choice: the layoutId of the group's selected edge. */
46
+ edgeId?: string;
47
+ /** The last input in the group: arrow keys move the edge without a glide. */
48
+ lastInput?: React.RefObject<ChoiceCardInput>;
49
+ }
50
+
51
+ const ChoiceCardGroupContext = React.createContext<ChoiceCardGroupState>({ mode: 'single' });
35
52
 
36
53
  type ChoiceCardGroupProps =
37
54
  | ({ multiple?: false } & RadioGroupPrimitive.Props)
@@ -49,7 +66,7 @@ function ChoiceCardGroup(props: ChoiceCardGroupProps) {
49
66
  if (fieldProps.multiple) {
50
67
  const { multiple: _, className, ...rest } = fieldProps;
51
68
  return (
52
- <ChoiceCardGroupContext.Provider value="multiple">
69
+ <ChoiceCardGroupContext.Provider value={MULTIPLE}>
53
70
  <CheckboxGroupPrimitive
54
71
  data-slot="choice-card-group"
55
72
  data-mode="multiple"
@@ -61,21 +78,102 @@ function ChoiceCardGroup(props: ChoiceCardGroupProps) {
61
78
  </ChoiceCardGroupContext.Provider>
62
79
  );
63
80
  }
64
- const { multiple: _, className, ...rest } = fieldProps;
81
+ return <SingleChoiceCardGroup {...fieldProps} />;
82
+ }
83
+
84
+ const MULTIPLE: ChoiceCardGroupState = { mode: 'multiple' };
85
+
86
+ function SingleChoiceCardGroup({
87
+ multiple: _,
88
+ className,
89
+ value,
90
+ defaultValue,
91
+ onValueChange,
92
+ onKeyDown,
93
+ onPointerDown,
94
+ ...rest
95
+ }: { multiple?: false } & RadioGroupPrimitive.Props) {
96
+ // The group keeps its own copy of the value so the selected card can host
97
+ // the moving edge, uncontrolled too.
98
+ const [uncontrolled, setUncontrolled] = React.useState(defaultValue);
99
+ const selected = value !== undefined ? value : uncontrolled;
100
+ const edgeId = React.useId();
101
+ const lastInput = React.useRef<ChoiceCardInput>(null);
102
+ const state = React.useMemo<ChoiceCardGroupState>(
103
+ () => ({ mode: 'single', selected, edgeId, lastInput }),
104
+ [selected, edgeId],
105
+ );
65
106
  return (
66
- <ChoiceCardGroupContext.Provider value="single">
107
+ <ChoiceCardGroupContext.Provider value={state}>
67
108
  <RadioGroupPrimitive
68
109
  data-slot="choice-card-group"
69
110
  data-mode="single"
70
- className={(state) =>
71
- cn(groupClassName, typeof className === 'function' ? className(state) : className)
111
+ className={(groupState) =>
112
+ cn(groupClassName, typeof className === 'function' ? className(groupState) : className)
72
113
  }
114
+ value={value}
115
+ defaultValue={defaultValue}
116
+ onValueChange={(next, details) => {
117
+ setUncontrolled(next);
118
+ onValueChange?.(next, details);
119
+ }}
120
+ onKeyDown={(event) => {
121
+ lastInput.current = 'keyboard';
122
+ onKeyDown?.(event);
123
+ }}
124
+ onPointerDown={(event) => {
125
+ lastInput.current = 'pointer';
126
+ onPointerDown?.(event);
127
+ }}
73
128
  {...rest}
74
129
  />
75
130
  </ChoiceCardGroupContext.Provider>
76
131
  );
77
132
  }
78
133
 
134
+ // The selected edge of a single-choice group: one element per group, inside
135
+ // the selected card, on the card's border box. When the selection changes it
136
+ // unmounts in the old card and mounts in the new one under the same layoutId,
137
+ // so Motion glides it between them.
138
+ // - A pointer pick glides on a spring with no bounce (0.25s). A new pick
139
+ // mid-flight retargets from where the edge is.
140
+ // - Arrow keys repeat, so the edge jumps there and fades in `fast`.
141
+ // - Reduced motion: no glide, the same `fast` fade.
142
+ // - The first render and a remount do not animate.
143
+ // The 2px line is an inset box-shadow, not a border: Motion corrects a shadow
144
+ // and a pixel radius while the edge stretches between cards of different
145
+ // sizes. The radius is read from the card, so it stays the card's token.
146
+ const GLIDE = { type: 'spring', visualDuration: 0.25, bounce: 0 } as const;
147
+ const FADE = { duration: 0.1, ease: [0.25, 0.1, 0.25, 1] } as const;
148
+
149
+ function SelectedEdge({ edgeId, input }: { edgeId: string; input: ChoiceCardInput }) {
150
+ const ref = React.useRef<HTMLSpanElement>(null);
151
+ const [radius, setRadius] = React.useState<number>();
152
+ React.useLayoutEffect(() => {
153
+ const card = ref.current?.parentElement;
154
+ if (card) setRadius(Number.parseFloat(getComputedStyle(card).borderTopLeftRadius));
155
+ }, []);
156
+ const reduce = useReducedMotionConfig();
157
+ // `input` is null on the first render of the group: nothing animates then.
158
+ const jump = reduce || input === 'keyboard';
159
+ return (
160
+ <motion.span
161
+ ref={ref}
162
+ layoutId={edgeId}
163
+ aria-hidden="true"
164
+ data-slot="choice-card-edge"
165
+ className="pointer-events-none absolute -inset-px z-10 rounded-[inherit]"
166
+ style={{
167
+ boxShadow: 'inset 0 0 0 2px var(--primary)',
168
+ ...(radius !== undefined ? { borderRadius: radius } : null),
169
+ }}
170
+ initial={jump && input !== null ? { opacity: 0 } : false}
171
+ animate={{ opacity: 1 }}
172
+ transition={jump ? { layout: { duration: 0 }, opacity: FADE } : { layout: GLIDE }}
173
+ />
174
+ );
175
+ }
176
+
79
177
  // The ids the card points its aria-labelledby and aria-describedby at. The
80
178
  // title is the name from the first render, so a card without a ChoiceCardTitle
81
179
  // needs an aria-label (the media variant). The description and the meta
@@ -105,13 +203,14 @@ function useChoiceCardPart(part: 'title' | 'description' | 'meta') {
105
203
  const choiceCardVariants = cva(
106
204
  [
107
205
  'group/choice-card flex text-start',
108
- // Selected: one 2px inset primary stroke on ::after. The ::after sits on
206
+ // Selected, multiple choice: one 2px inset primary stroke on ::after (a
207
+ // single-choice group draws a SelectedEdge instead, which moves). The ::after sits on
109
208
  // the border box (-inset-px) with the card's radius, so it covers the
110
209
  // border and adds the inner pixel in one curve. On the padding box
111
210
  // (inset-0) its corners would take the outer radius and leave a gap
112
211
  // between two strokes. So the card never clips its children: the media
113
212
  // variant rounds the image itself to the padding box instead.
114
- 'after:pointer-events-none after:absolute after:-inset-px after:z-10 after:rounded-[inherit] after:[transition:box-shadow_var(--transition-duration-fast)_var(--ease-soft)] data-checked:border-primary data-checked:hover:border-primary data-checked:after:shadow-[inset_0_0_0_2px_var(--primary)]',
213
+ 'after:pointer-events-none after:absolute after:-inset-px after:z-10 after:rounded-[inherit] after:[transition:box-shadow_var(--transition-duration-fast)_var(--ease-soft)] data-[mode=multiple]:data-checked:border-primary data-[mode=multiple]:data-checked:hover:border-primary data-[mode=multiple]:data-checked:after:shadow-[inset_0_0_0_2px_var(--primary)]',
115
214
  ],
116
215
  {
117
216
  variants: {
@@ -208,7 +307,9 @@ function ChoiceCard({
208
307
  children,
209
308
  ...props
210
309
  }: ChoiceCardProps) {
211
- const mode = React.useContext(ChoiceCardGroupContext);
310
+ const group = React.useContext(ChoiceCardGroupContext);
311
+ const mode = group.mode;
312
+ const showEdge = mode === 'single' && group.edgeId !== undefined && group.selected === value;
212
313
  const id = React.useId();
213
314
  const [mounted, setMounted] = React.useState<ReadonlySet<string>>(new Set());
214
315
  const register = React.useCallback((part: string) => {
@@ -249,6 +350,7 @@ function ChoiceCard({
249
350
  'data-slot': 'choice-card',
250
351
  'data-variant': variant,
251
352
  'data-size': size,
353
+ 'data-mode': mode,
252
354
  className: cn(interactiveCardVariants(), choiceCardVariants({ variant, size }), className),
253
355
  render: <div />,
254
356
  value,
@@ -258,6 +360,9 @@ function ChoiceCard({
258
360
  const content = (
259
361
  <ChoiceCardPartsContext.Provider value={parts}>
260
362
  <ChoiceCardIndicator mode={mode} />
363
+ {showEdge && group.edgeId ? (
364
+ <SelectedEdge edgeId={group.edgeId} input={group.lastInput?.current ?? null} />
365
+ ) : null}
261
366
  {children}
262
367
  </ChoiceCardPartsContext.Provider>
263
368
  );
@@ -0,0 +1,50 @@
1
+ import { migrateStorageKey } from '../../../lib/deprecated.ts';
2
+ import { PACKAGE_MANAGERS, type PackageManager } from './transform-command.tsx';
3
+
4
+ const STORAGE_KEY = 'vendure-ui:code-block:package-manager';
5
+ const STORAGE_EVENT = 'vendure-ui:code-block:package-manager-change';
6
+ // The 2.x key and event. The key moves on read; the event is sent under both
7
+ // names, so a 2.x listener still hears it. Remove in 4.0.
8
+ const OLD_STORAGE_KEY = 'vendure-ui-package-manager';
9
+ const OLD_STORAGE_EVENT = 'vendure-ui-package-manager-change';
10
+
11
+ /**
12
+ * Get the stored package manager preference from localStorage
13
+ */
14
+ function getStoredPackageManager(): PackageManager {
15
+ if (typeof window === 'undefined') return 'npm';
16
+ migrateStorageKey('localStorage', OLD_STORAGE_KEY, STORAGE_KEY);
17
+ try {
18
+ const stored = localStorage.getItem(STORAGE_KEY);
19
+ if (stored && PACKAGE_MANAGERS.includes(stored as PackageManager)) {
20
+ return stored as PackageManager;
21
+ }
22
+ } catch {
23
+ // localStorage might not be available
24
+ }
25
+ return 'npm';
26
+ }
27
+
28
+ /**
29
+ * Store the package manager preference in localStorage
30
+ */
31
+ function setStoredPackageManager(pm: PackageManager): void {
32
+ if (typeof window === 'undefined') return;
33
+ try {
34
+ localStorage.setItem(STORAGE_KEY, pm);
35
+ // Dispatch a custom event so other CodeBlock instances can update
36
+ window.dispatchEvent(new CustomEvent(STORAGE_EVENT, { detail: pm }));
37
+ window.dispatchEvent(new CustomEvent(OLD_STORAGE_EVENT, { detail: pm }));
38
+ } catch {
39
+ // localStorage might not be available
40
+ }
41
+ }
42
+
43
+ export {
44
+ getStoredPackageManager,
45
+ OLD_STORAGE_EVENT,
46
+ OLD_STORAGE_KEY,
47
+ STORAGE_EVENT,
48
+ STORAGE_KEY,
49
+ setStoredPackageManager,
50
+ };
@@ -1,4 +1,4 @@
1
- import { supportsNotationComments } from '@vendure-io/ui/lib/highlight';
1
+ import { supportsNotationComments } from '../../../lib/highlight.ts';
2
2
 
3
3
  /**
4
4
  * Process code to extract filename directive and strip Shiki notations for unsupported languages.
@@ -5,12 +5,8 @@ import { DirectionProvider } from '@vendure-io/ui/components/atoms/direction';
5
5
  import { ScrollArea, ScrollBar } from '@vendure-io/ui/components/atoms/scroll-area';
6
6
  import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
7
7
  import { useCopyFeedback } from '@vendure-io/ui/components/molecules/copy-feedback-provider';
8
+ import { useFormatSettings } from '@vendure-io/ui/components/molecules/format-provider';
8
9
  import { useCopy } from '@vendure-io/ui/hooks/use-copy';
9
- import {
10
- highlightCode,
11
- normalizeLanguage,
12
- type SupportedLanguage,
13
- } from '@vendure-io/ui/lib/highlight';
14
10
  import { cn } from '@vendure-io/ui/lib/utils';
15
11
  import {
16
12
  CheckIcon,
@@ -25,7 +21,14 @@ import { motion, useReducedMotion } from 'motion/react';
25
21
  import { type ComponentProps, type ReactNode, useEffect, useId, useMemo, useState } from 'react';
26
22
  // Relative imports: `code-block/*` is closed in the package exports, so a
27
23
  // `@vendure-io/ui/...` self-reference to these modules fails in consumers.
24
+ import { useMessages } from '../../lib/format-messages.ts';
25
+ import { highlightCode, normalizeLanguage, type SupportedLanguage } from '../../lib/highlight.ts';
28
26
  import { FileTypeIcon, matchFileTypeIcon } from './code-block/file-type-icons.tsx';
27
+ import {
28
+ getStoredPackageManager,
29
+ STORAGE_EVENT,
30
+ setStoredPackageManager,
31
+ } from './code-block/package-manager-storage.ts';
29
32
  import { processCode } from './code-block/process-code.ts';
30
33
  import {
31
34
  isPackageManagerCommand,
@@ -34,6 +37,31 @@ import {
34
37
  transformCommand,
35
38
  } from './code-block/transform-command.tsx';
36
39
 
40
+ /**
41
+ * The texts of a CodeBlock. Each one falls back to `FormatProvider`'s
42
+ * `messages.codeBlock`, then to English.
43
+ */
44
+ interface CodeBlockLabels {
45
+ /** Label of the copy button. @default `messages.codeBlock.copyCode`, "Copy code" */
46
+ copyCode?: string;
47
+ /** Label of the copy button after a copy. @default `messages.codeBlock.copied`, "Copied!" */
48
+ copied?: string;
49
+ /** Header of a shell block with no filename and no package-manager tabs. @default `messages.codeBlock.terminal`, "Terminal" */
50
+ terminal?: string;
51
+ /** Header of a block with no filename and no language. @default `messages.codeBlock.code`, "Code" */
52
+ code?: string;
53
+ /** Label of the button that collapses a long block. @default `messages.codeBlock.showLess`, "Show less" */
54
+ showLess?: string;
55
+ /**
56
+ * Label of the button that expands a long block. `{count}` is replaced by the
57
+ * number of hidden lines, formatted with `FormatProvider`'s `locale`.
58
+ * @default `messages.codeBlock.showMore`, "Show {count} more lines"
59
+ */
60
+ showMore?: string;
61
+ }
62
+
63
+ type CodeBlockTexts = Required<CodeBlockLabels>;
64
+
37
65
  type CodeBlockProps = Omit<ComponentProps<'div'>, 'children'> & {
38
66
  /**
39
67
  * The code to render, as a plain string. Anything else (elements, numbers)
@@ -63,6 +91,8 @@ type CodeBlockProps = Omit<ComponentProps<'div'>, 'children'> & {
63
91
  onCopied?: () => void;
64
92
  /** Called when the clipboard write fails. Falls back to `CopyFeedbackProvider`. */
65
93
  onCopyError?: (error: Error) => void;
94
+ /** Translate the texts of the header, the copy button and the expander. Each text also reads `FormatProvider`'s `messages.codeBlock`. */
95
+ labels?: CodeBlockLabels;
66
96
  };
67
97
 
68
98
  interface CodeBlockActionProps {
@@ -72,42 +102,9 @@ interface CodeBlockActionProps {
72
102
  disabled?: boolean;
73
103
  }
74
104
 
75
- const STORAGE_KEY = 'vendure-ui-package-manager';
76
- const STORAGE_EVENT = 'vendure-ui-package-manager-change';
77
-
78
105
  const COLLAPSE_THRESHOLD = 40; // Lines needed to trigger collapse
79
106
  const COLLAPSED_HEIGHT = 300; // px height when collapsed (roughly 15-20 lines)
80
107
 
81
- /**
82
- * Get the stored package manager preference from localStorage
83
- */
84
- function getStoredPackageManager(): PackageManager {
85
- if (typeof window === 'undefined') return 'npm';
86
- try {
87
- const stored = localStorage.getItem(STORAGE_KEY);
88
- if (stored && PACKAGE_MANAGERS.includes(stored as PackageManager)) {
89
- return stored as PackageManager;
90
- }
91
- } catch {
92
- // localStorage might not be available
93
- }
94
- return 'npm';
95
- }
96
-
97
- /**
98
- * Store the package manager preference in localStorage
99
- */
100
- function setStoredPackageManager(pm: PackageManager): void {
101
- if (typeof window === 'undefined') return;
102
- try {
103
- localStorage.setItem(STORAGE_KEY, pm);
104
- // Dispatch a custom event so other CodeBlock instances can update
105
- window.dispatchEvent(new CustomEvent(STORAGE_EVENT, { detail: pm }));
106
- } catch {
107
- // localStorage might not be available
108
- }
109
- }
110
-
111
108
  /**
112
109
  * Custom hook to manage package manager state with localStorage persistence
113
110
  */
@@ -220,9 +217,10 @@ interface CodeBlockToolbarProps {
220
217
  onCopy: () => void;
221
218
  isCopied: boolean;
222
219
  actions?: ReactNode;
220
+ texts: CodeBlockTexts;
223
221
  }
224
222
 
225
- function CodeBlockToolbar({ onCopy, isCopied, actions }: CodeBlockToolbarProps) {
223
+ function CodeBlockToolbar({ onCopy, isCopied, actions, texts }: CodeBlockToolbarProps) {
226
224
  return (
227
225
  <div className="flex items-center gap-0.5">
228
226
  {actions}
@@ -230,7 +228,7 @@ function CodeBlockToolbar({ onCopy, isCopied, actions }: CodeBlockToolbarProps)
230
228
  icon={
231
229
  isCopied ? <CheckIcon className="size-4 text-success" /> : <CopyIcon className="size-4" />
232
230
  }
233
- label={isCopied ? 'Copied!' : 'Copy code'}
231
+ label={isCopied ? texts.copied : texts.copyCode}
234
232
  onClick={onCopy}
235
233
  />
236
234
  </div>
@@ -299,6 +297,7 @@ interface CodeBlockHeaderProps {
299
297
  onSelectManager?: (pm: PackageManager) => void;
300
298
  toolbar: ReactNode;
301
299
  id: string;
300
+ texts: CodeBlockTexts;
302
301
  }
303
302
 
304
303
  function CodeBlockHeader({
@@ -310,6 +309,7 @@ function CodeBlockHeader({
310
309
  onSelectManager,
311
310
  toolbar,
312
311
  id,
312
+ texts,
313
313
  }: CodeBlockHeaderProps) {
314
314
  const getLabel = () => {
315
315
  if (filename) {
@@ -332,7 +332,7 @@ function CodeBlockHeader({
332
332
  return (
333
333
  <div className="text-muted-foreground flex items-center gap-1.5 text-sm">
334
334
  <TerminalIcon className="size-3.5" />
335
- <span>Terminal</span>
335
+ <span>{texts.terminal}</span>
336
336
  </div>
337
337
  );
338
338
  }
@@ -340,7 +340,7 @@ function CodeBlockHeader({
340
340
  return (
341
341
  <div className="text-muted-foreground flex items-center gap-1.5 text-sm">
342
342
  <CodeIcon className="size-3.5" />
343
- <span>{language ? language.charAt(0).toUpperCase() + language.slice(1) : 'Code'}</span>
343
+ <span>{language ? language.charAt(0).toUpperCase() + language.slice(1) : texts.code}</span>
344
344
  </div>
345
345
  );
346
346
  };
@@ -373,9 +373,11 @@ interface ExpandButtonProps {
373
373
  isExpanded: boolean;
374
374
  hiddenLineCount: number;
375
375
  onToggle: () => void;
376
+ texts: CodeBlockTexts;
376
377
  }
377
378
 
378
- function ExpandButton({ isExpanded, hiddenLineCount, onToggle }: ExpandButtonProps) {
379
+ function ExpandButton({ isExpanded, hiddenLineCount, onToggle, texts }: ExpandButtonProps) {
380
+ const { locale } = useFormatSettings();
379
381
  return (
380
382
  <div className="border-border bg-muted/30 flex items-center justify-center border-t py-2">
381
383
  <Button
@@ -387,12 +389,15 @@ function ExpandButton({ isExpanded, hiddenLineCount, onToggle }: ExpandButtonPro
387
389
  {isExpanded ? (
388
390
  <>
389
391
  <ChevronUpIcon className="size-3.5" />
390
- Show less
392
+ {texts.showLess}
391
393
  </>
392
394
  ) : (
393
395
  <>
394
396
  <ChevronDownIcon className="size-3.5" />
395
- Show {hiddenLineCount} more lines
397
+ {texts.showMore.replace(
398
+ '{count}',
399
+ new Intl.NumberFormat(locale).format(hiddenLineCount),
400
+ )}
396
401
  </>
397
402
  )}
398
403
  </Button>
@@ -473,8 +478,10 @@ export function CodeBlock({
473
478
  actions,
474
479
  onCopied,
475
480
  onCopyError,
481
+ labels,
476
482
  ...props
477
483
  }: CodeBlockProps) {
484
+ const texts = useMessages('codeBlock', labels);
478
485
  const { copied, copy } = useCopy();
479
486
  const copyFeedback = useCopyFeedback();
480
487
  const [activePackageManager, setActivePackageManager] = usePackageManager();
@@ -516,7 +523,9 @@ export function CodeBlock({
516
523
  else (onCopyError ?? copyFeedback.onCopyError)?.(new Error('Failed to copy to the clipboard'));
517
524
  };
518
525
 
519
- const toolbar = <CodeBlockToolbar onCopy={handleCopy} isCopied={copied} actions={actions} />;
526
+ const toolbar = (
527
+ <CodeBlockToolbar onCopy={handleCopy} isCopied={copied} actions={actions} texts={texts} />
528
+ );
520
529
 
521
530
  const showCollapsed = isCollapsible && !isExpanded;
522
531
 
@@ -539,6 +548,7 @@ export function CodeBlock({
539
548
  onSelectManager={setActivePackageManager}
540
549
  toolbar={toolbar}
541
550
  id={instanceId}
551
+ texts={texts}
542
552
  />
543
553
  )}
544
554
 
@@ -591,6 +601,7 @@ export function CodeBlock({
591
601
  isExpanded={isExpanded}
592
602
  hiddenLineCount={hiddenLineCount}
593
603
  onToggle={() => setIsExpanded(!isExpanded)}
604
+ texts={texts}
594
605
  />
595
606
  )}
596
607
  </div>
@@ -598,4 +609,4 @@ export function CodeBlock({
598
609
  }
599
610
 
600
611
  export { CodeBlockAction };
601
- export type { CodeBlockProps, CodeBlockActionProps, PackageManager };
612
+ export type { CodeBlockProps, CodeBlockActionProps, CodeBlockLabels, PackageManager };
@@ -11,9 +11,11 @@ import {
11
11
  InputGroupAddon,
12
12
  InputGroupInput,
13
13
  } from '@vendure-io/ui/components/atoms/input-group';
14
- import { Spinner } from '@vendure-io/ui/components/atoms/spinner';
15
14
  import { cn } from '@vendure-io/ui/lib/utils';
15
+ import { Loader2Icon } from 'lucide-react';
16
16
  import * as React from 'react';
17
+ import { warnDeprecated } from '../../lib/deprecated.ts';
18
+ import { useMessages } from '../../lib/format-messages.ts';
17
19
 
18
20
  export interface ComboboxFreeTextItem {
19
21
  /**
@@ -49,16 +51,34 @@ export interface ComboboxFreeTextProps<T extends ComboboxFreeTextItem = Combobox
49
51
  * debounced against `value` — and they are assumed to be already filtered
50
52
  * (the component does no client-side filtering). Items may carry extra fields
51
53
  * beyond `value`/`label`/`description`; those flow through to `onSelectItem`.
54
+ *
55
+ * `undefined` means the first fetch has not returned yet: the field shows
56
+ * its spinner and the open list shows a loading row, one suggestion tall.
57
+ * `[]` means there are no suggestions: the list stays closed.
58
+ */
59
+ items: readonly T[] | undefined;
60
+ /**
61
+ * A refetch is running and the current `items` stay on screen. Shows the
62
+ * trailing spinner. For the first load, pass `items={undefined}` instead.
52
63
  */
53
- items: readonly T[];
54
- /** Async state of the suggestion source. Shows a trailing spinner while true. */
55
64
  isLoading?: boolean;
56
- /** @deprecated Use `isLoading`. Kept as an alias until the next major. */
65
+ /** @deprecated Use `isLoading`. Removed in 4.0. */
57
66
  loading?: boolean;
67
+ /**
68
+ * Announced by the spinner while the suggestions load.
69
+ * @default `messages.comboboxFreeText.loading`, "Loading…"
70
+ */
71
+ loadingLabel?: string;
58
72
  /** Render a suggestion row. Defaults to `label` over `description`. */
59
73
  renderItem?: (item: T) => React.ReactNode;
60
74
  /** id forwarded to the input. */
61
75
  id?: string;
76
+ /** Form field name of the input. */
77
+ name?: string;
78
+ /** Accessible name of the input, when no visible label points at it. */
79
+ 'aria-label'?: string;
80
+ /** Id of the element that names the input. */
81
+ 'aria-labelledby'?: string;
62
82
  placeholder?: string;
63
83
  /** Render the error state (sets `aria-invalid` on the input). */
64
84
  invalid?: boolean;
@@ -94,20 +114,33 @@ function ComboboxFreeText<T extends ComboboxFreeTextItem = ComboboxFreeTextItem>
94
114
  onValueChange,
95
115
  onSelectItem,
96
116
  items,
97
- isLoading,
117
+ isLoading: isLoadingProp,
98
118
  loading,
119
+ loadingLabel,
99
120
  renderItem = defaultRenderItem,
100
121
  id,
122
+ name,
123
+ 'aria-label': ariaLabel,
124
+ 'aria-labelledby': ariaLabelledBy,
101
125
  placeholder,
102
126
  invalid,
103
127
  disabled,
104
128
  className,
105
129
  }: ComboboxFreeTextProps<T>) {
130
+ if (loading !== undefined) warnDeprecated('ComboboxFreeText', 'loading', 'isLoading');
131
+ const isLoading = isLoadingProp ?? loading;
106
132
  // Base UI opens the popup on every keystroke; we only want it open when there
107
133
  // is something pickable, so a no-match closes it silently instead of flashing
108
134
  // an empty box. `open` tracks the user's intent (typing, Escape, blur,
109
135
  // select) and we AND it with "items present" to derive the rendered state.
136
+ // While the first fetch runs (`items` undefined) the list opens on its
137
+ // loading row, so the field shows that suggestions are coming.
110
138
  const [open, setOpen] = React.useState(false);
139
+ const t = useMessages('comboboxFreeText', { loading: loadingLabel });
140
+ const firstLoad = items === undefined;
141
+ // The list lines up with the whole field, as a Combobox list does. Anchored
142
+ // to the input, it would widen when the trailing spinner goes away.
143
+ const fieldRef = React.useRef<HTMLDivElement>(null);
111
144
 
112
145
  return (
113
146
  <Autocomplete.Root
@@ -123,28 +156,31 @@ function ComboboxFreeText<T extends ComboboxFreeTextItem = ComboboxFreeTextItem>
123
156
  // The literal is checked against Base UI's public `reason` union, so a
124
157
  // future rename surfaces as a type error rather than silent breakage.
125
158
  if (onSelectItem && details.reason === 'item-press') {
126
- const picked = items.find((i) => i.value === next);
159
+ const picked = items?.find((i) => i.value === next);
127
160
  if (picked) {
128
161
  onSelectItem(picked);
129
162
  }
130
163
  }
131
164
  }}
132
- open={open && items.length > 0}
165
+ open={open && (firstLoad || items.length > 0)}
133
166
  onOpenChange={setOpen}
134
167
  disabled={disabled}
135
168
  >
136
- <InputGroup className={cn('w-full', className)}>
169
+ <InputGroup ref={fieldRef} className={cn('w-full', className)}>
137
170
  <Autocomplete.Input
138
171
  render={<InputGroupInput />}
139
172
  id={id}
173
+ name={name}
174
+ aria-label={ariaLabel}
175
+ aria-labelledby={ariaLabelledBy}
140
176
  placeholder={placeholder}
141
177
  disabled={disabled}
142
178
  aria-invalid={invalid || undefined}
143
179
  // Base UI only requests open on a keystroke; for pick-or-create
144
180
  // fields the suggestions should also show on focus/click so a small
145
- // known option set is discoverable without typing. The
146
- // `items.length > 0` gate on `open` keeps an empty popup from
147
- // flashing when there is nothing to pick.
181
+ // known option set is discoverable without typing. The gate on
182
+ // `open` keeps an empty popup from flashing when there is nothing
183
+ // to pick.
148
184
  onFocus={() => setOpen(true)}
149
185
  onClick={() => setOpen(true)}
150
186
  // Enter has a local meaning here — commit the highlighted suggestion
@@ -159,19 +195,38 @@ function ComboboxFreeText<T extends ComboboxFreeTextItem = ComboboxFreeTextItem>
159
195
  }
160
196
  }}
161
197
  />
162
- {(isLoading ?? loading) ? (
198
+ {isLoading || firstLoad ? (
163
199
  <InputGroupAddon align="inline-end">
164
- <Spinner />
200
+ {/* Decorative: the status below announces the load. */}
201
+ <Loader2Icon aria-hidden className="size-4 animate-spin motion-reduce:animate-none" />
165
202
  </InputGroupAddon>
166
203
  ) : null}
167
204
  </InputGroup>
168
- <ComboboxContent>
169
- <ComboboxList>
170
- {items.map((item) => (
171
- <ComboboxItem key={item.value} value={item.value}>
172
- {renderItem(item)}
173
- </ComboboxItem>
174
- ))}
205
+ {/* Stays mounted, so screen readers announce the change: a status
206
+ that mounts with its text is not reliably read. */}
207
+ <Autocomplete.Status data-slot="combobox-free-text-status" className="sr-only">
208
+ {isLoading || firstLoad ? t.loading : null}
209
+ </Autocomplete.Status>
210
+ <ComboboxContent anchor={fieldRef}>
211
+ <ComboboxList aria-busy={isLoading || firstLoad || undefined}>
212
+ {firstLoad ? (
213
+ // Decorative: the status announces the load. One
214
+ // suggestion row tall (the ComboboxItem height), so a single
215
+ // one-line suggestion takes its place without a jump.
216
+ <div
217
+ data-slot="combobox-free-text-loading"
218
+ aria-hidden="true"
219
+ className="text-muted-foreground flex min-h-8.5 items-center justify-center"
220
+ >
221
+ <Loader2Icon className="size-4 motion-safe:animate-spin" />
222
+ </div>
223
+ ) : (
224
+ items.map((item) => (
225
+ <ComboboxItem key={item.value} value={item.value}>
226
+ {renderItem(item)}
227
+ </ComboboxItem>
228
+ ))
229
+ )}
175
230
  </ComboboxList>
176
231
  </ComboboxContent>
177
232
  </Autocomplete.Root>