Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: sidebar flickering

Marc Seitz78b1aa4186d6
diff --git a/components/layouts/app.tsx b/components/layouts/app.tsx
index ae32c3fc..fa1d1605 100644
--- a/components/layouts/app.tsx
+++ b/components/layouts/app.tsx
@@ -1,6 +1,8 @@
-import Cookies from "js-cookie";
import { useRouter } from "next/router";
-import { useEffect, useState } from "react";
+
+import { useCallback, useEffect, useRef, useState } from "react";
+
+import Cookies from "js-cookie";
import { AppBreadcrumb } from "@/components/layouts/breadcrumb";
import TrialBanner from "@/components/layouts/trial-banner";
@@ -18,27 +20,86 @@ import {
import { BlockingModal } from "./blocking-modal";
+const DATAROOM_SIDEBAR_COOKIE_NAME = "sidebar:dataroom-state";
+
+// Helper to get initial sidebar state synchronously (avoids flash)
+function getInitialSidebarState(isDataroom: boolean): boolean {
+ if (typeof window === "undefined") return false; // SSR: default closed to avoid flash
+
+ // For dataroom pages, check dataroom-specific cookie first
+ if (isDataroom) {
+ const dataroomCookie = Cookies.get(DATAROOM_SIDEBAR_COOKIE_NAME);
+ if (dataroomCookie !== undefined) {
+ return dataroomCookie === "true";
+ }
+ // No dataroom preference set yet - default to closed for datarooms
+ return false;
+ }
+
+ // For non-dataroom pages, use main cookie
+ const mainCookie = Cookies.get(SIDEBAR_COOKIE_NAME);
+ if (mainCookie !== undefined) {
+ return mainCookie === "true";
+ }
+
+ return true; // Default open for non-dataroom pages
+}
+
export default function AppLayout({ children }: { children: React.ReactNode }) {
const router = useRouter();
const isDataroom = router.pathname.startsWith("/datarooms/[id]");
- const [sidebarOpen, setSidebarOpen] = useState(true);
+ // Use lazy initializer to compute initial state synchronously (avoids flash)
+ const [sidebarOpen, setSidebarOpen] = useState(() =>
+ getInitialSidebarState(isDataroom),
+ );
+
+ // Track previous dataroom state for transitions
+ const prevIsDataroomRef = useRef<boolean>(isDataroom);
+ const isFirstRenderRef = useRef(true);
- // Sync with cookie on mount (client-side only)
+ // Handle initial mount and transitions between dataroom/non-dataroom
useEffect(() => {
- const cookieValue = Cookies.get(SIDEBAR_COOKIE_NAME);
- if (cookieValue !== undefined) {
- setSidebarOpen(cookieValue === "true");
+ if (isFirstRenderRef.current) {
+ isFirstRenderRef.current = false;
+ // Set cookie on initial mount if in dataroom and no preference exists
+ if (
+ isDataroom &&
+ Cookies.get(DATAROOM_SIDEBAR_COOKIE_NAME) === undefined
+ ) {
+ Cookies.set(DATAROOM_SIDEBAR_COOKIE_NAME, "false", { expires: 7 });
+ }
+ return;
}
- }, []);
- // Close sidebar when entering a dataroom
- useEffect(() => {
- if (isDataroom) {
+ // Transitioning from non-dataroom to dataroom
+ if (!prevIsDataroomRef.current && isDataroom) {
setSidebarOpen(false);
+ Cookies.set(DATAROOM_SIDEBAR_COOKIE_NAME, "false", { expires: 7 });
}
+
+ // Transitioning from dataroom to non-dataroom
+ if (prevIsDataroomRef.current && !isDataroom) {
+ Cookies.remove(DATAROOM_SIDEBAR_COOKIE_NAME);
+ // Restore main sidebar state
+ const mainCookie = Cookies.get(SIDEBAR_COOKIE_NAME);
+ setSidebarOpen(mainCookie === "true");
+ }
+
+ prevIsDataroomRef.current = isDataroom;
}, [isDataroom]);
+ // Handle sidebar state changes - save to appropriate cookie
+ const handleSidebarOpenChange = useCallback(
+ (open: boolean) => {
+ setSidebarOpen(open);
+ if (isDataroom) {
+ Cookies.set(DATAROOM_SIDEBAR_COOKIE_NAME, String(open), { expires: 7 });
+ }
+ },
+ [isDataroom],
+ );
+
// const { isAnnualPlan, isFree } = usePlan();
// const [showYearlyBanner, setShowYearlyBanner] = useState<boolean | null>(null);
@@ -59,7 +120,7 @@ export default function AppLayout({ children }: { children: React.ReactNode }) {
// }, [isFree, isAnnualPlan]);
return (
- <SidebarProvider open={sidebarOpen} onOpenChange={setSidebarOpen}>
+ <SidebarProvider open={sidebarOpen} onOpenChange={handleSidebarOpenChange}>
<div className="flex flex-1 flex-col gap-x-1 bg-gray-50 dark:bg-black md:flex-row">
<AppSidebar />
<SidebarInset className="ring-1 ring-gray-200 dark:ring-gray-800">