Skip to content

Commit 4fe728f

Browse files
committed
feat(sidebar): widen default sidebar and make it resizable
Default width goes from 12rem to 256px so long settings entries like "Personal access tokens" fit. The shadcn SidebarRail now resizes the sidebar by dragging (192px–400px, persisted in localStorage); a plain click still toggles it.
1 parent 4c7c357 commit 4fe728f

2 files changed

Lines changed: 105 additions & 8 deletions

File tree

‎src/components/Layout/AppSidebar.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
SidebarMenuSub,
2020
SidebarMenuSubButton,
2121
SidebarMenuSubItem,
22+
SidebarRail,
2223
SidebarSeparator,
2324
SidebarTrigger,
2425
useSidebar
@@ -340,6 +341,7 @@ const AppSidebarInner = React.memo(function AppSidebarInner({
340341
"read"
341342
)}
342343
</SidebarContent>
344+
<SidebarRail />
343345
</Sidebar>
344346
);
345347
});

‎src/components/ui/sidebar.tsx‎

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ import {
2525

2626
const SIDEBAR_COOKIE_NAME = "sidebar_state";
2727
const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7;
28-
const SIDEBAR_WIDTH = "12rem";
28+
const SIDEBAR_WIDTH_STORAGE_KEY = "sidebar_width";
29+
// Widths are in pixels so they can be adjusted by dragging the rail.
30+
const SIDEBAR_WIDTH = 256;
31+
const SIDEBAR_MIN_WIDTH = 192;
32+
const SIDEBAR_MAX_WIDTH = 400;
2933
const SIDEBAR_WIDTH_MOBILE = "18rem";
3034
const SIDEBAR_WIDTH_ICON = "3rem";
3135
const SIDEBAR_KEYBOARD_SHORTCUT = "b";
3236

37+
const clampSidebarWidth = (width: number) =>
38+
Math.min(SIDEBAR_MAX_WIDTH, Math.max(SIDEBAR_MIN_WIDTH, width));
39+
3340
type SidebarContextProps = {
3441
state: "expanded" | "collapsed";
3542
open: boolean;
@@ -38,6 +45,10 @@ type SidebarContextProps = {
3845
setOpenMobile: (open: boolean) => void;
3946
isMobile: boolean;
4047
toggleSidebar: () => void;
48+
width: number;
49+
setWidth: (width: number) => void;
50+
isResizing: boolean;
51+
setIsResizing: (isResizing: boolean) => void;
4152
};
4253

4354
const SidebarContext = React.createContext<SidebarContextProps | null>(null);
@@ -93,6 +104,23 @@ const SidebarProvider = React.forwardRef<
93104
[setOpenProp, open]
94105
);
95106

107+
const [width, _setWidth] = React.useState(() => {
108+
const stored = Number(
109+
typeof window !== "undefined"
110+
? window.localStorage.getItem(SIDEBAR_WIDTH_STORAGE_KEY)
111+
: NaN
112+
);
113+
return Number.isFinite(stored) && stored > 0
114+
? clampSidebarWidth(stored)
115+
: SIDEBAR_WIDTH;
116+
});
117+
const [isResizing, setIsResizing] = React.useState(false);
118+
const setWidth = React.useCallback((value: number) => {
119+
const clamped = clampSidebarWidth(value);
120+
_setWidth(clamped);
121+
window.localStorage.setItem(SIDEBAR_WIDTH_STORAGE_KEY, String(clamped));
122+
}, []);
123+
96124
// Helper to toggle the sidebar.
97125
const toggleSidebar = React.useCallback(() => {
98126
return isMobile
@@ -128,9 +156,24 @@ const SidebarProvider = React.forwardRef<
128156
isMobile,
129157
openMobile,
130158
setOpenMobile,
131-
toggleSidebar
159+
toggleSidebar,
160+
width,
161+
setWidth,
162+
isResizing,
163+
setIsResizing
132164
}),
133-
[state, open, setOpen, isMobile, openMobile, setOpenMobile, toggleSidebar]
165+
[
166+
state,
167+
open,
168+
setOpen,
169+
isMobile,
170+
openMobile,
171+
setOpenMobile,
172+
toggleSidebar,
173+
width,
174+
setWidth,
175+
isResizing
176+
]
134177
);
135178

136179
return (
@@ -139,13 +182,15 @@ const SidebarProvider = React.forwardRef<
139182
<div
140183
style={
141184
{
142-
"--sidebar-width": SIDEBAR_WIDTH,
185+
"--sidebar-width": `${width}px`,
143186
"--sidebar-width-icon": SIDEBAR_WIDTH_ICON,
144187
...style
145188
} as React.CSSProperties
146189
}
190+
data-resizing={isResizing ? "true" : undefined}
147191
className={cn(
148192
"group/sidebar-wrapper flex min-h-svh w-full has-[[data-variant=inset]]:bg-sidebar",
193+
isResizing && "cursor-col-resize select-none",
149194
className
150195
)}
151196
ref={ref}
@@ -233,6 +278,8 @@ const Sidebar = React.forwardRef<
233278
<div
234279
className={cn(
235280
"relative w-[--sidebar-width] bg-transparent transition-[width] duration-200 ease-linear",
281+
// Follow the pointer immediately while dragging the rail.
282+
"group-data-[resizing=true]/sidebar-wrapper:transition-none",
236283
"group-data-[collapsible=offcanvas]:w-0",
237284
"group-data-[side=right]:rotate-180",
238285
variant === "floating" || variant === "inset"
@@ -243,6 +290,7 @@ const Sidebar = React.forwardRef<
243290
<div
244291
className={cn(
245292
"fixed inset-y-0 z-10 hidden h-svh w-[--sidebar-width] transition-[left,right,width] duration-200 ease-linear md:flex",
293+
"group-data-[resizing=true]/sidebar-wrapper:transition-none",
246294
side === "left"
247295
? "left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]"
248296
: "right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]",
@@ -297,19 +345,66 @@ const SidebarRail = React.forwardRef<
297345
HTMLButtonElement,
298346
React.ComponentProps<"button">
299347
>(({ className, ...props }, ref) => {
300-
const { toggleSidebar } = useSidebar();
348+
const { toggleSidebar, state, width, setWidth, setIsResizing } = useSidebar();
349+
// Set when the pointer moved far enough to count as a drag, so the
350+
// click that follows pointerup does not also toggle the sidebar.
351+
const dragRef = React.useRef<{
352+
startX: number;
353+
startWidth: number;
354+
dragged: boolean;
355+
} | null>(null);
301356

302357
return (
303358
<button
304359
ref={ref}
305360
data-sidebar="rail"
306361
aria-label="Toggle Sidebar"
307362
tabIndex={-1}
308-
onClick={toggleSidebar}
309-
title="Toggle Sidebar"
363+
onPointerDown={(event) => {
364+
if (state === "collapsed" || event.button !== 0) {
365+
return;
366+
}
367+
event.currentTarget.setPointerCapture(event.pointerId);
368+
dragRef.current = {
369+
startX: event.clientX,
370+
startWidth: width,
371+
dragged: false
372+
};
373+
}}
374+
onPointerMove={(event) => {
375+
const drag = dragRef.current;
376+
if (!drag) {
377+
return;
378+
}
379+
const delta = event.clientX - drag.startX;
380+
if (!drag.dragged && Math.abs(delta) < 3) {
381+
return;
382+
}
383+
if (!drag.dragged) {
384+
drag.dragged = true;
385+
setIsResizing(true);
386+
}
387+
const side = event.currentTarget
388+
.closest("[data-side]")
389+
?.getAttribute("data-side");
390+
setWidth(drag.startWidth + (side === "right" ? -delta : delta));
391+
}}
392+
onPointerUp={() => setIsResizing(false)}
393+
onPointerCancel={() => {
394+
dragRef.current = null;
395+
setIsResizing(false);
396+
}}
397+
onClick={() => {
398+
const dragged = dragRef.current?.dragged;
399+
dragRef.current = null;
400+
if (!dragged) {
401+
toggleSidebar();
402+
}
403+
}}
404+
title="Drag to resize, click to toggle sidebar"
310405
className={cn(
311406
"absolute inset-y-0 z-20 hidden w-4 -translate-x-1/2 transition-all ease-linear after:absolute after:inset-y-0 after:left-1/2 after:w-[2px] hover:after:bg-sidebar-border group-data-[side=left]:-right-4 group-data-[side=right]:left-0 sm:flex",
312-
"[[data-side=left]_&]:cursor-w-resize [[data-side=right]_&]:cursor-e-resize",
407+
"cursor-col-resize",
313408
"[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize",
314409
"group-data-[collapsible=offcanvas]:translate-x-0 group-data-[collapsible=offcanvas]:after:left-full group-data-[collapsible=offcanvas]:hover:bg-sidebar",
315410
"[[data-side=left][data-collapsible=offcanvas]_&]:-right-2",

0 commit comments

Comments
 (0)