Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(ui): stop the sidebars flickering during navigation

Sidwebworksbe9d767193aa
diff --git a/apps/erp/app/components/Layout/Navigation/CollapsibleSidebar.tsx b/apps/erp/app/components/Layout/Navigation/CollapsibleSidebar.tsx
index 9a306f4be0..e13a313686 100644
--- a/apps/erp/app/components/Layout/Navigation/CollapsibleSidebar.tsx
+++ b/apps/erp/app/components/Layout/Navigation/CollapsibleSidebar.tsx
@@ -18,9 +18,12 @@ import {
forwardRef,
useContext,
useEffect,
- useMemo
+ useMemo,
+ useRef
} from "react";
import { LuPanelLeft } from "react-icons/lu";
+import type { Location } from "react-router";
+import { useLocation, useNavigation } from "react-router";
import { useOptimisticLocation } from "~/hooks";
import { useUIStore } from "~/stores/ui";
@@ -52,12 +55,14 @@ export function CollapsibleSidebarProvider({ children }: PropsWithChildren) {
(state) => state.setHasContentSidebar
);
+ // Every module layout mounts its own provider. Opening on mount undid a
+ // collapse on each module change, and on a phone opened the drawer for a
+ // frame before closing it, so only a change of breakpoint moves it.
+ const wasMobile = useRef(false);
useEffect(() => {
- if (isMobile) {
- setSidebarOpen(false);
- } else {
- setSidebarOpen(true);
- }
+ if (isMobile) setSidebarOpen(false);
+ else if (wasMobile.current) setSidebarOpen(true);
+ wasMobile.current = isMobile;
}, [isMobile, setSidebarOpen]);
// Tell the (global) Topbar that this route has a content sub-nav, so it can
@@ -106,9 +111,21 @@ CollapsibleSidebarTrigger.displayName = "CollapsibleSidebarTrigger";
// ease-out-quart: feels snappy and responsive for sidebar expand/collapse
const easeOutQuart = [0.165, 0.84, 0.44, 1] as const;
+/**
+ * The location a module sidebar highlights against: the pending one while it
+ * is still inside this sidebar, otherwise the one on screen. A sidebar about
+ * to be replaced keeps its highlight instead of going blank for the length of
+ * the loader.
+ */
+export function useSidebarLocation(isInside: (pathname: string) => boolean) {
+ const current = useLocation();
+ const pending: Location | undefined = useNavigation().location;
+ return pending && isInside(pending.pathname) ? pending : current;
+}
+
export const CollapsibleSidebar = ({
children,
- width = 180
+ width = 240
}: PropsWithChildren<{ width?: number }>) => {
const { isOpen } = useCollapsibleSidebar();
const shouldReduceMotion = useReducedMotion();
diff --git a/apps/erp/app/components/Layout/Navigation/ContentSidebar.tsx b/apps/erp/app/components/Layout/Navigation/ContentSidebar.tsx
index 64b6117f0e..b5fc717e32 100644
--- a/apps/erp/app/components/Layout/Navigation/ContentSidebar.tsx
+++ b/apps/erp/app/components/Layout/Navigation/ContentSidebar.tsx
@@ -4,12 +4,14 @@
import { Button, VStack } from "@carbon/react";
import { Link } from "react-router";
-import { useOptimisticLocation, useUrlParams } from "~/hooks";
+import { useUrlParams } from "~/hooks";
import type { Route } from "~/types";
-import { CollapsibleSidebar } from "./CollapsibleSidebar";
+import { CollapsibleSidebar, useSidebarLocation } from "./CollapsibleSidebar";
const ContentSidebar = ({ links }: { links: Route[] }) => {
- const location = useOptimisticLocation();
+ const location = useSidebarLocation((pathname) =>
+ links.some((route) => pathname.includes(route.to))
+ );
const [params] = useUrlParams();
const filter = params.get("q") ?? undefined;
diff --git a/apps/erp/app/components/Layout/Navigation/GroupedContentSidebar.tsx b/apps/erp/app/components/Layout/Navigation/GroupedContentSidebar.tsx
index 054036380a..c9a01dd80b 100644
--- a/apps/erp/app/components/Layout/Navigation/GroupedContentSidebar.tsx
+++ b/apps/erp/app/components/Layout/Navigation/GroupedContentSidebar.tsx
@@ -28,10 +28,22 @@ import {
} from "react-icons/lu";
import { Link, useSubmit } from "react-router";
import { ConfirmDelete } from "~/components/Modals";
-import { useOptimisticLocation } from "~/hooks";
import type { RouteGroup } from "~/types";
import { path } from "~/utils/path";
-import { CollapsibleSidebar } from "./CollapsibleSidebar";
+import { CollapsibleSidebar, useSidebarLocation } from "./CollapsibleSidebar";
+
+type GroupedRoute = RouteGroup["routes"][number];
+
+const matchesRoute = (
+ route: GroupedRoute,
+ pathname: string,
+ exactMatch: boolean
+) =>
+ route.isActive
+ ? route.isActive(pathname)
+ : exactMatch
+ ? pathname === route.to
+ : pathname.includes(route.to);
const GroupedContentSidebar = ({
groups,
@@ -43,7 +55,11 @@ const GroupedContentSidebar = ({
exactMatch?: boolean;
}) => {
const { t } = useLingui();
- const location = useOptimisticLocation();
+ const location = useSidebarLocation((pathname) =>
+ groups.some((group) =>
+ group.routes.some((route) => matchesRoute(route, pathname, exactMatch))
+ )
+ );
const submit = useSubmit();
const [expandedViews, setExpandedViews] = useState<Record<string, boolean>>(
@@ -88,14 +104,13 @@ const GroupedContentSidebar = ({
{group.name}
</Subheading>
{group.routes.map((route) => {
- const isActive = route.isActive
- ? route.isActive(location.pathname)
- : exactMatch
- ? location.pathname === route.to
- : location.pathname.includes(route.to) &&
- !`${location.pathname}${location.search}`.includes(
- "view="
- );
+ const isActive =
+ matchesRoute(route, location.pathname, exactMatch) &&
+ (Boolean(route.isActive) ||
+ exactMatch ||
+ !`${location.pathname}${location.search}`.includes(
+ "view="
+ ));
const hasViews = route.views && route.views.length > 0;
const isExpanded = expandedViews[route.name];
@@ -187,7 +202,7 @@ const ViewsReorderGroup = ({
onDelete
}: {
views: { id: string; name: string; to: string; sortOrder: number }[];
- location: ReturnType<typeof useOptimisticLocation>;
+ location: ReturnType<typeof useSidebarLocation>;
onReorder: (
updates: { id: string; name: string; to: string; sortOrder: number }[]
) => void;
diff --git a/apps/erp/app/components/Layout/Navigation/PrimaryNavigation.tsx b/apps/erp/app/components/Layout/Navigation/PrimaryNavigation.tsx
index 0d9731960f..b752cfd2b3 100644
--- a/apps/erp/app/components/Layout/Navigation/PrimaryNavigation.tsx
+++ b/apps/erp/app/components/Layout/Navigation/PrimaryNavigation.tsx
@@ -27,7 +27,7 @@ import {
import { useLingui } from "@lingui/react/macro";
import { memo, useMemo } from "react";
import { LuSearch, LuSettings2 } from "react-icons/lu";
-import { useMatches, useNavigate } from "react-router";
+import { useLocation, useMatches, useNavigate } from "react-router";
import {
useModules,
useOptimisticLocation,
@@ -78,8 +78,8 @@ const PrimaryNavigation = () => {
const { t, i18n } = useLingui();
const { isMobile } = useSidebar();
const permissions = usePermissions();
- const location = useOptimisticLocation();
- const currentModule = getModule(location.pathname);
+ const committedModule = getModule(useLocation().pathname);
+ const pendingModule = getModule(useOptimisticLocation().pathname);
const links = useModules();
const settingsModule = useSettingsModule();
const matchedModules = useMatches().reduce((acc, match) => {
@@ -92,9 +92,21 @@ const PrimaryNavigation = () => {
return acc;
}, new Set<string>());
+ // While a navigation is pending the highlight moves to the destination at
+ // once, and off the module being left: `matchedModules` still describes the
+ // page on screen, so honouring both lit two modules for the length of the
+ // loader. A destination that is no module's own path (a detail page, whose
+ // module is only known from its route handle) keeps the current highlight.
+ const pendingIsModule =
+ pendingModule !== committedModule &&
+ (pendingModule === "get-started" ||
+ [...links, settingsModule].some(
+ (link) => link && getModule(link.to) === pendingModule
+ ));
+ const currentModule = pendingIsModule ? pendingModule : committedModule;
const isModuleActive = (to: string) => {
const m = getModule(to);
- return currentModule === m || matchedModules.has(m);
+ return currentModule === m || (!pendingIsModule && matchedModules.has(m));
};
const editMode = useNavigationEditMode();
diff --git a/packages/react/src/NavRail.tsx b/packages/react/src/NavRail.tsx
index 15f476ac2d..3219652057 100644
--- a/packages/react/src/NavRail.tsx
+++ b/packages/react/src/NavRail.tsx
@@ -47,6 +47,11 @@ export const navRailItemClasses = [
* icon rail that grows to 208px while a mouse hovers it or while it is pinned
* open (a `SidebarTrigger`, or ⌘B where the provider binds it), and a left drawer below `md`. Open state
* comes from `SidebarProvider`, so it must be rendered inside one.
+ *
+ * Hovering expands the rail OVER the page; only a pinned rail takes layout
+ * space. A hover is transient and usually ends in a navigation, so resizing
+ * the page for it re-laid-out every table twice, the second time while the
+ * destination was rendering.
*/
// A pointer only passing over the rail (on its way to the page) shouldn't open it.
const HOVER_OPEN_DELAY_MS = 150;
@@ -151,25 +156,31 @@ export function NavRail({
);
}
- const state = forceExpanded || open || hovered ? "expanded" : "collapsed";
+ const pinned = forceExpanded || open;
+ const state = pinned || hovered ? "expanded" : "collapsed";
return (
- // The wrapper (not just the inner nav) grows on expand, so the rail pushes
- // the rest of the layout right instead of floating over it. Sticky so it
- // stays in view in shells whose page scrolls as a whole.
+ // The wrapper is the rail's footprint in the layout: it grows only when
+ // pinned, pushing the page right. The nav inside is out of flow, so a
+ // hover widens it without moving anything else. Sticky so it stays in
+ // view in shells whose page scrolls as a whole.
<div
- data-state={state}
+ data-pinned={pinned}
className={cn(
- "sticky top-0 h-svh flex-col z-50 hidden md:flex shrink-0",
- "w-14 data-[state=expanded]:w-[13rem]",
- "transition-[width] duration-200"
+ "sticky top-0 h-svh z-50 hidden md:block shrink-0",
+ "w-14 data-[pinned=true]:w-[13rem]",
+ "transition-[width] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none"
)}
>
<nav
ref={navRef}
data-state={state}
+ data-floating={!pinned && hovered}
className={cn(
- "bg-background py-2 group z-10 h-full w-full",
+ "absolute inset-y-0 left-0 bg-background py-2 group",
+ "w-14 data-[state=expanded]:w-[13rem]",
+ "transition-[width,box-shadow] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none",
+ "data-[floating=true]:shadow-xl data-[floating=true]:ring-1 data-[floating=true]:ring-border",
"flex flex-col justify-between",
"hide-scrollbar overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-accent"
)}