@faast-app/ui 0.0.0-stage → 0.7.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 (173) hide show
  1. package/README.md +13 -2
  2. package/dist/components/Alert.d.ts +30 -0
  3. package/dist/components/Alert.js +19 -0
  4. package/dist/components/AppShell.d.ts +63 -0
  5. package/dist/components/AppShell.js +80 -0
  6. package/dist/components/Avatar.d.ts +18 -0
  7. package/dist/components/Avatar.js +21 -0
  8. package/dist/components/Badge.d.ts +24 -0
  9. package/dist/components/Badge.js +10 -0
  10. package/dist/components/Button.d.ts +71 -0
  11. package/dist/components/Button.js +40 -0
  12. package/dist/components/Card.d.ts +23 -0
  13. package/dist/components/Card.js +17 -0
  14. package/dist/components/Chip.d.ts +16 -0
  15. package/dist/components/Chip.js +9 -0
  16. package/dist/components/Choice.d.ts +38 -0
  17. package/dist/components/Choice.js +53 -0
  18. package/dist/components/Dialog.d.ts +44 -0
  19. package/dist/components/Dialog.js +51 -0
  20. package/dist/components/EmptyState.d.ts +16 -0
  21. package/dist/components/EmptyState.js +8 -0
  22. package/dist/components/FileDropArea.d.ts +62 -0
  23. package/dist/components/FileDropArea.js +90 -0
  24. package/dist/components/FormField.d.ts +35 -0
  25. package/dist/components/FormField.js +32 -0
  26. package/dist/components/HelpMenu.d.ts +46 -0
  27. package/dist/components/HelpMenu.js +104 -0
  28. package/dist/components/Icon.d.ts +24 -0
  29. package/dist/components/Icon.js +195 -0
  30. package/dist/components/Input.d.ts +37 -0
  31. package/dist/components/Input.js +31 -0
  32. package/dist/components/KpiStrip.d.ts +32 -0
  33. package/dist/components/KpiStrip.js +14 -0
  34. package/dist/components/LockScreen.d.ts +34 -0
  35. package/dist/components/LockScreen.js +22 -0
  36. package/dist/components/Logo.d.ts +27 -0
  37. package/dist/components/Logo.js +12 -0
  38. package/dist/components/Navigation.d.ts +37 -0
  39. package/dist/components/Navigation.js +29 -0
  40. package/dist/components/OptionList.d.ts +27 -0
  41. package/dist/components/OptionList.js +18 -0
  42. package/dist/components/OtpInput.d.ts +31 -0
  43. package/dist/components/OtpInput.js +102 -0
  44. package/dist/components/Pagination.d.ts +31 -0
  45. package/dist/components/Pagination.js +41 -0
  46. package/dist/components/PasswordField.d.ts +32 -0
  47. package/dist/components/PasswordField.js +29 -0
  48. package/dist/components/SegmentedControl.d.ts +35 -0
  49. package/dist/components/SegmentedControl.js +52 -0
  50. package/dist/components/Select.d.ts +15 -0
  51. package/dist/components/Select.js +10 -0
  52. package/dist/components/SessionTimeoutDialog.d.ts +39 -0
  53. package/dist/components/SessionTimeoutDialog.js +62 -0
  54. package/dist/components/Spinner.d.ts +25 -0
  55. package/dist/components/Spinner.js +15 -0
  56. package/dist/components/StatTiles.d.ts +35 -0
  57. package/dist/components/StatTiles.js +12 -0
  58. package/dist/components/Stepper.d.ts +26 -0
  59. package/dist/components/Stepper.js +26 -0
  60. package/dist/components/StickyActionBar.d.ts +28 -0
  61. package/dist/components/StickyActionBar.js +12 -0
  62. package/dist/components/Table.d.ts +69 -0
  63. package/dist/components/Table.js +73 -0
  64. package/dist/components/TenantMark.d.ts +27 -0
  65. package/dist/components/TenantMark.js +15 -0
  66. package/dist/components/Timeline.d.ts +31 -0
  67. package/dist/components/Timeline.js +18 -0
  68. package/dist/components/Toast.d.ts +43 -0
  69. package/dist/components/Toast.js +46 -0
  70. package/dist/components/Toggletip.d.ts +26 -0
  71. package/dist/components/Toggletip.js +47 -0
  72. package/dist/components/Tooltip.d.ts +24 -0
  73. package/dist/components/Tooltip.js +73 -0
  74. package/dist/components/Tour.d.ts +66 -0
  75. package/dist/components/Tour.js +161 -0
  76. package/dist/components/TourOffer.d.ts +22 -0
  77. package/dist/components/TourOffer.js +33 -0
  78. package/dist/components/WelcomeDialog.d.ts +45 -0
  79. package/dist/components/WelcomeDialog.js +38 -0
  80. package/dist/data-table/components/ClientDataTable.d.ts +22 -0
  81. package/dist/data-table/components/ClientDataTable.js +29 -0
  82. package/dist/data-table/components/DataTableFooter.d.ts +11 -0
  83. package/dist/data-table/components/DataTableFooter.js +16 -0
  84. package/dist/data-table/components/DataTableView.d.ts +9 -0
  85. package/dist/data-table/components/DataTableView.js +95 -0
  86. package/dist/data-table/components/ServerDataTable.d.ts +16 -0
  87. package/dist/data-table/components/ServerDataTable.js +17 -0
  88. package/dist/data-table/core/columnVisibility.d.ts +15 -0
  89. package/dist/data-table/core/columnVisibility.js +34 -0
  90. package/dist/data-table/core/constants.d.ts +20 -0
  91. package/dist/data-table/core/constants.js +21 -0
  92. package/dist/data-table/core/filters/registry.d.ts +33 -0
  93. package/dist/data-table/core/filters/registry.js +393 -0
  94. package/dist/data-table/core/filters/types.d.ts +88 -0
  95. package/dist/data-table/core/filters/types.js +1 -0
  96. package/dist/data-table/core/group.d.ts +15 -0
  97. package/dist/data-table/core/group.js +73 -0
  98. package/dist/data-table/core/paginate.d.ts +13 -0
  99. package/dist/data-table/core/paginate.js +27 -0
  100. package/dist/data-table/core/query.d.ts +65 -0
  101. package/dist/data-table/core/query.js +138 -0
  102. package/dist/data-table/core/search.d.ts +10 -0
  103. package/dist/data-table/core/search.js +35 -0
  104. package/dist/data-table/core/selection.d.ts +24 -0
  105. package/dist/data-table/core/selection.js +59 -0
  106. package/dist/data-table/core/sort.d.ts +14 -0
  107. package/dist/data-table/core/sort.js +41 -0
  108. package/dist/data-table/core/text.d.ts +5 -0
  109. package/dist/data-table/core/text.js +27 -0
  110. package/dist/data-table/core/types.d.ts +40 -0
  111. package/dist/data-table/core/types.js +1 -0
  112. package/dist/data-table/core/url.d.ts +43 -0
  113. package/dist/data-table/core/url.js +194 -0
  114. package/dist/data-table/hooks/common.d.ts +14 -0
  115. package/dist/data-table/hooks/common.js +30 -0
  116. package/dist/data-table/hooks/small-hooks.d.ts +12 -0
  117. package/dist/data-table/hooks/small-hooks.js +111 -0
  118. package/dist/data-table/hooks/useClientDataTable.d.ts +35 -0
  119. package/dist/data-table/hooks/useClientDataTable.js +311 -0
  120. package/dist/data-table/hooks/useServerDataTable.d.ts +38 -0
  121. package/dist/data-table/hooks/useServerDataTable.js +209 -0
  122. package/dist/data-table/hooks/useServerRows.d.ts +27 -0
  123. package/dist/data-table/hooks/useServerRows.js +82 -0
  124. package/dist/data-table/hooks/useTableState.d.ts +39 -0
  125. package/dist/data-table/hooks/useTableState.js +219 -0
  126. package/dist/data-table/index.d.ts +42 -0
  127. package/dist/data-table/index.js +22 -0
  128. package/dist/data-table/labels.d.ts +77 -0
  129. package/dist/data-table/labels.js +77 -0
  130. package/dist/data-table/toolbar/ActiveFilters.d.ts +10 -0
  131. package/dist/data-table/toolbar/ActiveFilters.js +53 -0
  132. package/dist/data-table/toolbar/BulkActionBar.d.ts +15 -0
  133. package/dist/data-table/toolbar/BulkActionBar.js +22 -0
  134. package/dist/data-table/toolbar/ColumnVisibilityDialog.d.ts +7 -0
  135. package/dist/data-table/toolbar/ColumnVisibilityDialog.js +15 -0
  136. package/dist/data-table/toolbar/DataTableToolbar.d.ts +19 -0
  137. package/dist/data-table/toolbar/DataTableToolbar.js +12 -0
  138. package/dist/data-table/toolbar/FilterFields.d.ts +11 -0
  139. package/dist/data-table/toolbar/FilterFields.js +58 -0
  140. package/dist/data-table/toolbar/FiltersControl.d.ts +34 -0
  141. package/dist/data-table/toolbar/FiltersControl.js +70 -0
  142. package/dist/data-table/toolbar/PageSizeSelect.d.ts +7 -0
  143. package/dist/data-table/toolbar/PageSizeSelect.js +12 -0
  144. package/dist/data-table/toolbar/QuickFilters.d.ts +7 -0
  145. package/dist/data-table/toolbar/QuickFilters.js +9 -0
  146. package/dist/data-table/toolbar/ResultsCount.d.ts +7 -0
  147. package/dist/data-table/toolbar/ResultsCount.js +9 -0
  148. package/dist/data-table/toolbar/SearchField.d.ts +36 -0
  149. package/dist/data-table/toolbar/SearchField.js +64 -0
  150. package/dist/data-table/toolbar/SortSelect.d.ts +10 -0
  151. package/dist/data-table/toolbar/SortSelect.js +30 -0
  152. package/dist/data-table/types.d.ts +334 -0
  153. package/dist/data-table/types.js +1 -0
  154. package/dist/index.d.ts +91 -0
  155. package/dist/index.js +49 -0
  156. package/dist/styles.css +4668 -0
  157. package/dist/theme/ThemeProvider.d.ts +33 -0
  158. package/dist/theme/ThemeProvider.js +77 -0
  159. package/dist/theme/ThemeToggle.d.ts +16 -0
  160. package/dist/theme/ThemeToggle.js +27 -0
  161. package/dist/utils/cx.d.ts +2 -0
  162. package/dist/utils/cx.js +4 -0
  163. package/dist/utils/devWarn.d.ts +2 -0
  164. package/dist/utils/devWarn.js +10 -0
  165. package/dist/utils/tourGeometry.d.ts +61 -0
  166. package/dist/utils/tourGeometry.js +135 -0
  167. package/dist/utils/useFloatingPosition.d.ts +9 -0
  168. package/dist/utils/useFloatingPosition.js +45 -0
  169. package/dist/utils/useModalDialog.d.ts +7 -0
  170. package/dist/utils/useModalDialog.js +49 -0
  171. package/dist/utils/useTourAnchorRect.d.ts +6 -0
  172. package/dist/utils/useTourAnchorRect.js +40 -0
  173. package/package.json +50 -4
@@ -0,0 +1,194 @@
1
+ import { ALL_QUICK_FILTER, ID_PATTERN, MAX_SEARCH_LENGTH, MAX_URL_PARAMS_READ, MAX_URL_VALUE_LENGTH, PAGE_SIZE_OPTIONS, } from './constants.js';
2
+ import { isFilterValueEmpty, normalizeFilterValue, parseFilterValue, serializeFilterValue, validateFilterValue, } from './filters/registry.js';
3
+ import { emptyFilterValues, sortEquals } from './query.js';
4
+ import { sanitizeSearch } from './text.js';
5
+ const PAGE_PATTERN = /^[1-9]\d{0,4}$/;
6
+ const SORT_PATTERN = /^([a-z][a-z0-9_-]{0,31})\.(asc|desc)$/;
7
+ const NONE = 'none';
8
+ /** Query parameter names this schema owns (all of them, whether or not they are in the URL). */
9
+ export function tableUrlKeys(schema) {
10
+ const p = schema.prefix ?? '';
11
+ const keys = ['q', 'page', 'size', 'sort', 'group', 'qf'].map((k) => p + k);
12
+ for (const def of validFilters(schema))
13
+ keys.push(`${p}f.${def.id}`);
14
+ return keys;
15
+ }
16
+ function validFilters(schema) {
17
+ return schema.filters.filter((d) => ID_PATTERN.test(d.id)).slice(0, MAX_URL_PARAMS_READ - 6);
18
+ }
19
+ function sizeOptions(schema) {
20
+ const allowed = schema.pageSizeOptions?.filter((s) => PAGE_SIZE_OPTIONS.includes(s));
21
+ return allowed && allowed.length > 0 ? allowed : PAGE_SIZE_OPTIONS;
22
+ }
23
+ function groupDefault(schema) {
24
+ return schema.defaults.groupBy;
25
+ }
26
+ /**
27
+ * Wire form of a query. Defaults are omitted and keys are sorted so URLs are stable. Only
28
+ * declared, valid values are written.
29
+ */
30
+ export function serializeTableQuery(query, schema) {
31
+ const p = schema.prefix ?? '';
32
+ const d = schema.defaults;
33
+ const entries = [];
34
+ const search = sanitizeSearch(query.search);
35
+ if (search !== d.search && search !== '')
36
+ entries.push([`${p}q`, search]);
37
+ if (Number.isInteger(query.page) && query.page > 1 && query.page <= 99999) {
38
+ entries.push([`${p}page`, String(query.page)]);
39
+ }
40
+ if (query.pageSize !== d.pageSize && sizeOptions(schema).includes(query.pageSize)) {
41
+ entries.push([`${p}size`, String(query.pageSize)]);
42
+ }
43
+ if (query.sort &&
44
+ !sortEquals(query.sort, d.sort) &&
45
+ schema.columns.some((c) => c.id === query.sort?.columnId)) {
46
+ entries.push([
47
+ `${p}sort`,
48
+ `${query.sort.columnId}.${query.sort.direction === 'ascending' ? 'asc' : 'desc'}`,
49
+ ]);
50
+ }
51
+ else if (!query.sort && d.sort) {
52
+ // Cannot express "no sort" when a default exists; the default applies.
53
+ }
54
+ const group = query.groupBy;
55
+ const locked = schema.groups?.find((g) => g.locked);
56
+ if (group !== groupDefault(schema) && !(locked && group === locked.id)) {
57
+ if (group === null)
58
+ entries.push([`${p}group`, NONE]);
59
+ else if (schema.groups?.some((g) => g.id === group))
60
+ entries.push([`${p}group`, group]);
61
+ }
62
+ if (query.quickFilter !== d.quickFilter &&
63
+ query.quickFilter !== ALL_QUICK_FILTER &&
64
+ schema.quickFilters?.some((q) => q.id === query.quickFilter)) {
65
+ entries.push([`${p}qf`, query.quickFilter]);
66
+ }
67
+ for (const def of validFilters(schema)) {
68
+ const value = Object.prototype.hasOwnProperty.call(query.filters, def.id)
69
+ ? query.filters[def.id]
70
+ : undefined;
71
+ if (isFilterValueEmpty(def, value) || validateFilterValue(def, value))
72
+ continue;
73
+ const normalized = normalizeFilterValue(def, value);
74
+ const wire = serializeFilterValue(def, normalized);
75
+ if (wire.length > MAX_URL_VALUE_LENGTH)
76
+ continue;
77
+ entries.push([`${p}f.${def.id}`, wire]);
78
+ }
79
+ entries.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
80
+ const params = new URLSearchParams();
81
+ for (const [k, v] of entries)
82
+ params.append(k, v);
83
+ return params;
84
+ }
85
+ /**
86
+ * Reads a query from untrusted URL parameters. Never throws; invalid values are dropped
87
+ * (reported in `issues`) and defaults apply. Unknown parameters are ignored. See plan section 8.
88
+ */
89
+ export function parseTableQuery(params, schema) {
90
+ const issues = [];
91
+ const d = schema.defaults;
92
+ const p = schema.prefix ?? '';
93
+ const reject = (key, reason) => issues.push({ key, reason });
94
+ const read = (key) => {
95
+ let all;
96
+ try {
97
+ all = params.getAll(key);
98
+ }
99
+ catch {
100
+ return undefined;
101
+ }
102
+ if (all.length === 0)
103
+ return undefined;
104
+ if (all.length > 1)
105
+ reject(key, 'repeated');
106
+ const raw = all[0];
107
+ if (raw.length > MAX_URL_VALUE_LENGTH) {
108
+ reject(key, 'too-long');
109
+ return undefined;
110
+ }
111
+ return raw;
112
+ };
113
+ let search = d.search;
114
+ const rawQ = read(`${p}q`);
115
+ if (rawQ !== undefined)
116
+ search = sanitizeSearch(rawQ).slice(0, MAX_SEARCH_LENGTH);
117
+ let page = d.page;
118
+ const rawPage = read(`${p}page`);
119
+ if (rawPage !== undefined) {
120
+ if (PAGE_PATTERN.test(rawPage))
121
+ page = Number(rawPage);
122
+ else
123
+ reject(`${p}page`, 'invalid-page');
124
+ }
125
+ let pageSize = d.pageSize;
126
+ const rawSize = read(`${p}size`);
127
+ if (rawSize !== undefined) {
128
+ const n = /^\d{1,3}$/.test(rawSize) ? Number(rawSize) : NaN;
129
+ const match = sizeOptions(schema).find((s) => s === n);
130
+ if (match)
131
+ pageSize = match;
132
+ else
133
+ reject(`${p}size`, 'invalid-size');
134
+ }
135
+ let sort = d.sort;
136
+ const rawSort = read(`${p}sort`);
137
+ if (rawSort !== undefined) {
138
+ const m = SORT_PATTERN.exec(rawSort);
139
+ if (m && schema.columns.some((c) => c.id === m[1])) {
140
+ sort = { columnId: m[1], direction: m[2] === 'asc' ? 'ascending' : 'descending' };
141
+ }
142
+ else
143
+ reject(`${p}sort`, 'invalid-sort');
144
+ }
145
+ let groupBy = d.groupBy;
146
+ const rawGroup = read(`${p}group`);
147
+ if (rawGroup !== undefined) {
148
+ if (rawGroup === NONE)
149
+ groupBy = null;
150
+ else if (schema.groups?.some((g) => g.id === rawGroup))
151
+ groupBy = rawGroup;
152
+ else
153
+ reject(`${p}group`, 'invalid-group');
154
+ }
155
+ const locked = schema.groups?.find((g) => g.locked);
156
+ if (locked && groupBy === null)
157
+ groupBy = locked.id;
158
+ let quickFilter = d.quickFilter;
159
+ const rawQf = read(`${p}qf`);
160
+ if (rawQf !== undefined) {
161
+ if (schema.quickFilters?.some((q) => q.id === rawQf) && rawQf !== ALL_QUICK_FILTER) {
162
+ quickFilter = rawQf;
163
+ }
164
+ else
165
+ reject(`${p}qf`, 'invalid-quick-filter');
166
+ }
167
+ const filters = emptyFilterValues([...Object.entries(d.filters)]);
168
+ for (const def of validFilters(schema)) {
169
+ const key = `${p}f.${def.id}`;
170
+ const raw = read(key);
171
+ if (raw === undefined)
172
+ continue;
173
+ const value = parseFilterValue(def, raw);
174
+ if (value === undefined)
175
+ reject(key, 'invalid-filter');
176
+ else
177
+ filters[def.id] = value;
178
+ }
179
+ const query = { search, filters, quickFilter, sort, groupBy, page, pageSize };
180
+ // Canonical: the URL's own parameters are exactly the serialised valid query.
181
+ const expected = serializeTableQuery(query, schema);
182
+ let canonical = issues.length === 0;
183
+ if (canonical) {
184
+ for (const key of tableUrlKeys(schema)) {
185
+ const have = params.getAll(key);
186
+ const want = expected.getAll(key);
187
+ if (have.length !== want.length || have.some((v, i) => v !== want[i])) {
188
+ canonical = false;
189
+ break;
190
+ }
191
+ }
192
+ }
193
+ return { query, issues, canonical };
194
+ }
@@ -0,0 +1,14 @@
1
+ import type { ChipFormat } from '../core/filters/types.js';
2
+ import type { FilterValues, TableQuery } from '../core/types.js';
3
+ import type { DataTableLabels } from '../labels.js';
4
+ import type { FilterDef, GroupDef } from '../types.js';
5
+ export declare const EMPTY_LIST: readonly never[];
6
+ export declare const EMPTY_SET: ReadonlySet<string>;
7
+ export declare function chipFormat(labels: DataTableLabels): ChipFormat;
8
+ export declare function buildActiveChips<TRow>(defs: readonly FilterDef<TRow>[], values: FilterValues, labels: DataTableLabels): {
9
+ id: string;
10
+ label: string;
11
+ text: string;
12
+ }[];
13
+ export declare function hasActiveQuery(query: TableQuery, chipCount: number): boolean;
14
+ export declare function activeGroupOf<TRow>(groups: readonly GroupDef<TRow>[], query: TableQuery): GroupDef<TRow> | null;
@@ -0,0 +1,30 @@
1
+ import { activeFilters, filterChipText } from '../core/filters/registry.js';
2
+ import { ALL_QUICK_FILTER } from '../core/constants.js';
3
+ export const EMPTY_LIST = Object.freeze([]);
4
+ export const EMPTY_SET = new Set();
5
+ export function chipFormat(labels) {
6
+ return {
7
+ yes: labels.yes,
8
+ rangeFrom: labels.rangeFrom,
9
+ rangeTo: labels.rangeTo,
10
+ rangeBetween: labels.rangeBetween,
11
+ };
12
+ }
13
+ export function buildActiveChips(defs, values, labels) {
14
+ const format = chipFormat(labels);
15
+ return activeFilters(defs, values).map(({ def, value }) => {
16
+ const custom = def.formatChip;
17
+ const text = def.kind === 'numberRange' && custom
18
+ ? `${def.label}: ${custom(value)}`
19
+ : filterChipText(def, value, format);
20
+ return { id: def.id, label: def.label, text };
21
+ });
22
+ }
23
+ export function hasActiveQuery(query, chipCount) {
24
+ return query.search !== '' || chipCount > 0 || query.quickFilter !== ALL_QUICK_FILTER;
25
+ }
26
+ export function activeGroupOf(groups, query) {
27
+ const locked = groups.find((g) => g.locked);
28
+ const id = query.groupBy ?? locked?.id ?? null;
29
+ return id ? (groups.find((g) => g.id === id) ?? null) : null;
30
+ }
@@ -0,0 +1,12 @@
1
+ import type { ColumnDef, ColumnVisibilityApi, ColumnVisibilityOptions, ExpansionApi, TableA11y } from '../types.js';
2
+ /** SSR-safe `matchMedia` subscription. */
3
+ export declare function useMediaQuery(query: string): boolean;
4
+ /** One polite message per table plus the caption focus target. */
5
+ export declare function useTableA11y(): TableA11y;
6
+ /** Delays a "loading" condition so fast loads stay silent. */
7
+ export declare function useDelayedFlag(active: boolean, delayMs: number, onElapsed: () => void): void;
8
+ export declare function useExpansion<TRow>(enabled: boolean, mode: 'single' | 'multiple', _rows?: readonly TRow[]): ExpansionApi | null;
9
+ export declare function useColumnVisibility<TRow>(columns: readonly ColumnDef<TRow>[], options: ColumnVisibilityOptions | undefined): {
10
+ api: ColumnVisibilityApi;
11
+ visibleColumns: readonly ColumnDef<TRow>[];
12
+ };
@@ -0,0 +1,111 @@
1
+ import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
2
+ import { defaultHiddenColumns, isHideable, toggleColumnHidden } from '../core/columnVisibility.js';
3
+ /** SSR-safe `matchMedia` subscription. */
4
+ export function useMediaQuery(query) {
5
+ const subscribe = useCallback((notify) => {
6
+ if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
7
+ return () => { };
8
+ }
9
+ const list = window.matchMedia(query);
10
+ list.addEventListener('change', notify);
11
+ return () => list.removeEventListener('change', notify);
12
+ }, [query]);
13
+ const get = () => typeof window !== 'undefined' && typeof window.matchMedia === 'function'
14
+ ? window.matchMedia(query).matches
15
+ : false;
16
+ return useSyncExternalStore(subscribe, get, () => false);
17
+ }
18
+ /** One polite message per table plus the caption focus target. */
19
+ export function useTableA11y() {
20
+ const [liveMessage, setLiveMessage] = useState('');
21
+ const flip = useRef(false);
22
+ const caption = useRef(null);
23
+ const announce = useCallback((message) => {
24
+ // Alternate a trailing no-break space so an identical message is announced again.
25
+ flip.current = !flip.current;
26
+ setLiveMessage(flip.current ? message : message + String.fromCharCode(160));
27
+ }, []);
28
+ const registerTable = useCallback((element) => {
29
+ caption.current = element;
30
+ }, []);
31
+ const targets = useRef(new Map());
32
+ const registerTarget = useCallback((name, element) => {
33
+ if (element)
34
+ targets.current.set(name, element);
35
+ else
36
+ targets.current.delete(name);
37
+ }, []);
38
+ const focusTarget = useCallback((name) => {
39
+ const element = targets.current.get(name);
40
+ if (!element || !element.isConnected)
41
+ return false;
42
+ element.focus();
43
+ return true;
44
+ }, []);
45
+ const focusTable = useCallback(() => {
46
+ const element = caption.current;
47
+ if (!element)
48
+ return;
49
+ element.focus({ preventScroll: true });
50
+ const reduce = typeof window.matchMedia === 'function' &&
51
+ window.matchMedia('(prefers-reduced-motion: reduce)').matches;
52
+ element.scrollIntoView?.({ block: 'nearest', behavior: reduce ? 'auto' : 'smooth' });
53
+ }, []);
54
+ return useMemo(() => ({ registerTable, focusTable, registerTarget, focusTarget, liveMessage, announce }), [registerTable, focusTable, registerTarget, focusTarget, liveMessage, announce]);
55
+ }
56
+ /** Delays a "loading" condition so fast loads stay silent. */
57
+ export function useDelayedFlag(active, delayMs, onElapsed) {
58
+ const callback = useRef(onElapsed);
59
+ useEffect(() => {
60
+ callback.current = onElapsed;
61
+ });
62
+ useEffect(() => {
63
+ if (!active)
64
+ return;
65
+ const timer = setTimeout(() => callback.current(), delayMs);
66
+ return () => clearTimeout(timer);
67
+ }, [active, delayMs]);
68
+ }
69
+ export function useExpansion(enabled, mode, _rows) {
70
+ const [expanded, setExpanded] = useState(() => new Set());
71
+ const isExpanded = useCallback((id) => expanded.has(id), [expanded]);
72
+ const toggle = useCallback((id) => setExpanded((current) => {
73
+ const next = new Set(mode === 'single' ? [] : current);
74
+ if (current.has(id))
75
+ next.delete(id);
76
+ else
77
+ next.add(id);
78
+ return next;
79
+ }), [mode]);
80
+ const collapseAll = useCallback(() => setExpanded(new Set()), []);
81
+ return useMemo(() => (enabled ? { isExpanded, toggle, collapseAll } : null), [enabled, isExpanded, toggle, collapseAll]);
82
+ }
83
+ export function useColumnVisibility(columns, options) {
84
+ const [internal, setInternal] = useState(() => defaultHiddenColumns(columns));
85
+ const hidden = options?.hidden ?? internal;
86
+ const onChange = options?.onHiddenChange;
87
+ const controlled = options?.hidden !== undefined;
88
+ const write = useCallback((next) => {
89
+ if (!controlled)
90
+ setInternal(next);
91
+ onChange?.(next);
92
+ }, [controlled, onChange]);
93
+ const toggle = useCallback((id) => {
94
+ const next = toggleColumnHidden(hidden, id, columns);
95
+ if (next !== hidden)
96
+ write(next);
97
+ }, [hidden, columns, write]);
98
+ const reset = useCallback(() => write(defaultHiddenColumns(columns)), [columns, write]);
99
+ const visibleColumns = useMemo(() => {
100
+ const visible = columns.filter((c) => !hidden.has(c.id) || !isHideable(c));
101
+ return visible.length > 0 ? visible : columns;
102
+ }, [columns, hidden]);
103
+ const api = useMemo(() => ({
104
+ hidden,
105
+ isHidden: (id) => hidden.has(id),
106
+ toggle,
107
+ reset,
108
+ hideableColumns: columns.filter(isHideable).map((c) => ({ id: c.id, header: c.header })),
109
+ }), [hidden, toggle, reset, columns]);
110
+ return { api, visibleColumns };
111
+ }
@@ -0,0 +1,35 @@
1
+ import type { PageSize } from '../core/constants.js';
2
+ import type { QueryChangeMeta, RowId, SortState, TableQuery } from '../core/types.js';
3
+ import { type DataTableLabels } from '../labels.js';
4
+ import type { ClientTableController, ColumnDef, ColumnVisibilityOptions, FilterDef, GroupDef, QuickFilterDef, RowModelOptions } from '../types.js';
5
+ export interface UseClientDataTableOptions<TRow> extends RowModelOptions<TRow> {
6
+ /** The whole batch. Keep the reference stable (memoise it). */
7
+ rows: readonly TRow[];
8
+ columns: readonly ColumnDef<TRow>[];
9
+ filters?: readonly FilterDef<TRow>[];
10
+ quickFilters?: readonly QuickFilterDef<TRow>[];
11
+ groups?: readonly GroupDef<TRow>[];
12
+ defaultSort?: SortState | null;
13
+ /** Uncontrolled initial query, or controlled `query` + `onQueryChange` (URL). */
14
+ defaultQuery?: Partial<TableQuery>;
15
+ query?: TableQuery;
16
+ onQueryChange?: (next: TableQuery, meta: QueryChangeMeta) => void;
17
+ /** Enable selection with `true` or an options object. */
18
+ selection?: boolean | {
19
+ defaultSelected?: Iterable<RowId>;
20
+ /** Controlled selection. */
21
+ selected?: ReadonlySet<RowId>;
22
+ onSelectedChange?: (next: ReadonlySet<RowId>, meta: {
23
+ reason: 'user' | 'prune';
24
+ }) => void;
25
+ };
26
+ expansion?: boolean | {
27
+ mode?: 'single' | 'multiple';
28
+ };
29
+ columnVisibility?: ColumnVisibilityOptions;
30
+ pageSizeOptions?: readonly PageSize[];
31
+ /** Collator locale for text sorting. Default `es`. */
32
+ locale?: string;
33
+ labels?: Partial<DataTableLabels>;
34
+ }
35
+ export declare function useClientDataTable<TRow>(options: UseClientDataTableOptions<TRow>): ClientTableController<TRow>;