@vendure-io/ui 2.0.1 → 2.2.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.
@@ -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 !== undefined ? illustration : icon === undefined ? <ErrorIllustration /> : null;
62
- const resolvedIcon = icon === undefined ? <TriangleAlertIcon /> : icon;
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 role="alert" data-slot="error-state" className={cn('border', className)} {...props}>
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}
@@ -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
- /** Writes `text` to the clipboard. Resolves `true` on success, `false` on failure — never throws. */
14
- copy: (text: string) => Promise<boolean>;
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: string): Promise<boolean> => {
39
+ async (text: ClipboardSource): Promise<boolean> => {
36
40
  try {
37
- await navigator.clipboard.writeText(text);
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
+ }