@slipstream-ui/core 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +296 -15
- package/dist/index.d.ts +296 -15
- package/dist/index.js +1047 -473
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1096 -530
- package/dist/index.mjs.map +1 -1
- package/dist/manifests/app-shell.manifest.json +50 -0
- package/dist/manifests/badge.manifest.json +9 -4
- package/dist/manifests/breadcrumb.manifest.json +6 -0
- package/dist/manifests/button.manifest.json +14 -0
- package/dist/manifests/confirm-dialog.manifest.json +132 -0
- package/dist/manifests/data-table.manifest.json +50 -0
- package/dist/manifests/dialog.manifest.json +7 -0
- package/dist/manifests/drawer.manifest.json +7 -0
- package/dist/manifests/link.manifest.json +106 -0
- package/dist/manifests/page-header.manifest.json +30 -0
- package/dist/manifests/pagination.manifest.json +6 -0
- package/dist/manifests/prose.manifest.json +75 -0
- package/dist/manifests/search.manifest.json +41 -9
- package/dist/manifests/section-heading.manifest.json +108 -0
- package/dist/manifests/select.manifest.json +6 -0
- package/dist/manifests/site-footer.manifest.json +94 -0
- package/dist/manifests/site-header.manifest.json +130 -0
- package/dist/manifests/table-of-contents.manifest.json +87 -0
- package/package.json +2 -2
- package/src/components/alert/Alert.tsx +2 -2
- package/src/components/app-shell/AppShell.test.tsx +49 -1
- package/src/components/app-shell/AppShell.tsx +147 -43
- package/src/components/badge/Badge.test.tsx +21 -0
- package/src/components/badge/Badge.tsx +6 -0
- package/src/components/breadcrumb/Breadcrumb.test.tsx +16 -0
- package/src/components/breadcrumb/Breadcrumb.tsx +10 -5
- package/src/components/button/Button.test.tsx +40 -2
- package/src/components/button/Button.tsx +31 -7
- package/src/components/confirm-dialog/ConfirmDialog.test.tsx +46 -0
- package/src/components/confirm-dialog/ConfirmDialog.tsx +86 -0
- package/src/components/confirm-dialog/index.ts +1 -0
- package/src/components/data-table/DataTable.test.tsx +92 -0
- package/src/components/data-table/DataTable.tsx +98 -7
- package/src/components/dialog/Dialog.test.tsx +7 -0
- package/src/components/dialog/Dialog.tsx +4 -1
- package/src/components/drawer/Drawer.test.tsx +11 -2
- package/src/components/drawer/Drawer.tsx +5 -2
- package/src/components/input/Input.tsx +1 -0
- package/src/components/link/Link.test.tsx +60 -0
- package/src/components/link/Link.tsx +85 -0
- package/src/components/link/index.ts +1 -0
- package/src/components/page-header/PageHeader.test.tsx +17 -0
- package/src/components/page-header/PageHeader.tsx +22 -6
- package/src/components/pagination/Pagination.test.tsx +8 -0
- package/src/components/pagination/Pagination.tsx +16 -6
- package/src/components/prose/Prose.test.tsx +35 -0
- package/src/components/prose/Prose.tsx +61 -0
- package/src/components/prose/index.ts +1 -0
- package/src/components/search/Search.test.tsx +16 -0
- package/src/components/search/Search.tsx +11 -2
- package/src/components/section-heading/SectionHeading.test.tsx +32 -0
- package/src/components/section-heading/SectionHeading.tsx +54 -0
- package/src/components/section-heading/index.ts +1 -0
- package/src/components/select/Select.tsx +9 -7
- package/src/components/site-footer/SiteFooter.test.tsx +30 -0
- package/src/components/site-footer/SiteFooter.tsx +79 -0
- package/src/components/site-footer/index.ts +1 -0
- package/src/components/site-header/SiteHeader.test.tsx +53 -0
- package/src/components/site-header/SiteHeader.tsx +124 -0
- package/src/components/site-header/index.ts +1 -0
- package/src/components/switch/Switch.test.tsx +10 -0
- package/src/components/switch/Switch.tsx +8 -3
- package/src/components/table-of-contents/TableOfContents.test.tsx +31 -0
- package/src/components/table-of-contents/TableOfContents.tsx +100 -0
- package/src/components/table-of-contents/index.ts +1 -0
- package/src/components/textarea/Textarea.tsx +1 -0
- package/src/index.ts +7 -0
- package/src/manifests/app-shell.manifest.json +50 -0
- package/src/manifests/badge.manifest.json +9 -4
- package/src/manifests/breadcrumb.manifest.json +6 -0
- package/src/manifests/button.manifest.json +14 -0
- package/src/manifests/confirm-dialog.manifest.json +132 -0
- package/src/manifests/data-table.manifest.json +50 -0
- package/src/manifests/dialog.manifest.json +7 -0
- package/src/manifests/drawer.manifest.json +7 -0
- package/src/manifests/link.manifest.json +106 -0
- package/src/manifests/page-header.manifest.json +30 -0
- package/src/manifests/pagination.manifest.json +6 -0
- package/src/manifests/prose.manifest.json +75 -0
- package/src/manifests/search.manifest.json +41 -9
- package/src/manifests/section-heading.manifest.json +108 -0
- package/src/manifests/select.manifest.json +6 -0
- package/src/manifests/site-footer.manifest.json +94 -0
- package/src/manifests/site-header.manifest.json +130 -0
- package/src/manifests/table-of-contents.manifest.json +87 -0
- package/src/test/tailwind-v4.test.ts +103 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { fireEvent, render, screen, within } from '@testing-library/react';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
2
3
|
import { axe } from 'jest-axe';
|
|
3
4
|
import { describe, expect, it, vi } from 'vitest';
|
|
4
5
|
import { DataTable, type DataTableColumn } from './DataTable';
|
|
@@ -142,3 +143,94 @@ describe('DataTable loading and zebra', () => {
|
|
|
142
143
|
expect(screen.getByText('updown')).toBeInTheDocument();
|
|
143
144
|
});
|
|
144
145
|
});
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
describe('DataTable selection', () => {
|
|
149
|
+
const rows: Row[] = [
|
|
150
|
+
{ id: '1', name: 'Alpha', messages: 1 },
|
|
151
|
+
{ id: '2', name: 'Beta', messages: 2 },
|
|
152
|
+
{ id: '3', name: 'Gamma', messages: 3 },
|
|
153
|
+
];
|
|
154
|
+
const cols: DataTableColumn<Row>[] = [
|
|
155
|
+
{ key: 'name', header: 'Name' },
|
|
156
|
+
{ key: 'messages', header: 'Messages' },
|
|
157
|
+
];
|
|
158
|
+
|
|
159
|
+
it('renders selection checkboxes and select-all', async () => {
|
|
160
|
+
const user = userEvent.setup();
|
|
161
|
+
const onSelectedRowIdsChange = vi.fn();
|
|
162
|
+
render(
|
|
163
|
+
<DataTable
|
|
164
|
+
columns={cols}
|
|
165
|
+
data={rows}
|
|
166
|
+
rowId={(row) => row.id}
|
|
167
|
+
selectedRowIds={[]}
|
|
168
|
+
onSelectedRowIdsChange={onSelectedRowIdsChange}
|
|
169
|
+
caption="Runs"
|
|
170
|
+
/>
|
|
171
|
+
);
|
|
172
|
+
expect(screen.getByRole('checkbox', { name: 'Select all rows' })).toBeInTheDocument();
|
|
173
|
+
await user.click(screen.getByRole('checkbox', { name: 'Select row 2' }));
|
|
174
|
+
expect(onSelectedRowIdsChange).toHaveBeenCalledWith(['2']);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('selects all visible rows and shows bulk actions', async () => {
|
|
178
|
+
const user = userEvent.setup();
|
|
179
|
+
const onSelectedRowIdsChange = vi.fn();
|
|
180
|
+
render(
|
|
181
|
+
<DataTable
|
|
182
|
+
columns={cols}
|
|
183
|
+
data={rows}
|
|
184
|
+
rowId={(row) => row.id}
|
|
185
|
+
selectedRowIds={['2']}
|
|
186
|
+
onSelectedRowIdsChange={onSelectedRowIdsChange}
|
|
187
|
+
bulkActions={<button type="button">Archive</button>}
|
|
188
|
+
caption="Runs"
|
|
189
|
+
/>
|
|
190
|
+
);
|
|
191
|
+
expect(screen.getByText('1 selected')).toBeInTheDocument();
|
|
192
|
+
expect(screen.getByRole('button', { name: 'Archive' })).toBeInTheDocument();
|
|
193
|
+
await user.click(screen.getByRole('checkbox', { name: 'Select all rows' }));
|
|
194
|
+
expect(onSelectedRowIdsChange).toHaveBeenCalledWith(['2', '1', '3']);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('respects isRowSelectable', () => {
|
|
198
|
+
render(
|
|
199
|
+
<DataTable
|
|
200
|
+
columns={cols}
|
|
201
|
+
data={rows}
|
|
202
|
+
rowId={(row) => row.id}
|
|
203
|
+
selectedRowIds={[]}
|
|
204
|
+
onSelectedRowIdsChange={() => undefined}
|
|
205
|
+
isRowSelectable={(row) => row.id !== '2'}
|
|
206
|
+
caption="Runs"
|
|
207
|
+
/>
|
|
208
|
+
);
|
|
209
|
+
expect(screen.getByRole('checkbox', { name: 'Select row 2' })).toBeDisabled();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('DataTable manual pagination', () => {
|
|
214
|
+
const rows: Row[] = [
|
|
215
|
+
{ id: '1', name: 'Alpha', messages: 1 },
|
|
216
|
+
{ id: '2', name: 'Beta', messages: 2 },
|
|
217
|
+
];
|
|
218
|
+
const cols: DataTableColumn<Row>[] = [{ key: 'name', header: 'Name' }];
|
|
219
|
+
|
|
220
|
+
it('renders the current page as-is and uses total for the summary', () => {
|
|
221
|
+
render(
|
|
222
|
+
<DataTable
|
|
223
|
+
columns={cols}
|
|
224
|
+
data={rows}
|
|
225
|
+
rowId={(row) => row.id}
|
|
226
|
+
pageSize={25}
|
|
227
|
+
page={2}
|
|
228
|
+
total={312}
|
|
229
|
+
manualPagination
|
|
230
|
+
caption="Runs"
|
|
231
|
+
/>
|
|
232
|
+
);
|
|
233
|
+
expect(document.body.textContent).toContain('26–50 of 312');
|
|
234
|
+
expect(screen.getAllByRole('row')).toHaveLength(3); // header + 2
|
|
235
|
+
});
|
|
236
|
+
});
|
|
@@ -4,6 +4,7 @@ import * as React from 'react';
|
|
|
4
4
|
import { cn } from '../../utils/cn';
|
|
5
5
|
import { Pagination } from '../pagination/Pagination';
|
|
6
6
|
import { Skeleton } from '../skeleton/Skeleton';
|
|
7
|
+
import { Checkbox } from '../checkbox/Checkbox';
|
|
7
8
|
|
|
8
9
|
export type DataTableColumn<T> = {
|
|
9
10
|
/** Unique column key; also the value passed to onSortChange when sortable. */
|
|
@@ -51,12 +52,28 @@ export type DataTableProps<T> = {
|
|
|
51
52
|
pageSize?: number;
|
|
52
53
|
/** Controlled page (1-based) when pageSize is set. */
|
|
53
54
|
page?: number;
|
|
55
|
+
/** Total page count (manual/server pagination). */
|
|
56
|
+
pageCount?: number;
|
|
57
|
+
/** Total row count across all pages (summary + derived page count). */
|
|
58
|
+
total?: number;
|
|
59
|
+
/** Server pagination: render `data` as-is; don't slice or sort client-side. */
|
|
60
|
+
manualPagination?: boolean;
|
|
61
|
+
/** Selected row ids (controlled). Enables the leading selection column. */
|
|
62
|
+
selectedRowIds?: string[];
|
|
63
|
+
/** Fires with the full selection after a row or select-all toggle. */
|
|
64
|
+
onSelectedRowIdsChange?: (ids: string[]) => void;
|
|
65
|
+
/** Disable selection for individual rows. */
|
|
66
|
+
isRowSelectable?: (row: T) => boolean;
|
|
67
|
+
/** Rendered above the table while at least one row is selected. */
|
|
68
|
+
bulkActions?: React.ReactNode;
|
|
54
69
|
/** Fires on page change (controlled or not). */
|
|
55
70
|
onPageChange?: (page: number) => void;
|
|
56
71
|
/** Click/keyboard activation on a row. */
|
|
57
72
|
onRowClick?: (row: T) => void;
|
|
58
73
|
/** Message shown when data is empty. */
|
|
59
74
|
emptyMessage?: string;
|
|
75
|
+
/** Accessible label for the select-all checkbox (default "Select all rows"). */
|
|
76
|
+
selectAllLabel?: string;
|
|
60
77
|
/** Pin the header while the body scrolls (needs a max-height on the wrapper via className). */
|
|
61
78
|
stickyHeader?: boolean;
|
|
62
79
|
/** Compact row density. */
|
|
@@ -103,8 +120,16 @@ export function DataTable<T>({
|
|
|
103
120
|
pageSize,
|
|
104
121
|
page,
|
|
105
122
|
onPageChange,
|
|
123
|
+
pageCount: pageCountProp,
|
|
124
|
+
total,
|
|
125
|
+
manualPagination = false,
|
|
126
|
+
selectedRowIds,
|
|
127
|
+
onSelectedRowIdsChange,
|
|
128
|
+
isRowSelectable,
|
|
129
|
+
bulkActions,
|
|
106
130
|
onRowClick,
|
|
107
131
|
emptyMessage = 'No results.',
|
|
132
|
+
selectAllLabel = 'Select all rows',
|
|
108
133
|
stickyHeader,
|
|
109
134
|
density = 'default',
|
|
110
135
|
zebra,
|
|
@@ -154,11 +179,41 @@ export function DataTable<T>({
|
|
|
154
179
|
return copy;
|
|
155
180
|
}, [data, sort, columns]);
|
|
156
181
|
|
|
157
|
-
const
|
|
182
|
+
const effectiveTotal = total ?? sorted.length;
|
|
183
|
+
const computedPageCount = pageSize ? Math.max(1, Math.ceil(effectiveTotal / pageSize)) : 0;
|
|
184
|
+
const pageCount = pageCountProp ?? computedPageCount;
|
|
158
185
|
const safePage = Math.min(currentPage, pageCount || 1);
|
|
159
|
-
const visible =
|
|
160
|
-
?
|
|
161
|
-
:
|
|
186
|
+
const visible = manualPagination
|
|
187
|
+
? data
|
|
188
|
+
: pageSize
|
|
189
|
+
? sorted.slice((safePage - 1) * pageSize, safePage * pageSize)
|
|
190
|
+
: sorted;
|
|
191
|
+
|
|
192
|
+
// ----- Row selection (controlled or uncontrolled) -----
|
|
193
|
+
const [internalSelected, setInternalSelected] = React.useState<string[]>([]);
|
|
194
|
+
const selectionEnabled = selectedRowIds !== undefined || onSelectedRowIdsChange !== undefined;
|
|
195
|
+
const selectedIds = selectedRowIds ?? internalSelected;
|
|
196
|
+
const selectedSet = React.useMemo(() => new Set(selectedIds), [selectedIds]);
|
|
197
|
+
const selectableVisible = isRowSelectable ? visible.filter(isRowSelectable) : visible;
|
|
198
|
+
const allSelected = selectableVisible.length > 0 && selectableVisible.every((row) => selectedSet.has(rowId(row)));
|
|
199
|
+
const someSelected = selectableVisible.some((row) => selectedSet.has(rowId(row)));
|
|
200
|
+
|
|
201
|
+
const commitSelection = (next: string[]) => {
|
|
202
|
+
if (selectedRowIds === undefined) setInternalSelected(next);
|
|
203
|
+
onSelectedRowIdsChange?.(next);
|
|
204
|
+
};
|
|
205
|
+
const toggleAll = () => {
|
|
206
|
+
if (allSelected) {
|
|
207
|
+
const visibleIds = new Set(selectableVisible.map((row) => rowId(row)));
|
|
208
|
+
commitSelection(selectedIds.filter((id) => !visibleIds.has(id)));
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const merged = new Set([...selectedIds, ...selectableVisible.map((row) => rowId(row))]);
|
|
212
|
+
commitSelection([...merged]);
|
|
213
|
+
};
|
|
214
|
+
const toggleRow = (id: string) => {
|
|
215
|
+
commitSelection(selectedSet.has(id) ? selectedIds.filter((entry) => entry !== id) : [...selectedIds, id]);
|
|
216
|
+
};
|
|
162
217
|
|
|
163
218
|
const cellValue = (row: T, column: DataTableColumn<T>, index: number): React.ReactNode => {
|
|
164
219
|
if (column.cell) return column.cell(row, index);
|
|
@@ -173,6 +228,12 @@ export function DataTable<T>({
|
|
|
173
228
|
|
|
174
229
|
return (
|
|
175
230
|
<div className={cn('w-full space-y-3', className)}>
|
|
231
|
+
{selectionEnabled && selectedIds.length > 0 && bulkActions && (
|
|
232
|
+
<div className="flex flex-wrap items-center justify-between gap-2 rounded-lg border border-border bg-muted/40 px-3 py-2 text-sm">
|
|
233
|
+
<span className="font-medium">{selectedIds.length} selected</span>
|
|
234
|
+
<div className="flex flex-wrap items-center gap-2">{bulkActions}</div>
|
|
235
|
+
</div>
|
|
236
|
+
)}
|
|
176
237
|
<div className="w-full overflow-x-auto rounded-lg border border-border">
|
|
177
238
|
<table
|
|
178
239
|
className={cn('w-full caption-bottom', density === 'compact' ? 'text-xs' : 'text-sm')}
|
|
@@ -180,6 +241,18 @@ export function DataTable<T>({
|
|
|
180
241
|
{caption && <caption className="px-3 py-2 text-left text-xs text-muted-foreground">{caption}</caption>}
|
|
181
242
|
<thead className={cn('bg-muted/50', stickyHeader && 'sticky top-0 z-10 bg-background')}>
|
|
182
243
|
<tr>
|
|
244
|
+
{selectionEnabled && (
|
|
245
|
+
<th scope="col" className="w-10 border-b border-border px-3 py-2">
|
|
246
|
+
<Checkbox
|
|
247
|
+
aria-label={selectAllLabel}
|
|
248
|
+
checked={allSelected}
|
|
249
|
+
ref={(node) => {
|
|
250
|
+
if (node) node.indeterminate = someSelected && !allSelected;
|
|
251
|
+
}}
|
|
252
|
+
onCheckedChange={toggleAll}
|
|
253
|
+
/>
|
|
254
|
+
</th>
|
|
255
|
+
)}
|
|
183
256
|
{columns.map((column) => {
|
|
184
257
|
const isSorted = sort?.key === column.key;
|
|
185
258
|
const ariaSort = !column.sortable
|
|
@@ -231,6 +304,11 @@ export function DataTable<T>({
|
|
|
231
304
|
{ length: (loadingRows ?? pageSize ?? Math.min(visible.length, 5)) || 5 },
|
|
232
305
|
(_, skeletonRow) => (
|
|
233
306
|
<tr key={`skeleton-${skeletonRow}`}>
|
|
307
|
+
{selectionEnabled && (
|
|
308
|
+
<td className={cn('px-3 py-2', density === 'compact' && 'px-2 py-1')}>
|
|
309
|
+
<Skeleton className="h-4 w-4" />
|
|
310
|
+
</td>
|
|
311
|
+
)}
|
|
234
312
|
{columns.map((column) => (
|
|
235
313
|
<td key={column.key} className={cn('px-3 py-2', density === 'compact' && 'px-2 py-1')}>
|
|
236
314
|
{/* Height tracks density so skeleton rows match real row
|
|
@@ -244,7 +322,7 @@ export function DataTable<T>({
|
|
|
244
322
|
) : visible.length === 0 ? (
|
|
245
323
|
<tr>
|
|
246
324
|
<td
|
|
247
|
-
colSpan={columns.length}
|
|
325
|
+
colSpan={columns.length + (selectionEnabled ? 1 : 0)}
|
|
248
326
|
className="px-3 py-8 text-center text-sm text-muted-foreground"
|
|
249
327
|
>
|
|
250
328
|
{emptyMessage}
|
|
@@ -273,6 +351,19 @@ export function DataTable<T>({
|
|
|
273
351
|
'cursor-pointer transition-colors hover:bg-muted/50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring'
|
|
274
352
|
)}
|
|
275
353
|
>
|
|
354
|
+
{selectionEnabled && (
|
|
355
|
+
<td
|
|
356
|
+
className={cn('align-middle', density === 'compact' ? 'px-2 py-1' : 'px-3 py-2')}
|
|
357
|
+
onClick={(event) => event.stopPropagation()}
|
|
358
|
+
>
|
|
359
|
+
<Checkbox
|
|
360
|
+
aria-label={`Select row ${rowId(row)}`}
|
|
361
|
+
checked={selectedSet.has(rowId(row))}
|
|
362
|
+
disabled={isRowSelectable ? !isRowSelectable(row) : undefined}
|
|
363
|
+
onCheckedChange={() => toggleRow(rowId(row))}
|
|
364
|
+
/>
|
|
365
|
+
</td>
|
|
366
|
+
)}
|
|
276
367
|
{columns.map((column) => (
|
|
277
368
|
<td
|
|
278
369
|
key={column.key}
|
|
@@ -308,8 +399,8 @@ export function DataTable<T>({
|
|
|
308
399
|
className="min-h-8 leading-none text-xs text-muted-foreground"
|
|
309
400
|
aria-live="polite"
|
|
310
401
|
>
|
|
311
|
-
{(safePage - 1) * pageSize + 1}–{Math.min(safePage * pageSize,
|
|
312
|
-
{
|
|
402
|
+
{(safePage - 1) * pageSize + 1}–{Math.min(safePage * pageSize, effectiveTotal)} of{' '}
|
|
403
|
+
{effectiveTotal}
|
|
313
404
|
</p>
|
|
314
405
|
<Pagination page={safePage} pageCount={pageCount} onPageChange={setPage} />
|
|
315
406
|
</div>
|
|
@@ -247,3 +247,10 @@ describe('Dialog sizes, slots and compound parts', () => {
|
|
|
247
247
|
expect(body.className).toContain('overflow-y-auto');
|
|
248
248
|
});
|
|
249
249
|
});
|
|
250
|
+
|
|
251
|
+
describe('localized labels', () => {
|
|
252
|
+
it('uses the closeLabel override', () => {
|
|
253
|
+
render(<Dialog open onOpenChange={() => undefined} title="Title" closeLabel="Fermer">Body</Dialog>);
|
|
254
|
+
expect(screen.getByRole('button', { name: 'Fermer' })).toBeInTheDocument();
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -30,6 +30,8 @@ export type DialogProps = {
|
|
|
30
30
|
closeOnBackdrop?: boolean;
|
|
31
31
|
/** Hide the close (✕) button in the header; Escape and backdrop still close. */
|
|
32
32
|
showClose?: boolean;
|
|
33
|
+
/** Accessible label for the close button (default "Close dialog"). */
|
|
34
|
+
closeLabel?: string;
|
|
33
35
|
/** Focused when the dialog opens; defaults to the first focusable element. */
|
|
34
36
|
initialFocusRef?: React.RefObject<HTMLElement | null>;
|
|
35
37
|
/** Accessible name when `title` is omitted (required with a custom header). */
|
|
@@ -114,6 +116,7 @@ export function Dialog({
|
|
|
114
116
|
bodyClassName,
|
|
115
117
|
closeOnBackdrop = true,
|
|
116
118
|
showClose = true,
|
|
119
|
+
closeLabel = 'Close dialog',
|
|
117
120
|
initialFocusRef,
|
|
118
121
|
ariaLabel,
|
|
119
122
|
animate = false,
|
|
@@ -214,7 +217,7 @@ export function Dialog({
|
|
|
214
217
|
<button
|
|
215
218
|
type="button"
|
|
216
219
|
onClick={() => onOpenChange(false)}
|
|
217
|
-
aria-label=
|
|
220
|
+
aria-label={closeLabel}
|
|
218
221
|
className="rounded p-1 text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
219
222
|
>
|
|
220
223
|
✕
|
|
@@ -155,12 +155,14 @@ describe('Drawer sizes and custom extents', () => {
|
|
|
155
155
|
});
|
|
156
156
|
|
|
157
157
|
it('accepts a CSS length height for top/bottom drawers', () => {
|
|
158
|
+
// Use an absolute length: jsdom 30 drops relative units (vh/vw) from the
|
|
159
|
+
// CSSStyleDeclaration, so a vh assertion tests jsdom rather than us.
|
|
158
160
|
render(
|
|
159
|
-
<Drawer open onOpenChange={vi.fn()} title="Custom" side="top" height="
|
|
161
|
+
<Drawer open onOpenChange={vi.fn()} title="Custom" side="top" height="320px">
|
|
160
162
|
body
|
|
161
163
|
</Drawer>
|
|
162
164
|
);
|
|
163
|
-
expect(screen.getByRole('dialog')).toHaveStyle({ height: '
|
|
165
|
+
expect(screen.getByRole('dialog')).toHaveStyle({ height: '320px' });
|
|
164
166
|
});
|
|
165
167
|
|
|
166
168
|
it('ignores width/height that do not match the side axis', () => {
|
|
@@ -172,3 +174,10 @@ describe('Drawer sizes and custom extents', () => {
|
|
|
172
174
|
expect(screen.getByRole('dialog')).not.toHaveAttribute('style');
|
|
173
175
|
});
|
|
174
176
|
});
|
|
177
|
+
|
|
178
|
+
describe('localized labels', () => {
|
|
179
|
+
it('uses the closeLabel override', () => {
|
|
180
|
+
render(<Drawer open onOpenChange={() => undefined} title="Title" closeLabel="Fermer">Body</Drawer>);
|
|
181
|
+
expect(screen.getByRole('button', { name: 'Fermer' })).toBeInTheDocument();
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -32,6 +32,8 @@ export type DrawerProps = {
|
|
|
32
32
|
closeOnBackdrop?: boolean;
|
|
33
33
|
/** Hide the close (✕) button in the header (default true). */
|
|
34
34
|
showClose?: boolean;
|
|
35
|
+
/** Accessible label for the close button (default "Close drawer"). */
|
|
36
|
+
closeLabel?: string;
|
|
35
37
|
/** Focused when the drawer opens; defaults to the first focusable element. */
|
|
36
38
|
initialFocusRef?: React.RefObject<HTMLElement | null>;
|
|
37
39
|
/** Overrides the accessible name (defaults to the title). */
|
|
@@ -116,6 +118,7 @@ export function Drawer({
|
|
|
116
118
|
height,
|
|
117
119
|
closeOnBackdrop = true,
|
|
118
120
|
showClose = true,
|
|
121
|
+
closeLabel = 'Close drawer',
|
|
119
122
|
initialFocusRef,
|
|
120
123
|
ariaLabel,
|
|
121
124
|
animate = false,
|
|
@@ -228,8 +231,8 @@ export function Drawer({
|
|
|
228
231
|
<button
|
|
229
232
|
type="button"
|
|
230
233
|
onClick={() => onOpenChange(false)}
|
|
231
|
-
aria-label=
|
|
232
|
-
className="-
|
|
234
|
+
aria-label={closeLabel}
|
|
235
|
+
className="-me-1 rounded p-1 text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
233
236
|
>
|
|
234
237
|
✕
|
|
235
238
|
</button>
|
|
@@ -14,6 +14,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
|
|
|
14
14
|
'placeholder:text-muted-foreground',
|
|
15
15
|
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2',
|
|
16
16
|
'disabled:cursor-not-allowed disabled:opacity-50',
|
|
17
|
+
'aria-invalid:border-destructive aria-invalid:ring-2 aria-invalid:ring-destructive/20',
|
|
17
18
|
className
|
|
18
19
|
)}
|
|
19
20
|
ref={ref}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import { axe } from 'jest-axe';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
import { Link } from './Link';
|
|
5
|
+
|
|
6
|
+
describe('Link', () => {
|
|
7
|
+
it('renders an anchor with the default variant', () => {
|
|
8
|
+
render(<Link href="/docs">Docs</Link>);
|
|
9
|
+
const link = screen.getByRole('link', { name: 'Docs' });
|
|
10
|
+
expect(link).toHaveAttribute('href', '/docs');
|
|
11
|
+
expect(link).toHaveClass('text-primary');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('auto-detects external URLs and adds rel/noopener', () => {
|
|
15
|
+
render(<Link href="https://example.com">Example</Link>);
|
|
16
|
+
const link = screen.getByRole('link', { name: /Example/ });
|
|
17
|
+
expect(link).toHaveAttribute('target', '_blank');
|
|
18
|
+
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
|
|
19
|
+
expect(screen.getByText('(opens in a new tab)')).toHaveClass('sr-only');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('never opens mailto/tel in a new tab', () => {
|
|
23
|
+
render(<Link href="mailto:hi@example.com">Mail</Link>);
|
|
24
|
+
expect(screen.getByRole('link', { name: 'Mail' })).not.toHaveAttribute('target');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('can opt out of external handling', () => {
|
|
28
|
+
render(
|
|
29
|
+
<Link href="https://example.com" external={false}>
|
|
30
|
+
Stay
|
|
31
|
+
</Link>
|
|
32
|
+
);
|
|
33
|
+
const link = screen.getByRole('link', { name: 'Stay' });
|
|
34
|
+
expect(link).not.toHaveAttribute('target');
|
|
35
|
+
expect(screen.queryByText('(opens in a new tab)')).not.toBeInTheDocument();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('supports asChild with a router link', () => {
|
|
39
|
+
function RouterLink({ href, children, ...props }: React.ComponentProps<'a'>) {
|
|
40
|
+
return (
|
|
41
|
+
<a data-router href={href} {...props}>
|
|
42
|
+
{children}
|
|
43
|
+
</a>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
render(
|
|
47
|
+
<Link asChild variant="underline">
|
|
48
|
+
<RouterLink href="/posts">Posts</RouterLink>
|
|
49
|
+
</Link>
|
|
50
|
+
);
|
|
51
|
+
const link = screen.getByRole('link', { name: 'Posts' });
|
|
52
|
+
expect(link).toHaveAttribute('data-router');
|
|
53
|
+
expect(link).toHaveClass('underline');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('has no accessibility violations', async () => {
|
|
57
|
+
const { container } = render(<Link href="/docs">Docs</Link>);
|
|
58
|
+
expect(await axe(container)).toHaveNoViolations();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
5
|
+
import { cva, type VariantProps } from 'class-variance-authority';
|
|
6
|
+
import { cn } from '../../utils/cn';
|
|
7
|
+
|
|
8
|
+
const linkVariants = cva('inline-flex items-center gap-1 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background rounded-sm', {
|
|
9
|
+
variants: {
|
|
10
|
+
variant: {
|
|
11
|
+
default: 'text-primary hover:underline underline-offset-4',
|
|
12
|
+
muted: 'text-muted-foreground hover:text-foreground',
|
|
13
|
+
underline: 'underline underline-offset-4 hover:text-primary',
|
|
14
|
+
arrow: 'font-medium text-primary hover:underline underline-offset-4',
|
|
15
|
+
external: 'text-primary hover:underline underline-offset-4',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
defaultVariants: { variant: 'default' },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export type LinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement> &
|
|
22
|
+
VariantProps<typeof linkVariants> & {
|
|
23
|
+
/** Render the child element (router Link) with link styling. */
|
|
24
|
+
asChild?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Force external handling (target=_blank + rel). Absolute http(s) hrefs
|
|
27
|
+
* are detected automatically; mailto:/tel: links never open a new tab.
|
|
28
|
+
*/
|
|
29
|
+
external?: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const ABSOLUTE = /^https?:\/\//i;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Inline link primitive with the project's link variants. Keeps a real
|
|
36
|
+
* `<a href>` (SEO, modified clicks, middle-click) and adds safe external
|
|
37
|
+
* handling: absolute URLs get target="_blank" + rel="noopener noreferrer"
|
|
38
|
+
* and a screen-reader note, unless `external={false}`.
|
|
39
|
+
*
|
|
40
|
+
* For client-side routing, wrap your router's link: `<Link asChild><RouterLink …/></Link>`.
|
|
41
|
+
*/
|
|
42
|
+
export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(function Link(
|
|
43
|
+
{ className, variant, asChild = false, external, target, rel, children, ...props },
|
|
44
|
+
ref
|
|
45
|
+
) {
|
|
46
|
+
const isAbsolute = typeof props.href === 'string' && ABSOLUTE.test(props.href);
|
|
47
|
+
const isExternal = external ?? isAbsolute;
|
|
48
|
+
const Comp = asChild ? Slot : 'a';
|
|
49
|
+
|
|
50
|
+
if (asChild) {
|
|
51
|
+
return (
|
|
52
|
+
<Comp
|
|
53
|
+
ref={ref}
|
|
54
|
+
className={cn(linkVariants({ variant }), className)}
|
|
55
|
+
target={isExternal ? target ?? '_blank' : target}
|
|
56
|
+
rel={isExternal ? rel ?? 'noopener noreferrer' : rel}
|
|
57
|
+
{...props}
|
|
58
|
+
>
|
|
59
|
+
{children}
|
|
60
|
+
</Comp>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<Comp
|
|
66
|
+
ref={ref}
|
|
67
|
+
className={cn(linkVariants({ variant }), className)}
|
|
68
|
+
target={isExternal ? target ?? '_blank' : target}
|
|
69
|
+
rel={isExternal ? rel ?? 'noopener noreferrer' : rel}
|
|
70
|
+
{...props}
|
|
71
|
+
>
|
|
72
|
+
{children}
|
|
73
|
+
{isExternal && variant !== 'muted' && (
|
|
74
|
+
<>
|
|
75
|
+
<span aria-hidden="true" className="text-[0.75em]">
|
|
76
|
+
↗
|
|
77
|
+
</span>
|
|
78
|
+
<span className="sr-only">(opens in a new tab)</span>
|
|
79
|
+
</>
|
|
80
|
+
)}
|
|
81
|
+
</Comp>
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
export type LinkVariant = NonNullable<VariantProps<typeof linkVariants>['variant']>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Link';
|
|
@@ -29,3 +29,20 @@ describe('PageHeader', () => {
|
|
|
29
29
|
expect(await axe(container)).toHaveNoViolations();
|
|
30
30
|
});
|
|
31
31
|
});
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
describe('PageHeader hero options', () => {
|
|
35
|
+
it('renders eyebrow, hero size and centered alignment', () => {
|
|
36
|
+
render(
|
|
37
|
+
<PageHeader
|
|
38
|
+
title="Your AI knows the system"
|
|
39
|
+
eyebrow="AI-first"
|
|
40
|
+
size="hero"
|
|
41
|
+
align="center"
|
|
42
|
+
description="Ship correct UI the first time."
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
expect(screen.getByText('AI-first')).toBeInTheDocument();
|
|
46
|
+
expect(screen.getByRole('heading', { level: 1 })).toHaveClass('text-4xl');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -10,6 +10,19 @@ export type PageHeaderProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
|
10
10
|
actions?: React.ReactNode;
|
|
11
11
|
/** Small slot under the title row (tabs, filters). */
|
|
12
12
|
children?: React.ReactNode;
|
|
13
|
+
/** Eyebrow/kicker above the title (category, date, overline). */
|
|
14
|
+
eyebrow?: React.ReactNode;
|
|
15
|
+
/** Heading scale — 'hero' is for marketing landing pages. */
|
|
16
|
+
size?: 'sm' | 'md' | 'lg' | 'hero';
|
|
17
|
+
/** Text alignment; hero marketing pages often center. */
|
|
18
|
+
align?: 'left' | 'center';
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const sizeClasses: Record<NonNullable<PageHeaderProps['size']>, string> = {
|
|
22
|
+
sm: 'text-xl',
|
|
23
|
+
md: 'text-2xl',
|
|
24
|
+
lg: 'text-3xl',
|
|
25
|
+
hero: 'text-4xl sm:text-5xl lg:text-[3.4rem] lg:leading-[1.1]',
|
|
13
26
|
};
|
|
14
27
|
|
|
15
28
|
/**
|
|
@@ -18,16 +31,19 @@ export type PageHeaderProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
|
18
31
|
* and heading hierarchy.
|
|
19
32
|
*/
|
|
20
33
|
export const PageHeader = React.forwardRef<HTMLDivElement, PageHeaderProps>(function PageHeader(
|
|
21
|
-
{ title, description, breadcrumbs, actions, className, children, ...props },
|
|
34
|
+
{ title, description, breadcrumbs, actions, eyebrow, size = 'md', align = 'left', className, children, ...props },
|
|
22
35
|
ref
|
|
23
36
|
) {
|
|
24
37
|
return (
|
|
25
|
-
<div ref={ref} className={cn('flex flex-col gap-2', className)} {...props}>
|
|
38
|
+
<div ref={ref} className={cn('flex flex-col gap-2', align === 'center' && 'items-center text-center', className)} {...props}>
|
|
26
39
|
{breadcrumbs && <div className="text-sm text-muted-foreground">{breadcrumbs}</div>}
|
|
27
|
-
|
|
28
|
-
<
|
|
29
|
-
|
|
30
|
-
|
|
40
|
+
{eyebrow && (
|
|
41
|
+
<p className="text-xs font-medium uppercase tracking-wide text-primary">{eyebrow}</p>
|
|
42
|
+
)}
|
|
43
|
+
<div className={cn('flex w-full flex-wrap items-start justify-between gap-3', align === 'center' && 'justify-center')}>
|
|
44
|
+
<div className={cn('min-w-0', align === 'center' && 'flex flex-col items-center')}>
|
|
45
|
+
<h1 className={cn('font-semibold tracking-tight', sizeClasses[size])}>{title}</h1>
|
|
46
|
+
{description && <p className="mt-2 max-w-2xl text-base text-muted-foreground">{description}</p>}
|
|
31
47
|
</div>
|
|
32
48
|
{actions && <div className="flex shrink-0 items-center gap-2">{actions}</div>}
|
|
33
49
|
</div>
|
|
@@ -113,3 +113,11 @@ describe('Pagination contracts and summary', () => {
|
|
|
113
113
|
expect(screen.getByText('Showing 11–20 of 40 rows')).toBeInTheDocument();
|
|
114
114
|
});
|
|
115
115
|
});
|
|
116
|
+
|
|
117
|
+
describe('localized labels', () => {
|
|
118
|
+
it('uses the landmark and control overrides', () => {
|
|
119
|
+
render(<Pagination page={1} pageCount={3} labels={{ pagination: 'Pagination FR', next: 'Suivant' }} />);
|
|
120
|
+
expect(screen.getByRole('navigation', { name: 'Pagination FR' })).toBeInTheDocument();
|
|
121
|
+
expect(screen.getByRole('button', { name: 'Suivant' })).toBeInTheDocument();
|
|
122
|
+
});
|
|
123
|
+
});
|