@dashforge/tw 0.1.0-beta → 0.2.1-beta

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 (195) hide show
  1. package/A11Y.md +130 -0
  2. package/CHANGELOG.md +231 -0
  3. package/dist/index.d.ts +1 -82
  4. package/dist/index.esm.js +1608 -11
  5. package/dist/src/components/AspectRatio/AspectRatio.d.ts +36 -0
  6. package/dist/src/components/AspectRatio/AspectRatio.d.ts.map +1 -0
  7. package/dist/src/components/AspectRatio/aspectRatio.types.d.ts +50 -0
  8. package/dist/src/components/AspectRatio/aspectRatio.types.d.ts.map +1 -0
  9. package/dist/src/components/Box/Box.d.ts +36 -0
  10. package/dist/src/components/Box/Box.d.ts.map +1 -0
  11. package/dist/src/components/Box/box.types.d.ts +54 -0
  12. package/dist/src/components/Box/box.types.d.ts.map +1 -0
  13. package/dist/src/components/Box/box.variants.d.ts +402 -0
  14. package/dist/src/components/Box/box.variants.d.ts.map +1 -0
  15. package/dist/src/components/Button/Button.d.ts.map +1 -1
  16. package/dist/src/components/Checkbox/Checkbox.d.ts.map +1 -1
  17. package/dist/src/components/Container/Container.d.ts +35 -0
  18. package/dist/src/components/Container/Container.d.ts.map +1 -0
  19. package/dist/src/components/Container/container.types.d.ts +41 -0
  20. package/dist/src/components/Container/container.types.d.ts.map +1 -0
  21. package/dist/src/components/Container/container.variants.d.ts +93 -0
  22. package/dist/src/components/Container/container.variants.d.ts.map +1 -0
  23. package/dist/src/components/Divider/Divider.d.ts +50 -0
  24. package/dist/src/components/Divider/Divider.d.ts.map +1 -0
  25. package/dist/src/components/Divider/divider.types.d.ts +47 -0
  26. package/dist/src/components/Divider/divider.types.d.ts.map +1 -0
  27. package/dist/src/components/Divider/divider.variants.d.ts +129 -0
  28. package/dist/src/components/Divider/divider.variants.d.ts.map +1 -0
  29. package/dist/src/components/Grid/Grid.d.ts +32 -0
  30. package/dist/src/components/Grid/Grid.d.ts.map +1 -0
  31. package/dist/src/components/Grid/grid.types.d.ts +103 -0
  32. package/dist/src/components/Grid/grid.types.d.ts.map +1 -0
  33. package/dist/src/components/Grid/grid.variants.d.ts +453 -0
  34. package/dist/src/components/Grid/grid.variants.d.ts.map +1 -0
  35. package/dist/src/components/NumberField/NumberField.d.ts.map +1 -1
  36. package/dist/src/components/RadioGroup/RadioGroup.d.ts.map +1 -1
  37. package/dist/src/components/Stack/Stack.d.ts +24 -0
  38. package/dist/src/components/Stack/Stack.d.ts.map +1 -0
  39. package/dist/src/components/Stack/stack.types.d.ts +61 -0
  40. package/dist/src/components/Stack/stack.types.d.ts.map +1 -0
  41. package/dist/src/components/Stack/stack.variants.d.ts +163 -0
  42. package/dist/src/components/Stack/stack.variants.d.ts.map +1 -0
  43. package/dist/src/components/Typography/Typography.d.ts +42 -0
  44. package/dist/src/components/Typography/Typography.d.ts.map +1 -0
  45. package/dist/src/components/Typography/typography.types.d.ts +61 -0
  46. package/dist/src/components/Typography/typography.types.d.ts.map +1 -0
  47. package/dist/src/components/Typography/typography.variants.d.ts +167 -0
  48. package/dist/src/components/Typography/typography.variants.d.ts.map +1 -0
  49. package/dist/src/components/VisuallyHidden/VisuallyHidden.d.ts +42 -0
  50. package/dist/src/components/VisuallyHidden/VisuallyHidden.d.ts.map +1 -0
  51. package/dist/src/components/VisuallyHidden/visuallyHidden.types.d.ts +42 -0
  52. package/dist/src/components/VisuallyHidden/visuallyHidden.types.d.ts.map +1 -0
  53. package/dist/src/index.d.ts +23 -1
  54. package/dist/src/index.d.ts.map +1 -1
  55. package/package.json +3 -3
  56. package/src/components/AspectRatio/AspectRatio.test.tsx +173 -0
  57. package/src/components/AspectRatio/AspectRatio.tsx +73 -0
  58. package/src/components/AspectRatio/aspectRatio.types.ts +54 -0
  59. package/src/components/Box/Box.test.tsx +349 -0
  60. package/src/components/Box/Box.tsx +83 -0
  61. package/src/components/Box/box.types.ts +61 -0
  62. package/src/components/Box/box.variants.ts +211 -0
  63. package/src/components/Button/Button.tsx +11 -0
  64. package/src/components/Checkbox/Checkbox.tsx +26 -2
  65. package/src/components/Container/Container.test.tsx +209 -0
  66. package/src/components/Container/Container.tsx +74 -0
  67. package/src/components/Container/container.types.ts +45 -0
  68. package/src/components/Container/container.variants.ts +81 -0
  69. package/src/components/Divider/Divider.test.tsx +241 -0
  70. package/src/components/Divider/Divider.tsx +140 -0
  71. package/src/components/Divider/divider.types.ts +52 -0
  72. package/src/components/Divider/divider.variants.ts +115 -0
  73. package/src/components/Grid/Grid.test.tsx +321 -0
  74. package/src/components/Grid/Grid.tsx +148 -0
  75. package/src/components/Grid/grid.types.ts +112 -0
  76. package/src/components/Grid/grid.variants.ts +133 -0
  77. package/src/components/NumberField/NumberField.tsx +30 -2
  78. package/src/components/RadioGroup/RadioGroup.tsx +23 -1
  79. package/src/components/Stack/Stack.test.tsx +309 -0
  80. package/src/components/Stack/Stack.tsx +115 -0
  81. package/src/components/Stack/stack.types.ts +68 -0
  82. package/src/components/Stack/stack.variants.ts +80 -0
  83. package/src/components/Typography/Typography.test.tsx +230 -0
  84. package/src/components/Typography/Typography.tsx +112 -0
  85. package/src/components/Typography/typography.types.ts +65 -0
  86. package/src/components/Typography/typography.variants.ts +113 -0
  87. package/src/components/VisuallyHidden/VisuallyHidden.test.tsx +86 -0
  88. package/src/components/VisuallyHidden/VisuallyHidden.tsx +59 -0
  89. package/src/components/VisuallyHidden/visuallyHidden.types.ts +45 -0
  90. package/src/index.ts +50 -1
  91. package/LICENSE +0 -21
  92. package/dist/components/AppShell/AppShell.d.ts +0 -32
  93. package/dist/components/AppShell/AppShell.d.ts.map +0 -1
  94. package/dist/components/AppShell/appShell.types.d.ts +0 -62
  95. package/dist/components/AppShell/appShell.types.d.ts.map +0 -1
  96. package/dist/components/AppShell/appShell.variants.d.ts +0 -65
  97. package/dist/components/AppShell/appShell.variants.d.ts.map +0 -1
  98. package/dist/components/Autocomplete/Autocomplete.d.ts +0 -32
  99. package/dist/components/Autocomplete/Autocomplete.d.ts.map +0 -1
  100. package/dist/components/Autocomplete/autocomplete.types.d.ts +0 -214
  101. package/dist/components/Autocomplete/autocomplete.types.d.ts.map +0 -1
  102. package/dist/components/Autocomplete/autocomplete.variants.d.ts +0 -214
  103. package/dist/components/Autocomplete/autocomplete.variants.d.ts.map +0 -1
  104. package/dist/components/Breadcrumbs/Breadcrumbs.d.ts +0 -23
  105. package/dist/components/Breadcrumbs/Breadcrumbs.d.ts.map +0 -1
  106. package/dist/components/Breadcrumbs/breadcrumbs.types.d.ts +0 -98
  107. package/dist/components/Breadcrumbs/breadcrumbs.types.d.ts.map +0 -1
  108. package/dist/components/Breadcrumbs/breadcrumbs.variants.d.ts +0 -85
  109. package/dist/components/Breadcrumbs/breadcrumbs.variants.d.ts.map +0 -1
  110. package/dist/components/Button/Button.d.ts +0 -44
  111. package/dist/components/Button/Button.d.ts.map +0 -1
  112. package/dist/components/Button/button.types.d.ts +0 -66
  113. package/dist/components/Button/button.types.d.ts.map +0 -1
  114. package/dist/components/Button/button.variants.d.ts +0 -104
  115. package/dist/components/Button/button.variants.d.ts.map +0 -1
  116. package/dist/components/Checkbox/Checkbox.d.ts +0 -31
  117. package/dist/components/Checkbox/Checkbox.d.ts.map +0 -1
  118. package/dist/components/Checkbox/checkbox.types.d.ts +0 -86
  119. package/dist/components/Checkbox/checkbox.types.d.ts.map +0 -1
  120. package/dist/components/Checkbox/checkbox.variants.d.ts +0 -109
  121. package/dist/components/Checkbox/checkbox.variants.d.ts.map +0 -1
  122. package/dist/components/ConfirmDialog/ConfirmDialog.d.ts +0 -38
  123. package/dist/components/ConfirmDialog/ConfirmDialog.d.ts.map +0 -1
  124. package/dist/components/ConfirmDialog/confirmDialog.types.d.ts +0 -80
  125. package/dist/components/ConfirmDialog/confirmDialog.types.d.ts.map +0 -1
  126. package/dist/components/ConfirmDialog/confirmDialog.variants.d.ts +0 -90
  127. package/dist/components/ConfirmDialog/confirmDialog.variants.d.ts.map +0 -1
  128. package/dist/components/DateTimePicker/DateTimePicker.d.ts +0 -49
  129. package/dist/components/DateTimePicker/DateTimePicker.d.ts.map +0 -1
  130. package/dist/components/DateTimePicker/dateTimePicker.types.d.ts +0 -95
  131. package/dist/components/DateTimePicker/dateTimePicker.types.d.ts.map +0 -1
  132. package/dist/components/DateTimePicker/dateTimePicker.variants.d.ts +0 -165
  133. package/dist/components/DateTimePicker/dateTimePicker.variants.d.ts.map +0 -1
  134. package/dist/components/LeftNav/LeftNav.d.ts +0 -34
  135. package/dist/components/LeftNav/LeftNav.d.ts.map +0 -1
  136. package/dist/components/LeftNav/leftNav.types.d.ts +0 -136
  137. package/dist/components/LeftNav/leftNav.types.d.ts.map +0 -1
  138. package/dist/components/LeftNav/leftNav.variants.d.ts +0 -143
  139. package/dist/components/LeftNav/leftNav.variants.d.ts.map +0 -1
  140. package/dist/components/NumberField/NumberField.d.ts +0 -18
  141. package/dist/components/NumberField/NumberField.d.ts.map +0 -1
  142. package/dist/components/NumberField/numberField.types.d.ts +0 -79
  143. package/dist/components/NumberField/numberField.types.d.ts.map +0 -1
  144. package/dist/components/NumberField/numberField.variants.d.ts +0 -169
  145. package/dist/components/NumberField/numberField.variants.d.ts.map +0 -1
  146. package/dist/components/OTPField/OTPField.d.ts +0 -25
  147. package/dist/components/OTPField/OTPField.d.ts.map +0 -1
  148. package/dist/components/OTPField/otpField.types.d.ts +0 -74
  149. package/dist/components/OTPField/otpField.types.d.ts.map +0 -1
  150. package/dist/components/OTPField/otpField.variants.d.ts +0 -105
  151. package/dist/components/OTPField/otpField.variants.d.ts.map +0 -1
  152. package/dist/components/RadioGroup/RadioGroup.d.ts +0 -35
  153. package/dist/components/RadioGroup/RadioGroup.d.ts.map +0 -1
  154. package/dist/components/RadioGroup/radioGroup.types.d.ts +0 -103
  155. package/dist/components/RadioGroup/radioGroup.types.d.ts.map +0 -1
  156. package/dist/components/RadioGroup/radioGroup.variants.d.ts +0 -166
  157. package/dist/components/RadioGroup/radioGroup.variants.d.ts.map +0 -1
  158. package/dist/components/Snackbar/Snackbar.d.ts +0 -37
  159. package/dist/components/Snackbar/Snackbar.d.ts.map +0 -1
  160. package/dist/components/Snackbar/snackbar.types.d.ts +0 -86
  161. package/dist/components/Snackbar/snackbar.types.d.ts.map +0 -1
  162. package/dist/components/Snackbar/snackbar.variants.d.ts +0 -153
  163. package/dist/components/Snackbar/snackbar.variants.d.ts.map +0 -1
  164. package/dist/components/Switch/Switch.d.ts +0 -15
  165. package/dist/components/Switch/Switch.d.ts.map +0 -1
  166. package/dist/components/Switch/switch.types.d.ts +0 -47
  167. package/dist/components/Switch/switch.types.d.ts.map +0 -1
  168. package/dist/components/Switch/switch.variants.d.ts +0 -105
  169. package/dist/components/Switch/switch.variants.d.ts.map +0 -1
  170. package/dist/components/TextField/TextField.d.ts +0 -25
  171. package/dist/components/TextField/TextField.d.ts.map +0 -1
  172. package/dist/components/TextField/textField.types.d.ts +0 -72
  173. package/dist/components/TextField/textField.types.d.ts.map +0 -1
  174. package/dist/components/TextField/textField.variants.d.ts +0 -160
  175. package/dist/components/TextField/textField.variants.d.ts.map +0 -1
  176. package/dist/components/Textarea/Textarea.d.ts +0 -16
  177. package/dist/components/Textarea/Textarea.d.ts.map +0 -1
  178. package/dist/components/Textarea/textarea.types.d.ts +0 -63
  179. package/dist/components/Textarea/textarea.types.d.ts.map +0 -1
  180. package/dist/components/Textarea/textarea.variants.d.ts +0 -197
  181. package/dist/components/Textarea/textarea.variants.d.ts.map +0 -1
  182. package/dist/components/TopBar/TopBar.d.ts +0 -33
  183. package/dist/components/TopBar/TopBar.d.ts.map +0 -1
  184. package/dist/components/TopBar/topBar.types.d.ts +0 -49
  185. package/dist/components/TopBar/topBar.types.d.ts.map +0 -1
  186. package/dist/components/TopBar/topBar.variants.d.ts +0 -79
  187. package/dist/components/TopBar/topBar.variants.d.ts.map +0 -1
  188. package/dist/components/_shared/resolveValidationState.d.ts +0 -42
  189. package/dist/components/_shared/resolveValidationState.d.ts.map +0 -1
  190. package/dist/hooks/useAccessState.d.ts +0 -37
  191. package/dist/hooks/useAccessState.d.ts.map +0 -1
  192. package/dist/index.d.ts.map +0 -1
  193. package/dist/tsconfig.lib.tsbuildinfo +0 -1
  194. package/dist/utils/cn.d.ts +0 -26
  195. package/dist/utils/cn.d.ts.map +0 -1
@@ -0,0 +1,115 @@
1
+ import { tv, type VariantProps } from 'tailwind-variants';
2
+
3
+ /**
4
+ * `dividerVariants` — visual separator with two rendering modes (line-only
5
+ * vs labeled). Two interconnected TV recipes:
6
+ *
7
+ * • `dividerVariants` — root container (block layout, alignment)
8
+ * • `dividerLineVariants` — the actual line segments (border style,
9
+ * color, orientation)
10
+ *
11
+ * Two recipes (not one with slots) because the labeled mode renders
12
+ * THREE elements (left line · label · right line) while the line-only
13
+ * mode is ONE element. Splitting keeps each TV catalogue small and
14
+ * the type unions narrow.
15
+ *
16
+ * Mental model:
17
+ * • Without `children` → renders an `<hr>` (or a div if vertical)
18
+ * with the line styles applied directly.
19
+ * • With `children` → renders a flex row with two `<span>` line
20
+ * segments either side of the label. The label's flex-shrink keeps
21
+ * it from being squashed; the line segments share the remaining
22
+ * space according to `align`.
23
+ *
24
+ * a11y: the root always carries `role="separator"` + `aria-orientation`
25
+ * — handled in Divider.tsx, not in TV (it's a prop, not a class).
26
+ */
27
+
28
+ /*
29
+ * Root container — only relevant when label is present (flex layout).
30
+ * For line-only, the root IS the line.
31
+ */
32
+ export const dividerVariants = tv({
33
+ base: 'flex items-center',
34
+
35
+ variants: {
36
+ orientation: {
37
+ horizontal: 'w-full',
38
+ vertical: 'h-full flex-col',
39
+ },
40
+
41
+ /*
42
+ * Label alignment along the divider's main axis. Implemented by
43
+ * setting the flex-basis of the two line segments asymmetrically
44
+ * — handled in the variants for `dividerLineVariants` below via
45
+ * compound logic. The root just needs the flex layout.
46
+ */
47
+ align: {
48
+ start: '',
49
+ center: '',
50
+ end: '',
51
+ },
52
+ },
53
+
54
+ defaultVariants: {
55
+ orientation: 'horizontal',
56
+ align: 'center',
57
+ },
58
+ });
59
+
60
+ export type DividerVariants = VariantProps<typeof dividerVariants>;
61
+
62
+ /*
63
+ * Line segment(s).
64
+ *
65
+ * Border styles (solid/dashed/dotted) are applied as `border-t-{style}`
66
+ * for horizontal, `border-l-{style}` for vertical. Color drives the
67
+ * border-* color token to the intent.
68
+ *
69
+ * The `segment` axis distinguishes whether this is a line-only render
70
+ * (full width) vs a labeled-mode segment (flex-1 grows to share space).
71
+ */
72
+ export const dividerLineVariants = tv({
73
+ base: '',
74
+
75
+ variants: {
76
+ orientation: {
77
+ horizontal: 'h-0 border-t',
78
+ vertical: 'w-0 border-l self-stretch',
79
+ },
80
+
81
+ variant: {
82
+ solid: 'border-solid',
83
+ dashed: 'border-dashed',
84
+ dotted: 'border-dotted',
85
+ },
86
+
87
+ color: {
88
+ neutral: 'border-neutral-200 dark:border-neutral-800',
89
+ primary: 'border-primary-300 dark:border-primary-700',
90
+ secondary: 'border-secondary-300 dark:border-secondary-700',
91
+ success: 'border-success-300 dark:border-success-700',
92
+ warning: 'border-warning-300 dark:border-warning-700',
93
+ danger: 'border-danger-300 dark:border-danger-700',
94
+ info: 'border-info-300 dark:border-info-700',
95
+ },
96
+
97
+ /*
98
+ * `segment` controls whether the line spans full width (line-only
99
+ * mode) or grows to fill space (labeled-mode flex segment).
100
+ */
101
+ segment: {
102
+ full: 'w-full',
103
+ grow: 'flex-1',
104
+ },
105
+ },
106
+
107
+ defaultVariants: {
108
+ orientation: 'horizontal',
109
+ variant: 'solid',
110
+ color: 'neutral',
111
+ segment: 'full',
112
+ },
113
+ });
114
+
115
+ export type DividerLineVariants = VariantProps<typeof dividerLineVariants>;
@@ -0,0 +1,321 @@
1
+ // @vitest-environment jsdom
2
+ import { describe, it, expect } from 'vitest';
3
+ import { render } from '@testing-library/react';
4
+ import { Grid } from './Grid.js';
5
+
6
+ /**
7
+ * Suite covers:
8
+ * • Container role — display:grid + cols + spacing + autoFlow
9
+ * • Item role — col-span at xs/sm/md/lg/xl breakpoints
10
+ * • Default semantics — container without cols → 12; item without xs → 'full'
11
+ * • Polymorphism — as / asChild on both roles
12
+ * • Override — sx wins over variant defaults
13
+ *
14
+ * We don't enumerate every breakpoint × span (5 × 14 = 70 combos);
15
+ * spot-checks across xs/md/xl with span 6/auto/full cover the
16
+ * cascade. If those work, the rest works.
17
+ */
18
+ describe('<Grid>', () => {
19
+ // ─── Container role ─────────────────────────────────────────────────
20
+ describe('container', () => {
21
+ it('renders display:grid + default cols=12', () => {
22
+ const { container } = render(<Grid container>x</Grid>);
23
+ const cls = container.firstElementChild?.className ?? '';
24
+ expect(cls).toContain('grid');
25
+ expect(cls).toContain('grid-cols-12');
26
+ });
27
+
28
+ it('container with cols={6}', () => {
29
+ const { container } = render(<Grid container cols={6}>x</Grid>);
30
+ expect(container.firstElementChild?.className).toContain('grid-cols-6');
31
+ });
32
+
33
+ it('spacing={4} emits gap-4', () => {
34
+ const { container } = render(<Grid container spacing={4}>x</Grid>);
35
+ expect(container.firstElementChild?.className).toContain('gap-4');
36
+ });
37
+
38
+ it('spacingX + spacingY emit gap-x / gap-y', () => {
39
+ const { container } = render(
40
+ <Grid container spacingX={6} spacingY={2}>x</Grid>,
41
+ );
42
+ const cls = container.firstElementChild?.className ?? '';
43
+ expect(cls).toContain('gap-x-6');
44
+ expect(cls).toContain('gap-y-2');
45
+ });
46
+
47
+ it('autoFlow="dense"', () => {
48
+ const { container } = render(<Grid container autoFlow="dense">x</Grid>);
49
+ expect(container.firstElementChild?.className).toContain('grid-flow-dense');
50
+ });
51
+ });
52
+
53
+ // ─── Item role ──────────────────────────────────────────────────────
54
+ describe('item', () => {
55
+ it('item without props defaults to col-span-full (xs="full")', () => {
56
+ const { container } = render(<Grid>x</Grid>);
57
+ const cls = container.firstElementChild?.className ?? '';
58
+ expect(cls).toContain('col-span-full');
59
+ // never accidentally a container
60
+ expect(cls).not.toContain('grid-cols-12');
61
+ // confirm it's NOT display:grid (no `grid` class)
62
+ expect(cls.split(' ')).not.toContain('grid');
63
+ });
64
+
65
+ it('item xs={6}', () => {
66
+ const { container } = render(<Grid xs={6}>x</Grid>);
67
+ expect(container.firstElementChild?.className).toContain('col-span-6');
68
+ });
69
+
70
+ it('item with responsive cascade xs={12} md={6} lg={4}', () => {
71
+ const { container } = render(<Grid xs={12} md={6} lg={4}>x</Grid>);
72
+ const cls = container.firstElementChild?.className ?? '';
73
+ expect(cls).toContain('col-span-12');
74
+ expect(cls).toContain('md:col-span-6');
75
+ expect(cls).toContain('lg:col-span-4');
76
+ });
77
+
78
+ it('item xs="auto"', () => {
79
+ const { container } = render(<Grid xs="auto">x</Grid>);
80
+ expect(container.firstElementChild?.className).toContain('col-auto');
81
+ });
82
+
83
+ it('item sm="full" xl={3}', () => {
84
+ const { container } = render(<Grid sm="full" xl={3}>x</Grid>);
85
+ const cls = container.firstElementChild?.className ?? '';
86
+ expect(cls).toContain('sm:col-span-full');
87
+ expect(cls).toContain('xl:col-span-3');
88
+ });
89
+ });
90
+
91
+ // ─── Composition: container > item ──────────────────────────────────
92
+ describe('container > item composition', () => {
93
+ it('container with item children produces the canonical class chain', () => {
94
+ const { container } = render(
95
+ <Grid container spacing={4}>
96
+ <Grid xs={12} md={6}>a</Grid>
97
+ <Grid xs={12} md={6}>b</Grid>
98
+ </Grid>,
99
+ );
100
+ const root = container.firstElementChild;
101
+ expect(root?.className).toContain('grid');
102
+ expect(root?.className).toContain('gap-4');
103
+ const items = root?.querySelectorAll(':scope > div') ?? [];
104
+ expect(items.length).toBe(2);
105
+ items.forEach((item) => {
106
+ expect(item.className).toContain('col-span-12');
107
+ expect(item.className).toContain('md:col-span-6');
108
+ });
109
+ });
110
+ });
111
+
112
+ // ─── Override + sx ──────────────────────────────────────────────────
113
+ describe('override', () => {
114
+ it('sx wins over default cols on container', () => {
115
+ const { container } = render(
116
+ <Grid container sx="grid-cols-4">x</Grid>,
117
+ );
118
+ const cls = container.firstElementChild?.className ?? '';
119
+ expect(cls).toContain('grid-cols-4');
120
+ // tailwind-merge collapses the default 12 → 4
121
+ expect(cls).not.toContain('grid-cols-12');
122
+ });
123
+
124
+ it('sx adds utilities Grid does not expose', () => {
125
+ const { container } = render(<Grid container sx="min-h-screen">x</Grid>);
126
+ expect(container.firstElementChild?.className).toContain('min-h-screen');
127
+ });
128
+ });
129
+
130
+ // ─── Polymorphism ───────────────────────────────────────────────────
131
+ describe('polymorphism', () => {
132
+ it('container as="section" renders <section> + grid classes', () => {
133
+ const { container } = render(<Grid container as="section">x</Grid>);
134
+ const el = container.firstElementChild;
135
+ expect(el?.tagName).toBe('SECTION');
136
+ expect(el?.className).toContain('grid');
137
+ });
138
+
139
+ it('item asChild paints onto child element', () => {
140
+ const { container } = render(
141
+ <Grid xs={6} asChild>
142
+ <article>x</article>
143
+ </Grid>,
144
+ );
145
+ const el = container.firstElementChild;
146
+ expect(el?.tagName).toBe('ARTICLE');
147
+ expect(el?.className).toContain('col-span-6');
148
+ });
149
+ });
150
+
151
+ // ─── DOM cleanliness (no role-specific props leak as HTML attrs) ────
152
+ describe('DOM cleanliness', () => {
153
+ it('container props (cols/spacing/autoFlow) do not leak to DOM', () => {
154
+ const { container } = render(
155
+ <Grid container cols={4} spacing={2} autoFlow="dense">x</Grid>,
156
+ );
157
+ const el = container.firstElementChild as HTMLElement;
158
+ expect(el.hasAttribute('cols')).toBe(false);
159
+ expect(el.hasAttribute('spacing')).toBe(false);
160
+ expect(el.hasAttribute('autoflow')).toBe(false);
161
+ });
162
+
163
+ it('item props (xs/sm/md/lg/xl) do not leak to DOM', () => {
164
+ const { container } = render(
165
+ <Grid xs={12} md={6} lg={4}>x</Grid>,
166
+ );
167
+ const el = container.firstElementChild as HTMLElement;
168
+ expect(el.hasAttribute('xs')).toBe(false);
169
+ expect(el.hasAttribute('md')).toBe(false);
170
+ expect(el.hasAttribute('lg')).toBe(false);
171
+ });
172
+ });
173
+
174
+ // ─── F11-bis edge cases: responsive cascade across ALL breakpoints ──
175
+ // The original suite spot-checked xs/md/lg/sm/xl with a couple of
176
+ // values each. The publish-validation pass enumerates each breakpoint
177
+ // for the high-traffic span values (1, 6, 12, auto, full) so a future
178
+ // mapping-table edit can't silently drop one.
179
+ describe('responsive cascade — every breakpoint × representative span', () => {
180
+ const BREAKPOINTS = ['sm', 'md', 'lg', 'xl'] as const;
181
+
182
+ it.each(BREAKPOINTS)('breakpoint %s × span=1', (bp) => {
183
+ const props = { [bp]: 1 as const };
184
+ const { container } = render(<Grid {...props}>x</Grid>);
185
+ expect(container.firstElementChild?.className).toContain(`${bp}:col-span-1`);
186
+ });
187
+
188
+ it.each(BREAKPOINTS)('breakpoint %s × span=6', (bp) => {
189
+ const props = { [bp]: 6 as const };
190
+ const { container } = render(<Grid {...props}>x</Grid>);
191
+ expect(container.firstElementChild?.className).toContain(`${bp}:col-span-6`);
192
+ });
193
+
194
+ it.each(BREAKPOINTS)('breakpoint %s × span=12', (bp) => {
195
+ const props = { [bp]: 12 as const };
196
+ const { container } = render(<Grid {...props}>x</Grid>);
197
+ expect(container.firstElementChild?.className).toContain(`${bp}:col-span-12`);
198
+ });
199
+
200
+ it.each(BREAKPOINTS)('breakpoint %s × auto', (bp) => {
201
+ const props = { [bp]: 'auto' as const };
202
+ const { container } = render(<Grid {...props}>x</Grid>);
203
+ expect(container.firstElementChild?.className).toContain(`${bp}:col-auto`);
204
+ });
205
+
206
+ it.each(BREAKPOINTS)('breakpoint %s × full', (bp) => {
207
+ const props = { [bp]: 'full' as const };
208
+ const { container } = render(<Grid {...props}>x</Grid>);
209
+ expect(container.firstElementChild?.className).toContain(`${bp}:col-span-full`);
210
+ });
211
+
212
+ it('full cascade xs=12 sm=6 md=4 lg=3 xl=2 emits all five', () => {
213
+ const { container } = render(
214
+ <Grid xs={12} sm={6} md={4} lg={3} xl={2}>x</Grid>,
215
+ );
216
+ const cls = container.firstElementChild?.className ?? '';
217
+ expect(cls).toContain('col-span-12');
218
+ expect(cls).toContain('sm:col-span-6');
219
+ expect(cls).toContain('md:col-span-4');
220
+ expect(cls).toContain('lg:col-span-3');
221
+ expect(cls).toContain('xl:col-span-2');
222
+ });
223
+ });
224
+
225
+ // ─── F11-bis edge cases: autoFlow all five varianti ─────────────────
226
+ describe('autoFlow — every value', () => {
227
+ const FLOWS = ['row', 'col', 'dense', 'row-dense', 'col-dense'] as const;
228
+ const EXPECTED: Record<typeof FLOWS[number], string> = {
229
+ row: 'grid-flow-row',
230
+ col: 'grid-flow-col',
231
+ dense: 'grid-flow-dense',
232
+ 'row-dense': 'grid-flow-row-dense',
233
+ 'col-dense': 'grid-flow-col-dense',
234
+ };
235
+
236
+ it.each(FLOWS)('autoFlow="%s" emits the right grid-flow-* class', (flow) => {
237
+ const { container } = render(<Grid container autoFlow={flow}>x</Grid>);
238
+ expect(container.firstElementChild?.className).toContain(EXPECTED[flow]);
239
+ });
240
+ });
241
+
242
+ // ─── F11-bis edge cases: container cols all valid values ────────────
243
+ describe('cols — every valid value', () => {
244
+ const COLS = [1, 2, 3, 4, 6, 12] as const;
245
+ it.each(COLS)('container cols=%s emits grid-cols-%s', (n) => {
246
+ const { container } = render(<Grid container cols={n}>x</Grid>);
247
+ expect(container.firstElementChild?.className).toContain(`grid-cols-${n}`);
248
+ });
249
+ });
250
+
251
+ // ─── F11-bis edge cases: spacingX vs spacingY independence ──────────
252
+ describe('spacingX / spacingY independence', () => {
253
+ it('spacingX without spacingY emits only gap-x', () => {
254
+ const { container } = render(<Grid container spacingX={4}>x</Grid>);
255
+ const cls = container.firstElementChild?.className ?? '';
256
+ expect(cls).toContain('gap-x-4');
257
+ expect(cls).not.toMatch(/gap-y-/);
258
+ // standalone `gap-` shouldn't appear unless spacing was passed
259
+ expect(cls).not.toMatch(/gap-\d/);
260
+ });
261
+
262
+ it('spacingY without spacingX emits only gap-y', () => {
263
+ const { container } = render(<Grid container spacingY={2}>x</Grid>);
264
+ const cls = container.firstElementChild?.className ?? '';
265
+ expect(cls).toContain('gap-y-2');
266
+ expect(cls).not.toMatch(/gap-x-/);
267
+ });
268
+
269
+ it('spacing + spacingX: spacing applies symmetrically, spacingX overrides on X', () => {
270
+ const { container } = render(
271
+ <Grid container spacing={4} spacingX={8}>x</Grid>,
272
+ );
273
+ const cls = container.firstElementChild?.className ?? '';
274
+ expect(cls).toContain('gap-4');
275
+ expect(cls).toContain('gap-x-8');
276
+ });
277
+ });
278
+
279
+ // ─── F11-bis edge cases: empty container / orphan item ──────────────
280
+ describe('empty children + orphan item', () => {
281
+ it('container with NO children emits grid + cols, renders empty div', () => {
282
+ const { container } = render(<Grid container />);
283
+ const el = container.firstElementChild as HTMLElement;
284
+ expect(el.className).toContain('grid');
285
+ expect(el.className).toContain('grid-cols-12');
286
+ expect(el.children.length).toBe(0);
287
+ });
288
+
289
+ it('orphan item (no container parent) still emits col-span-* (consumer mistake)', () => {
290
+ // Doesn't crash — produces a col-span-* class that has no effect
291
+ // outside a CSS Grid parent. Documented behaviour: the consumer
292
+ // owns the composition.
293
+ const { container } = render(<Grid xs={6}>x</Grid>);
294
+ expect(container.firstElementChild?.className).toContain('col-span-6');
295
+ });
296
+ });
297
+
298
+ // ─── F11-bis edge cases: composition deep nesting ───────────────────
299
+ describe('deep nesting — Grid inside Grid item', () => {
300
+ it('nested Grid container inside an item works (12-col inside a 6-span)', () => {
301
+ const { container } = render(
302
+ <Grid container spacing={4}>
303
+ <Grid xs={6}>
304
+ <Grid container spacing={2}>
305
+ <Grid xs={6}>inner-a</Grid>
306
+ <Grid xs={6}>inner-b</Grid>
307
+ </Grid>
308
+ </Grid>
309
+ <Grid xs={6}>outer-b</Grid>
310
+ </Grid>,
311
+ );
312
+ // Outer: grid + 12-col + gap-4
313
+ const outer = container.firstElementChild;
314
+ expect(outer?.className).toContain('grid');
315
+ expect(outer?.className).toContain('grid-cols-12');
316
+ expect(outer?.className).toContain('gap-4');
317
+ // Inner items count: outer has 2 direct children
318
+ expect(outer?.children.length).toBe(2);
319
+ });
320
+ });
321
+ });
@@ -0,0 +1,148 @@
1
+ import { forwardRef, type ElementType, type ReactElement } from 'react';
2
+ import { Slot } from '@radix-ui/react-slot';
3
+ import { cn } from '../../utils/cn.js';
4
+ import { gridVariants } from './grid.variants.js';
5
+ import type {
6
+ GridProps,
7
+ GridContainerProps,
8
+ GridItemProps,
9
+ ColSpan,
10
+ GridSpacingStep,
11
+ } from './grid.types.js';
12
+
13
+ /**
14
+ * `<Grid>` — CSS Grid container + item, polymorphic in role.
15
+ *
16
+ * API surface mirrors MUI Grid v2:
17
+ * • <Grid container spacing={4} cols={12}> ← container
18
+ * • <Grid xs={12} md={6}> ← item
19
+ *
20
+ * Engine is REAL CSS Grid (not flexbox, unlike MUI v2 internals).
21
+ * Benefits over flexbox-based implementation:
22
+ * • Native `gap` (no negative-margin tricks)
23
+ * • `col-span-N` maps directly to `grid-column: span N`
24
+ * • Vertical alignment without manual `align-items` per item
25
+ * • Auto-flow for dense packing without DOM gymnastics
26
+ *
27
+ * TypeScript discriminated union (see grid.types.ts) makes mixing
28
+ * impossible: `<Grid container xs={6}>` won't compile. Same for the
29
+ * inverse. IntelliSense filters by role.
30
+ *
31
+ * Defaults at the component level (not at the TV level — see the
32
+ * variants file for the rationale):
33
+ * • container: cols defaults to 12 (MUI convention)
34
+ * • item: xs defaults to 'full' so a forgotten prop doesn't
35
+ * collapse the cell to 1/12th of a row
36
+ *
37
+ * `asChild` on a CONTAINER is technically allowed but rarely useful:
38
+ * `display:grid` only applies to direct children, so the slotted
39
+ * element has to already be a viable container parent. Use sparingly;
40
+ * test asserts the wrapping still emits the right class chain.
41
+ */
42
+ export const Grid = forwardRef<HTMLElement, GridProps>(
43
+ function Grid(props, ref) {
44
+ const { as, asChild = false, sx, children, ...rest } = props as GridProps & {
45
+ as?: ElementType;
46
+ asChild?: boolean;
47
+ sx?: string;
48
+ children?: React.ReactNode;
49
+ };
50
+ /*
51
+ * Discriminated union runtime branch.
52
+ *
53
+ * TS narrows GridProps to ONE of the two shapes (container or item)
54
+ * at the call site, but inside this function body we receive the
55
+ * union — so we manually narrow via a single cast per branch.
56
+ * `as GridContainerProps` / `as GridItemProps` is sound because
57
+ * `container === true` is the literal discriminator declared in
58
+ * the types file; the cast just makes TS read the right branch.
59
+ */
60
+ const isContainer = (props as { container?: boolean }).container === true;
61
+
62
+ let classes: string;
63
+ let containerPayload: GridContainerProps | null = null;
64
+ let itemPayload: GridItemProps | null = null;
65
+
66
+ if (isContainer) {
67
+ containerPayload = props as GridContainerProps;
68
+ /*
69
+ * Container branch — pass ONLY container axes to TV. Default cols
70
+ * to 12 (MUI v2 convention) so a forgotten prop doesn't collapse
71
+ * to grid-cols-1. The spacing axes are passed straight through;
72
+ * TV silently drops undefined values.
73
+ */
74
+ classes = cn(
75
+ gridVariants({
76
+ container: true,
77
+ cols: (containerPayload.cols ?? 12) as 1 | 2 | 3 | 4 | 6 | 12,
78
+ spacing: containerPayload.spacing,
79
+ spacingX: containerPayload.spacingX,
80
+ spacingY: containerPayload.spacingY,
81
+ autoFlow: containerPayload.autoFlow,
82
+ }),
83
+ sx,
84
+ );
85
+ } else {
86
+ itemPayload = props as GridItemProps;
87
+ /*
88
+ * Item branch — default `xs` to 'full' so a forgotten breakpoint
89
+ * prop doesn't collapse the cell to grid-auto's minimum width.
90
+ */
91
+ classes = cn(
92
+ gridVariants({
93
+ xs: (itemPayload.xs ?? 'full') as ColSpan,
94
+ sm: itemPayload.sm,
95
+ md: itemPayload.md,
96
+ lg: itemPayload.lg,
97
+ xl: itemPayload.xl,
98
+ }),
99
+ sx,
100
+ );
101
+ }
102
+
103
+ /*
104
+ * Strip role-specific props from `rest` before spreading on the DOM
105
+ * element. Without this, React warns:
106
+ * "React does not recognize the `cols` prop on a DOM element"
107
+ */
108
+ const domRest: Record<string, unknown> = { ...(rest as Record<string, unknown>) };
109
+ if (containerPayload) {
110
+ delete domRest.cols;
111
+ delete domRest.spacing;
112
+ delete domRest.spacingX;
113
+ delete domRest.spacingY;
114
+ delete domRest.autoFlow;
115
+ }
116
+ if (itemPayload) {
117
+ delete domRest.xs;
118
+ delete domRest.sm;
119
+ delete domRest.md;
120
+ delete domRest.lg;
121
+ delete domRest.xl;
122
+ }
123
+ // `container` discriminator itself must never reach the DOM.
124
+ delete domRest.container;
125
+ // Silence unused vars that the union typings require us to receive.
126
+ void itemPayload; void containerPayload;
127
+ // The unused-vars helper from grid.types is a type-only import,
128
+ // referenced here to keep ESM tree-shake clean — TS handles imports.
129
+ void ({} as GridSpacingStep);
130
+
131
+ if (asChild) {
132
+ return (
133
+ <Slot ref={ref} className={classes} {...domRest}>
134
+ {children as ReactElement}
135
+ </Slot>
136
+ );
137
+ }
138
+
139
+ const Tag = (as ?? 'div') as ElementType;
140
+ return (
141
+ <Tag ref={ref as never} className={classes} {...domRest}>
142
+ {children}
143
+ </Tag>
144
+ );
145
+ },
146
+ );
147
+
148
+ Grid.displayName = 'Grid';