@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.
Files changed (134) hide show
  1. package/CHANGELOG.md +234 -12
  2. package/COVERAGE.md +2 -2
  3. package/PARITY.md +7 -9
  4. package/PERFORMANCE.md +3 -4
  5. package/README.md +74 -15
  6. package/THEME-AUDIT.md +3 -4
  7. package/dashforge-tw-1.0.0.tgz +0 -0
  8. package/dist/index.esm.js +3127 -748
  9. package/dist/src/components/Alert/Alert.d.ts +65 -0
  10. package/dist/src/components/Alert/Alert.d.ts.map +1 -0
  11. package/dist/src/components/Alert/alert.types.d.ts +130 -0
  12. package/dist/src/components/Alert/alert.types.d.ts.map +1 -0
  13. package/dist/src/components/Alert/alert.variants.d.ts +85 -0
  14. package/dist/src/components/Alert/alert.variants.d.ts.map +1 -0
  15. package/dist/src/components/Avatar/Avatar.d.ts +48 -0
  16. package/dist/src/components/Avatar/Avatar.d.ts.map +1 -0
  17. package/dist/src/components/Avatar/avatar.types.d.ts +162 -0
  18. package/dist/src/components/Avatar/avatar.types.d.ts.map +1 -0
  19. package/dist/src/components/Avatar/avatar.variants.d.ts +208 -0
  20. package/dist/src/components/Avatar/avatar.variants.d.ts.map +1 -0
  21. package/dist/src/components/Badge/Badge.d.ts +63 -0
  22. package/dist/src/components/Badge/Badge.d.ts.map +1 -0
  23. package/dist/src/components/Badge/badge.types.d.ts +124 -0
  24. package/dist/src/components/Badge/badge.types.d.ts.map +1 -0
  25. package/dist/src/components/Badge/badge.variants.d.ts +262 -0
  26. package/dist/src/components/Badge/badge.variants.d.ts.map +1 -0
  27. package/dist/src/components/Box/Box.d.ts.map +1 -1
  28. package/dist/src/components/Box/box.types.d.ts +53 -0
  29. package/dist/src/components/Box/box.types.d.ts.map +1 -1
  30. package/dist/src/components/Button/Button.d.ts.map +1 -1
  31. package/dist/src/components/Button/button.types.d.ts +35 -0
  32. package/dist/src/components/Button/button.types.d.ts.map +1 -1
  33. package/dist/src/components/Card/Card.d.ts +107 -0
  34. package/dist/src/components/Card/Card.d.ts.map +1 -0
  35. package/dist/src/components/Card/card.types.d.ts +119 -0
  36. package/dist/src/components/Card/card.types.d.ts.map +1 -0
  37. package/dist/src/components/Chip/Chip.d.ts +67 -0
  38. package/dist/src/components/Chip/Chip.d.ts.map +1 -0
  39. package/dist/src/components/Chip/chip.types.d.ts +113 -0
  40. package/dist/src/components/Chip/chip.types.d.ts.map +1 -0
  41. package/dist/src/components/Chip/chip.variants.d.ts +155 -0
  42. package/dist/src/components/Chip/chip.variants.d.ts.map +1 -0
  43. package/dist/src/components/IconButton/IconButton.d.ts +57 -0
  44. package/dist/src/components/IconButton/IconButton.d.ts.map +1 -0
  45. package/dist/src/components/IconButton/iconButton.types.d.ts +96 -0
  46. package/dist/src/components/IconButton/iconButton.types.d.ts.map +1 -0
  47. package/dist/src/components/IconButton/iconButton.variants.d.ts +26 -0
  48. package/dist/src/components/IconButton/iconButton.variants.d.ts.map +1 -0
  49. package/dist/src/components/Menu/Menu.d.ts +118 -0
  50. package/dist/src/components/Menu/Menu.d.ts.map +1 -0
  51. package/dist/src/components/Menu/menu.types.d.ts +179 -0
  52. package/dist/src/components/Menu/menu.types.d.ts.map +1 -0
  53. package/dist/src/components/Menu/menu.variants.d.ts +129 -0
  54. package/dist/src/components/Menu/menu.variants.d.ts.map +1 -0
  55. package/dist/src/components/Snackbar/Snackbar.d.ts.map +1 -1
  56. package/dist/src/components/Snackbar/snackbar.types.d.ts +40 -3
  57. package/dist/src/components/Snackbar/snackbar.types.d.ts.map +1 -1
  58. package/dist/src/components/Snackbar/snackbar.variants.d.ts +12 -57
  59. package/dist/src/components/Snackbar/snackbar.variants.d.ts.map +1 -1
  60. package/dist/src/components/Spinner/Spinner.d.ts +44 -0
  61. package/dist/src/components/Spinner/Spinner.d.ts.map +1 -0
  62. package/dist/src/components/Spinner/spinner.types.d.ts +89 -0
  63. package/dist/src/components/Spinner/spinner.types.d.ts.map +1 -0
  64. package/dist/src/components/Spinner/spinner.variants.d.ts +111 -0
  65. package/dist/src/components/Spinner/spinner.variants.d.ts.map +1 -0
  66. package/dist/src/components/Table/cells/RenderChip.d.ts +15 -72
  67. package/dist/src/components/Table/cells/RenderChip.d.ts.map +1 -1
  68. package/dist/src/components/_shared/severity/index.d.ts +21 -0
  69. package/dist/src/components/_shared/severity/index.d.ts.map +1 -0
  70. package/dist/src/components/_shared/severity/severity.types.d.ts +48 -0
  71. package/dist/src/components/_shared/severity/severity.types.d.ts.map +1 -0
  72. package/dist/src/components/_shared/severity/severityIcons.d.ts +45 -0
  73. package/dist/src/components/_shared/severity/severityIcons.d.ts.map +1 -0
  74. package/dist/src/components/_shared/severity/severityVariants.d.ts +22 -0
  75. package/dist/src/components/_shared/severity/severityVariants.d.ts.map +1 -0
  76. package/dist/src/index.d.ts +27 -2
  77. package/dist/src/index.d.ts.map +1 -1
  78. package/package.json +8 -7
  79. package/src/components/Alert/Alert.test.tsx +302 -0
  80. package/src/components/Alert/Alert.tsx +186 -0
  81. package/src/components/Alert/alert.types.ts +144 -0
  82. package/src/components/Alert/alert.variants.ts +71 -0
  83. package/src/components/Autocomplete/autocomplete.variants.ts +1 -1
  84. package/src/components/Avatar/Avatar.test.tsx +287 -0
  85. package/src/components/Avatar/Avatar.tsx +304 -0
  86. package/src/components/Avatar/avatar.types.ts +205 -0
  87. package/src/components/Avatar/avatar.variants.ts +194 -0
  88. package/src/components/Badge/Badge.test.tsx +385 -0
  89. package/src/components/Badge/Badge.tsx +174 -0
  90. package/src/components/Badge/badge.types.ts +154 -0
  91. package/src/components/Badge/badge.variants.ts +161 -0
  92. package/src/components/Box/Box.test.tsx +21 -0
  93. package/src/components/Box/Box.tsx +32 -3
  94. package/src/components/Box/box.types.ts +55 -0
  95. package/src/components/Button/Button.test.tsx +21 -0
  96. package/src/components/Button/Button.tsx +28 -27
  97. package/src/components/Button/button.types.ts +36 -0
  98. package/src/components/Card/Card.test.tsx +291 -0
  99. package/src/components/Card/Card.tsx +241 -0
  100. package/src/components/Card/card.types.ts +132 -0
  101. package/src/components/Chip/Chip.test.tsx +316 -0
  102. package/src/components/Chip/Chip.tsx +224 -0
  103. package/src/components/Chip/chip.types.ts +128 -0
  104. package/src/components/Chip/chip.variants.ts +173 -0
  105. package/src/components/DataGrid/visibility/ColumnVisibilityMenu.tsx +1 -1
  106. package/src/components/IconButton/IconButton.test.tsx +367 -0
  107. package/src/components/IconButton/IconButton.tsx +159 -0
  108. package/src/components/IconButton/iconButton.types.ts +106 -0
  109. package/src/components/IconButton/iconButton.variants.ts +31 -0
  110. package/src/components/LeftNav/leftNav.variants.ts +2 -2
  111. package/src/components/Menu/Menu.test.tsx +408 -0
  112. package/src/components/Menu/Menu.tsx +335 -0
  113. package/src/components/Menu/menu.types.ts +221 -0
  114. package/src/components/Menu/menu.variants.ts +132 -0
  115. package/src/components/Pagination/pagination.variants.ts +1 -1
  116. package/src/components/Snackbar/Snackbar.tsx +46 -28
  117. package/src/components/Snackbar/snackbar.types.ts +47 -3
  118. package/src/components/Snackbar/snackbar.variants.ts +29 -22
  119. package/src/components/Spinner/Spinner.test.tsx +199 -0
  120. package/src/components/Spinner/Spinner.tsx +158 -0
  121. package/src/components/Spinner/spinner.types.ts +113 -0
  122. package/src/components/Spinner/spinner.variants.ts +83 -0
  123. package/src/components/Table/cells/RenderChip.tsx +24 -87
  124. package/src/components/Table/cells/RowActionsMenu.tsx +1 -1
  125. package/src/components/TextField/TextField.test.tsx +5 -1
  126. package/src/components/_shared/severity/index.ts +33 -0
  127. package/src/components/_shared/severity/severity.types.ts +50 -0
  128. package/src/components/_shared/severity/severityIcons.tsx +104 -0
  129. package/src/components/_shared/severity/severityVariants.test.ts +136 -0
  130. package/src/components/_shared/severity/severityVariants.ts +115 -0
  131. package/src/index.ts +163 -1
  132. package/vite.config.ts +8 -0
  133. package/vitest.config.mts +8 -1
  134. 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
- position,
235
- severity: sev,
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
- <span
248
- aria-hidden="true"
249
- className={cn(
250
- itemClasses.icon(),
251
- slotProps?.icon?.className
252
- )}
253
- >
254
- {SEVERITY_GLYPH[sev]}
255
- </span>
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
- /** Visual severity — drives icon color + accent on the snackbar surface. */
5
- export type SnackbarSeverity = 'info' | 'success' | 'warning' | 'danger';
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<SnackbarOptions, 'severity' | 'autoHideMs' | 'showClose'>;
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
- * - `icon` — leading severity icon
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 bg-neutral-50 text-neutral-900',
25
- // Subtle enter transition — opacity + translate, kept short so a
26
- // burst of snackbars feels snappy. Gated on motion-reduce
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 slide animation.
29
- 'transition-all duration-200 motion-reduce:transition-none motion-reduce:duration-0',
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 w-5 h-5 inline-flex items-center justify-center',
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
- 'text-primary-700 hover:bg-primary-100',
38
- 'outline-none focus-visible:ring-2 focus-visible:ring-primary-500/50',
39
- 'transition-colors',
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 text-neutral-500 hover:bg-neutral-200 hover:text-neutral-900',
44
- 'outline-none focus-visible:ring-2 focus-visible:ring-primary-500/50',
45
- 'transition-colors',
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';