@dashforge/tw 0.11.0-beta → 1.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/CHANGELOG.md +234 -12
- package/COVERAGE.md +2 -2
- package/PARITY.md +7 -9
- package/PERFORMANCE.md +3 -4
- package/README.md +74 -15
- package/THEME-AUDIT.md +3 -4
- package/dashforge-tw-1.0.0.tgz +0 -0
- package/dist/index.esm.js +3127 -748
- package/dist/src/components/Alert/Alert.d.ts +65 -0
- package/dist/src/components/Alert/Alert.d.ts.map +1 -0
- package/dist/src/components/Alert/alert.types.d.ts +130 -0
- package/dist/src/components/Alert/alert.types.d.ts.map +1 -0
- package/dist/src/components/Alert/alert.variants.d.ts +85 -0
- package/dist/src/components/Alert/alert.variants.d.ts.map +1 -0
- package/dist/src/components/Avatar/Avatar.d.ts +48 -0
- package/dist/src/components/Avatar/Avatar.d.ts.map +1 -0
- package/dist/src/components/Avatar/avatar.types.d.ts +162 -0
- package/dist/src/components/Avatar/avatar.types.d.ts.map +1 -0
- package/dist/src/components/Avatar/avatar.variants.d.ts +208 -0
- package/dist/src/components/Avatar/avatar.variants.d.ts.map +1 -0
- package/dist/src/components/Badge/Badge.d.ts +63 -0
- package/dist/src/components/Badge/Badge.d.ts.map +1 -0
- package/dist/src/components/Badge/badge.types.d.ts +124 -0
- package/dist/src/components/Badge/badge.types.d.ts.map +1 -0
- package/dist/src/components/Badge/badge.variants.d.ts +262 -0
- package/dist/src/components/Badge/badge.variants.d.ts.map +1 -0
- package/dist/src/components/Box/Box.d.ts.map +1 -1
- package/dist/src/components/Box/box.types.d.ts +53 -0
- package/dist/src/components/Box/box.types.d.ts.map +1 -1
- package/dist/src/components/Button/Button.d.ts.map +1 -1
- package/dist/src/components/Button/button.types.d.ts +35 -0
- package/dist/src/components/Button/button.types.d.ts.map +1 -1
- package/dist/src/components/Card/Card.d.ts +107 -0
- package/dist/src/components/Card/Card.d.ts.map +1 -0
- package/dist/src/components/Card/card.types.d.ts +119 -0
- package/dist/src/components/Card/card.types.d.ts.map +1 -0
- package/dist/src/components/Chip/Chip.d.ts +67 -0
- package/dist/src/components/Chip/Chip.d.ts.map +1 -0
- package/dist/src/components/Chip/chip.types.d.ts +113 -0
- package/dist/src/components/Chip/chip.types.d.ts.map +1 -0
- package/dist/src/components/Chip/chip.variants.d.ts +155 -0
- package/dist/src/components/Chip/chip.variants.d.ts.map +1 -0
- package/dist/src/components/IconButton/IconButton.d.ts +57 -0
- package/dist/src/components/IconButton/IconButton.d.ts.map +1 -0
- package/dist/src/components/IconButton/iconButton.types.d.ts +96 -0
- package/dist/src/components/IconButton/iconButton.types.d.ts.map +1 -0
- package/dist/src/components/IconButton/iconButton.variants.d.ts +26 -0
- package/dist/src/components/IconButton/iconButton.variants.d.ts.map +1 -0
- package/dist/src/components/Menu/Menu.d.ts +118 -0
- package/dist/src/components/Menu/Menu.d.ts.map +1 -0
- package/dist/src/components/Menu/menu.types.d.ts +179 -0
- package/dist/src/components/Menu/menu.types.d.ts.map +1 -0
- package/dist/src/components/Menu/menu.variants.d.ts +129 -0
- package/dist/src/components/Menu/menu.variants.d.ts.map +1 -0
- package/dist/src/components/Snackbar/Snackbar.d.ts.map +1 -1
- package/dist/src/components/Snackbar/snackbar.types.d.ts +40 -3
- package/dist/src/components/Snackbar/snackbar.types.d.ts.map +1 -1
- package/dist/src/components/Snackbar/snackbar.variants.d.ts +12 -57
- package/dist/src/components/Snackbar/snackbar.variants.d.ts.map +1 -1
- package/dist/src/components/Spinner/Spinner.d.ts +44 -0
- package/dist/src/components/Spinner/Spinner.d.ts.map +1 -0
- package/dist/src/components/Spinner/spinner.types.d.ts +89 -0
- package/dist/src/components/Spinner/spinner.types.d.ts.map +1 -0
- package/dist/src/components/Spinner/spinner.variants.d.ts +111 -0
- package/dist/src/components/Spinner/spinner.variants.d.ts.map +1 -0
- package/dist/src/components/Table/cells/RenderChip.d.ts +15 -72
- package/dist/src/components/Table/cells/RenderChip.d.ts.map +1 -1
- package/dist/src/components/_shared/severity/index.d.ts +21 -0
- package/dist/src/components/_shared/severity/index.d.ts.map +1 -0
- package/dist/src/components/_shared/severity/severity.types.d.ts +48 -0
- package/dist/src/components/_shared/severity/severity.types.d.ts.map +1 -0
- package/dist/src/components/_shared/severity/severityIcons.d.ts +45 -0
- package/dist/src/components/_shared/severity/severityIcons.d.ts.map +1 -0
- package/dist/src/components/_shared/severity/severityVariants.d.ts +22 -0
- package/dist/src/components/_shared/severity/severityVariants.d.ts.map +1 -0
- package/dist/src/index.d.ts +27 -2
- package/dist/src/index.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/components/Alert/Alert.test.tsx +302 -0
- package/src/components/Alert/Alert.tsx +186 -0
- package/src/components/Alert/alert.types.ts +144 -0
- package/src/components/Alert/alert.variants.ts +71 -0
- package/src/components/Autocomplete/autocomplete.variants.ts +1 -1
- package/src/components/Avatar/Avatar.test.tsx +287 -0
- package/src/components/Avatar/Avatar.tsx +304 -0
- package/src/components/Avatar/avatar.types.ts +205 -0
- package/src/components/Avatar/avatar.variants.ts +194 -0
- package/src/components/Badge/Badge.test.tsx +385 -0
- package/src/components/Badge/Badge.tsx +174 -0
- package/src/components/Badge/badge.types.ts +154 -0
- package/src/components/Badge/badge.variants.ts +161 -0
- package/src/components/Box/Box.test.tsx +21 -0
- package/src/components/Box/Box.tsx +32 -3
- package/src/components/Box/box.types.ts +55 -0
- package/src/components/Button/Button.test.tsx +21 -0
- package/src/components/Button/Button.tsx +28 -27
- package/src/components/Button/button.types.ts +36 -0
- package/src/components/Card/Card.test.tsx +291 -0
- package/src/components/Card/Card.tsx +241 -0
- package/src/components/Card/card.types.ts +132 -0
- package/src/components/Chip/Chip.test.tsx +316 -0
- package/src/components/Chip/Chip.tsx +224 -0
- package/src/components/Chip/chip.types.ts +128 -0
- package/src/components/Chip/chip.variants.ts +173 -0
- package/src/components/DataGrid/visibility/ColumnVisibilityMenu.tsx +1 -1
- package/src/components/IconButton/IconButton.test.tsx +367 -0
- package/src/components/IconButton/IconButton.tsx +159 -0
- package/src/components/IconButton/iconButton.types.ts +106 -0
- package/src/components/IconButton/iconButton.variants.ts +31 -0
- package/src/components/LeftNav/leftNav.variants.ts +2 -2
- package/src/components/Menu/Menu.test.tsx +408 -0
- package/src/components/Menu/Menu.tsx +335 -0
- package/src/components/Menu/menu.types.ts +221 -0
- package/src/components/Menu/menu.variants.ts +132 -0
- package/src/components/Pagination/pagination.variants.ts +1 -1
- package/src/components/Snackbar/Snackbar.tsx +46 -28
- package/src/components/Snackbar/snackbar.types.ts +47 -3
- package/src/components/Snackbar/snackbar.variants.ts +29 -22
- package/src/components/Spinner/Spinner.test.tsx +199 -0
- package/src/components/Spinner/Spinner.tsx +158 -0
- package/src/components/Spinner/spinner.types.ts +113 -0
- package/src/components/Spinner/spinner.variants.ts +83 -0
- package/src/components/Table/cells/RenderChip.tsx +24 -87
- package/src/components/Table/cells/RowActionsMenu.tsx +1 -1
- package/src/components/TextField/TextField.test.tsx +5 -1
- package/src/components/_shared/severity/index.ts +33 -0
- package/src/components/_shared/severity/severity.types.ts +50 -0
- package/src/components/_shared/severity/severityIcons.tsx +104 -0
- package/src/components/_shared/severity/severityVariants.test.ts +136 -0
- package/src/components/_shared/severity/severityVariants.ts +115 -0
- package/src/index.ts +163 -1
- package/vite.config.ts +8 -0
- package/vitest.config.mts +8 -1
- package/LICENSE +0 -21
|
@@ -9,12 +9,15 @@ import {
|
|
|
9
9
|
} from 'react';
|
|
10
10
|
import { cn } from '../../utils/cn.js';
|
|
11
11
|
import { snackbarVariants } from './snackbar.variants.js';
|
|
12
|
+
import {
|
|
13
|
+
getSeverityClasses,
|
|
14
|
+
} from '../_shared/severity/severityVariants.js';
|
|
15
|
+
import { getDefaultSeverityIcon } from '../_shared/severity/severityIcons.js';
|
|
12
16
|
import type {
|
|
13
17
|
SnackbarApi,
|
|
14
18
|
SnackbarOptions,
|
|
15
19
|
SnackbarProviderProps,
|
|
16
20
|
SnackbarRecord,
|
|
17
|
-
SnackbarSeverity,
|
|
18
21
|
} from './snackbar.types.js';
|
|
19
22
|
|
|
20
23
|
const SnackbarContext = createContext<SnackbarApi | null>(null);
|
|
@@ -35,19 +38,6 @@ export function useSnackbar(): SnackbarApi {
|
|
|
35
38
|
return ctx;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
/**
|
|
39
|
-
* Unicode glyphs for severities — keeps the bundle small (no icon dep)
|
|
40
|
-
* and stays accessible because consumers pass their own message text.
|
|
41
|
-
* The glyph is `aria-hidden`; the screen reader announces the message
|
|
42
|
-
* via the live region.
|
|
43
|
-
*/
|
|
44
|
-
const SEVERITY_GLYPH: Record<SnackbarSeverity, string> = {
|
|
45
|
-
info: 'ⓘ',
|
|
46
|
-
success: '✓',
|
|
47
|
-
warning: '⚠',
|
|
48
|
-
danger: '✕',
|
|
49
|
-
};
|
|
50
|
-
|
|
51
41
|
/**
|
|
52
42
|
* Dashforge TW Snackbar provider — transient, stacked toast notifications.
|
|
53
43
|
*
|
|
@@ -228,12 +218,24 @@ export function SnackbarProvider(props: SnackbarProviderProps) {
|
|
|
228
218
|
>
|
|
229
219
|
{visible.map((rec) => {
|
|
230
220
|
const sev = rec.severity ?? defaults?.severity ?? 'info';
|
|
221
|
+
const variant = rec.variant ?? defaults?.variant ?? 'standard';
|
|
231
222
|
const showClose =
|
|
232
223
|
rec.showClose ?? defaults?.showClose ?? true;
|
|
233
|
-
const itemClasses = snackbarVariants({
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
224
|
+
const itemClasses = snackbarVariants({ position });
|
|
225
|
+
// Severity color classes — sourced from the shared 3×4 matrix
|
|
226
|
+
// (`_shared/severity/`), same as Alert. Keeps the two
|
|
227
|
+
// components visually in lockstep.
|
|
228
|
+
const severityClasses = getSeverityClasses(variant, sev);
|
|
229
|
+
// Icon resolution — same tristate as Alert:
|
|
230
|
+
// undefined → default per-severity SVG (shared with Alert)
|
|
231
|
+
// ReactNode → consumer's icon
|
|
232
|
+
// false → no icon
|
|
233
|
+
const renderedIcon =
|
|
234
|
+
rec.icon === false
|
|
235
|
+
? null
|
|
236
|
+
: rec.icon !== undefined
|
|
237
|
+
? rec.icon
|
|
238
|
+
: getDefaultSeverityIcon(sev);
|
|
237
239
|
return (
|
|
238
240
|
<div
|
|
239
241
|
key={rec.id}
|
|
@@ -241,18 +243,22 @@ export function SnackbarProvider(props: SnackbarProviderProps) {
|
|
|
241
243
|
data-tick={rec.tick}
|
|
242
244
|
className={cn(
|
|
243
245
|
itemClasses.item(),
|
|
246
|
+
severityClasses.surface,
|
|
247
|
+
severityClasses.border,
|
|
244
248
|
slotProps?.item?.className
|
|
245
249
|
)}
|
|
246
250
|
>
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
251
|
+
{renderedIcon !== null && (
|
|
252
|
+
<span
|
|
253
|
+
className={cn(
|
|
254
|
+
itemClasses.icon(),
|
|
255
|
+
severityClasses.icon,
|
|
256
|
+
slotProps?.icon?.className
|
|
257
|
+
)}
|
|
258
|
+
>
|
|
259
|
+
{renderedIcon}
|
|
260
|
+
</span>
|
|
261
|
+
)}
|
|
256
262
|
<div
|
|
257
263
|
className={cn(
|
|
258
264
|
itemClasses.message(),
|
|
@@ -286,7 +292,19 @@ export function SnackbarProvider(props: SnackbarProviderProps) {
|
|
|
286
292
|
slotProps?.closeButton?.className
|
|
287
293
|
)}
|
|
288
294
|
>
|
|
289
|
-
|
|
295
|
+
<svg
|
|
296
|
+
width="1em"
|
|
297
|
+
height="1em"
|
|
298
|
+
viewBox="0 0 20 20"
|
|
299
|
+
fill="none"
|
|
300
|
+
stroke="currentColor"
|
|
301
|
+
strokeWidth={1.5}
|
|
302
|
+
strokeLinecap="round"
|
|
303
|
+
strokeLinejoin="round"
|
|
304
|
+
aria-hidden="true"
|
|
305
|
+
>
|
|
306
|
+
<path d="m6 6 8 8M14 6l-8 8" />
|
|
307
|
+
</svg>
|
|
290
308
|
</button>
|
|
291
309
|
)}
|
|
292
310
|
</div>
|
|
@@ -1,8 +1,27 @@
|
|
|
1
1
|
import type { ReactNode } from 'react';
|
|
2
2
|
import type { SnackbarVariants } from './snackbar.variants.js';
|
|
3
|
+
import type {
|
|
4
|
+
Severity,
|
|
5
|
+
SeverityVariant,
|
|
6
|
+
} from '../_shared/severity/severity.types.js';
|
|
3
7
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Visual severity — drives icon color + accent on the snackbar surface.
|
|
10
|
+
*
|
|
11
|
+
* Re-exported alias of the shared `Severity` type from
|
|
12
|
+
* `_shared/severity/`. Kept as a distinct local alias for
|
|
13
|
+
* backwards-compatible name (`SnackbarSeverity`) — pre-1.1.0 consumers
|
|
14
|
+
* that imported `SnackbarSeverity` keep their imports working.
|
|
15
|
+
*/
|
|
16
|
+
export type SnackbarSeverity = Severity;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Visual variant — `standard` (default, soft tinted surface), `filled`
|
|
20
|
+
* (solid colored surface), `outlined` (transparent + border + text).
|
|
21
|
+
* Re-exported alias of the shared `SeverityVariant`. New in 1.1.0:
|
|
22
|
+
* Snackbar now supports the same 3-way axis as Alert / future Banner.
|
|
23
|
+
*/
|
|
24
|
+
export type SnackbarVariant = SeverityVariant;
|
|
6
25
|
|
|
7
26
|
/** Corner anchor for the snackbar stack. */
|
|
8
27
|
export type SnackbarPosition =
|
|
@@ -19,6 +38,28 @@ export interface SnackbarOptions {
|
|
|
19
38
|
message: ReactNode;
|
|
20
39
|
/** Visual severity. @default 'info' */
|
|
21
40
|
severity?: SnackbarSeverity;
|
|
41
|
+
/**
|
|
42
|
+
* Visual variant — same 3-way axis as Alert. **New in 1.1.0.**
|
|
43
|
+
*
|
|
44
|
+
* - `'standard'` (default) — tinted soft surface, severity-toned text
|
|
45
|
+
* - `'filled'` — solid colored surface, light text (strong weight)
|
|
46
|
+
* - `'outlined'` — transparent surface, severity border + text
|
|
47
|
+
*
|
|
48
|
+
* @default 'standard'
|
|
49
|
+
*/
|
|
50
|
+
variant?: SnackbarVariant;
|
|
51
|
+
/**
|
|
52
|
+
* Icon control — same tristate as Alert. **New in 1.1.0.**
|
|
53
|
+
*
|
|
54
|
+
* - omitted / `undefined` → default per-severity icon (inline SVG
|
|
55
|
+
* shared with Alert via `_shared/severity/`)
|
|
56
|
+
* - `ReactNode` → consumer-provided icon
|
|
57
|
+
* - `false` → no icon (colored surface alone carries the severity)
|
|
58
|
+
*
|
|
59
|
+
* The legacy Unicode glyphs (`ⓘ ✓ ⚠ ✕`) shipped in 1.0.x have been
|
|
60
|
+
* REMOVED — see CHANGELOG for the visual breaking note.
|
|
61
|
+
*/
|
|
62
|
+
icon?: ReactNode | false;
|
|
22
63
|
/**
|
|
23
64
|
* Auto-dismiss after this many ms. `0` / negative ⇒ persistent
|
|
24
65
|
* (only dismissed by the close button or `dismiss(id)`).
|
|
@@ -77,7 +118,10 @@ export interface SnackbarProviderProps extends SnackbarVariants {
|
|
|
77
118
|
*/
|
|
78
119
|
maxVisible?: number;
|
|
79
120
|
/** Defaults merged into every enqueue. */
|
|
80
|
-
defaults?: Pick<
|
|
121
|
+
defaults?: Pick<
|
|
122
|
+
SnackbarOptions,
|
|
123
|
+
'severity' | 'variant' | 'autoHideMs' | 'showClose'
|
|
124
|
+
>;
|
|
81
125
|
/** Per-slot className overrides. */
|
|
82
126
|
slotProps?: SnackbarSlotProps;
|
|
83
127
|
}
|
|
@@ -3,13 +3,22 @@ import { tv, type VariantProps } from 'tailwind-variants';
|
|
|
3
3
|
/**
|
|
4
4
|
* Tailwind-variants recipe for `<Snackbar>` (stacked toast surface).
|
|
5
5
|
*
|
|
6
|
+
* **Sprint 4.4 refactor (1.1.0):** the severity color block has been
|
|
7
|
+
* REMOVED from this recipe. The 3×4 (variant × severity) color matrix
|
|
8
|
+
* now lives in `_shared/severity/severityVariants` and is consumed by
|
|
9
|
+
* Snackbar AND Alert (and any future Banner). This recipe owns layout
|
|
10
|
+
* + spacing + transitions only — the surface / border / icon colors
|
|
11
|
+
* are merged in at render time via `cn()`.
|
|
12
|
+
*
|
|
6
13
|
* Slots:
|
|
7
14
|
* - `container` — fixed-position outer stack
|
|
8
|
-
* - `item` — single snackbar surface
|
|
9
|
-
*
|
|
15
|
+
* - `item` — single snackbar surface (layout only; severity
|
|
16
|
+
* colors injected at render via `getSeverityClasses`)
|
|
17
|
+
* - `icon` — leading severity icon (layout only; tone class
|
|
18
|
+
* injected at render)
|
|
10
19
|
* - `message` — main text
|
|
11
20
|
* - `action` — optional action button
|
|
12
|
-
* - `closeButton` — trailing `×` button
|
|
21
|
+
* - `closeButton` — trailing close `×` button
|
|
13
22
|
*/
|
|
14
23
|
export const snackbarVariants = tv({
|
|
15
24
|
slots: {
|
|
@@ -21,28 +30,33 @@ export const snackbarVariants = tv({
|
|
|
21
30
|
'pointer-events-auto',
|
|
22
31
|
'flex items-start gap-3 px-4 py-3 w-full',
|
|
23
32
|
'rounded-lg border shadow-lg',
|
|
24
|
-
'text-sm
|
|
25
|
-
// Subtle enter transition — opacity
|
|
26
|
-
//
|
|
33
|
+
'text-sm',
|
|
34
|
+
// Subtle enter transition — opacity-only, kept short so a burst
|
|
35
|
+
// of snackbars feels snappy. Gated on motion-reduce
|
|
27
36
|
// (WCAG 2.3.3) — users who request reduced motion see snackbars
|
|
28
|
-
// pop in instantly without the
|
|
29
|
-
'transition-
|
|
37
|
+
// pop in instantly without the fade.
|
|
38
|
+
'transition-opacity duration-200 motion-reduce:transition-none motion-reduce:duration-0',
|
|
30
39
|
'data-[state=entered]:opacity-100 data-[state=exited]:opacity-0',
|
|
31
40
|
],
|
|
32
|
-
icon: 'shrink-0 mt-0.5
|
|
41
|
+
icon: 'shrink-0 mt-0.5 inline-flex items-center justify-center',
|
|
33
42
|
message: 'flex-1 min-w-0 leading-relaxed',
|
|
34
43
|
action: [
|
|
35
44
|
'shrink-0 inline-flex items-center justify-center px-2 h-7',
|
|
36
45
|
'rounded-md text-xs font-medium',
|
|
37
|
-
|
|
38
|
-
'
|
|
39
|
-
|
|
46
|
+
// Action button uses currentColor + a soft hover tint so it
|
|
47
|
+
// adapts to whatever (variant, severity) surface it's on. The
|
|
48
|
+
// hover tint relies on a 10% opacity overlay of the current
|
|
49
|
+
// foreground — works in both light and dark severity surfaces.
|
|
50
|
+
'opacity-90 hover:opacity-100 hover:bg-current/10',
|
|
51
|
+
'outline-none focus-visible:ring-2 focus-visible:ring-current',
|
|
52
|
+
'transition-opacity',
|
|
40
53
|
],
|
|
41
54
|
closeButton: [
|
|
42
55
|
'shrink-0 inline-flex items-center justify-center w-6 h-6',
|
|
43
|
-
'rounded-md
|
|
44
|
-
'
|
|
45
|
-
'
|
|
56
|
+
'rounded-md',
|
|
57
|
+
'opacity-70 hover:opacity-100',
|
|
58
|
+
'outline-none focus-visible:ring-2 focus-visible:ring-current',
|
|
59
|
+
'transition-opacity',
|
|
46
60
|
],
|
|
47
61
|
},
|
|
48
62
|
variants: {
|
|
@@ -58,16 +72,9 @@ export const snackbarVariants = tv({
|
|
|
58
72
|
},
|
|
59
73
|
'bottom-right': { container: 'bottom-0 right-0 items-end' },
|
|
60
74
|
},
|
|
61
|
-
severity: {
|
|
62
|
-
info: { item: 'border-primary-200', icon: 'text-primary-600' },
|
|
63
|
-
success: { item: 'border-success-200', icon: 'text-success-600' },
|
|
64
|
-
warning: { item: 'border-warning-200', icon: 'text-warning-600' },
|
|
65
|
-
danger: { item: 'border-danger-200', icon: 'text-danger-600' },
|
|
66
|
-
},
|
|
67
75
|
},
|
|
68
76
|
defaultVariants: {
|
|
69
77
|
position: 'bottom-right',
|
|
70
|
-
severity: 'info',
|
|
71
78
|
},
|
|
72
79
|
});
|
|
73
80
|
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
|
|
4
|
+
import { render, screen, cleanup, act } from '@testing-library/react';
|
|
5
|
+
import { Spinner } from './Spinner.js';
|
|
6
|
+
|
|
7
|
+
void React;
|
|
8
|
+
afterEach(() => cleanup());
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Unit tests for `<Spinner>`. Covers:
|
|
12
|
+
* - Default rendering (role, label, animate-spin)
|
|
13
|
+
* - Size axis (5 enum values → w/h spacing classes)
|
|
14
|
+
* - Color → text-{color}-600 class
|
|
15
|
+
* - color omitted → no text class (inherits currentColor)
|
|
16
|
+
* - Thickness → SVG stroke-width
|
|
17
|
+
* - withTrack → second <circle> with low opacity
|
|
18
|
+
* - Delay → null for first N ms, then visible
|
|
19
|
+
* - visibleWhen → null on false (regardless of delay)
|
|
20
|
+
* - className / sx overrides
|
|
21
|
+
* - motion-reduce gating (class present)
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
describe('<Spinner>', () => {
|
|
25
|
+
describe('basic rendering', () => {
|
|
26
|
+
it('renders with role="status"', () => {
|
|
27
|
+
render(<Spinner />);
|
|
28
|
+
expect(screen.getByRole('status')).toBeTruthy();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('exposes the default "Loading" label to screen readers', () => {
|
|
32
|
+
render(<Spinner />);
|
|
33
|
+
expect(screen.getByText('Loading')).toBeTruthy();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('respects custom label', () => {
|
|
37
|
+
render(<Spinner label="Saving" />);
|
|
38
|
+
expect(screen.getByText('Saving')).toBeTruthy();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('applies animate-spin class', () => {
|
|
42
|
+
const { container } = render(<Spinner />);
|
|
43
|
+
const root = container.firstElementChild!;
|
|
44
|
+
expect(root.className).toContain('animate-spin');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('applies motion-reduce:animate-none class (WCAG 2.3.3)', () => {
|
|
48
|
+
const { container } = render(<Spinner />);
|
|
49
|
+
const root = container.firstElementChild!;
|
|
50
|
+
expect(root.className).toContain('motion-reduce:animate-none');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('renders the spinning arc SVG path', () => {
|
|
54
|
+
const { container } = render(<Spinner />);
|
|
55
|
+
const path = container.querySelector('path');
|
|
56
|
+
expect(path).toBeTruthy();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('size axis', () => {
|
|
61
|
+
it.each([
|
|
62
|
+
['xs', 'w-3'],
|
|
63
|
+
['sm', 'w-4'],
|
|
64
|
+
['md', 'w-5'],
|
|
65
|
+
['lg', 'w-6'],
|
|
66
|
+
['xl', 'w-8'],
|
|
67
|
+
] as const)('size=%s → %s', (size, expected) => {
|
|
68
|
+
const { container } = render(<Spinner size={size} />);
|
|
69
|
+
const root = container.firstElementChild!;
|
|
70
|
+
expect(root.className).toContain(expected);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('color resolution', () => {
|
|
75
|
+
it('omits text-* class when color is undefined (inherits currentColor)', () => {
|
|
76
|
+
const { container } = render(<Spinner />);
|
|
77
|
+
const root = container.firstElementChild!;
|
|
78
|
+
// No text-{color}-600 class — color flows in via currentColor
|
|
79
|
+
// from the parent.
|
|
80
|
+
expect(root.className).not.toMatch(/text-(neutral|primary|secondary|success|warning|danger|info)-/);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('applies text-primary-600 when color="primary"', () => {
|
|
84
|
+
const { container } = render(<Spinner color="primary" />);
|
|
85
|
+
const root = container.firstElementChild!;
|
|
86
|
+
expect(root.className).toContain('text-primary-600');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('applies text-danger-600 when color="danger"', () => {
|
|
90
|
+
const { container } = render(<Spinner color="danger" />);
|
|
91
|
+
const root = container.firstElementChild!;
|
|
92
|
+
expect(root.className).toContain('text-danger-600');
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('thickness', () => {
|
|
97
|
+
it('default thickness=md → stroke-width=2.25', () => {
|
|
98
|
+
const { container } = render(<Spinner />);
|
|
99
|
+
const path = container.querySelector('path')!;
|
|
100
|
+
expect(path.getAttribute('stroke-width')).toBe('2.25');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('thickness=thin → stroke-width=1.5', () => {
|
|
104
|
+
const { container } = render(<Spinner thickness="thin" />);
|
|
105
|
+
const path = container.querySelector('path')!;
|
|
106
|
+
expect(path.getAttribute('stroke-width')).toBe('1.5');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('thickness=thick → stroke-width=3', () => {
|
|
110
|
+
const { container } = render(<Spinner thickness="thick" />);
|
|
111
|
+
const path = container.querySelector('path')!;
|
|
112
|
+
expect(path.getAttribute('stroke-width')).toBe('3');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('withTrack', () => {
|
|
117
|
+
it('does NOT render the track circle by default', () => {
|
|
118
|
+
const { container } = render(<Spinner />);
|
|
119
|
+
// Only the arc <path> — no <circle>.
|
|
120
|
+
expect(container.querySelector('circle')).toBeNull();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('renders a track circle when withTrack=true', () => {
|
|
124
|
+
const { container } = render(<Spinner withTrack />);
|
|
125
|
+
const circle = container.querySelector('circle');
|
|
126
|
+
expect(circle).toBeTruthy();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('track has stroke-opacity=0.2', () => {
|
|
130
|
+
const { container } = render(<Spinner withTrack />);
|
|
131
|
+
const circle = container.querySelector('circle')!;
|
|
132
|
+
expect(circle.getAttribute('stroke-opacity')).toBe('0.2');
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('delay (anti-flash)', () => {
|
|
137
|
+
beforeEach(() => {
|
|
138
|
+
vi.useFakeTimers();
|
|
139
|
+
});
|
|
140
|
+
afterEach(() => {
|
|
141
|
+
vi.useRealTimers();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('renders null for the first `delay` ms', () => {
|
|
145
|
+
const { container } = render(<Spinner delay={150} />);
|
|
146
|
+
expect(container.firstChild).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('renders the spinner after `delay` ms have elapsed', () => {
|
|
150
|
+
const { container } = render(<Spinner delay={150} />);
|
|
151
|
+
expect(container.firstChild).toBeNull();
|
|
152
|
+
act(() => {
|
|
153
|
+
vi.advanceTimersByTime(150);
|
|
154
|
+
});
|
|
155
|
+
expect(container.firstChild).toBeTruthy();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('renders immediately when delay=0 (synchronous path)', () => {
|
|
159
|
+
const { container } = render(<Spinner delay={0} />);
|
|
160
|
+
expect(container.firstChild).toBeTruthy();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('renders immediately when delay is omitted', () => {
|
|
164
|
+
const { container } = render(<Spinner />);
|
|
165
|
+
expect(container.firstChild).toBeTruthy();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('visibleWhen bridge', () => {
|
|
170
|
+
it('renders when visibleWhen returns true', () => {
|
|
171
|
+
render(<Spinner visibleWhen={() => true} />);
|
|
172
|
+
expect(screen.getByRole('status')).toBeTruthy();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('returns null when visibleWhen returns false', () => {
|
|
176
|
+
const { container } = render(<Spinner visibleWhen={() => false} />);
|
|
177
|
+
expect(container.firstChild).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('renders normally when visibleWhen is omitted', () => {
|
|
181
|
+
render(<Spinner />);
|
|
182
|
+
expect(screen.getByRole('status')).toBeTruthy();
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('overrides', () => {
|
|
187
|
+
it('appends sx', () => {
|
|
188
|
+
const { container } = render(<Spinner sx="mt-4" />);
|
|
189
|
+
const root = container.firstElementChild!;
|
|
190
|
+
expect(root.className).toContain('mt-4');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('appends className', () => {
|
|
194
|
+
const { container } = render(<Spinner className="my-custom-spinner" />);
|
|
195
|
+
const root = container.firstElementChild!;
|
|
196
|
+
expect(root.className).toContain('my-custom-spinner');
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { forwardRef, useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { DashFormContext, useEngineVisibility } from '@dashforge/ui-core';
|
|
3
|
+
import { cn } from '../../utils/cn.js';
|
|
4
|
+
import {
|
|
5
|
+
SPINNER_STROKE_WIDTH,
|
|
6
|
+
SPINNER_TRACK_OPACITY,
|
|
7
|
+
spinnerVariants,
|
|
8
|
+
} from './spinner.variants.js';
|
|
9
|
+
import type { SpinnerProps } from './spinner.types.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `<Spinner>` — rotating-arc loading indicator.
|
|
13
|
+
*
|
|
14
|
+
* Visual: a partial-arc SVG rotating via Tailwind's `animate-spin`
|
|
15
|
+
* (pure CSS, GPU-accelerated). Optional `withTrack` renders a faint
|
|
16
|
+
* ghost ring (20% opacity of currentColor) behind the arc — the
|
|
17
|
+
* standard "premium spinner" pattern used by Stripe / Vercel / Linear.
|
|
18
|
+
*
|
|
19
|
+
* Color resolution:
|
|
20
|
+
* - `color="primary"` (or any 7 intent) → `text-{color}-600` class
|
|
21
|
+
* → SVG `stroke="currentColor"` picks it up
|
|
22
|
+
* - `color` omitted → no `text-*` class emitted → SVG inherits
|
|
23
|
+
* parent's text color via `currentColor`. **This is the right
|
|
24
|
+
* default for nested usage** (inside Button, Alert, Card).
|
|
25
|
+
*
|
|
26
|
+
* A11y:
|
|
27
|
+
* - `role="status"` on the wrapper
|
|
28
|
+
* - `aria-live="polite"` so SR queues the announcement
|
|
29
|
+
* - visually-hidden text label inside (`'Loading'` default)
|
|
30
|
+
* - `animate-spin` is gated on `motion-reduce` (WCAG 2.3.3)
|
|
31
|
+
*
|
|
32
|
+
* Delay (anti-flash):
|
|
33
|
+
* - `delay={150}` mounts as `null` for the first 150ms, then swaps
|
|
34
|
+
* to the actual SVG. Quick operations that finish before 150ms
|
|
35
|
+
* never render the spinner — eliminates the "flash" UX bug.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* // 1. Inside a Button — inherits text color
|
|
40
|
+
* <button><Spinner size="sm" /> Saving...</button>
|
|
41
|
+
*
|
|
42
|
+
* // 2. Standalone with explicit color
|
|
43
|
+
* <Spinner color="primary" size="lg" label="Loading dashboard data" />
|
|
44
|
+
*
|
|
45
|
+
* // 3. Anti-flash on quick submits
|
|
46
|
+
* <Spinner visibleWhen={() => isSubmitting} delay={150} />
|
|
47
|
+
*
|
|
48
|
+
* // 4. With track for busy backgrounds
|
|
49
|
+
* <Spinner color="primary" withTrack size="xl" />
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export const Spinner = forwardRef<HTMLSpanElement, SpinnerProps>(function Spinner(
|
|
53
|
+
props,
|
|
54
|
+
ref
|
|
55
|
+
) {
|
|
56
|
+
const {
|
|
57
|
+
size = 'md',
|
|
58
|
+
color,
|
|
59
|
+
thickness = 'md',
|
|
60
|
+
withTrack = false,
|
|
61
|
+
label = 'Loading',
|
|
62
|
+
delay = 0,
|
|
63
|
+
visibleWhen,
|
|
64
|
+
sx,
|
|
65
|
+
className,
|
|
66
|
+
} = props;
|
|
67
|
+
|
|
68
|
+
// Bridge — engine-reactive visibility. Hook called unconditionally
|
|
69
|
+
// (rules-of-hooks). Outside a `<DashForm>`, predicate evaluated as
|
|
70
|
+
// a plain closure.
|
|
71
|
+
const bridge = useContext(DashFormContext);
|
|
72
|
+
const isBridgeVisible = useEngineVisibility(bridge?.engine, visibleWhen);
|
|
73
|
+
|
|
74
|
+
// Anti-flash delay — render `null` for the first `delay` ms after
|
|
75
|
+
// the spinner becomes visible. Skipping the timer entirely when
|
|
76
|
+
// delay=0 keeps the synchronous render path for the common case.
|
|
77
|
+
const [delayElapsed, setDelayElapsed] = useState(delay <= 0);
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (delay <= 0) {
|
|
80
|
+
setDelayElapsed(true);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
setDelayElapsed(false);
|
|
84
|
+
const handle = setTimeout(() => setDelayElapsed(true), delay);
|
|
85
|
+
return () => clearTimeout(handle);
|
|
86
|
+
}, [delay]);
|
|
87
|
+
|
|
88
|
+
if (!isBridgeVisible || !delayElapsed) return null;
|
|
89
|
+
|
|
90
|
+
const classes = cn(
|
|
91
|
+
spinnerVariants({ size, color }),
|
|
92
|
+
sx,
|
|
93
|
+
className
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const strokeWidth = SPINNER_STROKE_WIDTH[thickness];
|
|
97
|
+
|
|
98
|
+
// Decorative mode — when label is explicitly empty (""), suppress
|
|
99
|
+
// role="status" and the visually-hidden label entirely. Used when
|
|
100
|
+
// the Spinner is embedded inside another component that already
|
|
101
|
+
// announces its loading state via `aria-busy` (e.g., Button) — the
|
|
102
|
+
// inner spinner is then purely visual chrome.
|
|
103
|
+
const isDecorative = label === '';
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<span
|
|
107
|
+
ref={ref}
|
|
108
|
+
className={classes}
|
|
109
|
+
role={isDecorative ? undefined : 'status'}
|
|
110
|
+
aria-live={isDecorative ? undefined : 'polite'}
|
|
111
|
+
aria-hidden={isDecorative ? true : undefined}
|
|
112
|
+
>
|
|
113
|
+
<svg
|
|
114
|
+
viewBox="0 0 24 24"
|
|
115
|
+
fill="none"
|
|
116
|
+
aria-hidden="true"
|
|
117
|
+
width="100%"
|
|
118
|
+
height="100%"
|
|
119
|
+
>
|
|
120
|
+
{/* Track ring — faint full circle behind the arc. Renders
|
|
121
|
+
only when `withTrack` is true. Uses currentColor with
|
|
122
|
+
explicit opacity so it follows the parent's text color in
|
|
123
|
+
BOTH light and dark mode without any `dark:` variant. */}
|
|
124
|
+
{withTrack && (
|
|
125
|
+
<circle
|
|
126
|
+
cx="12"
|
|
127
|
+
cy="12"
|
|
128
|
+
r="9"
|
|
129
|
+
stroke="currentColor"
|
|
130
|
+
strokeWidth={strokeWidth}
|
|
131
|
+
strokeOpacity={SPINNER_TRACK_OPACITY}
|
|
132
|
+
fill="none"
|
|
133
|
+
/>
|
|
134
|
+
)}
|
|
135
|
+
{/* Spinning arc — a 90° segment of the circle. The rotation
|
|
136
|
+
comes from the wrapper's `animate-spin` class, not from
|
|
137
|
+
transforming the SVG itself. `strokeLinecap="round"` gives
|
|
138
|
+
the soft, premium-looking ends. */}
|
|
139
|
+
<path
|
|
140
|
+
d="M21 12a9 9 0 0 0-9-9"
|
|
141
|
+
stroke="currentColor"
|
|
142
|
+
strokeWidth={strokeWidth}
|
|
143
|
+
strokeLinecap="round"
|
|
144
|
+
fill="none"
|
|
145
|
+
/>
|
|
146
|
+
</svg>
|
|
147
|
+
{/* Visually-hidden label for screen readers. Skipped entirely
|
|
148
|
+
in decorative mode. */}
|
|
149
|
+
{!isDecorative && (
|
|
150
|
+
<span className="absolute -m-px h-px w-px overflow-hidden whitespace-nowrap border-0 p-0 [clip:rect(0,0,0,0)]">
|
|
151
|
+
{label}
|
|
152
|
+
</span>
|
|
153
|
+
)}
|
|
154
|
+
</span>
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
Spinner.displayName = 'Spinner';
|