The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 06:45 UTC
perf(web): the sidebar primitives compile with the React Compiler (#20648)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 21:57 UTC
472a62a6a8f6157b9283dcee201f2780ade9a301
1 file changed
+73
-81
apps/web/src/components/ui/sidebar.tsx
+73
-81
@@ -110,60 +110,45 @@ function SidebarProvider({
110
const isMobile = useIsMobile();
111
const [openMobile, setOpenMobile] = React.useState(false);
112
const [mobileSheetMounted, setMobileSheetMounted] = React.useState(false);
113
- const setMobileSheetElement = React.useCallback((element: HTMLDivElement | null) => {
113
+ const setMobileSheetElement = (element: HTMLDivElement | null) => {
114
setMobileSheetMounted(element !== null);
115
- }, []);
115
+ };
116
117
const [_open, _setOpen] = React.useState(defaultOpen);
118
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
+ };
134
138
- const toggleSidebar = React.useCallback(() => {
135
+ const toggleSidebar = () => {
136
return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open);
140
- }, [isMobile, setOpen]);
137
+ };
138
139
const state = resolveSidebarState({ isMobile, open, openMobile });
140
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
+ };
152
153
return (
154
<SidebarContext value={contextValue}>
@@ -192,6 +177,31 @@ function SidebarProvider({
177
);
178
}
179
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
+
205
function Sidebar({
206
side = "left",
207
variant = "sidebar",
@@ -207,24 +217,8 @@ function Sidebar({
217
resizable?: boolean | SidebarResizableOptions;
218
}) {
219
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 };
222
223
if (collapsible === "none") {
224
return (
@@ -459,28 +453,26 @@ function SidebarRail({
453
},
454
};
455
}, String(open));
456
+ const refreshResize = React.useEffectEvent(() => resize.refresh());
457
React.useLayoutEffect(() => {
458
latestResizable.current = resolvedResizable;
464
- resize.refresh();
459
+ refreshResize();
460
}, [resolvedResizable]);
461
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
+ };
476
477
React.useLayoutEffect(() => {
478
if (!resolvedResizable?.storageKey || typeof window === "undefined") return;