@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
|
@@ -12,6 +12,8 @@ export type SidebarItem = {
|
|
|
12
12
|
badge?: React.ReactNode;
|
|
13
13
|
/** Force the active state (otherwise activeHref is compared). */
|
|
14
14
|
active?: boolean;
|
|
15
|
+
/** Nested items — renders a collapsible group with aria-expanded. */
|
|
16
|
+
children?: SidebarItem[];
|
|
15
17
|
};
|
|
16
18
|
|
|
17
19
|
export type SidebarSection = {
|
|
@@ -31,12 +33,32 @@ export type SidebarProps = Omit<React.HTMLAttributes<HTMLElement>, 'children'> &
|
|
|
31
33
|
linkComponent?: React.ElementType;
|
|
32
34
|
/** Accessible name of the nav landmark. Default 'Primary'. */
|
|
33
35
|
label?: string;
|
|
36
|
+
/** Show the collapse/expand control (icon-only rail when collapsed). */
|
|
37
|
+
collapsible?: boolean;
|
|
38
|
+
/** Initial collapsed state when uncontrolled. */
|
|
39
|
+
defaultCollapsed?: boolean;
|
|
40
|
+
/** Controlled collapsed state. */
|
|
41
|
+
collapsed?: boolean;
|
|
42
|
+
onCollapsedChange?: (collapsed: boolean) => void;
|
|
43
|
+
/** Groups open initially (in addition to groups containing the active route). */
|
|
44
|
+
defaultExpanded?: string[];
|
|
34
45
|
};
|
|
35
46
|
|
|
47
|
+
function containsActive(item: SidebarItem, activeHref: string | undefined): boolean {
|
|
48
|
+
if (!activeHref) return false;
|
|
49
|
+
if (item.href === activeHref) return true;
|
|
50
|
+
return (item.children ?? []).some((child) => containsActive(child, activeHref));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const itemClass =
|
|
54
|
+
'flex w-full items-center gap-2.5 rounded-md px-2.5 py-2 text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
55
|
+
const activeClass = 'bg-primary/10 font-medium text-primary';
|
|
56
|
+
const idleClass = 'text-muted-foreground hover:bg-accent hover:text-accent-foreground';
|
|
57
|
+
|
|
36
58
|
/**
|
|
37
|
-
* Sectioned navigation rail
|
|
38
|
-
*
|
|
39
|
-
*
|
|
59
|
+
* Sectioned navigation rail — now with nested groups and a collapsible
|
|
60
|
+
* icon-only mode. Purely presentational: you pass the active route and a
|
|
61
|
+
* router link component, so it stays stateless about routing.
|
|
40
62
|
*/
|
|
41
63
|
export function Sidebar({
|
|
42
64
|
sections,
|
|
@@ -45,50 +67,117 @@ export function Sidebar({
|
|
|
45
67
|
activeHref,
|
|
46
68
|
linkComponent,
|
|
47
69
|
label = 'Primary',
|
|
70
|
+
collapsible = false,
|
|
71
|
+
defaultCollapsed = false,
|
|
72
|
+
collapsed: collapsedProp,
|
|
73
|
+
onCollapsedChange,
|
|
74
|
+
defaultExpanded = [],
|
|
48
75
|
className,
|
|
49
76
|
...props
|
|
50
77
|
}: SidebarProps) {
|
|
51
78
|
const Link = linkComponent ?? 'a';
|
|
79
|
+
const [internalCollapsed, setInternalCollapsed] = React.useState(defaultCollapsed);
|
|
80
|
+
const collapsed = collapsedProp ?? internalCollapsed;
|
|
81
|
+
const [openGroups, setOpenGroups] = React.useState<string[]>(defaultExpanded);
|
|
82
|
+
|
|
83
|
+
const isOpen = (item: SidebarItem) =>
|
|
84
|
+
collapsed ? false : openGroups.includes(item.href) || containsActive(item, activeHref);
|
|
85
|
+
|
|
86
|
+
const toggleGroup = (href: string) => {
|
|
87
|
+
setOpenGroups((current) =>
|
|
88
|
+
current.includes(href) ? current.filter((entry) => entry !== href) : [...current, href]
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const toggleCollapsed = () => {
|
|
93
|
+
const next = !collapsed;
|
|
94
|
+
if (collapsedProp === undefined) setInternalCollapsed(next);
|
|
95
|
+
onCollapsedChange?.(next);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const renderItems = (items: SidebarItem[], depth = 0): React.ReactNode => (
|
|
99
|
+
<ul className={cn('space-y-0.5', depth > 0 && 'ml-3.5 border-l border-border pl-2.5')}>
|
|
100
|
+
{items.map((item) => {
|
|
101
|
+
const active = item.active ?? containsActive(item, activeHref);
|
|
102
|
+
const hasChildren = Boolean(item.children?.length);
|
|
103
|
+
const expanded = hasChildren && isOpen(item);
|
|
104
|
+
const panelId = `sidebar-group-${item.href.replace(/[^a-z0-9-]/gi, '-')}`;
|
|
105
|
+
|
|
106
|
+
if (hasChildren) {
|
|
107
|
+
return (
|
|
108
|
+
<li key={item.href}>
|
|
109
|
+
<button
|
|
110
|
+
type="button"
|
|
111
|
+
aria-expanded={expanded}
|
|
112
|
+
aria-controls={expanded ? panelId : undefined}
|
|
113
|
+
onClick={() => toggleGroup(item.href)}
|
|
114
|
+
title={collapsed ? item.label : undefined}
|
|
115
|
+
className={cn(itemClass, active ? activeClass : idleClass, collapsed && 'justify-center px-0')}
|
|
116
|
+
>
|
|
117
|
+
{item.icon && <span className="shrink-0 [&_svg]:size-4">{item.icon}</span>}
|
|
118
|
+
<span className={cn('min-w-0 flex-1 truncate text-left', collapsed && 'sr-only')}>{item.label}</span>
|
|
119
|
+
{!collapsed && (
|
|
120
|
+
<span aria-hidden="true" className={cn('shrink-0 text-xs transition-transform', expanded && 'rotate-90')}>
|
|
121
|
+
›
|
|
122
|
+
</span>
|
|
123
|
+
)}
|
|
124
|
+
</button>
|
|
125
|
+
{expanded && <div id={panelId}>{renderItems(item.children!, depth + 1)}</div>}
|
|
126
|
+
</li>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<li key={item.href}>
|
|
132
|
+
<Link
|
|
133
|
+
href={item.href}
|
|
134
|
+
aria-current={active ? 'page' : undefined}
|
|
135
|
+
title={collapsed ? item.label : undefined}
|
|
136
|
+
className={cn(itemClass, active ? activeClass : idleClass, collapsed && 'justify-center px-0')}
|
|
137
|
+
>
|
|
138
|
+
{item.icon && <span className="shrink-0 [&_svg]:size-4">{item.icon}</span>}
|
|
139
|
+
<span className={cn('min-w-0 flex-1 truncate', collapsed && 'sr-only')}>{item.label}</span>
|
|
140
|
+
{!collapsed && item.badge && <span className="shrink-0 text-xs">{item.badge}</span>}
|
|
141
|
+
</Link>
|
|
142
|
+
</li>
|
|
143
|
+
);
|
|
144
|
+
})}
|
|
145
|
+
</ul>
|
|
146
|
+
);
|
|
52
147
|
|
|
53
148
|
return (
|
|
54
|
-
<div
|
|
55
|
-
{
|
|
56
|
-
|
|
149
|
+
<div
|
|
150
|
+
data-collapsed={collapsed || undefined}
|
|
151
|
+
className={cn('flex h-full min-h-0 flex-col gap-5 p-3', className)}
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
{heading && <div className={cn('px-1', collapsed && 'sr-only')}>{heading}</div>}
|
|
155
|
+
<nav aria-label={label} className="flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto">
|
|
57
156
|
{sections.map((section, index) => (
|
|
58
157
|
<div key={section.title ?? `section-${index}`}>
|
|
59
158
|
{section.title && (
|
|
60
|
-
<p className=
|
|
159
|
+
<p className={cn('mb-1.5 px-2 text-xs font-medium uppercase tracking-wide text-muted-foreground', collapsed && 'sr-only')}>
|
|
61
160
|
{section.title}
|
|
62
161
|
</p>
|
|
63
162
|
)}
|
|
64
|
-
|
|
65
|
-
{section.items.map((item) => {
|
|
66
|
-
const active = item.active ?? (activeHref !== undefined && item.href === activeHref);
|
|
67
|
-
return (
|
|
68
|
-
<li key={item.href}>
|
|
69
|
-
<Link
|
|
70
|
-
href={item.href}
|
|
71
|
-
aria-current={active ? 'page' : undefined}
|
|
72
|
-
className={cn(
|
|
73
|
-
'flex items-center gap-2.5 rounded-md px-2.5 py-2 text-sm transition-colors',
|
|
74
|
-
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
|
|
75
|
-
active
|
|
76
|
-
? 'bg-primary/10 font-medium text-primary'
|
|
77
|
-
: 'text-muted-foreground hover:bg-accent hover:text-accent-foreground'
|
|
78
|
-
)}
|
|
79
|
-
>
|
|
80
|
-
{item.icon && <span className="shrink-0 [&_svg]:size-4">{item.icon}</span>}
|
|
81
|
-
<span className="min-w-0 flex-1 truncate">{item.label}</span>
|
|
82
|
-
{item.badge && <span className="shrink-0 text-xs">{item.badge}</span>}
|
|
83
|
-
</Link>
|
|
84
|
-
</li>
|
|
85
|
-
);
|
|
86
|
-
})}
|
|
87
|
-
</ul>
|
|
163
|
+
{renderItems(section.items)}
|
|
88
164
|
</div>
|
|
89
165
|
))}
|
|
90
166
|
</nav>
|
|
91
|
-
{
|
|
167
|
+
{collapsible && (
|
|
168
|
+
<button
|
|
169
|
+
type="button"
|
|
170
|
+
onClick={toggleCollapsed}
|
|
171
|
+
aria-expanded={!collapsed}
|
|
172
|
+
aria-label={collapsed ? 'Expand navigation' : 'Collapse navigation'}
|
|
173
|
+
className={cn(itemClass, idleClass, 'justify-center')}
|
|
174
|
+
>
|
|
175
|
+
<span aria-hidden="true" className={cn('transition-transform', collapsed && 'rotate-180')}>
|
|
176
|
+
«
|
|
177
|
+
</span>
|
|
178
|
+
</button>
|
|
179
|
+
)}
|
|
180
|
+
{footer && !collapsed && <div className="border-t border-border pt-3">{footer}</div>}
|
|
92
181
|
</div>
|
|
93
182
|
);
|
|
94
183
|
}
|
|
@@ -102,18 +191,34 @@ export type AppShellProps = React.HTMLAttributes<HTMLDivElement> & {
|
|
|
102
191
|
hideMobileMenu?: boolean;
|
|
103
192
|
/** Tailwind width class for the desktop rail. Default 'w-64'. */
|
|
104
193
|
sidebarWidth?: string;
|
|
194
|
+
/** Breakpoint at which the rail switches to the mobile drawer. Default 'md'. */
|
|
195
|
+
mobileBreakpoint?: 'sm' | 'md' | 'lg';
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const hideAt: Record<NonNullable<AppShellProps['mobileBreakpoint']>, string> = {
|
|
199
|
+
sm: 'hidden sm:block',
|
|
200
|
+
md: 'hidden md:block',
|
|
201
|
+
lg: 'hidden lg:block',
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const showBelow: Record<NonNullable<AppShellProps['mobileBreakpoint']>, string> = {
|
|
205
|
+
sm: 'sm:hidden',
|
|
206
|
+
md: 'md:hidden',
|
|
207
|
+
lg: 'lg:hidden',
|
|
105
208
|
};
|
|
106
209
|
|
|
107
210
|
/**
|
|
108
211
|
* Application frame: fixed-height sidebar, sticky header, scrolling main.
|
|
109
212
|
* On small screens the sidebar becomes a left Drawer opened by a burger
|
|
110
|
-
* button, so pages never need their own responsive nav wiring.
|
|
213
|
+
* button, so pages never need their own responsive nav wiring. A collapsed
|
|
214
|
+
* Sidebar (data-collapsed) shrinks the desktop rail automatically.
|
|
111
215
|
*/
|
|
112
216
|
export function AppShell({
|
|
113
217
|
sidebar,
|
|
114
218
|
header,
|
|
115
219
|
hideMobileMenu = false,
|
|
116
220
|
sidebarWidth = 'w-64',
|
|
221
|
+
mobileBreakpoint = 'md',
|
|
117
222
|
className,
|
|
118
223
|
children,
|
|
119
224
|
...props
|
|
@@ -123,7 +228,13 @@ export function AppShell({
|
|
|
123
228
|
return (
|
|
124
229
|
<div className={cn('flex min-h-screen bg-background text-foreground', className)} {...props}>
|
|
125
230
|
{sidebar && (
|
|
126
|
-
<aside
|
|
231
|
+
<aside
|
|
232
|
+
className={cn(
|
|
233
|
+
'shrink-0 border-r border-border has-[[data-collapsed]]:w-16',
|
|
234
|
+
hideAt[mobileBreakpoint],
|
|
235
|
+
sidebarWidth
|
|
236
|
+
)}
|
|
237
|
+
>
|
|
127
238
|
{sidebar}
|
|
128
239
|
</aside>
|
|
129
240
|
)}
|
|
@@ -135,7 +246,7 @@ export function AppShell({
|
|
|
135
246
|
type="button"
|
|
136
247
|
onClick={() => setMobileOpen(true)}
|
|
137
248
|
aria-label="Open navigation"
|
|
138
|
-
className=
|
|
249
|
+
className={cn('rounded-md border border-border p-2 text-muted-foreground hover:bg-muted', showBelow[mobileBreakpoint])}
|
|
139
250
|
>
|
|
140
251
|
<span aria-hidden="true">☰</span>
|
|
141
252
|
</button>
|
|
@@ -146,14 +257,7 @@ export function AppShell({
|
|
|
146
257
|
<main className="min-w-0 flex-1">{children}</main>
|
|
147
258
|
</div>
|
|
148
259
|
{sidebar && (
|
|
149
|
-
<Drawer
|
|
150
|
-
open={mobileOpen}
|
|
151
|
-
onOpenChange={setMobileOpen}
|
|
152
|
-
title="Navigation"
|
|
153
|
-
side="left"
|
|
154
|
-
size="sm"
|
|
155
|
-
animate
|
|
156
|
-
>
|
|
260
|
+
<Drawer open={mobileOpen} onOpenChange={setMobileOpen} title="Navigation" side="left" size="sm" animate>
|
|
157
261
|
{sidebar}
|
|
158
262
|
</Drawer>
|
|
159
263
|
)}
|
|
@@ -72,3 +72,24 @@ describe('Badge asChild', () => {
|
|
|
72
72
|
expect(link).toHaveClass('inline-flex', 'bg-primary');
|
|
73
73
|
});
|
|
74
74
|
});
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
describe('Badge semantic and alias variants', () => {
|
|
78
|
+
it('renders success, warning, info, primary and danger surfaces', () => {
|
|
79
|
+
const { container } = render(
|
|
80
|
+
<>
|
|
81
|
+
<Badge variant="success">ok</Badge>
|
|
82
|
+
<Badge variant="warning">warn</Badge>
|
|
83
|
+
<Badge variant="info">info</Badge>
|
|
84
|
+
<Badge variant="primary">primary</Badge>
|
|
85
|
+
<Badge variant="danger">danger</Badge>
|
|
86
|
+
</>
|
|
87
|
+
);
|
|
88
|
+
expect(screen.getByText('ok')).toHaveClass('bg-success');
|
|
89
|
+
expect(screen.getByText('warn')).toHaveClass('bg-warning');
|
|
90
|
+
expect(screen.getByText('info')).toHaveClass('bg-info');
|
|
91
|
+
expect(screen.getByText('primary')).toHaveClass('bg-primary');
|
|
92
|
+
expect(screen.getByText('danger')).toHaveClass('bg-destructive');
|
|
93
|
+
expect(container.querySelectorAll('div')).toHaveLength(5);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -9,6 +9,12 @@ const badgeVariants = cva(
|
|
|
9
9
|
variants: {
|
|
10
10
|
variant: {
|
|
11
11
|
default: 'border-transparent bg-primary text-primary-foreground hover:bg-primary/80',
|
|
12
|
+
// Aliases used by shadcn/Bootstrap-style codebases.
|
|
13
|
+
primary: 'border-transparent bg-primary text-primary-foreground hover:bg-primary/80',
|
|
14
|
+
danger: 'border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80',
|
|
15
|
+
success: 'border-transparent bg-success text-success-foreground hover:bg-success/80',
|
|
16
|
+
warning: 'border-transparent bg-warning text-warning-foreground hover:bg-warning/80',
|
|
17
|
+
info: 'border-transparent bg-info text-info-foreground hover:bg-info/80',
|
|
12
18
|
secondary: 'border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80',
|
|
13
19
|
destructive: 'border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80',
|
|
14
20
|
outline: 'text-foreground',
|
|
@@ -107,3 +107,19 @@ describe('Breadcrumb onItemClick', () => {
|
|
|
107
107
|
});
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
describe('Breadcrumb linkComponent', () => {
|
|
113
|
+
it('renders items with a custom router link', () => {
|
|
114
|
+
function RouterLink({ href, children, ...props }: React.ComponentProps<'a'>) {
|
|
115
|
+
return (
|
|
116
|
+
<a data-router href={href} {...props}>
|
|
117
|
+
{children}
|
|
118
|
+
</a>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
render(<Breadcrumb linkComponent={RouterLink} items={[{ label: 'Home', href: '/' }, { label: 'Docs' }]} />);
|
|
122
|
+
expect(screen.getByRole('link', { name: 'Home' })).toHaveAttribute('data-router');
|
|
123
|
+
expect(screen.getByText('Docs')).toHaveAttribute('aria-current', 'page');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -19,6 +19,8 @@ export type BreadcrumbProps = React.HTMLAttributes<HTMLElement> & {
|
|
|
19
19
|
* (e.g. via a router, state machine, or analytics wrapper).
|
|
20
20
|
*/
|
|
21
21
|
onItemClick?: (item: BreadcrumbItem, index: number) => void;
|
|
22
|
+
/** Custom link element for routers (Next Link, react-router Link). */
|
|
23
|
+
linkComponent?: React.ElementType;
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -27,7 +29,9 @@ export type BreadcrumbProps = React.HTMLAttributes<HTMLElement> & {
|
|
|
27
29
|
* announce the navigation landmark and current location.
|
|
28
30
|
*/
|
|
29
31
|
const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
30
|
-
({ items, separator = '/', onItemClick, className, ...props }, ref) =>
|
|
32
|
+
({ items, separator = '/', onItemClick, linkComponent, className, ...props }, ref) => {
|
|
33
|
+
const LinkEl = linkComponent ?? 'a';
|
|
34
|
+
return (
|
|
31
35
|
<nav
|
|
32
36
|
ref={ref}
|
|
33
37
|
aria-label="Breadcrumb"
|
|
@@ -45,12 +49,12 @@ const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
|
45
49
|
</span>
|
|
46
50
|
)}
|
|
47
51
|
{item.href && !isLast ? (
|
|
48
|
-
<
|
|
52
|
+
<LinkEl
|
|
49
53
|
href={item.href}
|
|
50
54
|
className="transition-colors hover:text-foreground"
|
|
51
55
|
onClick={
|
|
52
56
|
onItemClick
|
|
53
|
-
? (event) => {
|
|
57
|
+
? (event: React.MouseEvent<HTMLAnchorElement>) => {
|
|
54
58
|
event.preventDefault();
|
|
55
59
|
onItemClick(item, index);
|
|
56
60
|
}
|
|
@@ -58,7 +62,7 @@ const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
|
58
62
|
}
|
|
59
63
|
>
|
|
60
64
|
{item.label}
|
|
61
|
-
</
|
|
65
|
+
</LinkEl>
|
|
62
66
|
) : (
|
|
63
67
|
<span
|
|
64
68
|
aria-current={isLast ? 'page' : undefined}
|
|
@@ -72,7 +76,8 @@ const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>(
|
|
|
72
76
|
})}
|
|
73
77
|
</ol>
|
|
74
78
|
</nav>
|
|
75
|
-
|
|
79
|
+
);
|
|
80
|
+
}
|
|
76
81
|
);
|
|
77
82
|
Breadcrumb.displayName = 'Breadcrumb';
|
|
78
83
|
|
|
@@ -116,8 +116,8 @@ describe('Button outline color variants', () => {
|
|
|
116
116
|
expect(del).toHaveClass('border', 'border-destructive/50', 'text-destructive', 'bg-background');
|
|
117
117
|
expect(del.className).toContain('hover:bg-destructive/10');
|
|
118
118
|
expect(screen.getByRole('button', { name: 'Approve' })).toHaveClass('border-success/50', 'text-success');
|
|
119
|
-
expect(screen.getByRole('button', { name: 'Careful' })).toHaveClass('border-warning/50', 'text-warning');
|
|
120
|
-
expect(screen.getByRole('button', { name: 'Info' })).toHaveClass('border-info/50', 'text-info');
|
|
119
|
+
expect(screen.getByRole('button', { name: 'Careful' })).toHaveClass('border-warning/50', 'text-warning-ink');
|
|
120
|
+
expect(screen.getByRole('button', { name: 'Info' })).toHaveClass('border-info/50', 'text-info-ink');
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
it('keeps colored outlines accessible as links via asChild', () => {
|
|
@@ -130,3 +130,41 @@ describe('Button outline color variants', () => {
|
|
|
130
130
|
expect(link).toHaveClass('border-success/50', 'text-success');
|
|
131
131
|
});
|
|
132
132
|
});
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
describe('Button aliases and loading', () => {
|
|
136
|
+
it('accepts primary/danger aliases', () => {
|
|
137
|
+
render(
|
|
138
|
+
<>
|
|
139
|
+
<Button variant="primary">Go</Button>
|
|
140
|
+
<Button variant="danger">Delete</Button>
|
|
141
|
+
</>
|
|
142
|
+
);
|
|
143
|
+
expect(screen.getByRole('button', { name: 'Go' })).toHaveClass('bg-primary');
|
|
144
|
+
expect(screen.getByRole('button', { name: 'Delete' })).toHaveClass('bg-destructive');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('loading disables, sets aria-busy and announces', () => {
|
|
148
|
+
render(<Button loading>Save</Button>);
|
|
149
|
+
const button = screen.getByRole('button', { name: /Save/ });
|
|
150
|
+
expect(button).toBeDisabled();
|
|
151
|
+
expect(button).toHaveAttribute('aria-busy', 'true');
|
|
152
|
+
expect(screen.getByText('Loading')).toHaveClass('sr-only');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('asChild ignores loading chrome', () => {
|
|
156
|
+
render(
|
|
157
|
+
<Button asChild loading>
|
|
158
|
+
<a href="/x">Link</a>
|
|
159
|
+
</Button>
|
|
160
|
+
);
|
|
161
|
+
expect(screen.getByRole('link', { name: 'Link' })).not.toHaveAttribute('aria-busy');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('localized labels', () => {
|
|
166
|
+
it('uses the loadingLabel override', () => {
|
|
167
|
+
render(<Button loading loadingLabel="Chargement">Save</Button>);
|
|
168
|
+
expect(screen.getByText('Chargement')).toBeInTheDocument();
|
|
169
|
+
});
|
|
170
|
+
});
|
|
@@ -9,23 +9,25 @@ const buttonVariants = cva(
|
|
|
9
9
|
variants: {
|
|
10
10
|
variant: {
|
|
11
11
|
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
|
|
12
|
+
primary: 'bg-primary text-primary-foreground hover:bg-primary/90',
|
|
12
13
|
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
|
|
14
|
+
danger: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
|
|
13
15
|
// Semantic status variants — foregrounds are the paired AA tokens
|
|
14
16
|
// (white on dark green, ink on amber/cyan) in both themes.
|
|
15
17
|
success: 'bg-success text-success-foreground hover:bg-success/90',
|
|
16
18
|
warning: 'bg-warning text-warning-foreground hover:bg-warning/90',
|
|
17
19
|
info: 'bg-info text-info-foreground hover:bg-info/90',
|
|
18
20
|
outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
|
|
19
|
-
// Colored outlines: semantic border + label on the page
|
|
20
|
-
// a faint tint on hover
|
|
21
|
+
// Colored outlines: semantic border + AA ink label on the page
|
|
22
|
+
// surface, with a faint tint on hover - mirrors the Alert recipe.
|
|
21
23
|
'outline-destructive':
|
|
22
24
|
'border border-destructive/50 bg-background text-destructive hover:border-destructive hover:bg-destructive/10',
|
|
23
25
|
'outline-success':
|
|
24
26
|
'border border-success/50 bg-background text-success hover:border-success hover:bg-success/10',
|
|
25
27
|
'outline-warning':
|
|
26
|
-
'border border-warning/50 bg-background text-warning hover:border-warning hover:bg-warning/10',
|
|
28
|
+
'border border-warning/50 bg-background text-warning-ink hover:border-warning hover:bg-warning/10',
|
|
27
29
|
'outline-info':
|
|
28
|
-
'border border-info/50 bg-background text-info hover:border-info hover:bg-info/10',
|
|
30
|
+
'border border-info/50 bg-background text-info-ink hover:border-info hover:bg-info/10',
|
|
29
31
|
secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
|
|
30
32
|
ghost: 'hover:bg-accent hover:text-accent-foreground',
|
|
31
33
|
link: 'text-primary underline-offset-4 hover:underline',
|
|
@@ -46,17 +48,39 @@ const buttonVariants = cva(
|
|
|
46
48
|
|
|
47
49
|
export type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & VariantProps<typeof buttonVariants> & {
|
|
48
50
|
asChild?: boolean;
|
|
51
|
+
/** Shows a spinner, disables the button and sets aria-busy while an action runs. */
|
|
52
|
+
loading?: boolean;
|
|
53
|
+
/** Screen-reader text while loading (default "Loading"). */
|
|
54
|
+
loadingLabel?: string;
|
|
49
55
|
};
|
|
50
56
|
|
|
51
57
|
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
|
52
|
-
({ className, variant, size, asChild = false, ...props }, ref) => {
|
|
58
|
+
({ className, variant, size, asChild = false, loading = false, loadingLabel = 'Loading', disabled, children, ...props }, ref) => {
|
|
53
59
|
const Comp = asChild ? Slot : 'button';
|
|
60
|
+
if (asChild) {
|
|
61
|
+
return (
|
|
62
|
+
<Comp className={cn(buttonVariants({ variant, size, className }))} ref={ref} {...props}>
|
|
63
|
+
{children}
|
|
64
|
+
</Comp>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
54
67
|
return (
|
|
55
|
-
<
|
|
68
|
+
<button
|
|
56
69
|
className={cn(buttonVariants({ variant, size, className }))}
|
|
57
70
|
ref={ref}
|
|
71
|
+
disabled={disabled || loading}
|
|
72
|
+
aria-busy={loading || undefined}
|
|
58
73
|
{...props}
|
|
59
|
-
|
|
74
|
+
>
|
|
75
|
+
{loading && (
|
|
76
|
+
<span
|
|
77
|
+
aria-hidden="true"
|
|
78
|
+
className="h-4 w-4 shrink-0 animate-spin rounded-full border-2 border-current border-t-transparent"
|
|
79
|
+
/>
|
|
80
|
+
)}
|
|
81
|
+
{loading && <span className="sr-only">{loadingLabel}</span>}
|
|
82
|
+
{children}
|
|
83
|
+
</button>
|
|
60
84
|
);
|
|
61
85
|
}
|
|
62
86
|
);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import { axe } from 'jest-axe';
|
|
4
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { ConfirmDialog } from './ConfirmDialog';
|
|
6
|
+
|
|
7
|
+
describe('ConfirmDialog', () => {
|
|
8
|
+
it('renders title, description and actions', () => {
|
|
9
|
+
render(
|
|
10
|
+
<ConfirmDialog open onOpenChange={vi.fn()} title="Delete project" description="This cannot be undone." onConfirm={vi.fn()} />
|
|
11
|
+
);
|
|
12
|
+
expect(screen.getByRole('dialog', { name: 'Delete project' })).toBeInTheDocument();
|
|
13
|
+
expect(screen.getByRole('button', { name: 'Confirm' })).toBeInTheDocument();
|
|
14
|
+
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('runs onConfirm, shows pending and closes', async () => {
|
|
18
|
+
const user = userEvent.setup();
|
|
19
|
+
let resolve!: () => void;
|
|
20
|
+
const onConfirm = vi.fn(
|
|
21
|
+
() =>
|
|
22
|
+
new Promise<void>((done) => {
|
|
23
|
+
resolve = done;
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
const onOpenChange = vi.fn();
|
|
27
|
+
render(<ConfirmDialog open onOpenChange={onOpenChange} title="Publish" onConfirm={onConfirm} confirmLabel="Publish now" />);
|
|
28
|
+
await user.click(screen.getByRole('button', { name: 'Publish now' }));
|
|
29
|
+
expect(onConfirm).toHaveBeenCalledOnce();
|
|
30
|
+
expect(screen.getByRole('button', { name: /Publish now/ })).toHaveAttribute('aria-busy', 'true');
|
|
31
|
+
resolve();
|
|
32
|
+
await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false));
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('marks destructive confirms', () => {
|
|
36
|
+
render(<ConfirmDialog open onOpenChange={vi.fn()} title="Delete" destructive onConfirm={vi.fn()} confirmLabel="Delete" />);
|
|
37
|
+
expect(screen.getByRole('button', { name: 'Delete' })).toHaveClass('bg-destructive');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('has no accessibility violations', async () => {
|
|
41
|
+
const { baseElement } = render(
|
|
42
|
+
<ConfirmDialog open onOpenChange={vi.fn()} title="Confirm" description="Sure?" onConfirm={vi.fn()} />
|
|
43
|
+
);
|
|
44
|
+
expect(await axe(baseElement)).toHaveNoViolations();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { Button } from '../button/Button';
|
|
5
|
+
import { Dialog, DialogFooter } from '../dialog/Dialog';
|
|
6
|
+
|
|
7
|
+
export type ConfirmDialogProps = {
|
|
8
|
+
open: boolean;
|
|
9
|
+
onOpenChange: (open: boolean) => void;
|
|
10
|
+
title: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
/** Confirm button label. Default 'Confirm'. */
|
|
13
|
+
confirmLabel?: string;
|
|
14
|
+
/** Cancel button label. Default 'Cancel'. */
|
|
15
|
+
cancelLabel?: string;
|
|
16
|
+
/** Destructive confirmation (red confirm button + destructive panel accent). */
|
|
17
|
+
destructive?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Confirm handler. Return a promise to show a pending state; the dialog
|
|
20
|
+
* closes automatically when it resolves unless `closeOnConfirm` is false.
|
|
21
|
+
*/
|
|
22
|
+
onConfirm: () => void | Promise<void>;
|
|
23
|
+
closeOnConfirm?: boolean;
|
|
24
|
+
/** Extra body content above the actions. */
|
|
25
|
+
children?: React.ReactNode;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Accessible confirmation dialog built on Dialog: focus trap, Escape,
|
|
30
|
+
* backdrop dismissal and focus restoration come from the primitive, plus a
|
|
31
|
+
* pending state for async confirmations (79 hand-rolled confirm modals in
|
|
32
|
+
* the audited apps use this exact shape).
|
|
33
|
+
*/
|
|
34
|
+
export function ConfirmDialog({
|
|
35
|
+
open,
|
|
36
|
+
onOpenChange,
|
|
37
|
+
title,
|
|
38
|
+
description,
|
|
39
|
+
confirmLabel = 'Confirm',
|
|
40
|
+
cancelLabel = 'Cancel',
|
|
41
|
+
destructive = false,
|
|
42
|
+
onConfirm,
|
|
43
|
+
closeOnConfirm = true,
|
|
44
|
+
children,
|
|
45
|
+
}: ConfirmDialogProps) {
|
|
46
|
+
const [pending, setPending] = React.useState(false);
|
|
47
|
+
|
|
48
|
+
const handleConfirm = async () => {
|
|
49
|
+
try {
|
|
50
|
+
setPending(true);
|
|
51
|
+
await onConfirm();
|
|
52
|
+
if (closeOnConfirm) onOpenChange(false);
|
|
53
|
+
} finally {
|
|
54
|
+
setPending(false);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<Dialog
|
|
60
|
+
open={open}
|
|
61
|
+
onOpenChange={(next) => {
|
|
62
|
+
if (pending) return;
|
|
63
|
+
onOpenChange(next);
|
|
64
|
+
}}
|
|
65
|
+
title={title}
|
|
66
|
+
description={description}
|
|
67
|
+
size="sm"
|
|
68
|
+
footer={
|
|
69
|
+
<DialogFooter>
|
|
70
|
+
<Button variant="ghost" onClick={() => onOpenChange(false)} disabled={pending}>
|
|
71
|
+
{cancelLabel}
|
|
72
|
+
</Button>
|
|
73
|
+
<Button
|
|
74
|
+
variant={destructive ? 'destructive' : 'default'}
|
|
75
|
+
onClick={handleConfirm}
|
|
76
|
+
loading={pending}
|
|
77
|
+
>
|
|
78
|
+
{confirmLabel}
|
|
79
|
+
</Button>
|
|
80
|
+
</DialogFooter>
|
|
81
|
+
}
|
|
82
|
+
>
|
|
83
|
+
{children}
|
|
84
|
+
</Dialog>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ConfirmDialog';
|