@nickrobison/colophon-table 0.0.0-stage → 0.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 (59) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +55 -2
  3. package/package.json +72 -3
  4. package/src/components/CphSkeleton.test.tsx +145 -0
  5. package/src/components/CphSkeleton.tsx +42 -0
  6. package/src/components/CphTableEmpty.test.tsx +122 -0
  7. package/src/components/CphTableEmpty.tsx +42 -0
  8. package/src/components/DataTable/CphDataTable.test.tsx +408 -0
  9. package/src/components/DataTable/CphDataTable.tsx +121 -0
  10. package/src/components/DataTable/pinnedOffsets.ts +94 -0
  11. package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.test.tsx +172 -0
  12. package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.tsx +132 -0
  13. package/src/components/FilterRange/CphTableFilterRange.test.tsx +154 -0
  14. package/src/components/FilterRange/CphTableFilterRange.tsx +109 -0
  15. package/src/components/MobileCards/CphMobileCards.test.tsx +174 -0
  16. package/src/components/MobileCards/CphMobileCards.tsx +117 -0
  17. package/src/components/Pagination/CphCursorPagination.test.tsx +51 -0
  18. package/src/components/Pagination/CphCursorPagination.tsx +74 -0
  19. package/src/components/Pagination/CphPagination.test.tsx +98 -0
  20. package/src/components/Pagination/CphPagination.tsx +94 -0
  21. package/src/components/RowDetail/CphRowDetail.test.tsx +182 -0
  22. package/src/components/RowDetail/CphRowDetail.tsx +66 -0
  23. package/src/components/SimpleTable/CphSimpleTable.test.tsx +235 -0
  24. package/src/components/SimpleTable/CphSimpleTable.tsx +369 -0
  25. package/src/components/SortFilterSheet/CphSortFilterSheet.test.tsx +156 -0
  26. package/src/components/SortFilterSheet/CphSortFilterSheet.tsx +131 -0
  27. package/src/components/StatusChip/CphStatusChip.test.tsx +43 -0
  28. package/src/components/StatusChip/CphStatusChip.tsx +33 -0
  29. package/src/components/Summary/CphTableSummary.test.tsx +486 -0
  30. package/src/components/Summary/CphTableSummary.tsx +130 -0
  31. package/src/components/TableHead/CphTableHead.test.tsx +52 -0
  32. package/src/components/TableHead/CphTableHead.tsx +129 -0
  33. package/src/components/TableHead/HeaderCell.test.tsx +880 -0
  34. package/src/components/TableHead/HeaderCell.tsx +221 -0
  35. package/src/components/TableRow/Cell.test.tsx +306 -0
  36. package/src/components/TableRow/Cell.tsx +74 -0
  37. package/src/components/TableRow/CphTableRow.test.tsx +90 -0
  38. package/src/components/TableRow/CphTableRow.tsx +17 -0
  39. package/src/components/Toolbar/CphToolbar.test.tsx +150 -0
  40. package/src/components/Toolbar/CphToolbar.tsx +138 -0
  41. package/src/components.css +593 -0
  42. package/src/index.ts +69 -0
  43. package/src/table/cellRenderers.tsx +88 -0
  44. package/src/table/column.test.tsx +277 -0
  45. package/src/table/column.tsx +36 -0
  46. package/src/table/features.ts +69 -0
  47. package/src/table/pageSize.test.ts +128 -0
  48. package/src/table/pageSize.ts +47 -0
  49. package/src/table/summary.test.tsx +446 -0
  50. package/src/table/summary.ts +94 -0
  51. package/src/table/useCphTable.ts +90 -0
  52. package/src/table/useCphTableState.test.tsx +697 -0
  53. package/src/table/useCphTableState.ts +412 -0
  54. package/src/table-tokens.css +48 -0
  55. package/src/theme/CphTableDensity.test.tsx +53 -0
  56. package/src/theme/CphTableDensity.tsx +24 -0
  57. package/src/theme/cphTableDensityContext.ts +24 -0
  58. package/src/utils/composeClassName.test.ts +26 -0
  59. package/src/utils/composeClassName.ts +28 -0
@@ -0,0 +1,412 @@
1
+ /** @packageDocumentation Per-slice controlled state for TanStack Table v9. */
2
+
3
+ import type {
4
+ SortingState,
5
+ ColumnFiltersState,
6
+ ColumnVisibilityState,
7
+ ColumnPinningState,
8
+ RowSelectionState,
9
+ ExpandedState,
10
+ PaginationState,
11
+ OnChangeFn,
12
+ Updater,
13
+ } from "@tanstack/react-table";
14
+ import { useState } from "react";
15
+
16
+ import { DEFAULT_PAGE_SIZE } from "./pageSize";
17
+
18
+ /**
19
+ * Discriminated union for a single state slice.
20
+ *
21
+ * - Controlled: consumer owns the value and provides an `onChange` callback.
22
+ * The hook wires them straight through and keeps NO internal copy.
23
+ * - Uncontrolled: hook owns the state internally; `initialValue` seeds it.
24
+ */
25
+ type ControlledSlice<T> = {
26
+ readonly controlled: true;
27
+ readonly value: T;
28
+ readonly onChange: OnChangeFn<T>;
29
+ };
30
+
31
+ type UncontrolledSlice<T> = {
32
+ readonly controlled: false;
33
+ readonly initialValue?: T;
34
+ };
35
+
36
+ export type StateSlice<T> = ControlledSlice<T> | UncontrolledSlice<T>;
37
+
38
+ /** Input options for {@link useCphTableState}. */
39
+ export interface UseCphTableStateOptions {
40
+ /** Row sorting state (single-column sort enforced by `enableMultiSort: false`). */
41
+ readonly sorting?: StateSlice<SortingState>;
42
+ /** Column filter state. */
43
+ readonly columnFilters?: StateSlice<ColumnFiltersState>;
44
+ /** Column visibility state. */
45
+ readonly columnVisibility?: StateSlice<ColumnVisibilityState>;
46
+ /** Column pinning state (logical start/end). */
47
+ readonly columnPinning?: StateSlice<ColumnPinningState>;
48
+ /** Row selection state. */
49
+ readonly rowSelection?: StateSlice<RowSelectionState>;
50
+ /** Row expansion state. Single-row expansion is enforced when uncontrolled. */
51
+ readonly expanded?: StateSlice<ExpandedState>;
52
+ /** Pagination state. */
53
+ readonly pagination?: StateSlice<PaginationState>;
54
+ }
55
+
56
+ /**
57
+ * Return type for {@link useCphTableState}.
58
+ *
59
+ * Spread this into `useTable({ features: cphTableFeatures, columns, data, ...useCphTableState(...) })`.
60
+ * Each slice appears exactly once: either in `state` + `onXChange` (controlled) or in `initialState` (uncontrolled).
61
+ */
62
+ export interface UseCphTableStateReturn {
63
+ /** Controlled state slices (only includes slices where `controlled: true`). */
64
+ readonly state: Partial<{
65
+ readonly sorting: SortingState;
66
+ readonly columnFilters: ColumnFiltersState;
67
+ readonly columnVisibility: ColumnVisibilityState;
68
+ readonly columnPinning: ColumnPinningState;
69
+ readonly rowSelection: RowSelectionState;
70
+ readonly expanded: ExpandedState;
71
+ readonly pagination: PaginationState;
72
+ }>;
73
+ /** Change callbacks for controlled slices (only included when controlled). */
74
+ readonly onSortingChange?: OnChangeFn<SortingState>;
75
+ readonly onColumnFiltersChange?: OnChangeFn<ColumnFiltersState>;
76
+ readonly onColumnVisibilityChange?: OnChangeFn<ColumnVisibilityState>;
77
+ readonly onColumnPinningChange?: OnChangeFn<ColumnPinningState>;
78
+ readonly onRowSelectionChange?: OnChangeFn<RowSelectionState>;
79
+ readonly onExpandedChange?: OnChangeFn<ExpandedState>;
80
+ readonly onPaginationChange?: OnChangeFn<PaginationState>;
81
+ /** Uncontrolled initial state slices (only includes slices where `controlled: false`). */
82
+ readonly initialState: Partial<{
83
+ readonly sorting: SortingState;
84
+ readonly columnFilters: ColumnFiltersState;
85
+ readonly columnVisibility: ColumnVisibilityState;
86
+ readonly columnPinning: ColumnPinningState;
87
+ readonly rowSelection: RowSelectionState;
88
+ readonly expanded: ExpandedState;
89
+ readonly pagination: PaginationState;
90
+ }>;
91
+ }
92
+
93
+ /**
94
+ * Resolves a slice to its controlled value, consumer change handler, and initial value.
95
+ *
96
+ * @returns `[controlledValue, consumerChangeHandler, initialValue]` where exactly one of
97
+ * `controlledValue`/`consumerChangeHandler` or `initialValue` is defined per slice.
98
+ */
99
+ function resolveSlice<T>(
100
+ slice: StateSlice<T> | undefined,
101
+ defaultInitial: T,
102
+ ): [
103
+ controlledValue: T | undefined,
104
+ consumerChangeHandler: OnChangeFn<T> | undefined,
105
+ initialValue: T | undefined,
106
+ ] {
107
+ if (!slice) {
108
+ return [undefined, undefined, defaultInitial];
109
+ }
110
+ if (slice.controlled) {
111
+ return [slice.value, slice.onChange, undefined];
112
+ }
113
+ return [undefined, undefined, slice.initialValue ?? defaultInitial];
114
+ }
115
+
116
+ /**
117
+ * Headless table state hook with per-slice controlled/uncontrolled support.
118
+ *
119
+ * Each of the seven state slices independently supports controlled OR uncontrolled use:
120
+ * - Controlled: pass `{ controlled: true, value, onChange }` — hook yields to consumer, no internal state.
121
+ * - Uncontrolled: pass `{ controlled: false, initialValue? }` — hook owns state internally.
122
+ *
123
+ * @example
124
+ * ```tsx
125
+ * // Fully uncontrolled
126
+ * const tableState = useCphTableState({});
127
+ *
128
+ * // Controlled sorting + pagination, rest uncontrolled
129
+ * const tableState = useCphTableState({
130
+ * sorting: { controlled: true, value: sorting, onChange: setSorting },
131
+ * pagination: { controlled: true, value: pagination, onChange: setPagination },
132
+ * });
133
+ *
134
+ * // Controlled expansion with single-row enforcement
135
+ * const tableState = useCphTableState({
136
+ * expanded: { controlled: true, value: expanded, onChange: setExpanded },
137
+ * });
138
+ * ```
139
+ */
140
+ export function useCphTableState(options: UseCphTableStateOptions = {}): UseCphTableStateReturn {
141
+ const {
142
+ sorting,
143
+ columnFilters,
144
+ columnVisibility,
145
+ columnPinning,
146
+ rowSelection,
147
+ expanded,
148
+ pagination,
149
+ } = options;
150
+
151
+ // Uncontrolled internal state (only used when slice is uncontrolled)
152
+ const [_internalSorting, setInternalSorting] = useState<SortingState>(() =>
153
+ sorting?.controlled === false ? (sorting.initialValue ?? []) : [],
154
+ );
155
+ const [_internalColumnFilters, setInternalColumnFilters] = useState<ColumnFiltersState>(() =>
156
+ columnFilters?.controlled === false ? (columnFilters.initialValue ?? []) : [],
157
+ );
158
+ const [_internalColumnVisibility, setInternalColumnVisibility] = useState<ColumnVisibilityState>(
159
+ () => (columnVisibility?.controlled === false ? (columnVisibility.initialValue ?? {}) : {}),
160
+ );
161
+ const [_internalColumnPinning, setInternalColumnPinning] = useState<ColumnPinningState>(() =>
162
+ columnPinning?.controlled === false
163
+ ? (columnPinning.initialValue ?? { start: [], end: [] })
164
+ : { start: [], end: [] },
165
+ );
166
+ const [_internalRowSelection, setInternalRowSelection] = useState<RowSelectionState>(() =>
167
+ rowSelection?.controlled === false ? (rowSelection.initialValue ?? {}) : {},
168
+ );
169
+ const [_internalExpanded, setInternalExpanded] = useState<ExpandedState>(() =>
170
+ expanded?.controlled === false ? (expanded.initialValue ?? {}) : {},
171
+ );
172
+ const [_internalPagination, setInternalPagination] = useState<PaginationState>(() =>
173
+ pagination?.controlled === false
174
+ ? (pagination.initialValue ?? { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE })
175
+ : { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE },
176
+ );
177
+
178
+ // Resolve each slice: controlled value, consumer handler, initial value
179
+ const [controlledSorting, consumerOnSortingChange, initialSorting] = resolveSlice(sorting, []);
180
+
181
+ const [controlledColumnFilters, consumerOnColumnFiltersChange, initialColumnFilters] =
182
+ resolveSlice(columnFilters, []);
183
+
184
+ const [controlledColumnVisibility, consumerOnColumnVisibilityChange, initialColumnVisibility] =
185
+ resolveSlice(columnVisibility, {});
186
+
187
+ const [controlledColumnPinning, consumerOnColumnPinningChange, initialColumnPinning] =
188
+ resolveSlice(columnPinning, { start: [], end: [] });
189
+
190
+ const [controlledRowSelection, consumerOnRowSelectionChange, initialRowSelection] = resolveSlice(
191
+ rowSelection,
192
+ {},
193
+ );
194
+
195
+ const [controlledExpanded, consumerOnExpandedChange, initialExpanded] = resolveSlice(
196
+ expanded,
197
+ {},
198
+ );
199
+
200
+ const [controlledPagination, consumerOnPaginationChange, initialPagination] = resolveSlice(
201
+ pagination,
202
+ { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE },
203
+ );
204
+
205
+ // Internal setters for uncontrolled slices (stable callbacks)
206
+ const internalOnSortingChange = (updaterOrValue: Updater<SortingState>) => {
207
+ setInternalSorting((current: SortingState) =>
208
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
209
+ );
210
+ };
211
+
212
+ const internalOnColumnFiltersChange = (updaterOrValue: Updater<ColumnFiltersState>) => {
213
+ setInternalColumnFilters((current: ColumnFiltersState) =>
214
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
215
+ );
216
+ };
217
+
218
+ const internalOnColumnVisibilityChange = (updaterOrValue: Updater<ColumnVisibilityState>) => {
219
+ setInternalColumnVisibility((current: ColumnVisibilityState) =>
220
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
221
+ );
222
+ };
223
+
224
+ const internalOnColumnPinningChange = (updaterOrValue: Updater<ColumnPinningState>) => {
225
+ setInternalColumnPinning((current: ColumnPinningState) =>
226
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
227
+ );
228
+ };
229
+
230
+ const internalOnRowSelectionChange = (updaterOrValue: Updater<RowSelectionState>) => {
231
+ setInternalRowSelection((current: RowSelectionState) =>
232
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
233
+ );
234
+ };
235
+
236
+ // Internal setter for expansion with single-row enforcement
237
+ const internalOnExpandedChange = (updaterOrValue: Updater<ExpandedState>) => {
238
+ setInternalExpanded((current: ExpandedState) => {
239
+ const next = typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue;
240
+
241
+ // `true` means "expand all rows" in TanStack — normalize to collapsed so
242
+ // the single-row guarantee holds no matter how the update arrives.
243
+ if (next === true) return {};
244
+ if (typeof next === "object" && next !== null) {
245
+ const record = next as Record<string, boolean>;
246
+ const keys = Object.keys(record);
247
+ const trueKeys = keys.filter((k) => record[k]);
248
+ if (trueKeys.length > 1) {
249
+ const lastKey = trueKeys[trueKeys.length - 1];
250
+ if (lastKey !== undefined) {
251
+ const singleExpansion: Record<string, boolean> = {};
252
+ singleExpansion[lastKey] = true;
253
+ return singleExpansion;
254
+ }
255
+ }
256
+ }
257
+ return next;
258
+ });
259
+ };
260
+
261
+ const internalOnPaginationChange = (updaterOrValue: Updater<PaginationState>) => {
262
+ setInternalPagination((current) =>
263
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
264
+ );
265
+ };
266
+
267
+ // Build the return object with conditional spreads to satisfy exactOptionalPropertyTypes.
268
+ // Controlled slices expose consumer values; uncontrolled slices expose the
269
+ // hook-owned values so updates through the internal handlers are visible
270
+ // to TanStack on the next render.
271
+ const state = {
272
+ ...(sorting?.controlled === true && controlledSorting !== undefined
273
+ ? { sorting: controlledSorting }
274
+ : {}),
275
+ ...(sorting?.controlled === false || sorting === undefined
276
+ ? { sorting: _internalSorting }
277
+ : {}),
278
+ ...(columnFilters?.controlled === true && controlledColumnFilters !== undefined
279
+ ? { columnFilters: controlledColumnFilters }
280
+ : {}),
281
+ ...(columnFilters?.controlled === false || columnFilters === undefined
282
+ ? { columnFilters: _internalColumnFilters }
283
+ : {}),
284
+ ...(columnVisibility?.controlled === true && controlledColumnVisibility !== undefined
285
+ ? { columnVisibility: controlledColumnVisibility }
286
+ : {}),
287
+ ...(columnVisibility?.controlled === false || columnVisibility === undefined
288
+ ? { columnVisibility: _internalColumnVisibility }
289
+ : {}),
290
+ ...(columnPinning?.controlled === true && controlledColumnPinning !== undefined
291
+ ? { columnPinning: controlledColumnPinning }
292
+ : {}),
293
+ ...(columnPinning?.controlled === false || columnPinning === undefined
294
+ ? { columnPinning: _internalColumnPinning }
295
+ : {}),
296
+ ...(rowSelection?.controlled === true && controlledRowSelection !== undefined
297
+ ? { rowSelection: controlledRowSelection }
298
+ : {}),
299
+ ...(rowSelection?.controlled === false || rowSelection === undefined
300
+ ? { rowSelection: _internalRowSelection }
301
+ : {}),
302
+ ...(expanded?.controlled === true && controlledExpanded !== undefined
303
+ ? { expanded: controlledExpanded }
304
+ : {}),
305
+ ...(expanded?.controlled === false || expanded === undefined
306
+ ? { expanded: _internalExpanded }
307
+ : {}),
308
+ ...(pagination?.controlled === true && controlledPagination !== undefined
309
+ ? { pagination: controlledPagination }
310
+ : {}),
311
+ ...(pagination?.controlled === false || pagination === undefined
312
+ ? { pagination: _internalPagination }
313
+ : {}),
314
+ };
315
+
316
+ const initialState = {
317
+ ...(initialSorting !== undefined ? { sorting: initialSorting } : {}),
318
+ ...(initialColumnFilters !== undefined ? { columnFilters: initialColumnFilters } : {}),
319
+ ...(initialColumnVisibility !== undefined ? { columnVisibility: initialColumnVisibility } : {}),
320
+ ...(initialColumnPinning !== undefined ? { columnPinning: initialColumnPinning } : {}),
321
+ ...(initialRowSelection !== undefined ? { rowSelection: initialRowSelection } : {}),
322
+ ...(initialExpanded !== undefined ? { expanded: initialExpanded } : {}),
323
+ ...(initialPagination !== undefined ? { pagination: initialPagination } : {}),
324
+ };
325
+
326
+ return {
327
+ state,
328
+ ...(sorting?.controlled === true && consumerOnSortingChange !== undefined
329
+ ? { onSortingChange: consumerOnSortingChange }
330
+ : { onSortingChange: internalOnSortingChange }),
331
+ ...(columnFilters?.controlled === true && consumerOnColumnFiltersChange !== undefined
332
+ ? { onColumnFiltersChange: consumerOnColumnFiltersChange }
333
+ : { onColumnFiltersChange: internalOnColumnFiltersChange }),
334
+ ...(columnVisibility?.controlled === true && consumerOnColumnVisibilityChange !== undefined
335
+ ? { onColumnVisibilityChange: consumerOnColumnVisibilityChange }
336
+ : { onColumnVisibilityChange: internalOnColumnVisibilityChange }),
337
+ ...(columnPinning?.controlled === true && consumerOnColumnPinningChange !== undefined
338
+ ? { onColumnPinningChange: consumerOnColumnPinningChange }
339
+ : { onColumnPinningChange: internalOnColumnPinningChange }),
340
+ ...(rowSelection?.controlled === true && consumerOnRowSelectionChange !== undefined
341
+ ? { onRowSelectionChange: consumerOnRowSelectionChange }
342
+ : { onRowSelectionChange: internalOnRowSelectionChange }),
343
+ ...(expanded?.controlled === true && consumerOnExpandedChange !== undefined
344
+ ? { onExpandedChange: consumerOnExpandedChange }
345
+ : { onExpandedChange: internalOnExpandedChange }),
346
+ ...(pagination?.controlled === true && consumerOnPaginationChange !== undefined
347
+ ? { onPaginationChange: consumerOnPaginationChange }
348
+ : { onPaginationChange: internalOnPaginationChange }),
349
+ initialState,
350
+ };
351
+ }
352
+
353
+ /**
354
+ * Dedicated hook for row selection state.
355
+ *
356
+ * Same controlled/uncontrolled contract as {@link useCphTableState}.
357
+ *
358
+ * @example
359
+ * ```tsx
360
+ * // Uncontrolled
361
+ * const { state, onRowSelectionChange, initialState } = useCphRowSelection({});
362
+ *
363
+ * // Controlled
364
+ * const { state, onRowSelectionChange } = useCphRowSelection({
365
+ * controlled: true,
366
+ * value: selection,
367
+ * onChange: setSelection,
368
+ * });
369
+ * ```
370
+ */
371
+ export interface UseCphRowSelectionOptions {
372
+ readonly controlled?: boolean;
373
+ readonly value?: RowSelectionState;
374
+ readonly onChange?: OnChangeFn<RowSelectionState>;
375
+ readonly initialValue?: RowSelectionState;
376
+ }
377
+
378
+ export interface UseCphRowSelectionReturn {
379
+ readonly state: { readonly rowSelection: RowSelectionState } | {};
380
+ readonly onRowSelectionChange?: OnChangeFn<RowSelectionState>;
381
+ readonly initialState: { readonly rowSelection: RowSelectionState } | {};
382
+ }
383
+
384
+ export function useCphRowSelection(
385
+ options: UseCphRowSelectionOptions = {},
386
+ ): UseCphRowSelectionReturn {
387
+ const { controlled = false, value, onChange, initialValue = {} } = options;
388
+
389
+ const [internalSelection, setInternalSelection] = useState<RowSelectionState>(initialValue);
390
+ const internalOnRowSelectionChange = (updaterOrValue: Updater<RowSelectionState>) => {
391
+ setInternalSelection((current: RowSelectionState) =>
392
+ typeof updaterOrValue === "function" ? updaterOrValue(current) : updaterOrValue,
393
+ );
394
+ };
395
+
396
+ if (controlled) {
397
+ if (value === undefined || onChange === undefined) {
398
+ throw new Error("useCphRowSelection: controlled mode requires both `value` and `onChange`");
399
+ }
400
+ return {
401
+ state: { rowSelection: value },
402
+ onRowSelectionChange: onChange,
403
+ initialState: {},
404
+ };
405
+ }
406
+
407
+ return {
408
+ state: { rowSelection: internalSelection },
409
+ onRowSelectionChange: internalOnRowSelectionChange,
410
+ initialState: { rowSelection: internalSelection },
411
+ };
412
+ }
@@ -0,0 +1,48 @@
1
+ /* @nickrobison/colophon-table — table-specific design tokens.
2
+ Every value derives from a core `--cph-*` token; no literal colours live here.
3
+ Density blocks MUST come after the base block: later wins at equal specificity,
4
+ so reversing this order silently breaks the Comfortable/Dense row heights. */
5
+
6
+ :root {
7
+ /* Geometry — Compact is the Tables Spec default. */
8
+ --cph-table-row-h: 36px;
9
+ --cph-table-cell-y: 8px;
10
+ --cph-table-head-h: 44px;
11
+ --cph-table-summary-h: 3.25rem;
12
+ --cph-table-min-w: 1170px;
13
+ --cph-table-scroll-max-h: 36rem;
14
+
15
+ /* Type — the spec floor is 12px; density never shrinks type to buy rows. */
16
+ --cph-table-type: 12px;
17
+ --cph-table-leading: 19px;
18
+
19
+ /* Motion */
20
+ --cph-table-pulse: 1400ms;
21
+
22
+ /* State tints. Row cells read --cph-table-row-bg so the tint shows through
23
+ the pinned column instead of being painted over by a static surface. */
24
+ --cph-table-hover: color-mix(in srgb, var(--cph-ink) 3%, var(--cph-paper));
25
+ --cph-table-selected: color-mix(in srgb, var(--cph-orange) 9%, var(--cph-paper));
26
+ --cph-table-accent-text: var(--cph-orange-dark);
27
+
28
+ /* Stacking order for the pinned-column / sticky-footer sandwich. */
29
+ --cph-table-z-pinned: 2;
30
+ --cph-table-z-pinned-head: 3;
31
+ --cph-table-z-summary: 4;
32
+ --cph-table-z-summary-pinned: 5;
33
+ }
34
+
35
+ [data-density="comfortable"] {
36
+ --cph-table-row-h: 44px;
37
+ --cph-table-cell-y: 12px;
38
+ }
39
+
40
+ [data-density="dense"] {
41
+ --cph-table-row-h: 28px;
42
+ --cph-table-cell-y: 4px;
43
+ }
44
+
45
+ [data-theme="dark"] {
46
+ /* Dark mode inverts the accent ramp: orange-dark is unreadable on a dark surface. */
47
+ --cph-table-accent-text: var(--cph-orange);
48
+ }
@@ -0,0 +1,53 @@
1
+ import { render, screen } from "@testing-library/react";
2
+
3
+ import { CphTableDensityProvider } from "./CphTableDensity";
4
+ import { resolveCphTableDensity, useCphTableDensity } from "./cphTableDensityContext";
5
+
6
+ function DensityProbe() {
7
+ return <span data-testid="density-probe">{useCphTableDensity()}</span>;
8
+ }
9
+
10
+ describe("resolveCphTableDensity", () => {
11
+ it("explicit density wins over context", () => {
12
+ expect(resolveCphTableDensity("dense", "comfortable")).toBe("dense");
13
+ });
14
+
15
+ it("falls back to context density when explicit is undefined", () => {
16
+ expect(resolveCphTableDensity(undefined, "comfortable")).toBe("comfortable");
17
+ });
18
+
19
+ it("falls back to compact spec default when both are undefined", () => {
20
+ expect(resolveCphTableDensity(undefined, undefined)).toBe("compact");
21
+ });
22
+ });
23
+
24
+ describe("CphTableDensityProvider", () => {
25
+ it("defaults to compact without an explicit density", () => {
26
+ render(
27
+ <CphTableDensityProvider>
28
+ <DensityProbe />
29
+ </CphTableDensityProvider>,
30
+ );
31
+ expect(screen.getByTestId("density-probe")).toHaveTextContent("compact");
32
+ });
33
+
34
+ it("provides an explicit density", () => {
35
+ render(
36
+ <CphTableDensityProvider density="dense">
37
+ <DensityProbe />
38
+ </CphTableDensityProvider>,
39
+ );
40
+ expect(screen.getByTestId("density-probe")).toHaveTextContent("dense");
41
+ });
42
+
43
+ it("nested providers inherit from the nearest table provider", () => {
44
+ render(
45
+ <CphTableDensityProvider density="dense">
46
+ <CphTableDensityProvider>
47
+ <DensityProbe />
48
+ </CphTableDensityProvider>
49
+ </CphTableDensityProvider>,
50
+ );
51
+ expect(screen.getByTestId("density-probe")).toHaveTextContent("dense");
52
+ });
53
+ });
@@ -0,0 +1,24 @@
1
+ import type { CphDensity } from "@nickrobison/colophon";
2
+ import { useContext, type ReactNode } from "react";
3
+
4
+ import { CphTableDensityContext, resolveCphTableDensity } from "./cphTableDensityContext";
5
+
6
+ /**
7
+ * Table density provider. A provider without an explicit `density` resolves
8
+ * to the compact spec default — core provider density is deliberately NOT
9
+ * inherited, so tables stay dense even inside comfortable layouts. Nested
10
+ * table providers inherit from the nearest table provider ancestor.
11
+ */
12
+ export function CphTableDensityProvider({
13
+ children,
14
+ density,
15
+ }: Readonly<{
16
+ children: ReactNode;
17
+ density?: CphDensity;
18
+ }>) {
19
+ const parent = useContext(CphTableDensityContext);
20
+ const resolved = resolveCphTableDensity(density, parent);
21
+ return (
22
+ <CphTableDensityContext.Provider value={resolved}>{children}</CphTableDensityContext.Provider>
23
+ );
24
+ }
@@ -0,0 +1,24 @@
1
+ import type { CphDensity } from "@nickrobison/colophon";
2
+ import { createContext, useContext } from "react";
3
+
4
+ /**
5
+ * Resolves the effective table density.
6
+ *
7
+ * The fallback is "compact" — not the core provider's "comfortable" default —
8
+ * because the Confluence Tables Spec makes Compact the default density for
9
+ * tables. That mismatch is deliberate: tables are dense, comparison-focused
10
+ * ledgers, so they should not inherit the comfortable spacing used by form
11
+ * fields and cards.
12
+ */
13
+ export function resolveCphTableDensity(
14
+ explicit?: CphDensity,
15
+ contextDensity?: CphDensity,
16
+ ): CphDensity {
17
+ return explicit ?? contextDensity ?? "compact";
18
+ }
19
+
20
+ export const CphTableDensityContext = createContext<CphDensity | undefined>(undefined);
21
+
22
+ export function useCphTableDensity(): CphDensity {
23
+ return useContext(CphTableDensityContext) ?? "compact";
24
+ }
@@ -0,0 +1,26 @@
1
+ import { composeClassName } from "./composeClassName";
2
+
3
+ const fn = () => "extra";
4
+
5
+ describe("composeClassName", () => {
6
+ it("returns base when incoming is undefined", () => {
7
+ expect(composeClassName("base", undefined)).toBe("base");
8
+ });
9
+
10
+ it("concatenates base and string incoming", () => {
11
+ expect(composeClassName("base", "extra")).toBe("base extra");
12
+ });
13
+
14
+ it("returns a function when incoming is a function", () => {
15
+ const result = composeClassName("base", fn);
16
+ expect(typeof result).toBe("function");
17
+ });
18
+
19
+ it("invoking the returned function returns a string starting with base", () => {
20
+ const result = composeClassName("base", fn);
21
+ const invoked = (result as (values: { defaultClassName: string | undefined }) => string)({
22
+ defaultClassName: undefined,
23
+ });
24
+ expect(invoked).toMatch(/^base /);
25
+ });
26
+ });
@@ -0,0 +1,28 @@
1
+ import type { ClassNameOrFunction } from "react-aria-components";
2
+
3
+ /**
4
+ * Composes a base class name with an incoming `className` value that may be
5
+ * a plain string, a React Aria render-props callback, or `undefined`.
6
+ *
7
+ * React Aria components accept `className` as either a string or a function
8
+ * `(renderProps) => string`. When a function is interpolated into a template
9
+ * literal it is stringified to its source text (e.g. `"cph-field function(){…}"`),
10
+ * which is the bug this helper prevents.
11
+ *
12
+ * - `undefined` → returns the base class unchanged.
13
+ * - string → returns `"base incoming"`.
14
+ * - function → returns a new function that calls the incoming callback with the
15
+ * render props and prepends the base class to the result.
16
+ */
17
+ export function composeClassName<T>(
18
+ base: string,
19
+ incoming: ClassNameOrFunction<T> | undefined,
20
+ ): ClassNameOrFunction<T> {
21
+ if (incoming === undefined) {
22
+ return base;
23
+ }
24
+ if (typeof incoming === "string") {
25
+ return `${base} ${incoming}`;
26
+ }
27
+ return (values: T & { defaultClassName: string | undefined }) => `${base} ${incoming(values)}`;
28
+ }