@vendure-io/ui 2.1.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 +73 -13
- 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 +138 -4
- package/src/components/molecules/data-table/data-table-view-options.tsx +2 -2
- package/src/components/molecules/data-table/data-table.tsx +916 -84
- 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
|
@@ -1,100 +1,466 @@
|
|
|
1
1
|
import { mergeProps } from '@base-ui/react/merge-props';
|
|
2
2
|
import { useRender } from '@base-ui/react/use-render';
|
|
3
|
+
import { contentInsetPull } from '@vendure-io/ui/lib/content-inset';
|
|
3
4
|
import type { Tone } from '@vendure-io/ui/lib/state-dictionary';
|
|
5
|
+
import {
|
|
6
|
+
statusChipFill,
|
|
7
|
+
statusChipLabel,
|
|
8
|
+
statusChipRing,
|
|
9
|
+
statusToneText,
|
|
10
|
+
statusToneVars,
|
|
11
|
+
} from '@vendure-io/ui/lib/tint-recipes';
|
|
12
|
+
import { toneIcons } from '@vendure-io/ui/lib/tone-icons';
|
|
4
13
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
5
14
|
import { cva } from 'class-variance-authority';
|
|
15
|
+
import type * as React from 'react';
|
|
16
|
+
import { isValidElement } from 'react';
|
|
17
|
+
// Relative: `time-tooltip` is internal.
|
|
18
|
+
import { InStatusDetail } from './time-tooltip.tsx';
|
|
6
19
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
20
|
+
type StatusBadgeAppearance = 'chip' | 'bare' | 'icon' | 'text' | 'dot';
|
|
21
|
+
type StatusBadgeSize = 'default' | 'lg';
|
|
22
|
+
type StatusBadgeReveal = 'always' | 'row';
|
|
23
|
+
|
|
24
|
+
// The chip takes its tone in three places, all mixed from one `--status-tone`
|
|
25
|
+
// colour: a faint 5% tone wash over the `muted` fill, a 1px inset outline at
|
|
26
|
+
// 34%, and a label tinted 30% from `foreground-secondary` towards the tone. A
|
|
27
|
+
// row of chips stays calm, and each one still reads its state at a glance.
|
|
28
|
+
// The outline is an inset ring, not a border, so it adds no width. Forced
|
|
29
|
+
// colours drop box shadows, so a transparent 1px outline, inset by its own
|
|
30
|
+
// width, draws the chip's box there instead (still no width). There is
|
|
31
|
+
// no solid mode: solid/brand chips stay on the base `Badge` (accent rationing
|
|
32
|
+
// applied at the API level).
|
|
11
33
|
//
|
|
12
|
-
// Slot = tone, with two aliases: `critical`
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
34
|
+
// Slot = tone, with two aliases: `critical` uses the `destructive` slots (the
|
|
35
|
+
// token keeps its shadcn-compatible name) and `progress` uses the `info`
|
|
36
|
+
// slots, so a chip in motion is blue before it lands on its outcome.
|
|
37
|
+
// `neutral` takes `neutral-text`: the muted grey in light mode, lifted in
|
|
38
|
+
// dark mode so a neutral chip does not read dim. `warning` draws its
|
|
39
|
+
// fill and outline in `warning-indicator`: mixed from `warning-text`, they
|
|
40
|
+
// read beige. Its label mixes 70% of `warning-text` instead. Class strings
|
|
41
|
+
// are static per tone so Tailwind's scanner picks them up.
|
|
18
42
|
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
43
|
+
// `--content-inset` is the distance from the box to the first letter: the
|
|
44
|
+
// start padding, plus the marker (13px icon or 6px dot, 14px icon in `lg`)
|
|
45
|
+
// and its 6px gap. A DataTable cell pulls the chip back by it, so the label
|
|
46
|
+
// lines up with the column header. `bare` has no padding, so only the marker
|
|
47
|
+
// counts.
|
|
48
|
+
const toneVars = statusToneVars satisfies Record<Tone, string>;
|
|
49
|
+
|
|
50
|
+
// `reveal="row"`: the badge is transparent at rest and fades in while its row
|
|
51
|
+
// (`group/row`) is hovered or holds keyboard focus, on one transition (150ms,
|
|
52
|
+
// ease-out), the same as the IdChip row reveal. It keeps its box, so nothing
|
|
53
|
+
// moves when it appears and the label still lines up with the column header.
|
|
54
|
+
// Only opacity changes, so reduced motion keeps the fade. Without hover
|
|
55
|
+
// (touch), the badge always shows. A tone change keeps its outline and fill
|
|
56
|
+
// transition, at the reveal's pace. Without `reveal`, a chip eases to a new
|
|
57
|
+
// tone over 300ms.
|
|
58
|
+
const rowRevealClass = [
|
|
59
|
+
'transition-[opacity,box-shadow,background-color,color] duration-moderate ease-out',
|
|
60
|
+
'opacity-0 group-hover/row:opacity-100 group-has-[:focus-visible]/row:opacity-100 [@media(hover:none)]:opacity-100',
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
const chipLabel = statusChipLabel;
|
|
64
|
+
|
|
65
|
+
// Code written for 2.x puts its own icon in `children`. Its `<svg>` is then a
|
|
66
|
+
// direct child of the badge, and the tone icon (or spinner) hides, so the
|
|
67
|
+
// badge shows one icon. A dot stays: 2.x had it too.
|
|
68
|
+
const ownIconHidesToneIcon = '[&:has(>svg)>[data-marker=icon]]:hidden';
|
|
69
|
+
|
|
70
|
+
// That own icon takes the tone colour, as the tone icon does, so two badges
|
|
71
|
+
// of one state look the same. The rule is in `:where()`, so it has no
|
|
72
|
+
// specificity: a colour class on the icon wins. Static per tone for
|
|
73
|
+
// Tailwind's scanner, matching `statusToneText`.
|
|
74
|
+
const ownIconToneText: Record<Tone, string> = {
|
|
75
|
+
neutral: '[:where(&>svg)]:text-neutral-text',
|
|
76
|
+
info: '[:where(&>svg)]:text-info-text',
|
|
77
|
+
success: '[:where(&>svg)]:text-success-text',
|
|
78
|
+
warning: '[:where(&>svg)]:text-warning-text',
|
|
79
|
+
critical: '[:where(&>svg)]:text-destructive-text',
|
|
80
|
+
progress: '[:where(&>svg)]:text-info-text',
|
|
81
|
+
};
|
|
82
|
+
|
|
21
83
|
const statusBadgeVariants = cva(
|
|
22
|
-
|
|
84
|
+
[
|
|
85
|
+
'inline-flex w-fit shrink-0 items-center justify-center overflow-hidden whitespace-nowrap rounded-lg font-medium',
|
|
86
|
+
statusChipFill,
|
|
87
|
+
statusChipRing,
|
|
88
|
+
chipLabel,
|
|
89
|
+
ownIconHidesToneIcon,
|
|
90
|
+
'outline-1 outline-transparent -outline-offset-1',
|
|
91
|
+
contentInsetPull,
|
|
92
|
+
],
|
|
93
|
+
{
|
|
94
|
+
variants: {
|
|
95
|
+
tone: toneVars,
|
|
96
|
+
size: {
|
|
97
|
+
default: [
|
|
98
|
+
'h-6 gap-1.5 px-2.5 text-xs/none has-data-[slot=status-badge-marker]:ps-2',
|
|
99
|
+
'[--content-inset:calc(var(--spacing)*2.5)] has-data-[marker=icon]:[--content-inset:calc(var(--spacing)*6.75)] has-data-[marker=dot]:[--content-inset:calc(var(--spacing)*5)]',
|
|
100
|
+
],
|
|
101
|
+
lg: [
|
|
102
|
+
'h-7 gap-1.5 px-3 text-sm/none has-data-[slot=status-badge-marker]:ps-2.5',
|
|
103
|
+
'[--content-inset:calc(var(--spacing)*3)] has-data-[marker=icon]:[--content-inset:calc(var(--spacing)*7.5)] has-data-[marker=dot]:[--content-inset:calc(var(--spacing)*5.5)]',
|
|
104
|
+
],
|
|
105
|
+
} satisfies Record<StatusBadgeSize, string[]>,
|
|
106
|
+
reveal: {
|
|
107
|
+
// A tone change eases the outline and label over 300ms; the hover
|
|
108
|
+
// fill of a tooltip trigger answers in 120ms.
|
|
109
|
+
always:
|
|
110
|
+
'[transition:box-shadow_var(--transition-duration-slow),color_var(--transition-duration-slow),background-color_var(--transition-duration-quick)]',
|
|
111
|
+
row: rowRevealClass,
|
|
112
|
+
} satisfies Record<StatusBadgeReveal, string | string[]>,
|
|
113
|
+
},
|
|
114
|
+
defaultVariants: {
|
|
115
|
+
tone: 'neutral',
|
|
116
|
+
size: 'default',
|
|
117
|
+
reveal: 'always',
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// The `bare` appearance: the chip without its box. The same icon and tinted
|
|
123
|
+
// label, no fill, outline or padding, for a state inside a tinted or accent
|
|
124
|
+
// surface, where a chip would be a box inside a box.
|
|
125
|
+
const bareVariants = cva(
|
|
126
|
+
[
|
|
127
|
+
'inline-flex w-fit shrink-0 items-center gap-1.5 whitespace-nowrap font-medium',
|
|
128
|
+
chipLabel,
|
|
129
|
+
ownIconHidesToneIcon,
|
|
130
|
+
contentInsetPull,
|
|
131
|
+
],
|
|
23
132
|
{
|
|
24
133
|
variants: {
|
|
25
|
-
tone:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
134
|
+
tone: toneVars,
|
|
135
|
+
size: {
|
|
136
|
+
default:
|
|
137
|
+
'text-xs/none has-data-[marker=icon]:[--content-inset:calc(var(--spacing)*4.75)] has-data-[marker=dot]:[--content-inset:calc(var(--spacing)*3)]',
|
|
138
|
+
lg: 'text-sm/none has-data-[marker=icon]:[--content-inset:calc(var(--spacing)*5)] has-data-[marker=dot]:[--content-inset:calc(var(--spacing)*3)]',
|
|
139
|
+
} satisfies Record<StatusBadgeSize, string>,
|
|
140
|
+
reveal: {
|
|
141
|
+
always: 'transition-colors duration-slow',
|
|
142
|
+
row: rowRevealClass,
|
|
143
|
+
} satisfies Record<StatusBadgeReveal, string | string[]>,
|
|
34
144
|
},
|
|
35
145
|
defaultVariants: {
|
|
36
146
|
tone: 'neutral',
|
|
147
|
+
size: 'default',
|
|
148
|
+
reveal: 'always',
|
|
37
149
|
},
|
|
38
150
|
},
|
|
39
151
|
);
|
|
40
152
|
|
|
153
|
+
// The `icon` appearance: the tone icon with no box, then the label in neutral
|
|
154
|
+
// text, and an optional second line under it. It is the status of a table row
|
|
155
|
+
// that every row has. A grid puts the icon in a column of its own, spanning
|
|
156
|
+
// both lines and centred against them, and the second line under the label.
|
|
157
|
+
// With a second line, the lines are 18px and 15px high (`text-sm/4.5`,
|
|
158
|
+
// `text-xs/3.75`): 33px, and a DataTable cell drops to 6px vertical padding
|
|
159
|
+
// for it (`py-1.5`), so the row is 46px with its divider. The 16px icon and its 8px gap make
|
|
160
|
+
// the `--content-inset`, so the label lines up with the column header.
|
|
161
|
+
const iconAppearanceClass = [
|
|
162
|
+
'inline-grid w-fit shrink-0 grid-cols-[auto_auto] items-center gap-x-2 whitespace-nowrap text-sm text-foreground-secondary',
|
|
163
|
+
'[--content-inset:calc(var(--spacing)*6)]',
|
|
164
|
+
contentInsetPull,
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
// The marker colour of each tone: the icon or spinner stroke.
|
|
168
|
+
const toneText: Record<Tone, string> = statusToneText;
|
|
169
|
+
|
|
170
|
+
// The dot fill of each tone. A dot is a filled shape, not a stroke or a
|
|
171
|
+
// word, so warning takes `warning-indicator`, the amber the warning chip
|
|
172
|
+
// fill mixes from: in `warning-text` the dot reads brown.
|
|
173
|
+
const toneDot: Record<Tone, string> = {
|
|
174
|
+
...toneText,
|
|
175
|
+
warning: 'text-warning-indicator',
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// The `text` appearance: the word itself carries the tone. A neutral word
|
|
179
|
+
// steps down to muted text at the same weight, so the column stays quiet and
|
|
180
|
+
// every word in it has the same weight.
|
|
181
|
+
const toneWord: Record<Tone, string> = {
|
|
182
|
+
neutral: 'text-muted-foreground',
|
|
183
|
+
info: 'text-info-text',
|
|
184
|
+
success: 'text-success-text',
|
|
185
|
+
warning: 'text-warning-text',
|
|
186
|
+
critical: 'text-destructive-text',
|
|
187
|
+
progress: 'text-info-text',
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// The focus ring of a badge that opens a tooltip: the system's 2px ring, 2px
|
|
191
|
+
// out, on the badge's radius (4px for a word or a bare badge). At rest a chip
|
|
192
|
+
// keeps its transparent forced-colours outline.
|
|
193
|
+
const triggerFocus =
|
|
194
|
+
'focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring';
|
|
195
|
+
|
|
196
|
+
// One revolution per 1.6s: slower than the default spin, so a status that
|
|
197
|
+
// stays in progress for minutes does not read as urgent. Under reduced motion
|
|
198
|
+
// the spinner stands still; the icon shape still says "in motion".
|
|
199
|
+
const spinnerClass = 'animate-spin-slow motion-reduce:animate-none';
|
|
200
|
+
|
|
201
|
+
// The icon a chip shows when the caller passes none comes from `toneIcons`,
|
|
202
|
+
// the map the toast reads too. `progress` always spins its loader.
|
|
203
|
+
const ProgressIcon = toneIcons.progress;
|
|
204
|
+
|
|
41
205
|
interface StatusBadgeProps extends useRender.ComponentProps<'span'> {
|
|
42
206
|
/** Semantic tone. Defaults to `neutral`. */
|
|
43
207
|
tone?: Tone;
|
|
44
208
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
209
|
+
* How the status renders.
|
|
210
|
+
* - `chip` (default): a tinted chip with a tone outline.
|
|
211
|
+
* - `bare`: the chip's icon and tinted label without the box. Use it for a
|
|
212
|
+
* state inside a tinted or accent surface (a card that is already
|
|
213
|
+
* coloured), where a chip would be a box inside a box.
|
|
214
|
+
* - `icon`: the tone icon with no box, then the label in neutral text, with
|
|
215
|
+
* an optional second line (`detail`). Use it for a status every row of a
|
|
216
|
+
* table shows (a deployments table).
|
|
217
|
+
* - `text`: only the word, in the tone colour. Not for a table column:
|
|
218
|
+
* a column uses `icon`.
|
|
219
|
+
* - `dot`: a lone 7px tone dot. The label stays in the DOM as screen-reader
|
|
220
|
+
* text. Use it only where there is no room for a word.
|
|
221
|
+
* @default 'chip'
|
|
222
|
+
*/
|
|
223
|
+
appearance?: StatusBadgeAppearance;
|
|
224
|
+
/** Chip height: `default` (24px) or `lg` (28px, for page headers). For `bare`, the matching text and icon size. Only for `appearance="chip"` and `"bare"`. @default 'default' */
|
|
225
|
+
size?: StatusBadgeSize;
|
|
226
|
+
/**
|
|
227
|
+
* Show a leading dot in the tone colour in place of the tone icon, for a
|
|
228
|
+
* group of chips that is not a list column (a list column uses
|
|
229
|
+
* `appearance="icon"`). Only for `appearance="chip"` and `"bare"`. `progress` keeps its
|
|
230
|
+
* spinner, regardless of this prop.
|
|
49
231
|
*/
|
|
50
232
|
dot?: boolean;
|
|
233
|
+
/**
|
|
234
|
+
* The leading icon, in the tone colour. Only for `appearance="chip"`, `"bare"` and `"icon"`.
|
|
235
|
+
* - Leave it out for the tone's own icon: `CircleCheck` (success),
|
|
236
|
+
* `TriangleAlert` (warning), `CircleX` (critical), `CircleDot` (info),
|
|
237
|
+
* `CircleDashed` (neutral) and a spinning `Loader` (progress).
|
|
238
|
+
* - Pass a lucide icon element for a state whose shape says more, e.g.
|
|
239
|
+
* `<MoonStarIcon />` for Asleep. It replaces the dot and the spinner too.
|
|
240
|
+
* - Pass `false` or `null` for no icon, on an inline chip whose label
|
|
241
|
+
* already names the tone. `progress` still shows its spinner, and `dot`
|
|
242
|
+
* still shows the dot. `appearance="icon"` always shows an icon.
|
|
243
|
+
*
|
|
244
|
+
* Put the icon here, not in `children`: the chip sizes and colours it. An
|
|
245
|
+
* `<svg>` in `children` (code written for 2.x) hides the tone icon, so the
|
|
246
|
+
* chip still shows one icon, in the tone colour unless you give it one,
|
|
247
|
+
* at the size you gave it.
|
|
248
|
+
*/
|
|
249
|
+
icon?: React.ReactNode;
|
|
250
|
+
/**
|
|
251
|
+
* `'row'` shows the badge only while its table row is hovered or holds
|
|
252
|
+
* keyboard focus. Pass it for the normal state of a list column (for
|
|
253
|
+
* example "Enabled"), and leave it out for the other states ("Disabled"),
|
|
254
|
+
* so only the exceptions show at rest. The badge stays in the layout and in
|
|
255
|
+
* the accessibility tree. The row needs the `group/row` class (the
|
|
256
|
+
* `DataTable` rows have it). @default 'always'
|
|
257
|
+
*/
|
|
258
|
+
reveal?: StatusBadgeReveal;
|
|
259
|
+
/**
|
|
260
|
+
* A second line under the label, in small muted text, for
|
|
261
|
+
* `appearance="icon"` only. Put one time in it, the one that matters for
|
|
262
|
+
* the state: an `ElapsedTime` for a state that is happening or waiting, a
|
|
263
|
+
* `RelativeTime` for when it finished ("2 minutes ago"), and a
|
|
264
|
+
* `RelativeTime` with `display="duration"` for how long a state that
|
|
265
|
+
* continues has lasted ("for 13 days"). The icon centres against both lines
|
|
266
|
+
* together. A time here with a tooltip is a tab stop, and its tooltip opens
|
|
267
|
+
* beside the status block, so the label stays readable.
|
|
268
|
+
*/
|
|
269
|
+
detail?: React.ReactNode;
|
|
51
270
|
}
|
|
52
271
|
|
|
53
272
|
function StatusBadge({
|
|
54
273
|
className,
|
|
55
274
|
tone = 'neutral',
|
|
275
|
+
appearance = 'chip',
|
|
276
|
+
size = 'default',
|
|
56
277
|
dot = false,
|
|
278
|
+
icon,
|
|
279
|
+
reveal = 'always',
|
|
280
|
+
detail,
|
|
57
281
|
render,
|
|
58
282
|
children,
|
|
59
283
|
...props
|
|
60
284
|
}: StatusBadgeProps) {
|
|
61
|
-
|
|
285
|
+
// The component never transforms or translates the label — children win.
|
|
286
|
+
// A consumer passes their translated string; `defaultLabel` from the
|
|
287
|
+
// dictionary is a fallback the caller resolves, not something rendered here.
|
|
288
|
+
let classes: string;
|
|
289
|
+
let content: React.ReactNode;
|
|
290
|
+
const hasDetail =
|
|
291
|
+
appearance === 'icon' && detail !== undefined && detail !== null && detail !== false;
|
|
292
|
+
|
|
293
|
+
if (appearance === 'dot') {
|
|
294
|
+
content = <span className="sr-only">{children}</span>;
|
|
295
|
+
// A lone dot stays still, also in progress: it marks a state in a
|
|
296
|
+
// breadcrumb or a menu, where motion would pull the eye from the page.
|
|
297
|
+
classes = cn(
|
|
298
|
+
'inline-flex size-1.75 shrink-0 rounded-full bg-current',
|
|
299
|
+
toneDot[tone],
|
|
300
|
+
reveal === 'row' && rowRevealClass,
|
|
301
|
+
);
|
|
302
|
+
} else if (appearance === 'icon') {
|
|
303
|
+
classes = cn(iconAppearanceClass, reveal === 'row' && rowRevealClass);
|
|
304
|
+
content = (
|
|
305
|
+
<>
|
|
306
|
+
<StatusBadgeMarker
|
|
307
|
+
tone={tone}
|
|
308
|
+
size="icon"
|
|
309
|
+
dot={false}
|
|
310
|
+
// The icon always shows here: it carries the tone.
|
|
311
|
+
icon={isValidElement(icon) ? icon : undefined}
|
|
312
|
+
className={hasDetail ? 'row-span-2' : undefined}
|
|
313
|
+
/>
|
|
314
|
+
<span data-slot="status-badge-label" className={hasDetail ? 'leading-4.5' : undefined}>
|
|
315
|
+
{children}
|
|
316
|
+
</span>
|
|
317
|
+
{hasDetail ? (
|
|
318
|
+
<span
|
|
319
|
+
data-slot="status-badge-detail"
|
|
320
|
+
className="col-start-2 text-xs/3.75 text-muted-foreground tabular-nums"
|
|
321
|
+
>
|
|
322
|
+
{/* A RelativeTime or ElapsedTime here is a tab stop, and its
|
|
323
|
+
tooltip opens beside the whole status block. */}
|
|
324
|
+
<InStatusDetail.Provider value={true}>{detail}</InStatusDetail.Provider>
|
|
325
|
+
</span>
|
|
326
|
+
) : null}
|
|
327
|
+
</>
|
|
328
|
+
);
|
|
329
|
+
} else if (appearance === 'text') {
|
|
330
|
+
classes = cn(
|
|
331
|
+
'inline-flex w-fit shrink-0 items-center gap-1.5 whitespace-nowrap text-sm font-medium',
|
|
332
|
+
toneWord[tone],
|
|
333
|
+
reveal === 'row' && rowRevealClass,
|
|
334
|
+
);
|
|
335
|
+
// The spinner comes after the word, so every word in a column starts on
|
|
336
|
+
// the same edge.
|
|
337
|
+
content = (
|
|
338
|
+
<>
|
|
339
|
+
{children}
|
|
340
|
+
{tone === 'progress' ? (
|
|
341
|
+
<ProgressIcon aria-hidden className={cn('size-3 shrink-0 stroke-2', spinnerClass)} />
|
|
342
|
+
) : null}
|
|
343
|
+
</>
|
|
344
|
+
);
|
|
345
|
+
} else {
|
|
346
|
+
classes = cn(
|
|
347
|
+
appearance === 'bare'
|
|
348
|
+
? bareVariants({ tone, size, reveal })
|
|
349
|
+
: statusBadgeVariants({ tone, size, reveal }),
|
|
350
|
+
ownIconToneText[tone],
|
|
351
|
+
);
|
|
352
|
+
content = (
|
|
353
|
+
<>
|
|
354
|
+
<StatusBadgeMarker tone={tone} size={size} dot={dot} icon={icon} />
|
|
355
|
+
{children}
|
|
356
|
+
</>
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// As a TooltipTrigger render target, the badge explains its state on hover
|
|
361
|
+
// and on focus, so it is a tab stop with the focus ring. A chip also takes
|
|
362
|
+
// the hover fill in place of its tone wash. A lone dot stays out of the tab
|
|
363
|
+
// order: it is too small to be a target.
|
|
364
|
+
const isTrigger = 'data-tooltip-trigger' in props && appearance !== 'dot';
|
|
62
365
|
|
|
63
366
|
return useRender({
|
|
64
367
|
defaultTagName: 'span',
|
|
65
368
|
props: mergeProps<'span'>(
|
|
66
369
|
{
|
|
67
|
-
className: cn(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// A consumer passes their translated string; `defaultLabel` from the
|
|
73
|
-
// dictionary is a fallback the caller resolves, not something rendered here.
|
|
74
|
-
children: (
|
|
75
|
-
<>
|
|
76
|
-
{showDot ? (
|
|
77
|
-
<span
|
|
78
|
-
aria-hidden
|
|
79
|
-
className={cn(
|
|
80
|
-
'size-1.5 shrink-0 rounded-full',
|
|
81
|
-
tone === 'progress'
|
|
82
|
-
? 'bg-info-subtle-foreground motion-safe:animate-pulse'
|
|
83
|
-
: 'bg-current',
|
|
84
|
-
)}
|
|
85
|
-
/>
|
|
86
|
-
) : null}
|
|
87
|
-
{children}
|
|
88
|
-
</>
|
|
370
|
+
className: cn(
|
|
371
|
+
classes,
|
|
372
|
+
isTrigger && triggerFocus,
|
|
373
|
+
isTrigger && (appearance === 'chip' ? 'hover:bg-accent' : 'rounded-md'),
|
|
374
|
+
className,
|
|
89
375
|
),
|
|
376
|
+
tabIndex: isTrigger ? 0 : undefined,
|
|
90
377
|
},
|
|
378
|
+
props,
|
|
379
|
+
{ children: content },
|
|
91
380
|
),
|
|
92
381
|
render,
|
|
93
382
|
state: {
|
|
94
383
|
slot: 'status-badge',
|
|
95
384
|
tone,
|
|
385
|
+
appearance,
|
|
386
|
+
// `data-reveal="row"` only when the badge reveals.
|
|
387
|
+
reveal: reveal === 'row' ? 'row' : undefined,
|
|
388
|
+
// `data-detail` when a second line shows: a DataTable cell takes less
|
|
389
|
+
// vertical padding for it.
|
|
390
|
+
detail: hasDetail || undefined,
|
|
96
391
|
},
|
|
97
392
|
});
|
|
98
393
|
}
|
|
99
394
|
|
|
395
|
+
function StatusBadgeMarker({
|
|
396
|
+
tone,
|
|
397
|
+
size,
|
|
398
|
+
dot,
|
|
399
|
+
icon,
|
|
400
|
+
className,
|
|
401
|
+
}: {
|
|
402
|
+
tone: Tone;
|
|
403
|
+
size: StatusBadgeSize | 'icon';
|
|
404
|
+
dot: boolean;
|
|
405
|
+
icon: React.ReactNode;
|
|
406
|
+
className?: string;
|
|
407
|
+
}) {
|
|
408
|
+
// 13px icons in the 12px chip, 14px in the 13px chip, 16px beside the
|
|
409
|
+
// 13px label of the `icon` appearance, all at stroke 2. The
|
|
410
|
+
// marker eases to a new tone with the outline, when a status changes in place.
|
|
411
|
+
// Size and stroke are `!important`: a Button sizes every icon inside it to
|
|
412
|
+
// 16px at stroke 1.75, with a selector that outranks this one, and a badge
|
|
413
|
+
// inside a Button keeps its own icon.
|
|
414
|
+
const iconClasses = cn(
|
|
415
|
+
'inline-flex shrink-0 transition-colors duration-slow [&_svg]:shrink-0 [&_svg]:stroke-2!',
|
|
416
|
+
size === 'icon'
|
|
417
|
+
? '[&_svg]:size-4!'
|
|
418
|
+
: size === 'lg'
|
|
419
|
+
? '[&_svg]:size-3.5!'
|
|
420
|
+
: '[&_svg]:size-3.25!',
|
|
421
|
+
toneText[tone],
|
|
422
|
+
className,
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
if (isValidElement(icon)) {
|
|
426
|
+
return (
|
|
427
|
+
<span aria-hidden data-marker="icon" data-slot="status-badge-marker" className={iconClasses}>
|
|
428
|
+
{icon}
|
|
429
|
+
</span>
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// `progress` keeps its spinner over a dot and without an icon: the motion is
|
|
434
|
+
// its meaning, and without it the chip looks like `info`.
|
|
435
|
+
if (tone === 'progress') {
|
|
436
|
+
return (
|
|
437
|
+
<span aria-hidden data-marker="icon" data-slot="status-badge-marker" className={iconClasses}>
|
|
438
|
+
<ProgressIcon className={spinnerClass} />
|
|
439
|
+
</span>
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (!dot) {
|
|
444
|
+
// `false`, `null` and any other value that is not an element (`''`, `0`)
|
|
445
|
+
// mean no icon. Only a left-out `icon` gets the tone's own.
|
|
446
|
+
if (icon !== undefined) return null;
|
|
447
|
+
const ToneIcon = toneIcons[tone];
|
|
448
|
+
return (
|
|
449
|
+
<span aria-hidden data-marker="icon" data-slot="status-badge-marker" className={iconClasses}>
|
|
450
|
+
<ToneIcon />
|
|
451
|
+
</span>
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return (
|
|
456
|
+
<span
|
|
457
|
+
aria-hidden
|
|
458
|
+
data-marker="dot"
|
|
459
|
+
data-slot="status-badge-marker"
|
|
460
|
+
className={cn('size-1.5 shrink-0 rounded-full bg-current', toneDot[tone])}
|
|
461
|
+
/>
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
100
465
|
export { StatusBadge, statusBadgeVariants };
|
|
466
|
+
export type { StatusBadgeAppearance, StatusBadgeProps, StatusBadgeReveal, StatusBadgeSize };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
type ReactElement,
|
|
6
|
+
type ReactNode,
|
|
7
|
+
useContext,
|
|
8
|
+
useRef,
|
|
9
|
+
useSyncExternalStore,
|
|
10
|
+
} from 'react';
|
|
11
|
+
import { formatElapsed } from '../../lib/format-elapsed.ts';
|
|
12
|
+
import { cn } from '../../lib/utils.ts';
|
|
13
|
+
// Relative: this module is internal (mapped to null in the package exports).
|
|
14
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '../atoms/tooltip.tsx';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* True inside the second line of a StatusBadge (`detail`). A time there is a
|
|
18
|
+
* tab stop by default, and its tooltip opens beside the whole status block.
|
|
19
|
+
*/
|
|
20
|
+
const InStatusDetail = createContext(false);
|
|
21
|
+
|
|
22
|
+
const focusRing =
|
|
23
|
+
'rounded-sm outline-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The tooltip of a RelativeTime or an ElapsedTime. The trigger is the `<time>`
|
|
27
|
+
* itself, so the focus ring stays on the time.
|
|
28
|
+
*
|
|
29
|
+
* Under a status label (a StatusBadge `detail`), the tooltip is anchored to the
|
|
30
|
+
* whole status block and opens at its inline end, centred on it. A row is 48px
|
|
31
|
+
* high, as a two-line tooltip, so it covers only the cells after the
|
|
32
|
+
* status in the same row: the label and the time stay readable, and so do the
|
|
33
|
+
* rows above and below. Without room at the end it flips to the start.
|
|
34
|
+
* Anywhere else it opens above the time, as every tooltip does.
|
|
35
|
+
*/
|
|
36
|
+
function TimeTooltip({
|
|
37
|
+
content,
|
|
38
|
+
focusable,
|
|
39
|
+
render,
|
|
40
|
+
children,
|
|
41
|
+
}: {
|
|
42
|
+
content: ReactNode;
|
|
43
|
+
/** A tab stop, so keyboard users can open it. Default: only in a status detail. */
|
|
44
|
+
focusable: boolean | undefined;
|
|
45
|
+
render: ReactElement<{ className?: string }>;
|
|
46
|
+
children: ReactNode;
|
|
47
|
+
}) {
|
|
48
|
+
const inDetail = useContext(InStatusDetail);
|
|
49
|
+
// The trigger renders a `<time>`; the ref type is the Trigger's default button.
|
|
50
|
+
const trigger = useRef<HTMLButtonElement>(null);
|
|
51
|
+
const tabStop = focusable ?? inDetail;
|
|
52
|
+
// Above or below, a tooltip covers the status of the next row; at the end it
|
|
53
|
+
// covers only the cells after the status in its own row.
|
|
54
|
+
const statusBlock = () =>
|
|
55
|
+
trigger.current?.closest<HTMLElement>('[data-slot=status-badge]') ?? trigger.current;
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<Tooltip>
|
|
59
|
+
<TooltipTrigger
|
|
60
|
+
ref={trigger}
|
|
61
|
+
// Opt-in tab stop so keyboard users can open the tooltip (WCAG 2.1.1).
|
|
62
|
+
tabIndex={tabStop ? 0 : undefined}
|
|
63
|
+
render={render}
|
|
64
|
+
className={cn(tabStop && focusRing, render.props.className)}
|
|
65
|
+
>
|
|
66
|
+
{children}
|
|
67
|
+
</TooltipTrigger>
|
|
68
|
+
<TooltipContent
|
|
69
|
+
className="tabular-nums"
|
|
70
|
+
side={inDetail ? 'inline-end' : 'top'}
|
|
71
|
+
anchor={inDetail ? statusBlock : undefined}
|
|
72
|
+
>
|
|
73
|
+
{content}
|
|
74
|
+
</TooltipContent>
|
|
75
|
+
</Tooltip>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function toTime(value: string | number | Date | null | undefined): number | null {
|
|
80
|
+
if (value === null || value === undefined) return null;
|
|
81
|
+
const time = (value instanceof Date ? value : new Date(value)).getTime();
|
|
82
|
+
return Number.isNaN(time) ? null : time;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The fields of `toLocaleString()`, so a range reads like a single time.
|
|
86
|
+
const dateTimeFields: Intl.DateTimeFormatOptions = {
|
|
87
|
+
year: 'numeric',
|
|
88
|
+
month: 'numeric',
|
|
89
|
+
day: 'numeric',
|
|
90
|
+
hour: 'numeric',
|
|
91
|
+
minute: 'numeric',
|
|
92
|
+
second: 'numeric',
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Two lines for something that ran from `start` to `end`: the range in the
|
|
97
|
+
* reader's locale (`11/11/2026, 10:14:47 – 10:15:29 AM`, the date once when
|
|
98
|
+
* both are on one day), then how long it took (`42s`), both in the reader's locale.
|
|
99
|
+
*/
|
|
100
|
+
function TimeSpan({
|
|
101
|
+
start,
|
|
102
|
+
end,
|
|
103
|
+
locale,
|
|
104
|
+
timeZone,
|
|
105
|
+
}: {
|
|
106
|
+
start: number;
|
|
107
|
+
end: number;
|
|
108
|
+
locale: string | undefined;
|
|
109
|
+
timeZone: string | undefined;
|
|
110
|
+
}) {
|
|
111
|
+
const format = new Intl.DateTimeFormat(locale, { ...dateTimeFields, timeZone });
|
|
112
|
+
return (
|
|
113
|
+
<>
|
|
114
|
+
<span className="block">{format.formatRange(Math.min(start, end), end)}</span>
|
|
115
|
+
<span className="block">{formatElapsed(end - start, { locale })}</span>
|
|
116
|
+
</>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function noSubscription() {
|
|
121
|
+
return () => {};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* False on the server and during hydration, true otherwise. A time depends on
|
|
126
|
+
* the reader's clock, locale and time zone, which the server does not know:
|
|
127
|
+
* while this is false, a time renders its fallback, so the server markup and
|
|
128
|
+
* the first client render are the same. React reads the client value from the
|
|
129
|
+
* first render of a browser-only app, so a time shows its text from the first
|
|
130
|
+
* paint there. After hydration React renders again with `true`.
|
|
131
|
+
*/
|
|
132
|
+
function useReadersClock(): boolean {
|
|
133
|
+
return useSyncExternalStore(
|
|
134
|
+
noSubscription,
|
|
135
|
+
() => true,
|
|
136
|
+
() => false,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { InStatusDetail, TimeSpan, TimeTooltip, toTime, useReadersClock };
|