From 8dd809f3323c3d0ba1cb0cba0210727f85419ec8 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Mon, 6 Nov 2023 20:17:46 -0800 Subject: [PATCH] prevent sidebar disappearing when in modal --- src/components/layouts/interior-page.tsx | 4 ++-- src/components/menu/side-nav.tsx | 18 ++---------------- .../pages/stanford-page/stanford-page-page.tsx | 2 +- src/lib/hooks/useActiveTrail.tsx | 5 +++-- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/components/layouts/interior-page.tsx b/src/components/layouts/interior-page.tsx index 00d7aeec..581cf9ce 100644 --- a/src/components/layouts/interior-page.tsx +++ b/src/components/layouts/interior-page.tsx @@ -3,13 +3,13 @@ import SideNav from "@components/menu/side-nav"; import {PropsWithChildren} from "react"; import {isDraftMode} from "@lib/drupal/utils"; -const InteriorPage = async ({children}: PropsWithChildren) => { +const InteriorPage = async ({children, currentPath}: PropsWithChildren<{ currentPath: string }>) => { const draftDev = isDraftMode(); const {tree} = await getMenu('main', {}, draftDev); return (
- +
{children}
diff --git a/src/components/menu/side-nav.tsx b/src/components/menu/side-nav.tsx index a6068f54..1e97a630 100644 --- a/src/components/menu/side-nav.tsx +++ b/src/components/menu/side-nav.tsx @@ -5,22 +5,8 @@ import useActiveTrail from "@lib/hooks/useActiveTrail"; import Link from "@components/elements/link"; import {DrupalMenuLinkContent} from "next-drupal"; -const getCurrentPageTitle = (activeTrail: string[], items: DrupalMenuLinkContent[], trail: string[]): string | undefined => { - const currentItem = items.find(item => item.id === trail.at(0)); - if (!currentItem || currentItem === undefined) return; - - if (currentItem.id === activeTrail.at(-1)) { - return currentItem.title; - } - - if (currentItem.items && currentItem.items.length > 0 && trail.length > 1) { - return getCurrentPageTitle(activeTrail, currentItem.items, trail.slice(1)); - } -} - - -const SideNav = ({menuItems}: { menuItems: DrupalMenuLinkContent[] }) => { - const activeTrail = useActiveTrail(menuItems); +const SideNav = ({menuItems, currentPath}: { menuItems: DrupalMenuLinkContent[], currentPath?: string }) => { + const activeTrail = useActiveTrail(menuItems, currentPath); // Peel off the menu items from the parent. const topMenuItem = activeTrail.length > 0 ? menuItems.find(item => item.id === activeTrail[0]) : false; diff --git a/src/components/nodes/pages/stanford-page/stanford-page-page.tsx b/src/components/nodes/pages/stanford-page/stanford-page-page.tsx index 7676de43..fd38ed55 100644 --- a/src/components/nodes/pages/stanford-page/stanford-page-page.tsx +++ b/src/components/nodes/pages/stanford-page/stanford-page-page.tsx @@ -16,7 +16,7 @@ const StanfordPagePage = ({node}: { node: BasicPageNodeType }) => { {!fullWidth && - + {node.su_page_components && } diff --git a/src/lib/hooks/useActiveTrail.tsx b/src/lib/hooks/useActiveTrail.tsx index 2df293c7..7e5e9c58 100644 --- a/src/lib/hooks/useActiveTrail.tsx +++ b/src/lib/hooks/useActiveTrail.tsx @@ -4,8 +4,9 @@ import {usePathname} from "next/navigation"; import {useMemo} from "react"; import {DrupalMenuLinkContent} from "next-drupal"; -const useActiveTrail = (menuItems: DrupalMenuLinkContent[]) => { - const currentPath = usePathname(); +const useActiveTrail = (menuItems: DrupalMenuLinkContent[], originalPath?: string) => { + const pathName = usePathname(); + const currentPath = originalPath || pathName; const getActiveTrail = (menuItems: DrupalMenuLinkContent[], trail: string[] = []): string[] => { let childTrail, currentTrail;