@djangocfg/layouts 2.1.628 → 2.1.635
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/package.json +12 -12
- package/src/layouts/PrivateLayout/components/SidebarBody.tsx +27 -2
- package/src/layouts/PrivateLayout/components/SidebarBrand.tsx +4 -4
- package/src/layouts/PrivateLayout/components/SidebarNavGroup.tsx +1 -1
- package/src/layouts/PrivateLayout/components/SidebarNavItem.tsx +1 -1
- package/src/layouts/PrivateLayout/components/SidebarViews.test.tsx +33 -0
- package/src/layouts/PrivateLayout/components/SidebarViews.tsx +48 -0
- package/src/layouts/PrivateLayout/context.tsx +2 -2
- package/src/layouts/PrivateLayout/geometry.test.ts +2 -2
- package/src/layouts/PrivateLayout/geometry.ts +2 -1
- package/src/layouts/PrivateLayout/index.ts +1 -0
- package/src/layouts/PrivateLayout/types.ts +19 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@djangocfg/layouts",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.635",
|
|
4
4
|
"description": "Simple, straightforward layout components for Next.js - import and use with props",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"layouts",
|
|
@@ -91,11 +91,11 @@
|
|
|
91
91
|
"test": "vitest run"
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
|
-
"@djangocfg/analytics": "^2.1.
|
|
95
|
-
"@djangocfg/api": "^2.1.
|
|
96
|
-
"@djangocfg/devtools": "^2.1.
|
|
97
|
-
"@djangocfg/i18n": "^2.1.
|
|
98
|
-
"@djangocfg/ui-core": "^2.1.
|
|
94
|
+
"@djangocfg/analytics": "^2.1.635",
|
|
95
|
+
"@djangocfg/api": "^2.1.635",
|
|
96
|
+
"@djangocfg/devtools": "^2.1.635",
|
|
97
|
+
"@djangocfg/i18n": "^2.1.635",
|
|
98
|
+
"@djangocfg/ui-core": "^2.1.635",
|
|
99
99
|
"@hookform/resolvers": "^5.2.2 || ^7.0.2",
|
|
100
100
|
"consola": "^3.4.2",
|
|
101
101
|
"lucide-react": "^1.68.0",
|
|
@@ -123,12 +123,12 @@
|
|
|
123
123
|
"qrcode.react": "^4.2.0"
|
|
124
124
|
},
|
|
125
125
|
"devDependencies": {
|
|
126
|
-
"@djangocfg/analytics": "^2.1.
|
|
127
|
-
"@djangocfg/api": "^2.1.
|
|
128
|
-
"@djangocfg/devtools": "^2.1.
|
|
129
|
-
"@djangocfg/i18n": "^2.1.
|
|
130
|
-
"@djangocfg/typescript-config": "^2.1.
|
|
131
|
-
"@djangocfg/ui-core": "^2.1.
|
|
126
|
+
"@djangocfg/analytics": "^2.1.635",
|
|
127
|
+
"@djangocfg/api": "^2.1.635",
|
|
128
|
+
"@djangocfg/devtools": "^2.1.635",
|
|
129
|
+
"@djangocfg/i18n": "^2.1.635",
|
|
130
|
+
"@djangocfg/typescript-config": "^2.1.635",
|
|
131
|
+
"@djangocfg/ui-core": "^2.1.635",
|
|
132
132
|
"@storybook/react-vite": "^10.6.5",
|
|
133
133
|
"@types/node": "^25.9.5",
|
|
134
134
|
"@types/react": "19.3.0",
|
|
@@ -15,13 +15,36 @@ import { PrivateSidebarAccount } from './PrivateSidebarAccount';
|
|
|
15
15
|
import { SidebarBrand } from './SidebarBrand';
|
|
16
16
|
import { SidebarNavGroup } from './SidebarNavGroup';
|
|
17
17
|
import { SidebarSlots } from './SidebarSlots';
|
|
18
|
+
import { SidebarViews } from './SidebarViews';
|
|
18
19
|
|
|
19
20
|
export function SidebarBody() {
|
|
20
21
|
const { sidebar, header, isMobile, accountInBar } = usePrivateLayoutContext();
|
|
21
22
|
|
|
23
|
+
const views = sidebar?.views;
|
|
24
|
+
// The last pick survives a reload (a viewer's own habit, not app state).
|
|
25
|
+
const [picked, setPicked] = React.useState<string | undefined>(() => {
|
|
26
|
+
try {
|
|
27
|
+
return typeof window === 'undefined' ? undefined : (window.localStorage.getItem('sidebar-view') ?? undefined);
|
|
28
|
+
} catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
const activeId = sidebar?.activeView ?? picked ?? views?.[0]?.id;
|
|
33
|
+
const activeView = views?.find((v) => v.id === activeId) ?? views?.[0];
|
|
34
|
+
const pick = (id: string) => {
|
|
35
|
+
setPicked(id);
|
|
36
|
+
try {
|
|
37
|
+
window.localStorage.setItem('sidebar-view', id);
|
|
38
|
+
} catch {
|
|
39
|
+
// Private mode: the pick lasts for the session.
|
|
40
|
+
}
|
|
41
|
+
sidebar?.onViewChange?.(id);
|
|
42
|
+
};
|
|
43
|
+
const shown = useMemo(() => [...(sidebar?.groups ?? []), ...(activeView?.groups ?? [])], [sidebar?.groups, activeView]);
|
|
44
|
+
|
|
22
45
|
const groups = useMemo(
|
|
23
46
|
() =>
|
|
24
|
-
|
|
47
|
+
shown.map((group) =>
|
|
25
48
|
// A group of launchers for sections nobody registered renders nothing itself (see SidebarNavGroup).
|
|
26
49
|
group.dynamic && group.items.length === 0 ? null : (
|
|
27
50
|
<SidebarNavGroup
|
|
@@ -30,7 +53,7 @@ export function SidebarBody() {
|
|
|
30
53
|
/>
|
|
31
54
|
),
|
|
32
55
|
),
|
|
33
|
-
[
|
|
56
|
+
[shown],
|
|
34
57
|
);
|
|
35
58
|
|
|
36
59
|
// Three zones: pinned header, the ONLY scroller (nav), pinned footer. The header gets a border once the nav scrolls.
|
|
@@ -51,6 +74,8 @@ export function SidebarBody() {
|
|
|
51
74
|
SIDEBAR_SPACING[isMobile ? 'mobile' : 'desktop'].body,
|
|
52
75
|
)}
|
|
53
76
|
>
|
|
77
|
+
{views && views.length > 0 && activeView ? <SidebarViews views={views} active={activeView.id} onChange={pick} /> : null}
|
|
78
|
+
{activeView?.menuStart}
|
|
54
79
|
<SidebarSlots />
|
|
55
80
|
{groups}
|
|
56
81
|
</div>
|
|
@@ -20,7 +20,7 @@ const BRAND_LINK =
|
|
|
20
20
|
'flex min-w-0 items-center gap-2.5 rounded-lg py-1 text-sidebar-foreground no-underline outline-none ring-sidebar-ring transition-opacity hover:no-underline hover:opacity-80 focus-visible:ring-2';
|
|
21
21
|
|
|
22
22
|
/** Brand name: semibold, one step larger on desktop (18px), 16px on a phone; one line, truncated. */
|
|
23
|
-
const BRAND_NAME = 'truncate text-base font-semibold tracking-tight md:text-
|
|
23
|
+
const BRAND_NAME = 'truncate text-base font-semibold tracking-tight md:text-sm md:font-medium';
|
|
24
24
|
|
|
25
25
|
function SidebarBrandRaw({ bordered = false }: { bordered?: boolean }) {
|
|
26
26
|
const { header, homeHref, brandTitle, brandMonogram, isMobile } = usePrivateLayoutContext();
|
|
@@ -47,13 +47,13 @@ function SidebarBrandRaw({ bordered = false }: { bordered?: boolean }) {
|
|
|
47
47
|
<span
|
|
48
48
|
className={cn(
|
|
49
49
|
'flex shrink-0 items-center justify-center rounded-lg bg-sidebar-primary',
|
|
50
|
-
isMobile ? 'size-7' : 'size-
|
|
50
|
+
isMobile ? 'size-7' : 'size-6',
|
|
51
51
|
)}
|
|
52
52
|
>
|
|
53
53
|
{header?.brandIcon ? (
|
|
54
|
-
<LucideIcon icon={header.brandIcon} className="size-4 text-sidebar-primary-foreground md:size-
|
|
54
|
+
<LucideIcon icon={header.brandIcon} className="size-4 text-sidebar-primary-foreground md:size-4" />
|
|
55
55
|
) : (
|
|
56
|
-
<span className="text-xs font-bold leading-none text-sidebar-primary-foreground">
|
|
56
|
+
<span className="text-xs font-bold leading-none md:text-[11px] text-sidebar-primary-foreground">
|
|
57
57
|
{brandMonogram}
|
|
58
58
|
</span>
|
|
59
59
|
)}
|
|
@@ -48,7 +48,7 @@ function GroupDivider({ inset }: { inset: string }) {
|
|
|
48
48
|
role="separator"
|
|
49
49
|
aria-orientation="horizontal"
|
|
50
50
|
data-sidebar="group-divider"
|
|
51
|
-
className={cn(inset, 'mb-3 mt-3 h-
|
|
51
|
+
className={cn(inset, 'mb-3 mt-3 h-(--sidebar-divider-h) shrink-0 bg-sidebar-border')}
|
|
52
52
|
/>
|
|
53
53
|
);
|
|
54
54
|
}
|
|
@@ -27,7 +27,7 @@ interface SidebarNavItemProps {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const ITEM_CLASS = cn(
|
|
30
|
-
'rounded-
|
|
30
|
+
'rounded-(--sidebar-item-radius) border-0 font-normal shadow-none transition-colors duration-150',
|
|
31
31
|
'text-sidebar-foreground/90 hover:bg-sidebar-accent/70 hover:text-sidebar-foreground',
|
|
32
32
|
'data-[active=true]:bg-sidebar-accent data-[active=true]:font-normal data-[active=true]:text-sidebar-foreground',
|
|
33
33
|
'[&>svg]:text-sidebar-foreground/80 [&>svg]:opacity-100 hover:[&>svg]:text-sidebar-foreground',
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import React, { act } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { Home, MessageCircle } from 'lucide-react';
|
|
5
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { SidebarViews } from './SidebarViews';
|
|
8
|
+
|
|
9
|
+
// react-dom + jsdom only: the package declares no testing-library.
|
|
10
|
+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
|
11
|
+
|
|
12
|
+
const views = [
|
|
13
|
+
{ id: 'home', label: 'Home', icon: Home, groups: [] },
|
|
14
|
+
{ id: 'chat', label: 'Chat', icon: MessageCircle, groups: [] },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
describe('SidebarViews', () => {
|
|
18
|
+
it('shows the label of the active view only, and reports a pick', () => {
|
|
19
|
+
const onChange = vi.fn();
|
|
20
|
+
const host = document.createElement('div');
|
|
21
|
+
document.body.appendChild(host);
|
|
22
|
+
const root = createRoot(host);
|
|
23
|
+
act(() => root.render(<SidebarViews views={views} active="home" onChange={onChange} />));
|
|
24
|
+
const [home, chat] = Array.from(host.querySelectorAll<HTMLElement>('[role=tab]'));
|
|
25
|
+
expect(home.getAttribute('aria-selected')).toBe('true');
|
|
26
|
+
expect(home.textContent).toBe('Home');
|
|
27
|
+
expect(chat.textContent).toBe('');
|
|
28
|
+
act(() => chat.click());
|
|
29
|
+
expect(onChange).toHaveBeenCalledWith('chat');
|
|
30
|
+
act(() => root.unmount());
|
|
31
|
+
host.remove();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The view switcher: a row of icon buttons at the top of the menu. The active view also shows its label, so the row
|
|
3
|
+
* names where you are without spending width on the others (Notion's Home / Chat / Calendar strip).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use client';
|
|
7
|
+
|
|
8
|
+
import React from 'react';
|
|
9
|
+
|
|
10
|
+
import { cn } from '@djangocfg/ui-core/lib';
|
|
11
|
+
|
|
12
|
+
import { LucideIcon } from '../../../components';
|
|
13
|
+
import type { SidebarViewConfig } from '../types';
|
|
14
|
+
|
|
15
|
+
interface SidebarViewsProps {
|
|
16
|
+
views: SidebarViewConfig[];
|
|
17
|
+
active: string;
|
|
18
|
+
onChange: (id: string) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function SidebarViews({ views, active, onChange }: SidebarViewsProps) {
|
|
22
|
+
return (
|
|
23
|
+
<div role="tablist" aria-label="Sidebar views" className="mb-1 flex items-center gap-1 px-2">
|
|
24
|
+
{views.map((view) => {
|
|
25
|
+
const on = view.id === active;
|
|
26
|
+
return (
|
|
27
|
+
<button
|
|
28
|
+
key={view.id}
|
|
29
|
+
type="button"
|
|
30
|
+
role="tab"
|
|
31
|
+
aria-selected={on}
|
|
32
|
+
aria-label={view.label}
|
|
33
|
+
title={view.label}
|
|
34
|
+
onClick={() => onChange(view.id)}
|
|
35
|
+
className={cn(
|
|
36
|
+
'flex h-(--sidebar-item-h) items-center gap-2 rounded-(--sidebar-item-radius) px-2 text-(length:--sidebar-item-font) transition-colors',
|
|
37
|
+
'[&>svg]:size-4 [&>svg]:shrink-0',
|
|
38
|
+
on ? 'bg-sidebar-accent font-medium text-sidebar-foreground' : 'text-sidebar-foreground/70 hover:bg-sidebar-accent/70 hover:text-sidebar-foreground',
|
|
39
|
+
)}
|
|
40
|
+
>
|
|
41
|
+
<LucideIcon icon={view.icon} />
|
|
42
|
+
{on ? <span>{view.label}</span> : null}
|
|
43
|
+
</button>
|
|
44
|
+
);
|
|
45
|
+
})}
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -73,8 +73,8 @@ export function PrivateLayoutProvider({
|
|
|
73
73
|
).toUpperCase();
|
|
74
74
|
|
|
75
75
|
const allItems = useMemo(
|
|
76
|
-
() => sidebar?.groups.flatMap((g) => g.items).filter(isLinkItem)
|
|
77
|
-
[sidebar?.groups],
|
|
76
|
+
() => [...(sidebar?.groups ?? []), ...(sidebar?.views?.flatMap((v) => v.groups) ?? [])].flatMap((g) => g.items).filter(isLinkItem),
|
|
77
|
+
[sidebar?.groups, sidebar?.views],
|
|
78
78
|
);
|
|
79
79
|
|
|
80
80
|
// The most specific item wins: `/a` is not active on `/a/b` when `/a/b` is its own item.
|
|
@@ -7,7 +7,7 @@ const SIZES = /\btext-(base|lg|xl|2xl)\b/;
|
|
|
7
7
|
describe('sidebar geometry', () => {
|
|
8
8
|
for (const form of ['desktop', 'mobile'] as const) {
|
|
9
9
|
it(`${form}: rows use 14px text and a 16-20px icon, not a large font`, () => {
|
|
10
|
-
expect(NAV_GEOMETRY[form].item).toMatch(/\btext-sm\
|
|
10
|
+
expect(NAV_GEOMETRY[form].item).toMatch(/\btext-(sm|\(length:--sidebar-item-font\))/);
|
|
11
11
|
expect(NAV_GEOMETRY[form].item).not.toMatch(SIZES);
|
|
12
12
|
expect(NAV_GEOMETRY[form].item).toMatch(/\[&>svg\]:size-(4|5)/);
|
|
13
13
|
expect(NAV_GEOMETRY[form].label).toMatch(/\btext-xs\b/);
|
|
@@ -22,7 +22,7 @@ describe('sidebar geometry', () => {
|
|
|
22
22
|
|
|
23
23
|
it('mobile rows are 40px with a 44px hit area (see touch-safe.css); desktop rows are 36px', () => {
|
|
24
24
|
expect(NAV_GEOMETRY.mobile.item).toMatch(/\bh-10\b/);
|
|
25
|
-
expect(NAV_GEOMETRY.desktop.item).
|
|
25
|
+
expect(NAV_GEOMETRY.desktop.item).toContain('h-(--sidebar-item-h)');
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
it('mobile header and footer never go under the status bar or home indicator', () => {
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
export const NAV_GEOMETRY = {
|
|
7
7
|
desktop: {
|
|
8
|
-
|
|
8
|
+
/** Row height, type size and corner come from ui-core tokens (`--sidebar-item-*`), so a preset sets them (Notion: 30px, 14px, 6px). */
|
|
9
|
+
item: 'h-(--sidebar-item-h) gap-2.5 px-2 text-(length:--sidebar-item-font) [&>svg]:size-4',
|
|
9
10
|
label: 'h-7 px-2 text-xs',
|
|
10
11
|
/** Horizontal inset of groups, slots and the divider; equals the header and footer inset. */
|
|
11
12
|
inset: 'px-2',
|
|
@@ -96,9 +96,27 @@ export interface SidebarFeaturedConfig {
|
|
|
96
96
|
// Sidebar Config
|
|
97
97
|
// ============================================================================
|
|
98
98
|
|
|
99
|
+
/**
|
|
100
|
+
* One view of the sidebar: a row in the view switcher (icon; the active one also shows its label) and the groups that
|
|
101
|
+
* replace the menu while it is active, as in Notion's Home / Chat / Calendar strip.
|
|
102
|
+
*/
|
|
103
|
+
export interface SidebarViewConfig<Id extends string = string> {
|
|
104
|
+
id: string;
|
|
105
|
+
label: string;
|
|
106
|
+
icon: LucideIcon;
|
|
107
|
+
groups: SidebarGroupConfig<Id>[];
|
|
108
|
+
/** Block above this view's groups (a "New chat" button, say). */
|
|
109
|
+
menuStart?: ReactNode;
|
|
110
|
+
}
|
|
111
|
+
|
|
99
112
|
export interface SidebarConfig<Id extends string = string> {
|
|
100
|
-
/** Grouped items with labels */
|
|
113
|
+
/** Grouped items with labels. With `views`, these are shown in every view, above the active view's own groups. */
|
|
101
114
|
groups: SidebarGroupConfig<Id>[];
|
|
115
|
+
/** A switcher row at the top of the menu; each view brings its own groups. Absent: the plain menu. */
|
|
116
|
+
views?: SidebarViewConfig<Id>[];
|
|
117
|
+
/** The active view's `id` (controlled). Absent: the sidebar remembers the last pick itself. */
|
|
118
|
+
activeView?: string;
|
|
119
|
+
onViewChange?: (id: string) => void;
|
|
102
120
|
/** Home link href */
|
|
103
121
|
homeHref?: string;
|
|
104
122
|
/**
|