@k3-tech/react-kit 0.0.69 → 0.0.71

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.
@@ -2,28 +2,24 @@ import {
2
2
  Sidebar,
3
3
  SidebarContent,
4
4
  SidebarFooter,
5
- SidebarGroup,
6
- SidebarGroupContent,
7
- SidebarGroupLabel,
8
5
  SidebarHeader,
9
6
  SidebarInset,
10
7
  SidebarMenu,
11
- SidebarMenuButton,
12
8
  SidebarMenuItem,
13
9
  SidebarProvider,
14
10
  SidebarTrigger,
11
+ useSidebar,
15
12
  } from '../../../../shadcn/ui/sidebar';
16
- import { Separator } from '../../../../shadcn/ui/separator';
17
13
  import {
18
- Package,
19
- LogOut,
20
- ChevronRight,
21
- ChevronDown,
22
- Circle,
23
- } from 'lucide-react';
14
+ Tooltip,
15
+ TooltipContent,
16
+ TooltipTrigger,
17
+ } from '../../../../shadcn/ui/tooltip';
18
+ import { cn } from '../../../../shadcn/lib/utils';
19
+ import { Package, LogOut, ChevronRight, Circle } from 'lucide-react';
24
20
 
25
21
  import { useLocation, Link } from '@tanstack/react-router';
26
- import { Fragment, useState, useCallback } from 'react';
22
+ import { useState, useCallback } from 'react';
27
23
  import ThemeToggle from './ThemeToggle';
28
24
  import { useAdminSidebarMenu } from '../hooks/menu';
29
25
  import type { AdminMenuItem } from '../types';
@@ -44,6 +40,96 @@ type AdminLayoutProps = {
44
40
  onLogout?: () => void | Promise<void>;
45
41
  };
46
42
 
43
+ // Design-system sizes: 264px expanded, 72px collapsed (icon rail).
44
+ const SIDEBAR_STYLE = {
45
+ '--sidebar-width': '16.5rem',
46
+ '--sidebar-width-icon': '4.5rem',
47
+ } as React.CSSProperties;
48
+
49
+ // Nav colour tokens from the K3Mart sidebar design. They are scoped to the
50
+ // sidebar body (not the theme) so every theme gets the same nav, and they are
51
+ // set on an element inside <Sidebar> so the mobile sheet — which is portalled
52
+ // out of the layout tree — picks them up too. Accent follows the theme's
53
+ // --sidebar-primary.
54
+ // Dark values use `[.dark_&]:` rather than `dark:` — theme CSS is compiled
55
+ // without the class-based `dark` variant (it lives in base.css), so `dark:`
56
+ // there falls back to prefers-color-scheme and ignores the theme toggle.
57
+ const NAV_TOKENS = [
58
+ '[--nav-accent:var(--sidebar-primary)]',
59
+ '[--nav-accent-fg:var(--nav-accent)]',
60
+ '[.dark_&]:[--nav-accent-fg:color-mix(in_srgb,var(--nav-accent)_60%,#ffffff)]',
61
+ '[--nav-text:#18181B] [.dark_&]:[--nav-text:#FAFAFA]',
62
+ '[--nav-text-2:#52525B] [.dark_&]:[--nav-text-2:#D4D4D8]',
63
+ '[--nav-text-3:#6B7280] [.dark_&]:[--nav-text-3:#A1A1AA]',
64
+ '[--nav-muted:#9CA3AF] [.dark_&]:[--nav-muted:#8A8A93]',
65
+ '[--nav-hover:#F1F1F3] [.dark_&]:[--nav-hover:#26262B]',
66
+ ].join(' ');
67
+
68
+ // Collapsed (icon rail) helpers — the Sidebar root carries data-collapsible.
69
+ const COLLAPSED_CENTER =
70
+ 'group-data-[collapsible=icon]:justify-center group-data-[collapsible=icon]:px-0';
71
+ const COLLAPSED_HIDDEN = 'group-data-[collapsible=icon]:hidden';
72
+
73
+ const FOCUS_RING =
74
+ 'outline-hidden focus-visible:ring-2 focus-visible:ring-sidebar-ring';
75
+ const ROW_BASE = `flex w-full items-center rounded-[7px] text-left transition-colors duration-100 ${FOCUS_RING} disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50`;
76
+ // Top-level row: icon + label (+ chevron for groups).
77
+ const TOP_ROW = `${ROW_BASE} gap-[11px] px-3 py-2 text-[13.5px] ${COLLAPSED_CENTER} group-data-[collapsible=icon]:py-2.5`;
78
+ // Nested row: text only.
79
+ const SUB_ROW = `${ROW_BASE} gap-2 px-3 py-2 text-[13px]`;
80
+ const ROW_IDLE = 'text-(color:--nav-text-2) hover:bg-(--nav-hover)';
81
+ const ROW_ACTIVE =
82
+ 'bg-[color-mix(in_srgb,var(--nav-accent)_15%,transparent)] font-semibold text-(color:--nav-accent-fg) hover:bg-[color-mix(in_srgb,var(--nav-accent)_24%,transparent)]';
83
+
84
+ function Chevron({ open }: { open: boolean }) {
85
+ return (
86
+ <ChevronRight
87
+ className={cn(
88
+ 'size-[15px] shrink-0 text-(color:--nav-muted) transition-transform duration-200',
89
+ open && 'rotate-90',
90
+ COLLAPSED_HIDDEN,
91
+ )}
92
+ />
93
+ );
94
+ }
95
+
96
+ function NavBadge({ value }: { value: AdminMenuItem['badge'] }) {
97
+ if (value === undefined || value === null || value === '') return null;
98
+ return (
99
+ <span
100
+ className={cn(
101
+ 'ml-auto shrink-0 rounded-full bg-[color-mix(in_srgb,var(--nav-accent)_12%,transparent)] px-2 py-0.5 text-xs font-medium text-(color:--nav-accent-fg)',
102
+ COLLAPSED_HIDDEN,
103
+ )}
104
+ >
105
+ {value}
106
+ </span>
107
+ );
108
+ }
109
+
110
+ // Tooltip with the item title, only shown on the collapsed icon rail.
111
+ function RailTooltip({
112
+ title,
113
+ children,
114
+ }: {
115
+ title: string;
116
+ children: React.ReactElement;
117
+ }) {
118
+ const { isMobile, state } = useSidebar();
119
+ return (
120
+ <Tooltip>
121
+ <TooltipTrigger asChild>{children}</TooltipTrigger>
122
+ <TooltipContent
123
+ side="right"
124
+ align="center"
125
+ hidden={state !== 'collapsed' || isMobile}
126
+ >
127
+ {title}
128
+ </TooltipContent>
129
+ </Tooltip>
130
+ );
131
+ }
132
+
47
133
  function AdminLayoutContent({
48
134
  children,
49
135
  defaultItemIcon,
@@ -88,98 +174,114 @@ function AdminLayoutContent({
88
174
  const isActive = isMenuItemActive(item.url);
89
175
  const isOpen =
90
176
  openMap[item.id] ?? (hasChildren && hasActiveDescendant(item));
177
+ const isTop = level === 0;
178
+ const Icon = item.icon ?? FallbackIcon;
91
179
 
92
- // Nesting indentation is provided by the bordered <ul> wrapper around each
93
- // child group (see below), so individual rows need no extra left padding.
94
- const indent = '';
180
+ const icon = isTop ? (
181
+ <span className="inline-flex size-[18px] shrink-0 items-center justify-center text-(color:--nav-text-3) group-aria-[current=page]/row:text-(color:--nav-accent-fg)">
182
+ <Icon className="size-[18px]" />
183
+ </span>
184
+ ) : null;
95
185
 
96
- // When a row is active its background is the (teal) sidebar-primary, so
97
- // icon + label must flip to the contrasting foreground to stay readable.
98
- const activeText = isActive
99
- ? 'text-sidebar-primary-foreground'
100
- : 'text-sidebar-foreground group-hover:text-sidebar-accent-foreground';
186
+ const label = (
187
+ <span className={cn('min-w-0 flex-1 truncate', COLLAPSED_HIDDEN)}>
188
+ {item.title}
189
+ </span>
190
+ );
101
191
 
102
- const content = (
103
- <SidebarMenuItem
104
- key={item.id}
105
- className="mb-1"
106
- >
107
- <SidebarMenuButton
108
- asChild={!!item.url && !hasChildren}
109
- isActive={isActive}
110
- tooltip={item.title}
192
+ let row: React.ReactElement;
193
+
194
+ if (hasChildren) {
195
+ row = (
196
+ <button
197
+ type="button"
198
+ aria-expanded={isOpen}
199
+ disabled={item.disabled}
111
200
  onClick={() => {
112
- if (hasChildren) setOpen(item.id);
201
+ item.onClick?.();
202
+ setOpen(item.id, !isOpen);
113
203
  }}
114
- disabled={item.disabled}
115
- className={`group relative overflow-hidden rounded-lg transition-all duration-200 hover:bg-[#FAFAFA] data-[active=true]:bg-sidebar-primary data-[active=true]:text-sidebar-primary-foreground ${indent}`}
204
+ className={cn(isTop ? TOP_ROW : SUB_ROW, ROW_IDLE)}
116
205
  >
117
- {item.url && !hasChildren ? (
118
- <Link to={item.url} className="flex items-center gap-3 px-3">
119
- {item.icon && (
120
- <item.icon
121
- className={`h-4 w-4 transition-colors ${activeText}`}
122
- />
123
- )}
124
- <span
125
- className={`font-medium transition-colors ${activeText}`}
126
- >
127
- {item.title}
128
- </span>
129
- {item.badge && (
130
- <span
131
- className={`ml-auto text-xs px-2 py-0.5 rounded-full ${isActive ? 'bg-white/20 text-sidebar-primary-foreground' : 'bg-sidebar-primary/10 text-sidebar-primary'}`}
132
- >
133
- {item.badge}
134
- </span>
135
- )}
136
- </Link>
137
- ) : (
138
- <div className="flex items-center gap-3 px-3 py-2.5 w-full">
139
- {hasChildren &&
140
- (isOpen ? (
141
- <ChevronDown
142
- className={`h-3.5 w-3.5 ${isActive ? 'text-sidebar-primary-foreground' : 'text-muted-foreground'}`}
143
- />
144
- ) : (
145
- <ChevronRight
146
- className={`h-3.5 w-3.5 ${isActive ? 'text-sidebar-primary-foreground' : 'text-muted-foreground'}`}
147
- />
148
- ))}
149
- {item.icon ? (
150
- <item.icon className={`h-4 w-4 ${activeText}`} />
151
- ) : (
152
- <FallbackIcon
153
- className={`h-3.5 w-3.5 ${isActive ? 'text-sidebar-primary-foreground' : 'text-muted-foreground'}`}
154
- />
155
- )}
156
- {item.url ? (
157
- <Link to={item.url} className="flex-1 text-left">
158
- <span className={`font-medium ${activeText}`}>
159
- {item.title}
160
- </span>
161
- </Link>
162
- ) : (
163
- <span className={`font-medium ${activeText} flex-1`}>
164
- {item.title}
165
- </span>
166
- )}
167
- {item.badge && (
168
- <span
169
- className={`ml-auto text-xs px-2 py-0.5 rounded-full ${isActive ? 'bg-white/20 text-sidebar-primary-foreground' : 'bg-sidebar-primary/10 text-sidebar-primary'}`}
170
- >
171
- {item.badge}
172
- </span>
173
- )}
174
- </div>
206
+ {icon}
207
+ {label}
208
+ <NavBadge value={item.badge} />
209
+ <Chevron open={isOpen} />
210
+ </button>
211
+ );
212
+ } else if (isTop && !item.icon && item.url) {
213
+ // Icon-less top-level link (e.g. "Production") renders as the design's
214
+ // sub-heading row. On the collapsed rail it falls back to an icon so it
215
+ // stays reachable.
216
+ row = (
217
+ <Link
218
+ to={item.url}
219
+ onClick={item.onClick}
220
+ aria-current={isActive ? 'page' : undefined}
221
+ aria-disabled={item.disabled || undefined}
222
+ className={cn(
223
+ 'flex w-full items-center rounded-[7px] px-3 pt-2 pb-1 text-left text-[12.5px] font-semibold transition-colors aria-disabled:pointer-events-none aria-disabled:opacity-50',
224
+ FOCUS_RING,
225
+ 'group-data-[collapsible=icon]:justify-center group-data-[collapsible=icon]:px-0 group-data-[collapsible=icon]:py-2.5 group-data-[collapsible=icon]:hover:bg-(--nav-hover)',
226
+ isActive
227
+ ? 'text-(color:--nav-accent-fg)'
228
+ : 'text-(color:--nav-text-3) hover:text-(color:--nav-text-2)',
175
229
  )}
176
- </SidebarMenuButton>
230
+ >
231
+ <span className="hidden size-[18px] shrink-0 items-center justify-center text-(color:--nav-text-3) group-data-[collapsible=icon]:inline-flex">
232
+ <FallbackIcon className="size-[18px]" />
233
+ </span>
234
+ {label}
235
+ <NavBadge value={item.badge} />
236
+ </Link>
237
+ );
238
+ } else if (item.url) {
239
+ row = (
240
+ <Link
241
+ to={item.url}
242
+ onClick={item.onClick}
243
+ aria-current={isActive ? 'page' : undefined}
244
+ aria-disabled={item.disabled || undefined}
245
+ className={cn(
246
+ 'group/row',
247
+ isTop ? TOP_ROW : SUB_ROW,
248
+ isActive ? ROW_ACTIVE : ROW_IDLE,
249
+ )}
250
+ >
251
+ {icon}
252
+ {label}
253
+ <NavBadge value={item.badge} />
254
+ </Link>
255
+ );
256
+ } else {
257
+ row = (
258
+ <button
259
+ type="button"
260
+ disabled={item.disabled}
261
+ onClick={item.onClick}
262
+ className={cn(isTop ? TOP_ROW : SUB_ROW, ROW_IDLE)}
263
+ >
264
+ {icon}
265
+ {label}
266
+ <NavBadge value={item.badge} />
267
+ </button>
268
+ );
269
+ }
270
+
271
+ return (
272
+ <SidebarMenuItem key={item.id}>
273
+ {isTop ? <RailTooltip title={item.title}>{row}</RailTooltip> : row}
177
274
  {hasChildren && isOpen && (
178
275
  // Submenu must be wrapped in a list element so the SidebarMenuItem
179
276
  // (rendered as <li>) children don't end up as direct descendants of
180
277
  // a <div> inside another <li> — that's invalid HTML and triggers a
181
278
  // React hydration warning.
182
- <ul className="mt-1 ml-[18px] flex flex-col list-none border-l border-sidebar-border pl-2 py-0.5 m-0 group-data-[collapsible=icon]:hidden">
279
+ <ul
280
+ className={cn(
281
+ 'mt-px mb-[3px] ml-5 flex flex-col gap-px border-l border-sidebar-border pl-2.5',
282
+ COLLAPSED_HIDDEN,
283
+ )}
284
+ >
183
285
  {(item.children ?? []).map((child: AdminMenuItem) =>
184
286
  renderItem(child, Math.min(level + 1, 3)),
185
287
  )}
@@ -187,74 +289,82 @@ function AdminLayoutContent({
187
289
  )}
188
290
  </SidebarMenuItem>
189
291
  );
190
-
191
- return content;
192
292
  };
193
293
 
194
294
  return (
195
295
  <div className="flex min-h-dvh w-full">
196
296
  <Sidebar
197
- className="bg-sidebar border-r border-sidebar-border"
297
+ className="border-r border-sidebar-border"
198
298
  collapsible={sidebarCollapsible ? 'icon' : 'none'}
199
299
  >
200
- <SidebarHeader className="bg-sidebar border-b border-sidebar-border">
201
- {sidebarHeader ? (
202
- sidebarHeader
203
- ) : (
204
- <div className="sidebar-header-content flex items-center justify-center px-4 py-3 group-data-[collapsible=icon]:px-0">
205
- <div className="flex items-center gap-2 group-data-[collapsible=icon]:gap-0">
206
- <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-[#1A9E8F] text-sidebar-primary-foreground">
207
- <SidebarHeaderIcon className="h-4 w-4" />
300
+ <div
301
+ className={cn(
302
+ 'flex h-full min-h-0 w-full flex-col bg-sidebar',
303
+ NAV_TOKENS,
304
+ )}
305
+ >
306
+ <SidebarHeader className="gap-0 p-0">
307
+ {sidebarHeader ? (
308
+ sidebarHeader
309
+ ) : (
310
+ <div
311
+ className={cn(
312
+ 'sidebar-header-content flex items-center gap-[11px] px-5 pt-[18px] pb-3.5',
313
+ COLLAPSED_CENTER,
314
+ )}
315
+ >
316
+ <div className="flex size-[34px] shrink-0 items-center justify-center rounded-[9px] bg-(--nav-accent) text-white">
317
+ <SidebarHeaderIcon className="size-4" />
208
318
  </div>
209
- <span className="font-bold text-[18px] text-[#0d9488] tracking-[-0.01em] group-data-[collapsible=icon]:hidden">
319
+ <span
320
+ className={cn(
321
+ 'truncate text-lg font-bold tracking-[-0.01em] text-(color:--nav-text)',
322
+ COLLAPSED_HIDDEN,
323
+ )}
324
+ >
210
325
  {sidebarTitle}
211
326
  </span>
212
327
  </div>
213
- </div>
214
- )}
215
- </SidebarHeader>
216
- <SidebarContent className="bg-sidebar px-2 py-4">
217
- {sidebarGroups.map((group, idx) => (
218
- <Fragment key={group.id}>
219
- <SidebarGroup>
220
- <SidebarGroupLabel className="text-[10.5px] font-semibold text-[#9CA3AF] uppercase tracking-[0.14em] px-3 pt-2 mb-1.5 group-data-[collapsible=icon]:hidden">
328
+ )}
329
+ </SidebarHeader>
330
+ <SidebarContent className="gap-0.5 overflow-x-hidden px-3 pt-1 pb-5 group-data-[collapsible=icon]:overflow-auto">
331
+ {sidebarGroups.map((group) => (
332
+ <div key={group.id} className="flex flex-col gap-0.5">
333
+ <div
334
+ className={cn(
335
+ 'px-3 pt-3.5 pb-1.5 text-[10.5px] font-bold uppercase tracking-[0.12em] text-(color:--nav-muted)',
336
+ COLLAPSED_HIDDEN,
337
+ )}
338
+ >
221
339
  {group.label}
222
- </SidebarGroupLabel>
223
- <SidebarGroupContent>
224
- <SidebarMenu className="group-data-[collapsible=icon]:items-center">
225
- {group.items.map((item) => renderItem(item, 0))}
226
- </SidebarMenu>
227
- </SidebarGroupContent>
228
- </SidebarGroup>
229
- {idx < sidebarGroups.length - 1 && (
230
- <Separator
231
- orientation="horizontal"
232
- className="bg-sidebar-border group-data-[collapsible=icon]:hidden"
233
- />
234
- )}
235
- </Fragment>
236
- ))}
237
- </SidebarContent>
238
- {onLogout && (
239
- <SidebarFooter className="bg-sidebar border-t border-sidebar-border px-2 py-3">
240
- <SidebarMenu>
241
- <SidebarMenuItem className="group-data-[collapsible=icon]:flex group-data-[collapsible=icon]:w-full group-data-[collapsible=icon]:justify-center">
242
- <SidebarMenuButton
243
- tooltip="Logout"
244
- className="cursor-pointer group relative overflow-hidden rounded-lg mx-1 transition-all duration-200 hover:bg-[#FAFAFA] hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:flex group-data-[collapsible=icon]:w-full group-data-[collapsible=icon]:items-center group-data-[collapsible=icon]:!justify-center group-data-[collapsible=icon]:!px-0 group-data-[collapsible=icon]:!gap-0"
245
- onClick={() => { void onLogout(); }}
340
+ </div>
341
+ <SidebarMenu className="gap-0.5">
342
+ {group.items.map((item) => renderItem(item, 0))}
343
+ </SidebarMenu>
344
+ </div>
345
+ ))}
346
+ </SidebarContent>
347
+ {onLogout && (
348
+ <SidebarFooter className="gap-0 border-t border-sidebar-border p-0">
349
+ <RailTooltip title="Logout">
350
+ <button
351
+ type="button"
352
+ onClick={() => {
353
+ void onLogout();
354
+ }}
355
+ className={cn(
356
+ 'flex w-full cursor-pointer items-center gap-[11px] px-5 py-3.5 text-left text-[13.5px] font-medium text-(color:--nav-text-2) transition-colors hover:bg-(--nav-hover)',
357
+ FOCUS_RING,
358
+ COLLAPSED_CENTER,
359
+ )}
246
360
  >
247
- <div className="flex items-center gap-3 px-3 py-2.5 group-data-[collapsible=icon]:w-full group-data-[collapsible=icon]:items-center group-data-[collapsible=icon]:!justify-center group-data-[collapsible=icon]:!gap-0 group-data-[collapsible=icon]:!px-0">
248
- <LogOut className="h-4 w-4 text-sidebar-foreground group-hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:mx-auto group-data-[collapsible=icon]:mr-0" />
249
- <span className="font-medium text-sidebar-foreground group-hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:hidden">
250
- Logout
251
- </span>
252
- </div>
253
- </SidebarMenuButton>
254
- </SidebarMenuItem>
255
- </SidebarMenu>
256
- </SidebarFooter>
257
- )}
361
+ <LogOut className="size-[18px] shrink-0" />
362
+ <span className={COLLAPSED_HIDDEN}>Logout</span>
363
+ </button>
364
+ </RailTooltip>
365
+ </SidebarFooter>
366
+ )}
367
+ </div>
258
368
  </Sidebar>
259
369
  <SidebarInset>
260
370
  <header className="flex h-[69px] shrink-0 items-center justify-between gap-4 border-b border-border/40 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 px-6">
@@ -269,7 +379,9 @@ function AdminLayoutContent({
269
379
  {headerAfterTheme}
270
380
  </div>
271
381
  </header>
272
- <main className="flex min-w-0 flex-1 flex-col gap-4 p-4">{children}</main>
382
+ <main className="flex min-w-0 flex-1 flex-col gap-4 p-4">
383
+ {children}
384
+ </main>
273
385
  </SidebarInset>
274
386
  </div>
275
387
  );
@@ -288,7 +400,7 @@ export default function AdminLayout({
288
400
  }: AdminLayoutProps) {
289
401
  return (
290
402
  <AdminMenuProvider>
291
- <SidebarProvider>
403
+ <SidebarProvider style={SIDEBAR_STYLE}>
292
404
  <AdminLayoutContent
293
405
  defaultItemIcon={defaultItemIcon}
294
406
  sidebarCollapsible={sidebarCollapsible}