TypeScript
perf(web): the sidebar primitives compile with the React Compiler (#20648)
Julius Marminge472a62a6a8f6
diff --git a/apps/web/src/components/ui/sidebar.tsx b/apps/web/src/components/ui/sidebar.tsxindex 5311ca94b26..3b45785623f 100644--- a/apps/web/src/components/ui/sidebar.tsx+++ b/apps/web/src/components/ui/sidebar.tsx@@ -110,60 +110,45 @@ function SidebarProvider({const isMobile = useIsMobile();const [openMobile, setOpenMobile] = React.useState(false);const [mobileSheetMounted, setMobileSheetMounted] = React.useState(false);- const setMobileSheetElement = React.useCallback((element: HTMLDivElement | null) => {+ const setMobileSheetElement = (element: HTMLDivElement | null) => {setMobileSheetMounted(element !== null);- }, []);+ };const [_open, _setOpen] = React.useState(defaultOpen);const open = openProp ?? _open;- const setOpen = React.useCallback(- async (value: boolean | ((value: boolean) => boolean)) => {- const openState = typeof value === "function" ? value(open) : value;- if (setOpenProp) {- setOpenProp(openState);- } else {- _setOpen(openState);- }-- await cookieStore.set({- expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,- name: SIDEBAR_COOKIE_NAME,- path: "/",- value: String(openState),- });- },- [setOpenProp, open],- );+ const setOpen = async (value: boolean | ((value: boolean) => boolean)) => {+ const openState = typeof value === "function" ? value(open) : value;+ if (setOpenProp) {+ setOpenProp(openState);+ } else {+ _setOpen(openState);+ }++ await cookieStore.set({+ expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,+ name: SIDEBAR_COOKIE_NAME,+ path: "/",+ value: String(openState),+ });+ };- const toggleSidebar = React.useCallback(() => {+ const toggleSidebar = () => {return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open);- }, [isMobile, setOpen]);+ };const state = resolveSidebarState({ isMobile, open, openMobile });- const contextValue = React.useMemo<SidebarContextProps>(- () => ({- isMobile,- open,- openMobile,- mobileSheetMounted,- setMobileSheetElement,- setOpen,- setOpenMobile,- state,- toggleSidebar,- }),- [- state,- open,- setOpen,- isMobile,- openMobile,- mobileSheetMounted,- setMobileSheetElement,- toggleSidebar,- ],- );+ const contextValue: SidebarContextProps = {+ isMobile,+ open,+ openMobile,+ mobileSheetMounted,+ setMobileSheetElement,+ setOpen,+ setOpenMobile,+ state,+ toggleSidebar,+ };return (<SidebarContext value={contextValue}>@@ -192,6 +177,31 @@ function SidebarProvider({);}+/** The rail's width restore and drag refresh key on this value. */+function useResolvedSidebarResizable({+ collapsible,+ isMobile,+ resizable,+}: {+ collapsible: "offcanvas" | "icon" | "none";+ isMobile: boolean;+ resizable: boolean | SidebarResizableOptions;+}): SidebarResolvedResizableOptions | null {+ "use memo";+ if (isMobile || collapsible === "none" || !resizable) {+ return null;+ }++ const options = typeof resizable === "boolean" ? {} : resizable;+ return {+ maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,+ minWidth: options.minWidth ?? SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH,+ storageKey: options.storageKey ?? null,+ ...(options.onResize ? { onResize: options.onResize } : {}),+ ...(options.shouldAcceptWidth ? { shouldAcceptWidth: options.shouldAcceptWidth } : {}),+ };+}+function Sidebar({side = "left",variant = "sidebar",@@ -207,24 +217,8 @@ function Sidebar({resizable?: boolean | SidebarResizableOptions;}) {const { isMobile, state, openMobile, setOpenMobile, setMobileSheetElement } = useSidebar();- const resolvedResizable = React.useMemo<SidebarResolvedResizableOptions | null>(() => {- if (isMobile || collapsible === "none" || !resizable) {- return null;- }-- const options = typeof resizable === "boolean" ? {} : resizable;- return {- maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,- minWidth: options.minWidth ?? SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH,- storageKey: options.storageKey ?? null,- ...(options.onResize ? { onResize: options.onResize } : {}),- ...(options.shouldAcceptWidth ? { shouldAcceptWidth: options.shouldAcceptWidth } : {}),- };- }, [collapsible, isMobile, resizable]);- const instanceContextValue = React.useMemo<SidebarInstanceContextProps>(- () => ({ side, resizable: resolvedResizable }),- [resolvedResizable, side],- );+ const resolvedResizable = useResolvedSidebarResizable({ collapsible, isMobile, resizable });+ const instanceContextValue: SidebarInstanceContextProps = { side, resizable: resolvedResizable };if (collapsible === "none") {return (@@ -459,28 +453,26 @@ function SidebarRail({},};}, String(open));+ const refreshResize = React.useEffectEvent(() => resize.refresh());React.useLayoutEffect(() => {latestResizable.current = resolvedResizable;- resize.refresh();+ refreshResize();}, [resolvedResizable]);- const handleClick = React.useCallback(- (event: React.MouseEvent<HTMLButtonElement>) => {- onClick?.(event);- if (event.defaultPrevented) return;- if (suppressClickRef.current) {- suppressClickRef.current = false;- event.preventDefault();- return;- }- if (resolvedResizable && open) {- event.preventDefault();- return;- }- toggleSidebar();- },- [onClick, open, resolvedResizable, toggleSidebar],- );+ const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {+ onClick?.(event);+ if (event.defaultPrevented) return;+ if (suppressClickRef.current) {+ suppressClickRef.current = false;+ event.preventDefault();+ return;+ }+ if (resolvedResizable && open) {+ event.preventDefault();+ return;+ }+ toggleSidebar();+ };React.useLayoutEffect(() => {if (!resolvedResizable?.storageKey || typeof window === "undefined") return;