@vendure-io/ui 2.2.0 → 3.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +19 -5
- package/src/components/atoms/accordion.tsx +21 -6
- package/src/components/atoms/alert-dialog.tsx +31 -6
- package/src/components/atoms/alert.tsx +59 -12
- package/src/components/atoms/avatar.tsx +1 -1
- package/src/components/atoms/badge.tsx +7 -2
- package/src/components/atoms/breadcrumb.tsx +137 -4
- package/src/components/atoms/button-group.tsx +30 -6
- package/src/components/atoms/button.tsx +128 -18
- package/src/components/atoms/calendar.tsx +6 -6
- package/src/components/atoms/card.tsx +130 -16
- package/src/components/atoms/carousel.tsx +16 -9
- package/src/components/atoms/chart.tsx +87 -1
- package/src/components/atoms/checkbox.tsx +85 -7
- package/src/components/atoms/collapsible.tsx +2 -1
- package/src/components/atoms/combobox.tsx +147 -21
- package/src/components/atoms/command.tsx +414 -12
- package/src/components/atoms/context-menu.tsx +28 -23
- package/src/components/atoms/dialog.tsx +151 -29
- package/src/components/atoms/direction.tsx +23 -2
- package/src/components/atoms/drawer.tsx +15 -10
- package/src/components/atoms/dropdown-menu.tsx +43 -28
- package/src/components/atoms/empty.tsx +4 -0
- package/src/components/atoms/field.tsx +41 -15
- package/src/components/atoms/hover-card.tsx +61 -8
- package/src/components/atoms/input-group.tsx +17 -9
- package/src/components/atoms/input-otp.tsx +6 -6
- package/src/components/atoms/input.tsx +7 -1
- package/src/components/atoms/item.tsx +3 -3
- package/src/components/atoms/kbd.tsx +32 -3
- package/src/components/atoms/label-swap.tsx +82 -0
- package/src/components/atoms/label.tsx +1 -1
- package/src/components/atoms/menubar.tsx +28 -26
- package/src/components/atoms/native-select.tsx +3 -2
- package/src/components/atoms/navigation-menu.tsx +13 -8
- package/src/components/atoms/pagination.tsx +2 -2
- package/src/components/atoms/popover.tsx +23 -5
- package/src/components/atoms/progress.tsx +46 -33
- package/src/components/atoms/radio-group.tsx +9 -4
- package/src/components/atoms/resizable.tsx +1 -1
- package/src/components/atoms/scroll-area.tsx +2 -2
- package/src/components/atoms/select.tsx +258 -19
- package/src/components/atoms/sheet.tsx +59 -23
- package/src/components/atoms/sidebar.tsx +333 -23
- package/src/components/atoms/skeleton.tsx +1 -1
- package/src/components/atoms/slider.tsx +1 -1
- package/src/components/atoms/sonner.tsx +128 -38
- package/src/components/atoms/spinner.tsx +1 -1
- package/src/components/atoms/switch.tsx +10 -5
- package/src/components/atoms/table.tsx +147 -7
- package/src/components/atoms/tabs.tsx +3 -3
- package/src/components/atoms/textarea.tsx +3 -1
- package/src/components/atoms/toggle-group.tsx +4 -2
- package/src/components/atoms/toggle.tsx +28 -6
- package/src/components/atoms/tooltip.tsx +85 -15
- package/src/components/molecules/anonymized-token.tsx +24 -8
- package/src/components/molecules/app-shell.tsx +53 -20
- package/src/components/molecules/arrow-link.tsx +40 -0
- package/src/components/molecules/chip.tsx +2 -2
- package/src/components/molecules/code-block.tsx +54 -31
- package/src/components/molecules/collapsible-section.tsx +129 -0
- package/src/components/molecules/confirm-dialog.tsx +113 -9
- package/src/components/molecules/copyable-text.tsx +75 -11
- package/src/components/molecules/data-table/data-table-bulk-actions.tsx +32 -4
- package/src/components/molecules/data-table/data-table-column-header.tsx +20 -3
- package/src/components/molecules/data-table/data-table-filters.tsx +1 -1
- package/src/components/molecules/data-table/data-table-helpers.tsx +68 -1
- package/src/components/molecules/data-table/data-table-selection-storage.tsx +115 -0
- package/src/components/molecules/data-table/data-table-types.tsx +121 -4
- package/src/components/molecules/data-table/data-table-view-options.tsx +2 -2
- package/src/components/molecules/data-table/data-table.tsx +837 -80
- package/src/components/molecules/data-table/table-pagination.tsx +2 -2
- package/src/components/molecules/date-picker.tsx +13 -7
- package/src/components/molecules/date-range-picker.tsx +13 -6
- package/src/components/molecules/date-time-picker.tsx +13 -7
- package/src/components/molecules/date-time.tsx +8 -1
- package/src/components/molecules/direction-link.tsx +130 -0
- package/src/components/molecules/elapsed-time.tsx +223 -0
- package/src/components/molecules/file-dropzone.tsx +2 -2
- package/src/components/molecules/id-chip.tsx +169 -32
- package/src/components/molecules/inline-code.tsx +4 -0
- package/src/components/molecules/issued-secret.tsx +318 -0
- package/src/components/molecules/list-row.tsx +349 -0
- package/src/components/molecules/live-indicator.tsx +83 -0
- package/src/components/molecules/page-header.tsx +117 -5
- package/src/components/molecules/password-input.tsx +2 -2
- package/src/components/molecules/relative-time.tsx +85 -56
- package/src/components/molecules/search-trigger.tsx +127 -0
- package/src/components/molecules/secret-value.tsx +26 -9
- package/src/components/molecules/skip-link.tsx +1 -1
- package/src/components/molecules/stat-card.tsx +4 -4
- package/src/components/molecules/state-views/empty-state.tsx +35 -17
- package/src/components/molecules/state-views/error-state.tsx +32 -14
- package/src/components/molecules/status-badge.tsx +415 -49
- package/src/components/molecules/time-tooltip.tsx +140 -0
- package/src/components/molecules/timeline.tsx +246 -0
- package/src/components/molecules/visibility-icon.tsx +89 -0
- package/src/hooks/use-active-label-size.ts +32 -0
- package/src/hooks/use-pinned-height.ts +55 -0
- package/src/hooks/use-scrolled.ts +27 -0
- package/src/lib/content-inset.ts +17 -0
- package/src/lib/control-name.ts +55 -0
- package/src/lib/dialog-padding.ts +48 -0
- package/src/lib/field-surface.ts +31 -0
- package/src/lib/format-elapsed.ts +67 -0
- package/src/lib/format-relative.ts +29 -1
- package/src/lib/highlight.ts +3 -3
- package/src/lib/menu-item.tsx +72 -0
- package/src/lib/outline-edge.ts +21 -0
- package/src/lib/popup-align.ts +67 -0
- package/src/lib/popup-direction.ts +22 -0
- package/src/lib/popup-motion.ts +19 -0
- package/src/lib/state-dictionary.ts +42 -6
- package/src/lib/syntax-colors.ts +8 -0
- package/src/lib/tint-recipes.ts +68 -0
- package/src/lib/tone-icons.ts +24 -0
- package/src/lib/trigger-slot.ts +16 -0
- package/src/lib/utils.ts +104 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Card, CardCount, type CardCountTone } from '@vendure-io/ui/components/atoms/card';
|
|
4
|
+
import {
|
|
5
|
+
Collapsible,
|
|
6
|
+
CollapsibleContent,
|
|
7
|
+
CollapsibleTrigger,
|
|
8
|
+
} from '@vendure-io/ui/components/atoms/collapsible';
|
|
9
|
+
import { cn } from '@vendure-io/ui/lib/utils';
|
|
10
|
+
import { ChevronDown } from 'lucide-react';
|
|
11
|
+
import type { ComponentProps, ReactNode } from 'react';
|
|
12
|
+
|
|
13
|
+
// The count says how many rows the section holds, open and closed. It is a
|
|
14
|
+
// CardCount, the same count as beside a card title. `detail` adds a short
|
|
15
|
+
// fact after it ("2 · 1 failed"). The tone goes to the CardCount of the
|
|
16
|
+
// detail when there is one, and to the whole count otherwise: the number stays
|
|
17
|
+
// muted beside a toned fact, and the section itself stays neutral (accent
|
|
18
|
+
// rationing).
|
|
19
|
+
|
|
20
|
+
type CollapsibleSectionProps = Omit<
|
|
21
|
+
ComponentProps<typeof Collapsible>,
|
|
22
|
+
'title' | 'render' | 'className' | 'children'
|
|
23
|
+
> & {
|
|
24
|
+
/** The section title. Also the name of the toggle button. */
|
|
25
|
+
title: ReactNode;
|
|
26
|
+
/** How many rows the section holds, beside the title. */
|
|
27
|
+
count?: number | string;
|
|
28
|
+
/** A short fact after the count, such as "1 failed". Renders as "{count} · {detail}". */
|
|
29
|
+
detail?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Colours the detail, or the whole count when there is no detail. `warning`:
|
|
32
|
+
* rows wait on the user. `critical`: rows have failed. Default `neutral`.
|
|
33
|
+
*/
|
|
34
|
+
tone?: CardCountTone;
|
|
35
|
+
/** Heading level of the title in the page outline. Default `2`. */
|
|
36
|
+
headingLevel?: 2 | 3 | 4 | 5 | 6;
|
|
37
|
+
className?: string;
|
|
38
|
+
/** The rows. Usually ListRow elements. */
|
|
39
|
+
children?: ReactNode;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A card-surface section whose header opens and closes its rows. The header
|
|
44
|
+
* is one full-width button, filled on hover like a row: title, count, and a
|
|
45
|
+
* down chevron that turns over when open. A disabled section shows no hover.
|
|
46
|
+
*/
|
|
47
|
+
function CollapsibleSection({
|
|
48
|
+
title,
|
|
49
|
+
count,
|
|
50
|
+
detail,
|
|
51
|
+
tone = 'neutral',
|
|
52
|
+
headingLevel = 2,
|
|
53
|
+
className,
|
|
54
|
+
children,
|
|
55
|
+
...props
|
|
56
|
+
}: CollapsibleSectionProps) {
|
|
57
|
+
const Heading = `h${headingLevel}` as const;
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<Collapsible
|
|
61
|
+
data-slot="collapsible-section"
|
|
62
|
+
render={<Card />}
|
|
63
|
+
className={cn('gap-0 py-0', className)}
|
|
64
|
+
{...props}
|
|
65
|
+
>
|
|
66
|
+
<Heading data-slot="collapsible-section-heading" className="m-0">
|
|
67
|
+
<CollapsibleTrigger
|
|
68
|
+
data-slot="collapsible-section-trigger"
|
|
69
|
+
className="group/collapsible-section-trigger flex min-h-12 w-full cursor-pointer items-center gap-2 ps-4.5 pe-4 text-start outline-none transition-colors duration-fast ease-default not-data-disabled:hover:bg-muted data-disabled:cursor-default focus-visible:outline-solid focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring"
|
|
70
|
+
>
|
|
71
|
+
{/* The title is quieter than the rows it labels. */}
|
|
72
|
+
<span
|
|
73
|
+
data-slot="collapsible-section-title"
|
|
74
|
+
className="min-w-0 text-sm font-semibold text-foreground-secondary"
|
|
75
|
+
>
|
|
76
|
+
{title}
|
|
77
|
+
</span>
|
|
78
|
+
{count != null ? (
|
|
79
|
+
<CardCount
|
|
80
|
+
data-slot="collapsible-section-count"
|
|
81
|
+
tone={detail ? 'neutral' : tone}
|
|
82
|
+
className="ms-0 inline-flex shrink-0 items-center gap-1.5 whitespace-nowrap"
|
|
83
|
+
>
|
|
84
|
+
{count}
|
|
85
|
+
{detail ? (
|
|
86
|
+
<>
|
|
87
|
+
<span aria-hidden="true">·</span>
|
|
88
|
+
<span className="sr-only">, </span>
|
|
89
|
+
<CardCount
|
|
90
|
+
data-slot="collapsible-section-count-detail"
|
|
91
|
+
tone={tone}
|
|
92
|
+
className="ms-0"
|
|
93
|
+
>
|
|
94
|
+
{detail}
|
|
95
|
+
</CardCount>
|
|
96
|
+
</>
|
|
97
|
+
) : null}
|
|
98
|
+
</CardCount>
|
|
99
|
+
) : null}
|
|
100
|
+
{/* The chevron turns on the panel's height timing, so both settle
|
|
101
|
+
together; its colour and opacity follow the hover on the fill's
|
|
102
|
+
timing. A disabled header shows no hover. */}
|
|
103
|
+
<ChevronDown
|
|
104
|
+
aria-hidden="true"
|
|
105
|
+
strokeWidth={1.75}
|
|
106
|
+
className="ms-auto size-3.75 shrink-0 text-muted-foreground opacity-70 [transition:rotate_var(--transition-duration-slow)_var(--ease-height),color_var(--transition-duration-fast)_var(--ease-default),opacity_var(--transition-duration-fast)_var(--ease-default)] group-[:hover:not([data-disabled])]/collapsible-section-trigger:text-foreground group-[:hover:not([data-disabled])]/collapsible-section-trigger:opacity-100 group-data-panel-open/collapsible-section-trigger:rotate-180 motion-reduce:[transition:color_var(--transition-duration-fast)_var(--ease-default),opacity_var(--transition-duration-fast)_var(--ease-default)]"
|
|
107
|
+
/>
|
|
108
|
+
</CollapsibleTrigger>
|
|
109
|
+
</Heading>
|
|
110
|
+
{/* Height runs on the height curve; under reduced motion it snaps and
|
|
111
|
+
only the rows fade. */}
|
|
112
|
+
<CollapsibleContent
|
|
113
|
+
data-slot="collapsible-section-panel"
|
|
114
|
+
className="group/collapsible-section-panel h-(--collapsible-panel-height) overflow-hidden border-t border-border data-ending-style:h-0 data-starting-style:h-0 motion-safe:transition-[height] motion-safe:duration-slow motion-safe:ease-height"
|
|
115
|
+
>
|
|
116
|
+
{/* Opening, the rows fade in over the second half of the height;
|
|
117
|
+
closing, they leave at once so the height settles on nothing. */}
|
|
118
|
+
<div
|
|
119
|
+
data-slot="collapsible-section-rows"
|
|
120
|
+
className="transition-opacity delay-(--transition-duration-moderate) duration-moderate ease-out group-data-ending-style/collapsible-section-panel:opacity-0 group-data-ending-style/collapsible-section-panel:delay-0 group-data-ending-style/collapsible-section-panel:duration-fast group-data-starting-style/collapsible-section-panel:opacity-0"
|
|
121
|
+
>
|
|
122
|
+
{children}
|
|
123
|
+
</div>
|
|
124
|
+
</CollapsibleContent>
|
|
125
|
+
</Collapsible>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { CollapsibleSection, type CollapsibleSectionProps };
|
|
@@ -12,11 +12,53 @@ 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 {
|
|
15
|
+
import { cn } from '@vendure-io/ui/lib/utils';
|
|
16
|
+
import {
|
|
17
|
+
type ComponentProps,
|
|
18
|
+
type ReactElement,
|
|
19
|
+
type ReactNode,
|
|
20
|
+
useId,
|
|
21
|
+
useRef,
|
|
22
|
+
useState,
|
|
23
|
+
} from 'react';
|
|
24
|
+
|
|
25
|
+
// The dialog's buttons show the ring on any focus, not only keyboard focus:
|
|
26
|
+
// the dialog moves focus itself when it opens, and after a mouse click some
|
|
27
|
+
// browsers do not match :focus-visible. With two buttons, the ring is how you
|
|
28
|
+
// see which one Enter presses.
|
|
29
|
+
const focusRing = 'focus:outline-solid focus:outline-2 focus:outline-offset-2 focus:outline-ring';
|
|
30
|
+
|
|
31
|
+
// The fields in `body` that can take focus when the dialog opens. Checkbox,
|
|
32
|
+
// Switch and Select render a span or button with the role, plus a hidden form
|
|
33
|
+
// input that `isTabbable` skips.
|
|
34
|
+
const FIELD_SELECTOR =
|
|
35
|
+
'input:not(:disabled), textarea:not(:disabled), select:not(:disabled), [contenteditable="true"], [role=checkbox], [role=switch], [role=combobox], [role=radio]';
|
|
36
|
+
|
|
37
|
+
// A field the user can reach with Tab. A hidden input (a DatePicker `name`, a
|
|
38
|
+
// form value) or a field inside an aria-hidden subtree would take focus
|
|
39
|
+
// nowhere visible, so Enter would do nothing.
|
|
40
|
+
function isTabbable(element: HTMLElement) {
|
|
41
|
+
if (element.tabIndex < 0) return false;
|
|
42
|
+
if (element instanceof HTMLInputElement && element.type === 'hidden') return false;
|
|
43
|
+
if (element.closest('[aria-hidden="true"]')) return false;
|
|
44
|
+
if (element.getAttribute('aria-disabled') === 'true') return false;
|
|
45
|
+
return element.checkVisibility?.() ?? true;
|
|
46
|
+
}
|
|
16
47
|
|
|
17
48
|
interface ConfirmDialogProps {
|
|
18
49
|
title: ReactNode;
|
|
50
|
+
/**
|
|
51
|
+
* An icon shown inline before the title, e.g. `<Trash2Icon />`. It is
|
|
52
|
+
* neutral, and takes the destructive tone when `variant="destructive"`.
|
|
53
|
+
*/
|
|
54
|
+
icon?: ReactNode;
|
|
19
55
|
description?: ReactNode;
|
|
56
|
+
/**
|
|
57
|
+
* Content between the description and the buttons. Use it for a field the
|
|
58
|
+
* user must fill before confirming ("type the name to confirm"), and pair it
|
|
59
|
+
* with `confirmDisabled` until the value matches.
|
|
60
|
+
*/
|
|
61
|
+
body?: ReactNode;
|
|
20
62
|
/**
|
|
21
63
|
* May return a promise: the dialog then keeps itself open, disables both
|
|
22
64
|
* buttons, shows a pending spinner on the action, and closes only on resolve.
|
|
@@ -25,7 +67,14 @@ interface ConfirmDialogProps {
|
|
|
25
67
|
onConfirm: () => void | Promise<void>;
|
|
26
68
|
confirmLabel?: ReactNode;
|
|
27
69
|
cancelLabel?: ReactNode;
|
|
28
|
-
|
|
70
|
+
/**
|
|
71
|
+
* `default`: Confirm is the filled primary and takes the focus.
|
|
72
|
+
* `caution`: a costly action that is not destructive ("Deploy anyway" after
|
|
73
|
+
* a failed check). Cancel takes the focus and Confirm is an outline button,
|
|
74
|
+
* so the dialog points one way.
|
|
75
|
+
* `destructive`: a loss. Cancel takes the focus and Confirm is solid red.
|
|
76
|
+
*/
|
|
77
|
+
variant?: 'default' | 'caution' | 'destructive';
|
|
29
78
|
/**
|
|
30
79
|
* Hold the confirm button disabled while the dialog cannot be confirmed yet,
|
|
31
80
|
* e.g. while the consequences it describes are still loading. Cancel stays
|
|
@@ -34,6 +83,15 @@ interface ConfirmDialogProps {
|
|
|
34
83
|
confirmDisabled?: boolean;
|
|
35
84
|
/** Why confirm is disabled, shown beside the buttons while `confirmDisabled` is set. */
|
|
36
85
|
confirmDisabledReason?: ReactNode;
|
|
86
|
+
/**
|
|
87
|
+
* The element that takes focus when the dialog opens. By default the first
|
|
88
|
+
* field in `body` that a user can tab to takes it; hidden inputs never do.
|
|
89
|
+
* Without one, Confirm takes it, so Enter confirms. A
|
|
90
|
+
* caution or destructive dialog, or one whose Confirm is disabled, focuses
|
|
91
|
+
* Cancel, so Enter never starts a costly action by accident. The focused button shows its ring at
|
|
92
|
+
* once, also when a mouse click opened the dialog.
|
|
93
|
+
*/
|
|
94
|
+
initialFocus?: ComponentProps<typeof AlertDialogContent>['initialFocus'];
|
|
37
95
|
/** Uncontrolled mode — the element that opens the dialog. */
|
|
38
96
|
children?: ReactElement;
|
|
39
97
|
/** Controlled mode — supply with `onOpenChange`. */
|
|
@@ -43,13 +101,16 @@ interface ConfirmDialogProps {
|
|
|
43
101
|
|
|
44
102
|
function ConfirmDialog({
|
|
45
103
|
title,
|
|
104
|
+
icon,
|
|
46
105
|
description,
|
|
106
|
+
body,
|
|
47
107
|
onConfirm,
|
|
48
108
|
confirmLabel = 'Confirm',
|
|
49
109
|
cancelLabel = 'Cancel',
|
|
50
110
|
variant = 'default',
|
|
51
111
|
confirmDisabled = false,
|
|
52
112
|
confirmDisabledReason,
|
|
113
|
+
initialFocus,
|
|
53
114
|
children,
|
|
54
115
|
open,
|
|
55
116
|
onOpenChange,
|
|
@@ -62,6 +123,17 @@ function ConfirmDialog({
|
|
|
62
123
|
const pendingRef = useRef(false);
|
|
63
124
|
const actualOpen = isControlled ? open : internalOpen;
|
|
64
125
|
const reasonId = useId();
|
|
126
|
+
const bodyRef = useRef<HTMLDivElement>(null);
|
|
127
|
+
const cancelRef = useRef<HTMLButtonElement>(null);
|
|
128
|
+
const confirmRef = useRef<HTMLButtonElement>(null);
|
|
129
|
+
|
|
130
|
+
const defaultInitialFocus = () => {
|
|
131
|
+
const fields = bodyRef.current?.querySelectorAll<HTMLElement>(FIELD_SELECTOR) ?? [];
|
|
132
|
+
const field = Array.from(fields).find(isTabbable);
|
|
133
|
+
if (field) return field;
|
|
134
|
+
if (variant !== 'default' || confirmDisabled) return cancelRef.current ?? true;
|
|
135
|
+
return confirmRef.current ?? true;
|
|
136
|
+
};
|
|
65
137
|
const showReason = confirmDisabled && !pending && Boolean(confirmDisabledReason);
|
|
66
138
|
|
|
67
139
|
const setOpen = (next: boolean) => {
|
|
@@ -93,33 +165,65 @@ function ConfirmDialog({
|
|
|
93
165
|
}}
|
|
94
166
|
>
|
|
95
167
|
{children ? <AlertDialogTrigger render={children} /> : null}
|
|
96
|
-
<AlertDialogContent
|
|
168
|
+
<AlertDialogContent
|
|
169
|
+
data-slot="confirm-dialog"
|
|
170
|
+
initialFocus={initialFocus ?? defaultInitialFocus}
|
|
171
|
+
>
|
|
97
172
|
<AlertDialogHeader>
|
|
98
|
-
|
|
173
|
+
{icon ? (
|
|
174
|
+
<AlertDialogTitle className="flex items-center gap-2.5">
|
|
175
|
+
<span
|
|
176
|
+
data-slot="confirm-dialog-icon"
|
|
177
|
+
aria-hidden="true"
|
|
178
|
+
className={cn(
|
|
179
|
+
"flex shrink-0 [&_svg:not([class*='size-'])]:size-4.5 [&_svg:not([class*='stroke-'])]:stroke-[1.75]",
|
|
180
|
+
variant === 'destructive' ? 'text-destructive-text' : 'text-muted-foreground',
|
|
181
|
+
)}
|
|
182
|
+
>
|
|
183
|
+
{icon}
|
|
184
|
+
</span>
|
|
185
|
+
<span>{title}</span>
|
|
186
|
+
</AlertDialogTitle>
|
|
187
|
+
) : (
|
|
188
|
+
<AlertDialogTitle>{title}</AlertDialogTitle>
|
|
189
|
+
)}
|
|
99
190
|
{description ? <AlertDialogDescription>{description}</AlertDialogDescription> : null}
|
|
100
191
|
</AlertDialogHeader>
|
|
192
|
+
{body ? (
|
|
193
|
+
<div ref={bodyRef} data-slot="confirm-dialog-body">
|
|
194
|
+
{body}
|
|
195
|
+
</div>
|
|
196
|
+
) : null}
|
|
101
197
|
<AlertDialogFooter>
|
|
102
198
|
{showReason ? (
|
|
103
199
|
<p
|
|
104
200
|
id={reasonId}
|
|
105
201
|
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:
|
|
202
|
+
className="text-muted-foreground self-center text-sm group-data-[size=sm]/alert-dialog-content:col-span-2 sm:me-auto"
|
|
107
203
|
>
|
|
108
204
|
{confirmDisabledReason}
|
|
109
205
|
</p>
|
|
110
206
|
) : null}
|
|
111
|
-
<AlertDialogCancel disabled={pending}
|
|
207
|
+
<AlertDialogCancel ref={cancelRef} disabled={pending} className={focusRing}>
|
|
208
|
+
{cancelLabel}
|
|
209
|
+
</AlertDialogCancel>
|
|
112
210
|
{/* AlertDialogAction is a plain Button (not a Close), so closing stays
|
|
113
211
|
under our control — the promise decides when the dialog leaves. */}
|
|
212
|
+
{/* While pending, the spinner takes the label's place. The label stays
|
|
213
|
+
in the layout (and the accessible name), so the button keeps its width. */}
|
|
114
214
|
<AlertDialogAction
|
|
115
|
-
variant={variant}
|
|
215
|
+
variant={variant === 'caution' ? 'outline' : variant}
|
|
116
216
|
disabled={pending || confirmDisabled}
|
|
117
217
|
aria-busy={pending || undefined}
|
|
118
218
|
aria-describedby={showReason ? reasonId : undefined}
|
|
119
219
|
onClick={handleConfirm}
|
|
220
|
+
ref={confirmRef}
|
|
221
|
+
className={cn('relative', focusRing)}
|
|
120
222
|
>
|
|
121
|
-
{pending
|
|
122
|
-
|
|
223
|
+
<span data-slot="confirm-dialog-label" className={cn(pending && 'opacity-0')}>
|
|
224
|
+
{confirmLabel}
|
|
225
|
+
</span>
|
|
226
|
+
{pending ? <Spinner className="absolute inset-0 m-auto" /> : null}
|
|
123
227
|
</AlertDialogAction>
|
|
124
228
|
</AlertDialogFooter>
|
|
125
229
|
</AlertDialogContent>
|
|
@@ -8,6 +8,67 @@ import { cn } from '@vendure-io/ui/lib/utils';
|
|
|
8
8
|
import { CheckIcon, CopyIcon } from 'lucide-react';
|
|
9
9
|
import type * as React from 'react';
|
|
10
10
|
|
|
11
|
+
interface UseCopyActionOptions {
|
|
12
|
+
/** How long `copied` stays true after a successful copy, in ms. @default 2000 */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
/** Called after a successful copy. Falls back to `CopyFeedbackProvider`. */
|
|
15
|
+
onCopied?: () => void;
|
|
16
|
+
/** Called when the clipboard write fails. Falls back to `CopyFeedbackProvider`. */
|
|
17
|
+
onCopyError?: (error: Error) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface CopyAction {
|
|
21
|
+
/** True for `timeout` ms after the last successful copy. */
|
|
22
|
+
copied: boolean;
|
|
23
|
+
/** Writes `value` to the clipboard and reports the result through the callbacks. */
|
|
24
|
+
copy: (value: ClipboardSource) => Promise<boolean>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The copy behaviour of `CopyButton` without the button: clipboard write,
|
|
29
|
+
* transient `copied` flag, and the callback → `CopyFeedbackProvider` fallback.
|
|
30
|
+
* Use it when a parent must know the copied state, for example to change a
|
|
31
|
+
* tooltip from "Copy" to "Copied". Pass the result to `CopyButton` as
|
|
32
|
+
* `copyAction` so the button and the parent share one state.
|
|
33
|
+
*/
|
|
34
|
+
function useCopyAction({ timeout, onCopied, onCopyError }: UseCopyActionOptions = {}): CopyAction {
|
|
35
|
+
const { copied, copy } = useCopy({ timeout });
|
|
36
|
+
const copyFeedback = useCopyFeedback();
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
copied,
|
|
40
|
+
copy: async (value) => {
|
|
41
|
+
const ok = await copy(value);
|
|
42
|
+
if (ok) (onCopied ?? copyFeedback.onCopied)?.();
|
|
43
|
+
else
|
|
44
|
+
(onCopyError ?? copyFeedback.onCopyError)?.(new Error('Failed to copy to the clipboard'));
|
|
45
|
+
return ok;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The copy icon that turns into a check-mark while `copied` is true. The two
|
|
52
|
+
* icons cross-fade; with motion allowed, the outgoing icon also shrinks and
|
|
53
|
+
* blurs. The check uses the text-safe `success-text` slot, never a raw green.
|
|
54
|
+
*/
|
|
55
|
+
function CopyStateIcon({ copied, className }: { copied: boolean; className?: string }) {
|
|
56
|
+
const swap =
|
|
57
|
+
'col-start-1 row-start-1 transition-[opacity,scale,filter] duration-moderate ease-out';
|
|
58
|
+
const out = 'opacity-0 motion-safe:scale-25 motion-safe:blur-[4px]';
|
|
59
|
+
return (
|
|
60
|
+
<span
|
|
61
|
+
aria-hidden
|
|
62
|
+
data-slot="copy-state-icon"
|
|
63
|
+
data-copied={copied || undefined}
|
|
64
|
+
className={cn('grid shrink-0 place-items-center', className)}
|
|
65
|
+
>
|
|
66
|
+
<CopyIcon className={cn(swap, copied && out)} />
|
|
67
|
+
<CheckIcon className={cn(swap, 'text-success-text', !copied && out)} />
|
|
68
|
+
</span>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
11
72
|
interface CopyButtonProps extends Omit<React.ComponentProps<typeof Button>, 'value' | 'children'> {
|
|
12
73
|
/**
|
|
13
74
|
* The text written to the clipboard, or a callback that loads it when the
|
|
@@ -31,12 +92,17 @@ interface CopyButtonProps extends Omit<React.ComponentProps<typeof Button>, 'val
|
|
|
31
92
|
copyLabel?: string;
|
|
32
93
|
/** Accessible label shown while the copied state is active. @default "Copied" */
|
|
33
94
|
copiedLabel?: string;
|
|
95
|
+
/**
|
|
96
|
+
* A shared copy state from `useCopyAction`. When set, the button uses it
|
|
97
|
+
* instead of its own, and `timeout`, `onCopied` and `onCopyError` belong on
|
|
98
|
+
* the hook call.
|
|
99
|
+
*/
|
|
100
|
+
copyAction?: CopyAction;
|
|
34
101
|
}
|
|
35
102
|
|
|
36
103
|
/**
|
|
37
104
|
* Ghost icon button that copies `value` and swaps its icon to a check-mark for a
|
|
38
|
-
* short window
|
|
39
|
-
* (never a raw green), per ADR 0003.
|
|
105
|
+
* short window (see `CopyStateIcon`).
|
|
40
106
|
*/
|
|
41
107
|
function CopyButton({
|
|
42
108
|
value,
|
|
@@ -49,10 +115,11 @@ function CopyButton({
|
|
|
49
115
|
size = 'icon-xs',
|
|
50
116
|
className,
|
|
51
117
|
onClick,
|
|
118
|
+
copyAction,
|
|
52
119
|
...props
|
|
53
120
|
}: CopyButtonProps) {
|
|
54
|
-
const
|
|
55
|
-
const
|
|
121
|
+
const ownAction = useCopyAction({ timeout, onCopied, onCopyError });
|
|
122
|
+
const { copied, copy } = copyAction ?? ownAction;
|
|
56
123
|
|
|
57
124
|
return (
|
|
58
125
|
<Button
|
|
@@ -65,14 +132,11 @@ function CopyButton({
|
|
|
65
132
|
onClick={async (event) => {
|
|
66
133
|
onClick?.(event);
|
|
67
134
|
if (event.defaultPrevented) return;
|
|
68
|
-
|
|
69
|
-
if (ok) (onCopied ?? copyFeedback.onCopied)?.();
|
|
70
|
-
else
|
|
71
|
-
(onCopyError ?? copyFeedback.onCopyError)?.(new Error('Failed to copy to the clipboard'));
|
|
135
|
+
await copy(value);
|
|
72
136
|
}}
|
|
73
137
|
{...props}
|
|
74
138
|
>
|
|
75
|
-
|
|
139
|
+
<CopyStateIcon copied={copied} />
|
|
76
140
|
</Button>
|
|
77
141
|
);
|
|
78
142
|
}
|
|
@@ -125,5 +189,5 @@ function CopyableText({
|
|
|
125
189
|
);
|
|
126
190
|
}
|
|
127
191
|
|
|
128
|
-
export { CopyButton, CopyableText };
|
|
129
|
-
export type { CopyButtonProps, CopyableTextProps };
|
|
192
|
+
export { CopyButton, CopyableText, CopyStateIcon, useCopyAction };
|
|
193
|
+
export type { CopyAction, CopyButtonProps, CopyableTextProps, UseCopyActionOptions };
|
|
@@ -14,6 +14,27 @@ import { getSelectedOriginals, getSelectedRowIds } from './data-table-helpers.ts
|
|
|
14
14
|
// `row.original` by id as pages are seen, letting `bulkActions` receive every
|
|
15
15
|
// selected original — not just the ones still on screen.
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Keep the latest border-box height of an element. DataTable no longer uses
|
|
19
|
+
* it (the bulk bar shares the controls row's grid cell); it stays exported
|
|
20
|
+
* for consumers. Example: hold a bulk bar at the height of the row it
|
|
21
|
+
* replaces, so the table below does not move. A
|
|
22
|
+
* ResizeObserver follows the row while it shows (it can wrap on a narrow
|
|
23
|
+
* card); the last height stays once the row has left.
|
|
24
|
+
*/
|
|
25
|
+
function useHeldHeight(): [React.RefCallback<HTMLElement>, number | undefined] {
|
|
26
|
+
const [height, setHeight] = React.useState<number>();
|
|
27
|
+
const ref = React.useCallback((node: HTMLElement | null) => {
|
|
28
|
+
if (!node) return;
|
|
29
|
+
setHeight(node.offsetHeight);
|
|
30
|
+
if (typeof ResizeObserver === 'undefined') return;
|
|
31
|
+
const observer = new ResizeObserver(() => setHeight(node.offsetHeight));
|
|
32
|
+
observer.observe(node);
|
|
33
|
+
return () => observer.disconnect();
|
|
34
|
+
}, []);
|
|
35
|
+
return [ref, height];
|
|
36
|
+
}
|
|
37
|
+
|
|
17
38
|
/**
|
|
18
39
|
* Accumulate `row.original` keyed by row id across page changes. Populated during
|
|
19
40
|
* render (idempotent map writes) so the current render's `bulkActions` can
|
|
@@ -44,17 +65,24 @@ function DataTableBulkActions<TData>({
|
|
|
44
65
|
const selectedRowIds = getSelectedRowIds(selection);
|
|
45
66
|
if (selectedRowIds.length === 0) return null;
|
|
46
67
|
|
|
68
|
+
const loaded = getSelectedOriginals(selection, cache);
|
|
47
69
|
const ctx: DataTableBulkActionContext<TData> = {
|
|
48
|
-
selection:
|
|
70
|
+
selection: loaded,
|
|
49
71
|
selectedRowIds,
|
|
72
|
+
selectedCount: selectedRowIds.length,
|
|
73
|
+
// The cache holds a row once the table has shown it, so an id it lacks is on
|
|
74
|
+
// a page not seen yet, still loading, or no longer exists.
|
|
75
|
+
missingRowIds:
|
|
76
|
+
loaded.length === selectedRowIds.length ? [] : selectedRowIds.filter((id) => !cache.has(id)),
|
|
50
77
|
table,
|
|
51
78
|
clearSelection: () => table.resetRowSelection(),
|
|
52
79
|
};
|
|
53
80
|
|
|
54
81
|
return (
|
|
55
82
|
// A plain band row, not a box: it takes the controls row's place inside
|
|
56
|
-
// the header band, so it carries no chrome of its own. min-h-9
|
|
57
|
-
//
|
|
83
|
+
// the header band, so it carries no chrome of its own. min-h-9 is the
|
|
84
|
+
// height of a bar on its own; DataTable sets min-h-0 and takes the height
|
|
85
|
+
// of the row the bar covers (the controls row or the column header row).
|
|
58
86
|
<div
|
|
59
87
|
data-slot="data-table-bulk-actions"
|
|
60
88
|
className={cn('flex min-h-9 flex-wrap items-center gap-2', className)}
|
|
@@ -65,4 +93,4 @@ function DataTableBulkActions<TData>({
|
|
|
65
93
|
);
|
|
66
94
|
}
|
|
67
95
|
|
|
68
|
-
export { DataTableBulkActions, useSelectionCache };
|
|
96
|
+
export { DataTableBulkActions, useHeldHeight, useSelectionCache };
|
|
@@ -8,7 +8,8 @@ import type * as React from 'react';
|
|
|
8
8
|
|
|
9
9
|
// The sortable header control rendered inside a `<TableHead>`. When the column
|
|
10
10
|
// can't sort it degrades to plain text — the header stays a passive label, no
|
|
11
|
-
// button, no affordance it can't honour.
|
|
11
|
+
// button, no affordance it can't honour. Both take their size, weight and
|
|
12
|
+
// colour from the `<TableHead>`. The `aria-sort` state lives on the
|
|
12
13
|
// enclosing `<TableHead>` (set by `data-table.tsx`), not here.
|
|
13
14
|
|
|
14
15
|
interface DataTableColumnHeaderProps<TData, TValue>
|
|
@@ -16,17 +17,23 @@ interface DataTableColumnHeaderProps<TData, TValue>
|
|
|
16
17
|
column: Column<TData, TValue>;
|
|
17
18
|
/** Accessible name for the sort toggle, e.g. `Sort by Name`. */
|
|
18
19
|
sortLabel?: string;
|
|
20
|
+
/**
|
|
21
|
+
* `'end'` puts the sort icon before the label and pulls the button forward,
|
|
22
|
+
* so the label ends where the column's end-aligned cells end.
|
|
23
|
+
*/
|
|
24
|
+
align?: 'start' | 'end';
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
function DataTableColumnHeader<TData, TValue>({
|
|
22
28
|
column,
|
|
23
29
|
sortLabel,
|
|
30
|
+
align = 'start',
|
|
24
31
|
className,
|
|
25
32
|
children,
|
|
26
33
|
...props
|
|
27
34
|
}: DataTableColumnHeaderProps<TData, TValue>) {
|
|
28
35
|
if (!column.getCanSort()) {
|
|
29
|
-
return <span className={
|
|
36
|
+
return <span className={className}>{children}</span>;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
const sorted = column.getIsSorted();
|
|
@@ -41,7 +48,17 @@ function DataTableColumnHeader<TData, TValue>({
|
|
|
41
48
|
// Tri-state cycle asc → desc → none is TanStack's default toggle (removal
|
|
42
49
|
// enabled). We only wire the click; the engine owns the state machine.
|
|
43
50
|
onClick={column.getToggleSortingHandler()}
|
|
44
|
-
|
|
51
|
+
// Pulls back by the button's own `--content-inset` (its padding and its
|
|
52
|
+
// 1px border), so the label starts where a plain header label would. An
|
|
53
|
+
// end-aligned header pulls forward instead and puts the icon first, so
|
|
54
|
+
// the label ends where the cells end.
|
|
55
|
+
data-align={align}
|
|
56
|
+
className={cn(
|
|
57
|
+
// The label takes the header's colour; hover still darkens it.
|
|
58
|
+
'h-8 gap-1.5 text-inherit data-[slot=data-table-column-header]:px-2.5',
|
|
59
|
+
align === 'end' ? '-me-(--content-inset) flex-row-reverse' : '-ms-(--content-inset)',
|
|
60
|
+
className,
|
|
61
|
+
)}
|
|
45
62
|
{...props}
|
|
46
63
|
>
|
|
47
64
|
{children}
|
|
@@ -84,7 +84,7 @@ function DataTableAddFilter<TData>({
|
|
|
84
84
|
<div className="flex flex-col gap-2">
|
|
85
85
|
<button
|
|
86
86
|
type="button"
|
|
87
|
-
className="text-muted-foreground hover:text-foreground -
|
|
87
|
+
className="text-muted-foreground hover:text-foreground -ms-1 flex items-center gap-1 rounded-md text-xs outline-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
|
|
88
88
|
onClick={() => setActiveId(null)}
|
|
89
89
|
>
|
|
90
90
|
<ChevronLeftIcon className="size-3" />
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { Column, ColumnDef, RowSelectionState, Table } from '@tanstack/react-table';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ControlledState,
|
|
4
|
+
DataTableColumnMeta,
|
|
5
|
+
} from '@vendure-io/ui/components/molecules/data-table/data-table-types';
|
|
3
6
|
|
|
4
7
|
// Pure, TanStack-shaped logic for the DataTable core. No JSX and no React state,
|
|
5
8
|
// so each function is unit-testable in isolation (the getRange/clampPage
|
|
@@ -71,3 +74,67 @@ export function getSelectedOriginals<TData>(
|
|
|
71
74
|
export function getSelectedRowIds(selection: RowSelectionState): string[] {
|
|
72
75
|
return Object.keys(selection).filter((id) => selection[id]);
|
|
73
76
|
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* How far each column must indent its header and cells, so that cell content
|
|
80
|
+
* that pulls back by its own start inset (`--content-inset`) stays inside the
|
|
81
|
+
* column gutter. Keyed by `data-column-id`; a column that needs no indent is
|
|
82
|
+
* left out.
|
|
83
|
+
*
|
|
84
|
+
* Content may pull back into its start padding by up to the gutter between
|
|
85
|
+
* two columns: the cell's end padding. In a middle column that reaches the
|
|
86
|
+
* cell edge. The first column's start padding is the card's inset and the
|
|
87
|
+
* leading column's holds the selection checkbox, so there the content keeps
|
|
88
|
+
* the rest of the padding clear. A bigger pull-back (a chip with a leading
|
|
89
|
+
* icon) indents the column by the rest instead, and its header and plain-text
|
|
90
|
+
* cells follow.
|
|
91
|
+
*
|
|
92
|
+
* The measure reads boxes, so it also covers a consumer's own margin. It
|
|
93
|
+
* checks the cell's first element and that element's first element (content
|
|
94
|
+
* one wrapper deep). The result does not change with the indent already
|
|
95
|
+
* applied, so a second measure returns the same result.
|
|
96
|
+
*/
|
|
97
|
+
export function measureColumnIndents(table: HTMLTableElement): Record<string, number> {
|
|
98
|
+
const indents: Record<string, number> = {};
|
|
99
|
+
// Array.from, not for…of: a consumer's lib may not type NodeList as iterable.
|
|
100
|
+
const cells = Array.from(
|
|
101
|
+
table.querySelectorAll<HTMLElement>(':scope > tbody > tr > td[data-column-id]'),
|
|
102
|
+
);
|
|
103
|
+
for (const cell of cells) {
|
|
104
|
+
const id = cell.dataset.columnId;
|
|
105
|
+
const first = cell.firstElementChild;
|
|
106
|
+
if (id == null || first == null) continue;
|
|
107
|
+
const style = getComputedStyle(cell);
|
|
108
|
+
const rtl = style.direction === 'rtl';
|
|
109
|
+
const start = (rect: DOMRect) => (rtl ? -rect.right : rect.left);
|
|
110
|
+
const cellStart = start(cell.getBoundingClientRect());
|
|
111
|
+
const indent = Number.parseFloat(cell.style.getPropertyValue('--column-indent')) || 0;
|
|
112
|
+
const padding = Number.parseFloat(style.paddingInlineStart) - indent;
|
|
113
|
+
const gutter = Math.min(padding, Number.parseFloat(style.paddingInlineEnd));
|
|
114
|
+
// The nearest the content may start to the cell edge, without the indent.
|
|
115
|
+
const clear = padding - gutter;
|
|
116
|
+
for (const content of [first, first.firstElementChild]) {
|
|
117
|
+
if (content == null) continue;
|
|
118
|
+
const rect = content.getBoundingClientRect();
|
|
119
|
+
if (rect.width === 0) continue;
|
|
120
|
+
// Rounded to a hundredth of a pixel, so float noise never moves the text.
|
|
121
|
+
const need = Math.round((clear + indent - (start(rect) - cellStart)) * 100) / 100;
|
|
122
|
+
if (need > (indents[id] ?? 0)) indents[id] = need;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return indents;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Shallow equality of two indent maps, so an unchanged measure keeps its state. */
|
|
129
|
+
export function sameIndents(a: Record<string, number>, b: Record<string, number>): boolean {
|
|
130
|
+
const keys = Object.keys(a);
|
|
131
|
+
return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The side a column aligns to, from its `meta.align` (see DataTableColumnMeta).
|
|
136
|
+
* TanStack types `meta` as the consumer's own ColumnMeta, so it is read loosely.
|
|
137
|
+
*/
|
|
138
|
+
export function columnAlign(meta: unknown): NonNullable<DataTableColumnMeta['align']> {
|
|
139
|
+
return (meta as DataTableColumnMeta | undefined)?.align === 'end' ? 'end' : 'start';
|
|
140
|
+
}
|