Files
PaperClipAI/ui/src/components/SidebarSection.tsx
T
50bff3b274 feat(ui): add collapsible sidebar rail and takeover panes (#7824)
## Thinking Path

> - Paperclip is the open source control plane people use to manage AI
agents, work, and company context.
> - The board UI sidebar is the main way operators keep orientation
across companies, projects, agents, issues, and settings.
> - The existing fixed expanded sidebar competes with route-specific
navigation, especially company settings and plugin routes that bring
their own contextual sidebar.
> - A collapsible primary rail preserves global navigation while giving
contextual pages more horizontal room.
> - This pull request adds a persisted collapsed rail, hover/focus peek,
keyboard toggle, and a secondary sidebar takeover model for settings and
plugin `routeSidebar` surfaces.
> - The benefit is a denser board shell that keeps the app rail
available without replacing it when a route needs its own navigation.

## Linked Issues or Issue Description

Paperclip issue: PAP-10638 Create collapsible sidebar branch.

Related GitHub PR found during duplicate search: #3838
(`feat/collapsible-sidebar`) covers a similar sidebar area but is a
different head branch and implementation. This PR intentionally packages
the work from `PAP-10638-collapsable-sidebar` into one reviewable
branch.

Problem description:

The board shell needs a first-class collapsed sidebar mode. Contextual
surfaces such as company settings and plugin route sidebars should not
replace the global app sidebar; they should collapse the app sidebar to
a rail and render their contextual navigation beside it.

## What Changed

- Added desktop collapsed/sidebar-peek state to `SidebarContext`,
including persisted user pins, route collapse requests, and forced
collapse for secondary-sidebar routes.
- Replaced the old resizable sidebar pane with `SidebarShell`, which
supports a fixed 64px rail, persisted expanded width, keyboard/pointer
resizing, and hover/focus peek overlay behavior.
- Updated `Sidebar`, sidebar nav items, project/agent sections, badges,
and account/company menu presentation for expanded, collapsed, and
peeking states.
- Added `RequestCollapsedSidebar` and `SecondarySidebar` so routes and
plugin `routeSidebar` slots can request contextual sidebar layouts
without replacing the primary app sidebar.
- Wired company settings and plugin route sidebars into the
secondary-pane takeover model.
- Added focused Vitest coverage for sidebar state precedence, shell
sizing, nav item rail rendering, keyboard shortcuts, layout takeover
behavior, and route collapse requests.
- Updated plugin authoring docs/spec references for route sidebar
behavior.

## Verification

Targeted local verification passed:

```sh
NODE_ENV=test pnpm run preflight:workspace-links && NODE_ENV=test pnpm exec vitest run ui/src/context/SidebarContext.test.tsx ui/src/components/SidebarShell.test.tsx ui/src/components/Sidebar.test.tsx ui/src/components/Layout.test.tsx ui/src/components/RequestCollapsedSidebar.test.tsx ui/src/components/SidebarNavItem.test.tsx ui/src/components/SidebarAgents.test.tsx ui/src/components/SidebarProjects.test.tsx ui/src/components/KeyboardShortcutsCheatsheet.test.tsx ui/src/hooks/useKeyboardShortcuts.test.tsx
```

Result: 10 test files passed, 88 tests passed.

Additional follow-up verification passed after review fixes:

```sh
NODE_ENV=test pnpm run preflight:workspace-links && NODE_ENV=test pnpm exec vitest run ui/src/components/Layout.test.tsx ui/src/context/SidebarContext.test.tsx && pnpm --filter /ui typecheck
```

Result: 2 test files passed, 28 tests passed, and UI typecheck passed.

Latest PR-head remote checks: Paperclip PR workflow, Snyk, Socket, and
Greptile are green; commitperclip `review` is cancelled in its
security-gate step after filing a non-blocking neutral `security-review`
check.

Notes:

- A direct run without `NODE_ENV=test` loads React's production build in
this workspace, where `act` is unavailable; the command above matches
the repo stable runner's test environment.
- I did not run Playwright/browser e2e or full workspace build/typecheck
in this PR-creation heartbeat.
- QA screenshots are attached in
https://github.com/paperclipai/paperclip/pull/7824#issuecomment-4661968387
for expanded, collapsed rail, hover peek, and settings secondary-sidebar
states.

## Risks

- Medium UI layout risk: this changes the board shell and primary
sidebar composition across many routes.
- Local storage migration risk is low: new collapsed state uses a new
key and existing width storage remains scoped to the sidebar width.
- Plugin route risk: plugin `routeSidebar` slots now render as secondary
panes on desktop, so plugin authors should confirm their route sidebar
content fits a 240px contextual pane.
- Mobile risk appears low because mobile keeps the drawer model and
gates collapsed/peek behavior to desktop.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

OpenAI Codex coding agent based on GPT-5, with local shell/git/GitHub
CLI tool use. Exact service-side model identifier and context window
were not exposed in this runtime.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-06-09 13:25:17 -05:00

242 lines
7.9 KiB
TypeScript

import { useState, type ComponentType, type ReactNode } from "react";
import { Link } from "@/lib/router";
import { ChevronRight } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useSidebar } from "../context/SidebarContext";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
import { useSidebarNavExpanded } from "./SidebarNavItem";
type SidebarSectionIcon = ComponentType<{ className?: string }>;
export type SidebarSectionMenuAction =
| {
type: "item";
label: string;
icon?: SidebarSectionIcon;
href?: string;
onSelect?: () => void;
}
| { type: "separator" };
export type SidebarSectionRadioChoice = {
label: string;
value: string;
};
type SidebarSectionMenu = {
actions?: SidebarSectionMenuAction[];
ariaLabel?: string;
radioChoices?: SidebarSectionRadioChoice[];
radioLabel?: string;
radioValue?: string;
onRadioValueChange?: (value: string) => void;
};
type SidebarSectionHeaderAction = {
ariaLabel: string;
icon: SidebarSectionIcon;
onClick: () => void;
};
interface SidebarSectionProps {
label: string;
children: ReactNode;
collapsible?: {
open: boolean;
onOpenChange: (open: boolean) => void;
};
menu?: SidebarSectionMenu;
headerAction?: SidebarSectionHeaderAction;
}
function SidebarSectionHeader({
collapsible,
headerAction,
label,
menu,
}: Pick<SidebarSectionProps, "collapsible" | "headerAction" | "label" | "menu">) {
const { isMobile } = useSidebar();
const [menuOpen, setMenuOpen] = useState(false);
const hasMenu = Boolean(
menu && ((menu.actions?.length ?? 0) > 0 || (menu.radioChoices?.length ?? 0) > 0),
);
const labelClassName = "text-[10px] font-medium uppercase tracking-widest font-mono text-muted-foreground/60";
const headerControlVisibilityClassName = isMobile
? "opacity-100"
: "opacity-0 group-hover/sidebar-section:opacity-100 group-focus-within/sidebar-section:opacity-100";
const caretClassName = cn(
"h-3 w-3 shrink-0 text-muted-foreground/60 transition-all",
headerControlVisibilityClassName,
collapsible?.open && "rotate-90",
menuOpen && "opacity-100",
);
const actionClassName = cn(
"h-5 w-5 shrink-0 text-muted-foreground/60 transition-opacity hover:text-foreground data-[state=open]:opacity-100",
headerControlVisibilityClassName,
);
const headerContent = <span className={labelClassName}>{label}</span>;
const HeaderActionIcon = headerAction?.icon;
const headingControl = hasMenu ? (
<DropdownMenu open={menuOpen} onOpenChange={setMenuOpen}>
<DropdownMenuTrigger asChild>
<button
type="button"
data-slot="icon-button"
className={cn(
"inline-flex min-w-0 max-w-full items-center rounded-md px-1 py-0.5 text-left outline-none transition-colors",
"hover:bg-accent/50 focus-visible:bg-accent/50 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1",
menuOpen && "bg-accent/50",
)}
aria-label={menu?.ariaLabel ?? `${label} actions`}
>
{headerContent}
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-48">
{menu?.actions?.map((action, index) => {
if (action.type === "separator") {
return <DropdownMenuSeparator key={`separator-${index}`} />;
}
const Icon = action.icon;
const content = (
<>
{Icon ? <Icon className="size-4" /> : null}
<span>{action.label}</span>
</>
);
if (action.href) {
return (
<DropdownMenuItem key={`${action.label}-${index}`} asChild>
<Link to={action.href}>{content}</Link>
</DropdownMenuItem>
);
}
return (
<DropdownMenuItem key={`${action.label}-${index}`} onSelect={action.onSelect}>
{content}
</DropdownMenuItem>
);
})}
{menu?.radioChoices && menu.radioChoices.length > 0 ? (
<DropdownMenuRadioGroup
value={menu.radioValue}
onValueChange={menu.onRadioValueChange}
aria-label={menu.radioLabel}
>
{menu.radioChoices.map((choice) => (
<DropdownMenuRadioItem key={choice.value} value={choice.value}>
{choice.label}
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
) : null}
</DropdownMenuContent>
</DropdownMenu>
) : (
<div className="inline-flex min-w-0 max-w-full items-center px-1 py-0.5">{headerContent}</div>
);
return (
<div className="group/sidebar-section px-3 py-1.5 pointer-coarse:py-1">
<div className="relative flex min-h-6 min-w-0 items-center gap-1">
{collapsible ? (
<CollapsibleTrigger asChild>
<button
type="button"
data-slot="icon-button"
className="absolute -left-4 flex h-5 w-5 items-center justify-center rounded-sm outline-none transition-colors hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1"
aria-label={collapsible.open ? `Collapse ${label}` : `Expand ${label}`}
>
<ChevronRight className={caretClassName} aria-hidden="true" />
</button>
</CollapsibleTrigger>
) : null}
{headingControl}
{headerAction && HeaderActionIcon ? (
<Button
variant="ghost"
size="icon-xs"
className={actionClassName}
aria-label={headerAction.ariaLabel}
onClick={headerAction.onClick}
>
<HeaderActionIcon className="h-3.5 w-3.5" />
</Button>
) : null}
</div>
</div>
);
}
export function SidebarSection({
label,
children,
collapsible,
menu,
headerAction,
}: SidebarSectionProps) {
const { collapsed, peeking } = useSidebar();
const forceExpanded = useSidebarNavExpanded();
const rail = collapsed && !peeking && !forceExpanded;
const content = <div className="flex flex-col gap-0.5 mt-0.5">{children}</div>;
// Collapsed rail: the section header would only show a clipped sliver of its
// label, so replace it with a thin divider. The label stays in the a11y tree,
// per-section carets/menus are dropped (no room + no toggle target in the
// rail), and the items always render so their icons stay reachable.
//
// The header wrapper mirrors the expanded header's vertical footprint exactly
// (outer `px-3 py-1.5 pointer-coarse:py-1` + inner `min-h-6`) so item icons land
// at the identical y-position in both states — no movement on collapse/expand
// (PAP-10676). The divider is vertically centered within that same row.
if (rail) {
return (
<div>
<div className="px-3 py-1.5 pointer-coarse:py-1">
<div className="flex min-h-6 items-center">
<span className="sr-only">{label}</span>
<div className="h-px w-full bg-border/60" aria-hidden="true" />
</div>
</div>
{content}
</div>
);
}
if (collapsible) {
return (
<Collapsible open={collapsible.open} onOpenChange={collapsible.onOpenChange}>
<SidebarSectionHeader
label={label}
collapsible={collapsible}
menu={menu}
headerAction={headerAction}
/>
<CollapsibleContent>{content}</CollapsibleContent>
</Collapsible>
);
}
return (
<div>
<SidebarSectionHeader label={label} menu={menu} headerAction={headerAction} />
{content}
</div>
);
}