@vendure-io/ui 2.0.0 → 2.1.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.
- package/package.json +2 -2
- package/src/components/atoms/alert.tsx +4 -0
- package/src/components/atoms/checkbox.tsx +2 -1
- package/src/components/atoms/field.tsx +134 -11
- package/src/components/atoms/input.tsx +4 -1
- package/src/components/atoms/native-select.tsx +4 -1
- package/src/components/atoms/progress.tsx +60 -10
- package/src/components/atoms/radio-group.tsx +2 -1
- package/src/components/atoms/select.tsx +2 -1
- package/src/components/atoms/switch.tsx +2 -1
- package/src/components/atoms/textarea.tsx +4 -1
- package/src/components/molecules/confirm-dialog.tsx +25 -3
- package/src/components/molecules/copyable-text.tsx +6 -2
- package/src/components/molecules/data-table/data-table-filters.tsx +1 -1
- package/src/components/molecules/data-table/data-table-view-options.tsx +5 -2
- package/src/components/molecules/id-chip.tsx +39 -9
- package/src/components/molecules/relative-time.tsx +66 -11
- package/src/components/molecules/secret-value.tsx +183 -0
- package/src/components/molecules/state-views/empty-state.tsx +18 -10
- package/src/components/molecules/state-views/error-state.tsx +29 -6
- package/src/hooks/use-copy.ts +8 -4
- package/src/lib/clipboard.ts +38 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vendure-io/ui",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "React component library for Vendure, built on shadcn/ui and Tailwind v4",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
6
|
"homepage": "https://github.com/vendurehq/design/tree/main/packages/ui",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"pack:check": "bun pm pack --dry-run"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@vendure-io/design-tokens": "^2.0.0
|
|
36
|
+
"@vendure-io/design-tokens": "^2.0.0",
|
|
37
37
|
"react": ">=19",
|
|
38
38
|
"react-dom": ">=19",
|
|
39
39
|
"next": ">=14",
|
|
@@ -11,6 +11,10 @@ const alertVariants = cva(
|
|
|
11
11
|
default: "bg-card text-card-foreground",
|
|
12
12
|
destructive:
|
|
13
13
|
"text-destructive-subtle-foreground bg-card *:data-[slot=alert-description]:text-destructive-subtle-foreground *:[svg]:text-current",
|
|
14
|
+
warning:
|
|
15
|
+
"text-warning-subtle-foreground bg-card *:data-[slot=alert-description]:text-warning-subtle-foreground *:[svg]:text-current",
|
|
16
|
+
info:
|
|
17
|
+
"text-info-subtle-foreground bg-card *:data-[slot=alert-description]:text-info-subtle-foreground *:[svg]:text-current",
|
|
14
18
|
},
|
|
15
19
|
},
|
|
16
20
|
defaultVariants: {
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox"
|
|
4
4
|
|
|
5
5
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
6
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
6
7
|
import { CheckIcon, MinusIcon } from "lucide-react"
|
|
7
8
|
|
|
8
9
|
function Checkbox({ className, indeterminate, ...props }: CheckboxPrimitive.Root.Props) {
|
|
@@ -14,7 +15,7 @@ function Checkbox({ className, indeterminate, ...props }: CheckboxPrimitive.Root
|
|
|
14
15
|
className
|
|
15
16
|
)}
|
|
16
17
|
indeterminate={indeterminate}
|
|
17
|
-
{...props}
|
|
18
|
+
{...useFieldControlProps(props)}
|
|
18
19
|
>
|
|
19
20
|
<CheckboxPrimitive.Indicator
|
|
20
21
|
data-slot="checkbox-indicator"
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useMemo,
|
|
10
|
+
useState,
|
|
11
|
+
} from "react"
|
|
4
12
|
import { cva, type VariantProps } from "class-variance-authority"
|
|
5
13
|
|
|
6
14
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
@@ -69,22 +77,107 @@ const fieldVariants = cva(
|
|
|
69
77
|
}
|
|
70
78
|
)
|
|
71
79
|
|
|
80
|
+
// Field links its control to the FieldDescription and FieldError it renders.
|
|
81
|
+
// Each message registers its id once mounted; controls read the context through
|
|
82
|
+
// useFieldControlProps and get aria-describedby (descriptions first, then
|
|
83
|
+
// errors) and aria-invalid while the Field is data-invalid. A control's own
|
|
84
|
+
// aria-describedby or aria-invalid wins.
|
|
85
|
+
type FieldMessageKind = "description" | "error"
|
|
86
|
+
|
|
87
|
+
type FieldContextValue = {
|
|
88
|
+
describedBy: string | undefined
|
|
89
|
+
invalid: boolean
|
|
90
|
+
register: (id: string, kind: FieldMessageKind) => () => void
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const FieldContext = createContext<FieldContextValue | null>(null)
|
|
94
|
+
|
|
95
|
+
/** Space-separated ids, descriptions before errors; undefined when empty. */
|
|
96
|
+
function fieldDescribedBy(
|
|
97
|
+
messages: ReadonlyArray<{ id: string; kind: FieldMessageKind }>
|
|
98
|
+
) {
|
|
99
|
+
const ids = [
|
|
100
|
+
...messages.filter((message) => message.kind === "description"),
|
|
101
|
+
...messages.filter((message) => message.kind === "error"),
|
|
102
|
+
].map((message) => message.id)
|
|
103
|
+
return ids.length ? ids.join(" ") : undefined
|
|
104
|
+
}
|
|
105
|
+
|
|
72
106
|
function Field({
|
|
73
107
|
className,
|
|
74
108
|
orientation = "vertical",
|
|
109
|
+
children,
|
|
75
110
|
...props
|
|
76
|
-
}: React.ComponentProps<"div"> &
|
|
111
|
+
}: React.ComponentProps<"div"> &
|
|
112
|
+
VariantProps<typeof fieldVariants> & {
|
|
113
|
+
"data-invalid"?: boolean | "true" | "false"
|
|
114
|
+
}) {
|
|
115
|
+
const [messages, setMessages] = useState<
|
|
116
|
+
Array<{ id: string; kind: FieldMessageKind }>
|
|
117
|
+
>([])
|
|
118
|
+
const register = useCallback((id: string, kind: FieldMessageKind) => {
|
|
119
|
+
setMessages((current) => [...current, { id, kind }])
|
|
120
|
+
return () =>
|
|
121
|
+
setMessages((current) => current.filter((message) => message.id !== id))
|
|
122
|
+
}, [])
|
|
123
|
+
const invalid =
|
|
124
|
+
props["data-invalid"] === true || props["data-invalid"] === "true"
|
|
125
|
+
const describedBy = fieldDescribedBy(messages)
|
|
126
|
+
const context = useMemo(
|
|
127
|
+
() => ({ describedBy, invalid, register }),
|
|
128
|
+
[describedBy, invalid, register]
|
|
129
|
+
)
|
|
130
|
+
|
|
77
131
|
return (
|
|
78
|
-
<
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
132
|
+
<FieldContext.Provider value={context}>
|
|
133
|
+
<div
|
|
134
|
+
role="group"
|
|
135
|
+
data-slot="field"
|
|
136
|
+
data-orientation={orientation}
|
|
137
|
+
className={cn(fieldVariants({ orientation }), className)}
|
|
138
|
+
{...props}
|
|
139
|
+
>
|
|
140
|
+
{children}
|
|
141
|
+
</div>
|
|
142
|
+
</FieldContext.Provider>
|
|
85
143
|
)
|
|
86
144
|
}
|
|
87
145
|
|
|
146
|
+
/** Gives a message a stable id and registers it with the enclosing Field. */
|
|
147
|
+
function useFieldMessageId(id: string | undefined, kind: FieldMessageKind) {
|
|
148
|
+
const generatedId = useId()
|
|
149
|
+
const messageId = id ?? generatedId
|
|
150
|
+
const register = useContext(FieldContext)?.register
|
|
151
|
+
useEffect(() => register?.(messageId, kind), [register, messageId, kind])
|
|
152
|
+
return messageId
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
type FieldControlAria = {
|
|
156
|
+
"aria-describedby"?: string
|
|
157
|
+
"aria-invalid"?: React.AriaAttributes["aria-invalid"]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Merges a Field's state into control props. Explicit props win. */
|
|
161
|
+
function mergeFieldControlProps<P extends FieldControlAria>(
|
|
162
|
+
props: P,
|
|
163
|
+
field: Pick<FieldContextValue, "describedBy" | "invalid"> | null
|
|
164
|
+
): P {
|
|
165
|
+
if (!field) return props
|
|
166
|
+
return {
|
|
167
|
+
...props,
|
|
168
|
+
"aria-describedby": props["aria-describedby"] ?? field.describedBy,
|
|
169
|
+
"aria-invalid": props["aria-invalid"] ?? (field.invalid || undefined),
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Adds the enclosing Field's aria-describedby and aria-invalid to a control's
|
|
175
|
+
* props. Props the consumer passes explicitly win.
|
|
176
|
+
*/
|
|
177
|
+
function useFieldControlProps<P extends FieldControlAria>(props: P): P {
|
|
178
|
+
return mergeFieldControlProps(props, useContext(FieldContext))
|
|
179
|
+
}
|
|
180
|
+
|
|
88
181
|
function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
|
|
89
182
|
return (
|
|
90
183
|
<div
|
|
@@ -128,9 +221,15 @@ function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
|
|
|
128
221
|
)
|
|
129
222
|
}
|
|
130
223
|
|
|
131
|
-
function FieldDescription({
|
|
224
|
+
function FieldDescription({
|
|
225
|
+
className,
|
|
226
|
+
id,
|
|
227
|
+
...props
|
|
228
|
+
}: React.ComponentProps<"p">) {
|
|
229
|
+
const messageId = useFieldMessageId(id, "description")
|
|
132
230
|
return (
|
|
133
231
|
<p
|
|
232
|
+
id={messageId}
|
|
134
233
|
data-slot="field-description"
|
|
135
234
|
className={cn(
|
|
136
235
|
"text-muted-foreground text-left text-sm leading-normal font-normal group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5",
|
|
@@ -177,6 +276,7 @@ function FieldError({
|
|
|
177
276
|
className,
|
|
178
277
|
children,
|
|
179
278
|
errors,
|
|
279
|
+
id,
|
|
180
280
|
...props
|
|
181
281
|
}: React.ComponentProps<"div"> & {
|
|
182
282
|
errors?: Array<{ message?: string } | undefined>
|
|
@@ -212,14 +312,34 @@ function FieldError({
|
|
|
212
312
|
return null
|
|
213
313
|
}
|
|
214
314
|
|
|
315
|
+
return (
|
|
316
|
+
<FieldErrorMessage
|
|
317
|
+
id={id}
|
|
318
|
+
className={className}
|
|
319
|
+
{...props}
|
|
320
|
+
>
|
|
321
|
+
{content}
|
|
322
|
+
</FieldErrorMessage>
|
|
323
|
+
)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Split from FieldError so only a rendered error registers its id.
|
|
327
|
+
function FieldErrorMessage({
|
|
328
|
+
className,
|
|
329
|
+
children,
|
|
330
|
+
id,
|
|
331
|
+
...props
|
|
332
|
+
}: React.ComponentProps<"div">) {
|
|
333
|
+
const messageId = useFieldMessageId(id, "error")
|
|
215
334
|
return (
|
|
216
335
|
<div
|
|
336
|
+
id={messageId}
|
|
217
337
|
role="alert"
|
|
218
338
|
data-slot="field-error"
|
|
219
339
|
className={cn("text-destructive-subtle-foreground text-sm font-normal", className)}
|
|
220
340
|
{...props}
|
|
221
341
|
>
|
|
222
|
-
{
|
|
342
|
+
{children}
|
|
223
343
|
</div>
|
|
224
344
|
)
|
|
225
345
|
}
|
|
@@ -235,4 +355,7 @@ export {
|
|
|
235
355
|
FieldSet,
|
|
236
356
|
FieldContent,
|
|
237
357
|
FieldTitle,
|
|
358
|
+
fieldDescribedBy,
|
|
359
|
+
mergeFieldControlProps,
|
|
360
|
+
useFieldControlProps,
|
|
238
361
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
1
3
|
import * as React from "react"
|
|
2
4
|
import { Input as InputPrimitive } from "@base-ui/react/input"
|
|
3
5
|
|
|
4
6
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
7
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
5
8
|
|
|
6
9
|
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
|
7
10
|
return (
|
|
@@ -12,7 +15,7 @@ function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
|
|
12
15
|
"dark:bg-input/30 border-input focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive file:text-foreground placeholder:text-muted-foreground h-9 w-full min-w-0 rounded-md border bg-transparent px-2.5 py-1 text-base transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium focus-visible:ring-3 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:ring-3 md:text-sm",
|
|
13
16
|
className
|
|
14
17
|
)}
|
|
15
|
-
{...props}
|
|
18
|
+
{...useFieldControlProps(props)}
|
|
16
19
|
/>
|
|
17
20
|
)
|
|
18
21
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
1
3
|
import * as React from "react"
|
|
2
4
|
|
|
3
5
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
6
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
4
7
|
import { ChevronDownIcon } from "lucide-react"
|
|
5
8
|
|
|
6
9
|
type NativeSelectProps = Omit<React.ComponentProps<"select">, "size"> & {
|
|
@@ -25,7 +28,7 @@ function NativeSelect({
|
|
|
25
28
|
data-slot="native-select"
|
|
26
29
|
data-size={size}
|
|
27
30
|
className="border-input placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive h-9 w-full min-w-0 appearance-none rounded-md border bg-transparent py-1 pr-8 pl-2.5 text-sm transition-[color,box-shadow] outline-none select-none focus-visible:ring-3 disabled:pointer-events-none disabled:cursor-not-allowed aria-invalid:ring-3 data-[size=sm]:h-8"
|
|
28
|
-
{...props}
|
|
31
|
+
{...useFieldControlProps(props)}
|
|
29
32
|
/>
|
|
30
33
|
<ChevronDownIcon className="text-muted-foreground pointer-events-none absolute top-1/2 right-2.5 size-4 -translate-y-1/2 select-none" aria-hidden="true" data-slot="native-select-icon" />
|
|
31
34
|
</div>
|
|
@@ -1,37 +1,85 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
|
|
3
3
|
import { Progress as ProgressPrimitive } from "@base-ui/react/progress"
|
|
4
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
4
5
|
|
|
5
6
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
6
7
|
|
|
8
|
+
// Tone names follow the state dictionary; `critical` renders with the
|
|
9
|
+
// `destructive` slots. A toned bar sits on the tone's subtle track. The solid
|
|
10
|
+
// tone slots are too close to the neutral track in luminance (down to 1.1:1),
|
|
11
|
+
// so the fill uses a fixed ramp step in light mode (600: at least 3.97:1 on the
|
|
12
|
+
// track and 4.35:1 on the page) and the subtle foreground in dark mode (at
|
|
13
|
+
// least 8.58:1 on the track), meeting 3:1 non-text contrast in both.
|
|
14
|
+
const progressTrackVariants = cva(
|
|
15
|
+
"relative flex h-1.5 w-full items-center overflow-x-hidden rounded-full",
|
|
16
|
+
{
|
|
17
|
+
variants: {
|
|
18
|
+
tone: {
|
|
19
|
+
default: "bg-control-border",
|
|
20
|
+
success: "bg-success-subtle",
|
|
21
|
+
warning: "bg-warning-subtle",
|
|
22
|
+
critical: "bg-destructive-subtle",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
defaultVariants: {
|
|
26
|
+
tone: "default",
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
const progressIndicatorVariants = cva("h-full transition-all", {
|
|
32
|
+
variants: {
|
|
33
|
+
tone: {
|
|
34
|
+
default: "bg-primary",
|
|
35
|
+
success: "bg-success-600 dark:bg-success-subtle-foreground",
|
|
36
|
+
warning: "bg-warning-600 dark:bg-warning-subtle-foreground",
|
|
37
|
+
critical: "bg-destructive-600 dark:bg-destructive-subtle-foreground",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
defaultVariants: {
|
|
41
|
+
tone: "default",
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
type ProgressTone = NonNullable<
|
|
46
|
+
VariantProps<typeof progressIndicatorVariants>["tone"]
|
|
47
|
+
>
|
|
48
|
+
|
|
7
49
|
function Progress({
|
|
8
50
|
className,
|
|
9
51
|
children,
|
|
10
52
|
value,
|
|
53
|
+
tone = "default",
|
|
11
54
|
...props
|
|
12
|
-
}: ProgressPrimitive.Root.Props
|
|
55
|
+
}: ProgressPrimitive.Root.Props & {
|
|
56
|
+
/** Indicator tone. Follows consequence: see the Progress guidance. */
|
|
57
|
+
tone?: ProgressTone
|
|
58
|
+
}) {
|
|
13
59
|
return (
|
|
14
60
|
<ProgressPrimitive.Root
|
|
15
61
|
value={value}
|
|
16
62
|
data-slot="progress"
|
|
63
|
+
data-tone={tone}
|
|
17
64
|
className={cn("flex flex-wrap gap-3", className)}
|
|
18
65
|
{...props}
|
|
19
66
|
>
|
|
20
67
|
{children}
|
|
21
|
-
<ProgressTrack>
|
|
22
|
-
<ProgressIndicator />
|
|
68
|
+
<ProgressTrack tone={tone}>
|
|
69
|
+
<ProgressIndicator tone={tone} />
|
|
23
70
|
</ProgressTrack>
|
|
24
71
|
</ProgressPrimitive.Root>
|
|
25
72
|
)
|
|
26
73
|
}
|
|
27
74
|
|
|
28
|
-
function ProgressTrack({
|
|
75
|
+
function ProgressTrack({
|
|
76
|
+
className,
|
|
77
|
+
tone,
|
|
78
|
+
...props
|
|
79
|
+
}: ProgressPrimitive.Track.Props & { tone?: ProgressTone }) {
|
|
29
80
|
return (
|
|
30
81
|
<ProgressPrimitive.Track
|
|
31
|
-
className={cn(
|
|
32
|
-
"bg-control-border relative flex h-1.5 w-full items-center overflow-x-hidden rounded-full",
|
|
33
|
-
className
|
|
34
|
-
)}
|
|
82
|
+
className={cn(progressTrackVariants({ tone }), className)}
|
|
35
83
|
data-slot="progress-track"
|
|
36
84
|
{...props}
|
|
37
85
|
/>
|
|
@@ -40,12 +88,13 @@ function ProgressTrack({ className, ...props }: ProgressPrimitive.Track.Props) {
|
|
|
40
88
|
|
|
41
89
|
function ProgressIndicator({
|
|
42
90
|
className,
|
|
91
|
+
tone,
|
|
43
92
|
...props
|
|
44
|
-
}: ProgressPrimitive.Indicator.Props) {
|
|
93
|
+
}: ProgressPrimitive.Indicator.Props & { tone?: ProgressTone }) {
|
|
45
94
|
return (
|
|
46
95
|
<ProgressPrimitive.Indicator
|
|
47
96
|
data-slot="progress-indicator"
|
|
48
|
-
className={cn(
|
|
97
|
+
className={cn(progressIndicatorVariants({ tone }), className)}
|
|
49
98
|
{...props}
|
|
50
99
|
/>
|
|
51
100
|
)
|
|
@@ -80,4 +129,5 @@ export {
|
|
|
80
129
|
ProgressIndicator,
|
|
81
130
|
ProgressLabel,
|
|
82
131
|
ProgressValue,
|
|
132
|
+
type ProgressTone,
|
|
83
133
|
}
|
|
@@ -4,13 +4,14 @@ import { Radio as RadioPrimitive } from "@base-ui/react/radio"
|
|
|
4
4
|
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group"
|
|
5
5
|
|
|
6
6
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
7
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
7
8
|
|
|
8
9
|
function RadioGroup({ className, ...props }: RadioGroupPrimitive.Props) {
|
|
9
10
|
return (
|
|
10
11
|
<RadioGroupPrimitive
|
|
11
12
|
data-slot="radio-group"
|
|
12
13
|
className={cn("grid w-full gap-3", className)}
|
|
13
|
-
{...props}
|
|
14
|
+
{...useFieldControlProps(props)}
|
|
14
15
|
/>
|
|
15
16
|
)
|
|
16
17
|
}
|
|
@@ -4,6 +4,7 @@ import * as React from "react"
|
|
|
4
4
|
import { Select as SelectPrimitive } from "@base-ui/react/select"
|
|
5
5
|
|
|
6
6
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
7
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
7
8
|
import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from "lucide-react"
|
|
8
9
|
|
|
9
10
|
const Select = SelectPrimitive.Root
|
|
@@ -44,7 +45,7 @@ function SelectTrigger({
|
|
|
44
45
|
"border-input data-placeholder:text-muted-foreground dark:bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive flex w-fit items-center justify-between gap-1.5 rounded-md border bg-transparent py-2 pr-2 pl-2.5 text-sm whitespace-nowrap transition-[color,box-shadow] outline-none focus-visible:ring-3 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:ring-3 data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
|
45
46
|
className
|
|
46
47
|
)}
|
|
47
|
-
{...props}
|
|
48
|
+
{...useFieldControlProps(props)}
|
|
48
49
|
>
|
|
49
50
|
{children}
|
|
50
51
|
<SelectPrimitive.Icon
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
|
|
4
4
|
|
|
5
5
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
6
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
6
7
|
|
|
7
8
|
function Switch({
|
|
8
9
|
className,
|
|
@@ -19,7 +20,7 @@ function Switch({
|
|
|
19
20
|
"data-checked:bg-primary data-unchecked:bg-control-border focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:ring-3 aria-invalid:ring-3 data-disabled:cursor-not-allowed data-disabled:opacity-50 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px]",
|
|
20
21
|
className
|
|
21
22
|
)}
|
|
22
|
-
{...props}
|
|
23
|
+
{...useFieldControlProps(props)}
|
|
23
24
|
>
|
|
24
25
|
<SwitchPrimitive.Thumb
|
|
25
26
|
data-slot="switch-thumb"
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
1
3
|
import * as React from "react"
|
|
2
4
|
|
|
3
5
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
6
|
+
import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
|
|
4
7
|
|
|
5
8
|
function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
|
|
6
9
|
return (
|
|
@@ -10,7 +13,7 @@ function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
|
|
|
10
13
|
"border-input dark:bg-input/30 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive placeholder:text-muted-foreground flex field-sizing-content min-h-16 w-full rounded-md border bg-transparent px-2.5 py-2 text-base transition-[color,box-shadow] outline-none focus-visible:ring-3 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:ring-3 md:text-sm",
|
|
11
14
|
className
|
|
12
15
|
)}
|
|
13
|
-
{...props}
|
|
16
|
+
{...useFieldControlProps(props)}
|
|
14
17
|
/>
|
|
15
18
|
)
|
|
16
19
|
}
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
AlertDialogTrigger,
|
|
13
13
|
} from '@vendure-io/ui/components/atoms/alert-dialog';
|
|
14
14
|
import { Spinner } from '@vendure-io/ui/components/atoms/spinner';
|
|
15
|
-
import { type ReactElement, type ReactNode, useRef, useState } from 'react';
|
|
15
|
+
import { type ReactElement, type ReactNode, useId, useRef, useState } from 'react';
|
|
16
16
|
|
|
17
17
|
interface ConfirmDialogProps {
|
|
18
18
|
title: ReactNode;
|
|
@@ -26,6 +26,14 @@ interface ConfirmDialogProps {
|
|
|
26
26
|
confirmLabel?: ReactNode;
|
|
27
27
|
cancelLabel?: ReactNode;
|
|
28
28
|
variant?: 'default' | 'destructive';
|
|
29
|
+
/**
|
|
30
|
+
* Hold the confirm button disabled while the dialog cannot be confirmed yet,
|
|
31
|
+
* e.g. while the consequences it describes are still loading. Cancel stays
|
|
32
|
+
* available.
|
|
33
|
+
*/
|
|
34
|
+
confirmDisabled?: boolean;
|
|
35
|
+
/** Why confirm is disabled, shown beside the buttons while `confirmDisabled` is set. */
|
|
36
|
+
confirmDisabledReason?: ReactNode;
|
|
29
37
|
/** Uncontrolled mode — the element that opens the dialog. */
|
|
30
38
|
children?: ReactElement;
|
|
31
39
|
/** Controlled mode — supply with `onOpenChange`. */
|
|
@@ -40,6 +48,8 @@ function ConfirmDialog({
|
|
|
40
48
|
confirmLabel = 'Confirm',
|
|
41
49
|
cancelLabel = 'Cancel',
|
|
42
50
|
variant = 'default',
|
|
51
|
+
confirmDisabled = false,
|
|
52
|
+
confirmDisabledReason,
|
|
43
53
|
children,
|
|
44
54
|
open,
|
|
45
55
|
onOpenChange,
|
|
@@ -51,6 +61,8 @@ function ConfirmDialog({
|
|
|
51
61
|
// re-render disables the button, so the in-flight flag lives in a ref too.
|
|
52
62
|
const pendingRef = useRef(false);
|
|
53
63
|
const actualOpen = isControlled ? open : internalOpen;
|
|
64
|
+
const reasonId = useId();
|
|
65
|
+
const showReason = confirmDisabled && !pending && Boolean(confirmDisabledReason);
|
|
54
66
|
|
|
55
67
|
const setOpen = (next: boolean) => {
|
|
56
68
|
if (!isControlled) setInternalOpen(next);
|
|
@@ -58,7 +70,7 @@ function ConfirmDialog({
|
|
|
58
70
|
};
|
|
59
71
|
|
|
60
72
|
const handleConfirm = async () => {
|
|
61
|
-
if (pendingRef.current) return;
|
|
73
|
+
if (pendingRef.current || confirmDisabled) return;
|
|
62
74
|
pendingRef.current = true;
|
|
63
75
|
try {
|
|
64
76
|
setPending(true);
|
|
@@ -87,13 +99,23 @@ function ConfirmDialog({
|
|
|
87
99
|
{description ? <AlertDialogDescription>{description}</AlertDialogDescription> : null}
|
|
88
100
|
</AlertDialogHeader>
|
|
89
101
|
<AlertDialogFooter>
|
|
102
|
+
{showReason ? (
|
|
103
|
+
<p
|
|
104
|
+
id={reasonId}
|
|
105
|
+
data-slot="confirm-dialog-disabled-reason"
|
|
106
|
+
className="text-muted-foreground self-center text-sm group-data-[size=sm]/alert-dialog-content:col-span-2 sm:mr-auto"
|
|
107
|
+
>
|
|
108
|
+
{confirmDisabledReason}
|
|
109
|
+
</p>
|
|
110
|
+
) : null}
|
|
90
111
|
<AlertDialogCancel disabled={pending}>{cancelLabel}</AlertDialogCancel>
|
|
91
112
|
{/* AlertDialogAction is a plain Button (not a Close), so closing stays
|
|
92
113
|
under our control — the promise decides when the dialog leaves. */}
|
|
93
114
|
<AlertDialogAction
|
|
94
115
|
variant={variant}
|
|
95
|
-
disabled={pending}
|
|
116
|
+
disabled={pending || confirmDisabled}
|
|
96
117
|
aria-busy={pending || undefined}
|
|
118
|
+
aria-describedby={showReason ? reasonId : undefined}
|
|
97
119
|
onClick={handleConfirm}
|
|
98
120
|
>
|
|
99
121
|
{pending ? <Spinner /> : null}
|
|
@@ -3,13 +3,17 @@
|
|
|
3
3
|
import { Button } from '@vendure-io/ui/components/atoms/button';
|
|
4
4
|
import { useCopyFeedback } from '@vendure-io/ui/components/molecules/copy-feedback-provider';
|
|
5
5
|
import { useCopy } from '@vendure-io/ui/hooks/use-copy';
|
|
6
|
+
import type { ClipboardSource } from '@vendure-io/ui/lib/clipboard';
|
|
6
7
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
7
8
|
import { CheckIcon, CopyIcon } from 'lucide-react';
|
|
8
9
|
import type * as React from 'react';
|
|
9
10
|
|
|
10
11
|
interface CopyButtonProps extends Omit<React.ComponentProps<typeof Button>, 'value' | 'children'> {
|
|
11
|
-
/**
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The text written to the clipboard, or a callback that loads it when the
|
|
14
|
+
* button is clicked (for values the server returns only on request).
|
|
15
|
+
*/
|
|
16
|
+
value: ClipboardSource;
|
|
13
17
|
/** How long the check-mark feedback stays visible, in ms. @default 2000 */
|
|
14
18
|
timeout?: number;
|
|
15
19
|
/**
|
|
@@ -15,7 +15,7 @@ import { SlidersHorizontalIcon } from 'lucide-react';
|
|
|
15
15
|
// Relative: `data-table-helpers` is closed in the package exports.
|
|
16
16
|
import { getHideableLeafColumns } from './data-table-helpers.tsx';
|
|
17
17
|
|
|
18
|
-
// The column
|
|
18
|
+
// The column picker ("Columns" trigger). Lists only hideable columns (`getCanHide()`), so
|
|
19
19
|
// the always-on `select`/`actions` display columns never appear here. Labels
|
|
20
20
|
// come from a string `ColumnDef.header`, falling back to the column id — `meta`
|
|
21
21
|
// is deliberately never read (it belongs to the consumer, make-or-break #7).
|
|
@@ -40,10 +40,13 @@ function DataTableViewOptions<TData>({
|
|
|
40
40
|
|
|
41
41
|
return (
|
|
42
42
|
<DropdownMenu>
|
|
43
|
+
{/* `ml-auto` pins the trigger to the end of the controls row, so it reads
|
|
44
|
+
as a table setting even when no search input fills the row. */}
|
|
43
45
|
<DropdownMenuTrigger
|
|
44
46
|
data-slot="data-table-view-options"
|
|
47
|
+
className="ml-auto"
|
|
45
48
|
render={
|
|
46
|
-
<Button variant="outline"
|
|
49
|
+
<Button variant="outline">
|
|
47
50
|
<SlidersHorizontalIcon />
|
|
48
51
|
{triggerLabel}
|
|
49
52
|
</Button>
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
|
|
3
4
|
import { CopyButton } from '@vendure-io/ui/components/molecules/copyable-text';
|
|
4
5
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
5
6
|
|
|
6
7
|
type TruncateMode = 'middle' | 'start' | 'none';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
* Shared chrome for the identifier-chip family (`IdChip`, `AnonymizedToken
|
|
10
|
-
* bordered, `bg-muted`, monospace `text-xs` pill sized to its
|
|
11
|
-
* one place so the
|
|
12
|
-
* (a plain value
|
|
10
|
+
* Shared chrome for the identifier-chip family (`IdChip`, `AnonymizedToken`,
|
|
11
|
+
* `SecretValue`): a bordered, `bg-muted`, monospace `text-xs` pill sized to its
|
|
12
|
+
* content. Kept in one place so the chips can't drift apart — only their
|
|
13
|
+
* *contents* differ (a plain value, a blurred reveal-on-hover secret, or a
|
|
14
|
+
* masked secret loaded on click).
|
|
13
15
|
*/
|
|
14
16
|
function chipChromeClass(copyable: boolean, className?: string): string {
|
|
15
17
|
return cn(
|
|
@@ -31,7 +33,7 @@ function truncateId(value: string, mode: TruncateMode): string {
|
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
interface IdChipProps {
|
|
34
|
-
/** The full value
|
|
36
|
+
/** The full value: always copied in full, and shown in a tooltip when the chip shortens it. */
|
|
35
37
|
value: string;
|
|
36
38
|
/** Override the rendered text (e.g. a longer prefix). Bypasses `truncate`. */
|
|
37
39
|
display?: string;
|
|
@@ -42,12 +44,21 @@ interface IdChipProps {
|
|
|
42
44
|
className?: string;
|
|
43
45
|
/** Called after a successful copy. Wire your toast here — the DS never toasts. Falls back to `CopyFeedbackProvider`. */
|
|
44
46
|
onCopied?: () => void;
|
|
47
|
+
/** Called when the clipboard write fails. Falls back to `CopyFeedbackProvider`. */
|
|
48
|
+
onCopyError?: (error: Error) => void;
|
|
49
|
+
/** Accessible label of the copy button before copying, e.g. "Copy commit SHA". @default "Copy" */
|
|
50
|
+
copyLabel?: string;
|
|
51
|
+
/** Accessible label of the copy button while the copied state is active. @default "Copied" */
|
|
52
|
+
copiedLabel?: string;
|
|
53
|
+
/** How long the check-mark feedback stays visible, in ms. @default 2000 */
|
|
54
|
+
timeout?: number;
|
|
45
55
|
}
|
|
46
56
|
|
|
47
57
|
/**
|
|
48
58
|
* A bordered, monospace chip for IDs/tokens with a built-in copy affordance.
|
|
49
59
|
* The rendered text is truncated for density while the full value is copied and
|
|
50
|
-
*
|
|
60
|
+
* shown in a DS Tooltip on hover, or when the copy button has focus. The chip
|
|
61
|
+
* adds no tab stop of its own; the copy button is its only interactive part.
|
|
51
62
|
*/
|
|
52
63
|
function IdChip({
|
|
53
64
|
value,
|
|
@@ -56,23 +67,42 @@ function IdChip({
|
|
|
56
67
|
copyable = true,
|
|
57
68
|
className,
|
|
58
69
|
onCopied,
|
|
70
|
+
onCopyError,
|
|
71
|
+
copyLabel,
|
|
72
|
+
copiedLabel,
|
|
73
|
+
timeout,
|
|
59
74
|
}: IdChipProps) {
|
|
60
75
|
const text = display ?? truncateId(value, truncate);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
<span data-slot="id-chip" title={value} className={chipChromeClass(copyable, className)}>
|
|
76
|
+
const chip = (
|
|
77
|
+
<span data-slot="id-chip" className={chipChromeClass(copyable, className)}>
|
|
64
78
|
<span className="truncate">{text}</span>
|
|
65
79
|
{/* 16px visual button to match the chip's density, but the ::after inset
|
|
66
80
|
extends the hit target to 24px for the WCAG 2.2 AA minimum. */}
|
|
67
81
|
{copyable ? (
|
|
68
82
|
<CopyButton
|
|
69
83
|
value={value}
|
|
84
|
+
timeout={timeout}
|
|
70
85
|
onCopied={onCopied}
|
|
86
|
+
onCopyError={onCopyError}
|
|
87
|
+
copyLabel={copyLabel}
|
|
88
|
+
copiedLabel={copiedLabel}
|
|
71
89
|
className="relative size-4 after:absolute after:-inset-1"
|
|
72
90
|
/>
|
|
73
91
|
) : null}
|
|
74
92
|
</span>
|
|
75
93
|
);
|
|
94
|
+
|
|
95
|
+
// Nothing is hidden when the chip shows the whole value.
|
|
96
|
+
if (text === value) return chip;
|
|
97
|
+
|
|
98
|
+
// The chip itself is the trigger, so hover anywhere on it opens the tooltip;
|
|
99
|
+
// focus on the copy button bubbles to it for keyboard users.
|
|
100
|
+
return (
|
|
101
|
+
<Tooltip>
|
|
102
|
+
<TooltipTrigger render={chip} />
|
|
103
|
+
<TooltipContent className="font-mono">{value}</TooltipContent>
|
|
104
|
+
</Tooltip>
|
|
105
|
+
);
|
|
76
106
|
}
|
|
77
107
|
|
|
78
108
|
export { chipChromeClass, IdChip };
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
|
|
3
4
|
import { useFormatSettings } from '@vendure-io/ui/components/molecules/format-provider';
|
|
4
5
|
import { type FormatRelativeOptions, formatRelative } from '@vendure-io/ui/lib/format-relative';
|
|
5
|
-
import {
|
|
6
|
+
import { cn } from '@vendure-io/ui/lib/utils';
|
|
7
|
+
import { type ComponentProps, type ReactNode, useEffect, useId, useState } from 'react';
|
|
6
8
|
|
|
7
9
|
function toDate(value: string | number | Date | null | undefined): Date | null {
|
|
8
10
|
if (value === null || value === undefined) return null;
|
|
@@ -23,6 +25,19 @@ interface RelativeTimeProps extends Omit<ComponentProps<'time'>, 'dateTime'> {
|
|
|
23
25
|
updateInterval?: number | false;
|
|
24
26
|
/** Rendered for nullish/invalid input. Default `'—'`. */
|
|
25
27
|
fallback?: ReactNode;
|
|
28
|
+
/**
|
|
29
|
+
* Show the absolute time in a DS Tooltip on hover. Default `true`. Pass
|
|
30
|
+
* `false` where the time sits inside another tooltip trigger, so the outer
|
|
31
|
+
* tooltip keeps its content. The absolute time stays the accessible
|
|
32
|
+
* description either way.
|
|
33
|
+
*/
|
|
34
|
+
tooltip?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Make the time a tab stop so keyboard users can open the tooltip. Default
|
|
37
|
+
* `false`. Opt in where the exact time matters to keyboard users, and never
|
|
38
|
+
* inside interactive content such as a link or a button. Needs `tooltip`.
|
|
39
|
+
*/
|
|
40
|
+
focusable?: boolean;
|
|
26
41
|
}
|
|
27
42
|
|
|
28
43
|
/**
|
|
@@ -30,7 +45,11 @@ interface RelativeTimeProps extends Omit<ComponentProps<'time'>, 'dateTime'> {
|
|
|
30
45
|
* first client render can disagree. The `<time>` carries
|
|
31
46
|
* `suppressHydrationWarning` and re-formats once right after mount; SSR
|
|
32
47
|
* consumers should still pin `locale`/`timeZone` via `FormatProvider` (or
|
|
33
|
-
* props) so the absolute
|
|
48
|
+
* props) so the absolute description matches too.
|
|
49
|
+
*
|
|
50
|
+
* The absolute time is never a native `title`. It is the accessible
|
|
51
|
+
* description (a hidden child linked by `aria-describedby`), and it shows in
|
|
52
|
+
* the DS Tooltip on hover unless `tooltip={false}`.
|
|
34
53
|
*/
|
|
35
54
|
function RelativeTime({
|
|
36
55
|
value,
|
|
@@ -39,8 +58,11 @@ function RelativeTime({
|
|
|
39
58
|
unitStyle,
|
|
40
59
|
updateInterval = 60_000,
|
|
41
60
|
fallback = '—',
|
|
61
|
+
tooltip = true,
|
|
62
|
+
focusable = false,
|
|
42
63
|
...props
|
|
43
64
|
}: RelativeTimeProps) {
|
|
65
|
+
const descriptionId = useId();
|
|
44
66
|
const settings = useFormatSettings();
|
|
45
67
|
// A bare counter to force a re-format on each tick; the value itself is unused.
|
|
46
68
|
const [, tick] = useState(0);
|
|
@@ -62,17 +84,50 @@ function RelativeTime({
|
|
|
62
84
|
if (!date) return <>{fallback}</>;
|
|
63
85
|
|
|
64
86
|
const resolvedLocale = locale ?? settings.locale;
|
|
87
|
+
const absolute = date.toLocaleString(resolvedLocale, { timeZone: settings.timeZone });
|
|
88
|
+
const relative = formatRelative(date, { locale: resolvedLocale, numeric, unitStyle });
|
|
89
|
+
const describedBy = [props['aria-describedby'], descriptionId].filter(Boolean).join(' ');
|
|
90
|
+
// The description is a `hidden` child, so the component renders one element.
|
|
91
|
+
// `hidden` keeps the text out of view and out of the reading flow;
|
|
92
|
+
// aria-describedby still resolves it.
|
|
93
|
+
const content = (
|
|
94
|
+
<>
|
|
95
|
+
{relative}
|
|
96
|
+
<span id={descriptionId} hidden suppressHydrationWarning>
|
|
97
|
+
{absolute}
|
|
98
|
+
</span>
|
|
99
|
+
</>
|
|
100
|
+
);
|
|
101
|
+
const timeProps = {
|
|
102
|
+
'data-slot': 'relative-time',
|
|
103
|
+
dateTime: date.toISOString(),
|
|
104
|
+
suppressHydrationWarning: true,
|
|
105
|
+
...props,
|
|
106
|
+
'aria-describedby': describedBy,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
if (!tooltip) return <time {...timeProps}>{content}</time>;
|
|
65
110
|
|
|
66
111
|
return (
|
|
67
|
-
<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
112
|
+
<Tooltip>
|
|
113
|
+
<TooltipTrigger
|
|
114
|
+
render={
|
|
115
|
+
<time
|
|
116
|
+
// Opt-in tab stop so keyboard users can open the tooltip (WCAG 2.1.1).
|
|
117
|
+
tabIndex={focusable ? 0 : undefined}
|
|
118
|
+
{...timeProps}
|
|
119
|
+
className={cn(
|
|
120
|
+
focusable &&
|
|
121
|
+
'focus-visible:ring-ring/50 rounded-sm outline-none focus-visible:ring-3',
|
|
122
|
+
props.className,
|
|
123
|
+
)}
|
|
124
|
+
/>
|
|
125
|
+
}
|
|
126
|
+
>
|
|
127
|
+
{content}
|
|
128
|
+
</TooltipTrigger>
|
|
129
|
+
<TooltipContent>{absolute}</TooltipContent>
|
|
130
|
+
</Tooltip>
|
|
76
131
|
);
|
|
77
132
|
}
|
|
78
133
|
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Button } from '@vendure-io/ui/components/atoms/button';
|
|
4
|
+
import { Spinner } from '@vendure-io/ui/components/atoms/spinner';
|
|
5
|
+
import {
|
|
6
|
+
CopyButton,
|
|
7
|
+
type CopyButtonProps,
|
|
8
|
+
} from '@vendure-io/ui/components/molecules/copyable-text';
|
|
9
|
+
import { chipChromeClass } from '@vendure-io/ui/components/molecules/id-chip';
|
|
10
|
+
import { EyeIcon, EyeOffIcon } from 'lucide-react';
|
|
11
|
+
import type * as React from 'react';
|
|
12
|
+
import { useReducer, useRef } from 'react';
|
|
13
|
+
|
|
14
|
+
interface SecretValueProps extends Omit<React.ComponentProps<'span'>, 'children'> {
|
|
15
|
+
/**
|
|
16
|
+
* Loads the full value on request, for secrets the server returns only when
|
|
17
|
+
* asked. Reveal and copy call it; each call may be audited, so it never runs
|
|
18
|
+
* on hover or focus.
|
|
19
|
+
*/
|
|
20
|
+
loadValue: () => Promise<string>;
|
|
21
|
+
/** Render the copy affordance. Copy while hidden loads without revealing. @default true */
|
|
22
|
+
copyable?: boolean;
|
|
23
|
+
/** How long the check-mark feedback stays visible, in ms. */
|
|
24
|
+
timeout?: CopyButtonProps['timeout'];
|
|
25
|
+
/** Called after a successful copy. Wire your toast here. Falls back to `CopyFeedbackProvider`. */
|
|
26
|
+
onCopied?: CopyButtonProps['onCopied'];
|
|
27
|
+
/** Called when the load or the clipboard write fails. Falls back to `CopyFeedbackProvider`. */
|
|
28
|
+
onCopyError?: CopyButtonProps['onCopyError'];
|
|
29
|
+
/** Accessible name while the value is masked. @default "Hidden value" */
|
|
30
|
+
hiddenLabel?: string;
|
|
31
|
+
/** Accessible label of the reveal button. @default "Show value" */
|
|
32
|
+
showLabel?: string;
|
|
33
|
+
/** Accessible label of the button that masks the value again. @default "Hide value" */
|
|
34
|
+
hideLabel?: string;
|
|
35
|
+
/** Accessible label before copying. @default "Copy value" */
|
|
36
|
+
copyLabel?: CopyButtonProps['copyLabel'];
|
|
37
|
+
/** Accessible label while the copied state is active. @default "Value copied" */
|
|
38
|
+
copiedLabel?: CopyButtonProps['copiedLabel'];
|
|
39
|
+
/** Shown inline when `loadValue` rejects. @default "Couldn't load the value." */
|
|
40
|
+
loadErrorMessage?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const MASK = '••••••••';
|
|
44
|
+
|
|
45
|
+
type RevealState =
|
|
46
|
+
| { status: 'hidden' }
|
|
47
|
+
| { status: 'loading' }
|
|
48
|
+
| { status: 'revealed'; value: string }
|
|
49
|
+
| { status: 'error' };
|
|
50
|
+
|
|
51
|
+
type RevealAction =
|
|
52
|
+
| { type: 'load' }
|
|
53
|
+
| { type: 'loaded'; value: string }
|
|
54
|
+
| { type: 'failed' }
|
|
55
|
+
| { type: 'hide' };
|
|
56
|
+
|
|
57
|
+
/** State of a SecretValue: hidden, loading, revealed or error. */
|
|
58
|
+
function revealReducer(state: RevealState, action: RevealAction): RevealState {
|
|
59
|
+
switch (action.type) {
|
|
60
|
+
case 'load':
|
|
61
|
+
return state.status === 'revealed' ? state : { status: 'loading' };
|
|
62
|
+
case 'loaded':
|
|
63
|
+
return state.status === 'loading' ? { status: 'revealed', value: action.value } : state;
|
|
64
|
+
case 'failed':
|
|
65
|
+
return state.status === 'loading' ? { status: 'error' } : state;
|
|
66
|
+
case 'hide':
|
|
67
|
+
return { status: 'hidden' };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Shows a secret that the server returns only on request. At rest it is a fixed
|
|
73
|
+
* mask; a click on the reveal button loads and shows the value, and copy loads
|
|
74
|
+
* it without showing it. Hiding drops the value from state.
|
|
75
|
+
*
|
|
76
|
+
* Use `AnonymizedToken` instead when the full value is already on the client.
|
|
77
|
+
*/
|
|
78
|
+
function SecretValue({
|
|
79
|
+
loadValue,
|
|
80
|
+
copyable = true,
|
|
81
|
+
timeout,
|
|
82
|
+
onCopied,
|
|
83
|
+
onCopyError,
|
|
84
|
+
hiddenLabel = 'Hidden value',
|
|
85
|
+
showLabel = 'Show value',
|
|
86
|
+
hideLabel = 'Hide value',
|
|
87
|
+
copyLabel = 'Copy value',
|
|
88
|
+
copiedLabel = 'Value copied',
|
|
89
|
+
loadErrorMessage = "Couldn't load the value.",
|
|
90
|
+
className,
|
|
91
|
+
...props
|
|
92
|
+
}: SecretValueProps) {
|
|
93
|
+
const [state, dispatch] = useReducer(revealReducer, { status: 'hidden' });
|
|
94
|
+
// Drops a load that settles after the user hid the value or asked again.
|
|
95
|
+
const request = useRef(0);
|
|
96
|
+
|
|
97
|
+
async function reveal() {
|
|
98
|
+
const id = ++request.current;
|
|
99
|
+
dispatch({ type: 'load' });
|
|
100
|
+
try {
|
|
101
|
+
const value = await loadValue();
|
|
102
|
+
if (id === request.current) dispatch({ type: 'loaded', value });
|
|
103
|
+
} catch {
|
|
104
|
+
if (id === request.current) dispatch({ type: 'failed' });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function hide() {
|
|
109
|
+
request.current++;
|
|
110
|
+
dispatch({ type: 'hide' });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const revealed = state.status === 'revealed';
|
|
114
|
+
const loading = state.status === 'loading';
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<span
|
|
118
|
+
data-slot="secret-value"
|
|
119
|
+
data-state={state.status}
|
|
120
|
+
role="group"
|
|
121
|
+
className={chipChromeClass(copyable, className)}
|
|
122
|
+
{...props}
|
|
123
|
+
>
|
|
124
|
+
{/* The mask is fixed: no fake prefix or suffix. The value is visible text
|
|
125
|
+
only after a deliberate reveal, and never in a title or label. */}
|
|
126
|
+
{revealed ? (
|
|
127
|
+
// `whitespace-normal` lets the value wrap inside a `whitespace-nowrap`
|
|
128
|
+
// parent such as a TableCell, so a long value grows the row in height
|
|
129
|
+
// instead of widening the table.
|
|
130
|
+
<code data-slot="secret-value-value" className="min-w-0 break-all whitespace-normal">
|
|
131
|
+
{state.value}
|
|
132
|
+
</code>
|
|
133
|
+
) : (
|
|
134
|
+
<>
|
|
135
|
+
<code data-slot="secret-value-value" aria-hidden="true">
|
|
136
|
+
{MASK}
|
|
137
|
+
</code>
|
|
138
|
+
<span className="sr-only">{hiddenLabel}</span>
|
|
139
|
+
</>
|
|
140
|
+
)}
|
|
141
|
+
<Button
|
|
142
|
+
type="button"
|
|
143
|
+
data-slot="secret-value-reveal"
|
|
144
|
+
variant="ghost"
|
|
145
|
+
size="icon-xs"
|
|
146
|
+
aria-label={revealed ? hideLabel : showLabel}
|
|
147
|
+
aria-busy={loading || undefined}
|
|
148
|
+
aria-disabled={loading || undefined}
|
|
149
|
+
className="text-muted-foreground hover:text-foreground relative size-4 shrink-0 after:absolute after:-inset-1"
|
|
150
|
+
onClick={() => {
|
|
151
|
+
if (loading) return;
|
|
152
|
+
if (revealed) hide();
|
|
153
|
+
else void reveal();
|
|
154
|
+
}}
|
|
155
|
+
>
|
|
156
|
+
{loading ? <Spinner /> : revealed ? <EyeOffIcon /> : <EyeIcon />}
|
|
157
|
+
</Button>
|
|
158
|
+
{copyable ? (
|
|
159
|
+
<CopyButton
|
|
160
|
+
value={revealed ? state.value : loadValue}
|
|
161
|
+
timeout={timeout}
|
|
162
|
+
onCopied={onCopied}
|
|
163
|
+
onCopyError={onCopyError}
|
|
164
|
+
copyLabel={copyLabel}
|
|
165
|
+
copiedLabel={copiedLabel}
|
|
166
|
+
className="relative size-4 shrink-0 after:absolute after:-inset-1"
|
|
167
|
+
/>
|
|
168
|
+
) : null}
|
|
169
|
+
{state.status === 'error' ? (
|
|
170
|
+
<span
|
|
171
|
+
data-slot="secret-value-error"
|
|
172
|
+
role="alert"
|
|
173
|
+
className="text-destructive-subtle-foreground pr-1 font-sans"
|
|
174
|
+
>
|
|
175
|
+
{loadErrorMessage}
|
|
176
|
+
</span>
|
|
177
|
+
) : null}
|
|
178
|
+
</span>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export { SecretValue };
|
|
183
|
+
export type { SecretValueProps };
|
|
@@ -31,6 +31,12 @@ export interface EmptyStateProps extends Omit<React.ComponentProps<typeof Empty>
|
|
|
31
31
|
* owns its perimeter.
|
|
32
32
|
*/
|
|
33
33
|
bordered?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* `compact` drops the illustration and icon and tightens the spacing, for a
|
|
36
|
+
* small panel or side column where the full state is too tall. Keep it to a
|
|
37
|
+
* title, a description and at most one action. Defaults to `default`.
|
|
38
|
+
*/
|
|
39
|
+
size?: 'default' | 'compact';
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
/**
|
|
@@ -49,36 +55,38 @@ function EmptyState({
|
|
|
49
55
|
description,
|
|
50
56
|
children,
|
|
51
57
|
bordered = true,
|
|
58
|
+
size = 'default',
|
|
52
59
|
className,
|
|
53
60
|
...props
|
|
54
61
|
}: EmptyStateProps) {
|
|
62
|
+
const compact = size === 'compact';
|
|
55
63
|
// Precedence: an explicit `illustration` (including `null`, to opt out)
|
|
56
64
|
// always wins; otherwise an explicit `icon` keeps rendering exactly as
|
|
57
65
|
// before. Only when neither is touched does the default illustration fill
|
|
58
66
|
// the media slot, so existing icon-only consumers see no change.
|
|
59
|
-
const resolvedIllustration =
|
|
60
|
-
illustration
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
) : null;
|
|
67
|
+
const resolvedIllustration = compact ? null : illustration !== undefined ? (
|
|
68
|
+
illustration
|
|
69
|
+
) : icon === undefined ? (
|
|
70
|
+
<EmptyCollectionIllustration />
|
|
71
|
+
) : null;
|
|
65
72
|
|
|
66
73
|
return (
|
|
67
74
|
<Empty
|
|
68
75
|
data-slot="empty-state"
|
|
69
|
-
|
|
76
|
+
data-size={size}
|
|
77
|
+
className={cn(bordered ? 'border' : 'rounded-none', compact && 'gap-2 p-4', className)}
|
|
70
78
|
{...props}
|
|
71
79
|
>
|
|
72
80
|
<EmptyHeader>
|
|
73
81
|
{resolvedIllustration ? (
|
|
74
82
|
<EmptyMedia>{resolvedIllustration}</EmptyMedia>
|
|
75
|
-
) : icon ? (
|
|
83
|
+
) : icon && !compact ? (
|
|
76
84
|
<EmptyMedia variant="icon">{icon}</EmptyMedia>
|
|
77
85
|
) : null}
|
|
78
|
-
<EmptyTitle>{title}</EmptyTitle>
|
|
86
|
+
<EmptyTitle className={cn(compact && 'text-style-card-title')}>{title}</EmptyTitle>
|
|
79
87
|
{description ? <EmptyDescription>{description}</EmptyDescription> : null}
|
|
80
88
|
</EmptyHeader>
|
|
81
|
-
{children ? <EmptyContent>{children}</EmptyContent> : null}
|
|
89
|
+
{children ? <EmptyContent className={cn(compact && 'gap-2')}>{children}</EmptyContent> : null}
|
|
82
90
|
</Empty>
|
|
83
91
|
);
|
|
84
92
|
}
|
|
@@ -34,6 +34,17 @@ export interface ErrorStateProps extends Omit<React.ComponentProps<typeof Empty>
|
|
|
34
34
|
retryLabel?: React.ReactNode;
|
|
35
35
|
/** Extra actions (e.g. a not-found "Go back" link) rendered beside retry. */
|
|
36
36
|
children?: React.ReactNode;
|
|
37
|
+
/**
|
|
38
|
+
* Draw the box. Defaults to `true`. Set `false` when the error state fills a
|
|
39
|
+
* region that already has a frame, such as a table body or a card.
|
|
40
|
+
*/
|
|
41
|
+
bordered?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* `compact` drops the illustration and icon and tightens the spacing, for a
|
|
44
|
+
* small panel or side column where the full state is too tall. Keep it to a
|
|
45
|
+
* title, a description and at most one action. Defaults to `default`.
|
|
46
|
+
*/
|
|
47
|
+
size?: 'default' | 'compact';
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
/**
|
|
@@ -50,19 +61,31 @@ function ErrorState({
|
|
|
50
61
|
onRetry,
|
|
51
62
|
retryLabel = 'Try again',
|
|
52
63
|
children,
|
|
64
|
+
bordered = true,
|
|
65
|
+
size = 'default',
|
|
53
66
|
className,
|
|
54
67
|
...props
|
|
55
68
|
}: ErrorStateProps) {
|
|
69
|
+
const compact = size === 'compact';
|
|
56
70
|
// Precedence: an explicit `illustration` (including `null`, to opt out)
|
|
57
71
|
// always wins; otherwise an explicit `icon` (including `null`) keeps
|
|
58
72
|
// rendering exactly as before. Only when neither is touched does the
|
|
59
73
|
// default illustration replace the default triangle icon.
|
|
60
|
-
const resolvedIllustration =
|
|
61
|
-
illustration
|
|
62
|
-
|
|
74
|
+
const resolvedIllustration = compact ? null : illustration !== undefined ? (
|
|
75
|
+
illustration
|
|
76
|
+
) : icon === undefined ? (
|
|
77
|
+
<ErrorIllustration />
|
|
78
|
+
) : null;
|
|
79
|
+
const resolvedIcon = compact ? null : icon === undefined ? <TriangleAlertIcon /> : icon;
|
|
63
80
|
|
|
64
81
|
return (
|
|
65
|
-
<Empty
|
|
82
|
+
<Empty
|
|
83
|
+
role="alert"
|
|
84
|
+
data-slot="error-state"
|
|
85
|
+
data-size={size}
|
|
86
|
+
className={cn(bordered ? 'border' : 'rounded-none', compact && 'gap-2 p-4', className)}
|
|
87
|
+
{...props}
|
|
88
|
+
>
|
|
66
89
|
<EmptyHeader>
|
|
67
90
|
{resolvedIllustration ? (
|
|
68
91
|
<EmptyMedia>{resolvedIllustration}</EmptyMedia>
|
|
@@ -71,11 +94,11 @@ function ErrorState({
|
|
|
71
94
|
{resolvedIcon}
|
|
72
95
|
</EmptyMedia>
|
|
73
96
|
) : null}
|
|
74
|
-
<EmptyTitle>{title}</EmptyTitle>
|
|
97
|
+
<EmptyTitle className={cn(compact && 'text-style-card-title')}>{title}</EmptyTitle>
|
|
75
98
|
{description ? <EmptyDescription>{description}</EmptyDescription> : null}
|
|
76
99
|
</EmptyHeader>
|
|
77
100
|
{onRetry || children ? (
|
|
78
|
-
<EmptyContent>
|
|
101
|
+
<EmptyContent className={cn(compact && 'gap-2')}>
|
|
79
102
|
{onRetry ? (
|
|
80
103
|
<Button variant="outline" size="sm" onClick={onRetry}>
|
|
81
104
|
{retryLabel}
|
package/src/hooks/use-copy.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
+
import { type ClipboardSource, writeToClipboard } from '@vendure-io/ui/lib/clipboard';
|
|
3
4
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
4
5
|
|
|
5
6
|
interface UseCopyOptions {
|
|
@@ -10,8 +11,11 @@ interface UseCopyOptions {
|
|
|
10
11
|
interface UseCopyReturn {
|
|
11
12
|
/** True for `timeout` ms after the last successful copy. */
|
|
12
13
|
copied: boolean;
|
|
13
|
-
/**
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Writes `text` to the clipboard, calling it first when it is a loader.
|
|
16
|
+
* Resolves `true` on success, `false` on failure — never throws.
|
|
17
|
+
*/
|
|
18
|
+
copy: (text: ClipboardSource) => Promise<boolean>;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
/**
|
|
@@ -32,9 +36,9 @@ export function useCopy({ timeout = 2000 }: UseCopyOptions = {}): UseCopyReturn
|
|
|
32
36
|
);
|
|
33
37
|
|
|
34
38
|
const copy = useCallback(
|
|
35
|
-
async (text:
|
|
39
|
+
async (text: ClipboardSource): Promise<boolean> => {
|
|
36
40
|
try {
|
|
37
|
-
await
|
|
41
|
+
await writeToClipboard(text);
|
|
38
42
|
setCopied(true);
|
|
39
43
|
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
40
44
|
timeoutRef.current = setTimeout(() => setCopied(false), timeout);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Text to copy, or a callback that loads it on demand. */
|
|
2
|
+
export type ClipboardSource = string | (() => Promise<string>);
|
|
3
|
+
|
|
4
|
+
type ClipboardWriter = Pick<Clipboard, 'writeText'> & Partial<Pick<Clipboard, 'write'>>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Writes text to the clipboard. A loader is called once. Where `ClipboardItem`
|
|
8
|
+
* accepts a promise, the write starts synchronously with a promise of the
|
|
9
|
+
* text, so Safari keeps the user gesture across the async load. Elsewhere it
|
|
10
|
+
* falls back to `writeText` once the text has loaded. Rejects when the loader
|
|
11
|
+
* or the clipboard fails.
|
|
12
|
+
*/
|
|
13
|
+
export async function writeToClipboard(
|
|
14
|
+
source: ClipboardSource,
|
|
15
|
+
clipboard: ClipboardWriter = navigator.clipboard,
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
if (typeof source === 'string') {
|
|
18
|
+
await clipboard.writeText(source);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const text = source();
|
|
23
|
+
if (clipboard.write && typeof ClipboardItem !== 'undefined') {
|
|
24
|
+
const blob = text.then((value) => new Blob([value], { type: 'text/plain' }));
|
|
25
|
+
// The write below owns this rejection; keep it from surfacing as unhandled
|
|
26
|
+
// when the ClipboardItem path is abandoned for the fallback.
|
|
27
|
+
blob.catch(() => {});
|
|
28
|
+
try {
|
|
29
|
+
await clipboard.write([new ClipboardItem({ 'text/plain': blob })]);
|
|
30
|
+
return;
|
|
31
|
+
} catch {
|
|
32
|
+
// A failed load is the real error. Otherwise the browser rejected the
|
|
33
|
+
// promise-valued item: fall back to writeText.
|
|
34
|
+
await text;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
await clipboard.writeText(await text);
|
|
38
|
+
}
|