Skip to content

Commit a718499

Browse files
perf(web): the sidebar primitives compile with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 70d60e0 commit a718499

2 files changed

Lines changed: 74 additions & 81 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ export const REACT_COMPILED_FILES = [
9696
"apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx",
9797
"apps/web/src/components/ThreadTerminalDrawer.tsx",
9898
"apps/web/src/components/settings/SettingsPanels.tsx",
99+
"apps/web/src/components/ui/sidebar.tsx",
99100
] as const;

‎apps/web/src/components/ui/sidebar.tsx‎

Lines changed: 73 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -110,60 +110,45 @@ function SidebarProvider({
110110
const isMobile = useIsMobile();
111111
const [openMobile, setOpenMobile] = React.useState(false);
112112
const [mobileSheetMounted, setMobileSheetMounted] = React.useState(false);
113-
const setMobileSheetElement = React.useCallback((element: HTMLDivElement | null) => {
113+
const setMobileSheetElement = (element: HTMLDivElement | null) => {
114114
setMobileSheetMounted(element !== null);
115-
}, []);
115+
};
116116

117117
const [_open, _setOpen] = React.useState(defaultOpen);
118118
const open = openProp ?? _open;
119-
const setOpen = React.useCallback(
120-
async (value: boolean | ((value: boolean) => boolean)) => {
121-
const openState = typeof value === "function" ? value(open) : value;
122-
if (setOpenProp) {
123-
setOpenProp(openState);
124-
} else {
125-
_setOpen(openState);
126-
}
127-
128-
await cookieStore.set({
129-
expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,
130-
name: SIDEBAR_COOKIE_NAME,
131-
path: "/",
132-
value: String(openState),
133-
});
134-
},
135-
[setOpenProp, open],
136-
);
119+
const setOpen = async (value: boolean | ((value: boolean) => boolean)) => {
120+
const openState = typeof value === "function" ? value(open) : value;
121+
if (setOpenProp) {
122+
setOpenProp(openState);
123+
} else {
124+
_setOpen(openState);
125+
}
126+
127+
await cookieStore.set({
128+
expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,
129+
name: SIDEBAR_COOKIE_NAME,
130+
path: "/",
131+
value: String(openState),
132+
});
133+
};
137134

138-
const toggleSidebar = React.useCallback(() => {
135+
const toggleSidebar = () => {
139136
return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open);
140-
}, [isMobile, setOpen]);
137+
};
141138

142139
const state = resolveSidebarState({ isMobile, open, openMobile });
143140

144-
const contextValue = React.useMemo<SidebarContextProps>(
145-
() => ({
146-
isMobile,
147-
open,
148-
openMobile,
149-
mobileSheetMounted,
150-
setMobileSheetElement,
151-
setOpen,
152-
setOpenMobile,
153-
state,
154-
toggleSidebar,
155-
}),
156-
[
157-
state,
158-
open,
159-
setOpen,
160-
isMobile,
161-
openMobile,
162-
mobileSheetMounted,
163-
setMobileSheetElement,
164-
toggleSidebar,
165-
],
166-
);
141+
const contextValue: SidebarContextProps = {
142+
isMobile,
143+
open,
144+
openMobile,
145+
mobileSheetMounted,
146+
setMobileSheetElement,
147+
setOpen,
148+
setOpenMobile,
149+
state,
150+
toggleSidebar,
151+
};
167152

168153
return (
169154
<SidebarContext value={contextValue}>
@@ -192,6 +177,31 @@ function SidebarProvider({
192177
);
193178
}
194179

180+
/** The rail's width restore and drag refresh key on this value. */
181+
function useResolvedSidebarResizable({
182+
collapsible,
183+
isMobile,
184+
resizable,
185+
}: {
186+
collapsible: "offcanvas" | "icon" | "none";
187+
isMobile: boolean;
188+
resizable: boolean | SidebarResizableOptions;
189+
}): SidebarResolvedResizableOptions | null {
190+
"use memo";
191+
if (isMobile || collapsible === "none" || !resizable) {
192+
return null;
193+
}
194+
195+
const options = typeof resizable === "boolean" ? {} : resizable;
196+
return {
197+
maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,
198+
minWidth: options.minWidth ?? SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH,
199+
storageKey: options.storageKey ?? null,
200+
...(options.onResize ? { onResize: options.onResize } : {}),
201+
...(options.shouldAcceptWidth ? { shouldAcceptWidth: options.shouldAcceptWidth } : {}),
202+
};
203+
}
204+
195205
function Sidebar({
196206
side = "left",
197207
variant = "sidebar",
@@ -207,24 +217,8 @@ function Sidebar({
207217
resizable?: boolean | SidebarResizableOptions;
208218
}) {
209219
const { isMobile, state, openMobile, setOpenMobile, setMobileSheetElement } = useSidebar();
210-
const resolvedResizable = React.useMemo<SidebarResolvedResizableOptions | null>(() => {
211-
if (isMobile || collapsible === "none" || !resizable) {
212-
return null;
213-
}
214-
215-
const options = typeof resizable === "boolean" ? {} : resizable;
216-
return {
217-
maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,
218-
minWidth: options.minWidth ?? SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH,
219-
storageKey: options.storageKey ?? null,
220-
...(options.onResize ? { onResize: options.onResize } : {}),
221-
...(options.shouldAcceptWidth ? { shouldAcceptWidth: options.shouldAcceptWidth } : {}),
222-
};
223-
}, [collapsible, isMobile, resizable]);
224-
const instanceContextValue = React.useMemo<SidebarInstanceContextProps>(
225-
() => ({ side, resizable: resolvedResizable }),
226-
[resolvedResizable, side],
227-
);
220+
const resolvedResizable = useResolvedSidebarResizable({ collapsible, isMobile, resizable });
221+
const instanceContextValue: SidebarInstanceContextProps = { side, resizable: resolvedResizable };
228222

229223
if (collapsible === "none") {
230224
return (
@@ -459,28 +453,26 @@ function SidebarRail({
459453
},
460454
};
461455
}, String(open));
456+
const refreshResize = React.useEffectEvent(() => resize.refresh());
462457
React.useLayoutEffect(() => {
463458
latestResizable.current = resolvedResizable;
464-
resize.refresh();
459+
refreshResize();
465460
}, [resolvedResizable]);
466461

467-
const handleClick = React.useCallback(
468-
(event: React.MouseEvent<HTMLButtonElement>) => {
469-
onClick?.(event);
470-
if (event.defaultPrevented) return;
471-
if (suppressClickRef.current) {
472-
suppressClickRef.current = false;
473-
event.preventDefault();
474-
return;
475-
}
476-
if (resolvedResizable && open) {
477-
event.preventDefault();
478-
return;
479-
}
480-
toggleSidebar();
481-
},
482-
[onClick, open, resolvedResizable, toggleSidebar],
483-
);
462+
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
463+
onClick?.(event);
464+
if (event.defaultPrevented) return;
465+
if (suppressClickRef.current) {
466+
suppressClickRef.current = false;
467+
event.preventDefault();
468+
return;
469+
}
470+
if (resolvedResizable && open) {
471+
event.preventDefault();
472+
return;
473+
}
474+
toggleSidebar();
475+
};
484476

485477
React.useLayoutEffect(() => {
486478
if (!resolvedResizable?.storageKey || typeof window === "undefined") return;

0 commit comments

Comments
 (0)