Skip to content

Commit

Permalink
Make active menu option highlighting more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
beverloo committed Jan 6, 2024
1 parent 3c0959a commit 4a8e47d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/admin/AdminSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ interface AdminSidebarMenuButtonItem {
* The URL for this list item entry. Must be absolute from the domain root.
*/
url: string;

/**
* Match to apply to the URL (or URL prefix) when deciding on highlight state. A prefix match is
* executed by default, but for root pages a strict match may be more appropriate.
*/
urlMatchMode?: 'prefix' | 'strict';

/**
* The URL prefix for this list entry, all sub-pages of which will be captured for active tab
* state. Defaults to the URL.
*/
urlPrefix?: string;
}

/**
Expand All @@ -120,6 +132,21 @@ export type AdminSidebarMenuEntry =
AdminSidebarMenuDivider |
(AdminSidebarMenuItemCommon & (AdminSidebarMenuButtonItem | AdminSidebarMenuSubMenuItem));

/**
* Decides whether a particular menu entry should be highlighted. Considered to be the case when the
* `pathname` starts with the `entry`'s URL (or URL prefix).
*/
function shouldHighlightEntry(pathname: string, entry: AdminSidebarMenuButtonItem) {
const matchMode = entry.urlMatchMode ?? 'prefix';
switch (matchMode) {
case 'prefix':
return pathname.startsWith(entry.urlPrefix ?? entry.url);
case 'strict':
return pathname === entry.url;
}
}


/**
* Props accepted by the <RenderSidebarMenu> component.
*/
Expand Down Expand Up @@ -221,7 +248,7 @@ function RenderSidebarMenu(props: RenderSidebarMenuProps) {
: kStyles.active
}
component={Link} href={entry.url}
selected={entry.url === pathname}>
selected={shouldHighlightEntry(pathname, entry)}>

{ entry.icon &&
<ListItemIcon sx={{ minWidth: '40px' }}>
Expand Down
1 change: 1 addition & 0 deletions app/admin/TopLevelLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default async function TopLevelLayout(props: React.PropsWithChildren) {
icon: <GridViewIcon />,
label: 'Dashboard',
url: '/admin',
urlMatchMode: 'strict',
},
{
icon: <FeedOutlinedIcon />,
Expand Down
2 changes: 2 additions & 0 deletions app/admin/events/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default async function EventLayout(props: React.PropsWithChildren<EventLa
icon: <EventNoteIcon />,
label: 'Program',
url: `/admin/events/${slug}/program/requests`,
urlPrefix: `/admin/events/${slug}/program`
});
}

Expand All @@ -142,6 +143,7 @@ export default async function EventLayout(props: React.PropsWithChildren<EventLa
icon: <GridViewIcon />,
label: 'Dashboard',
url: `/admin/events/${slug}`,
urlMatchMode: 'strict',
},
{
icon: <HotelIcon />,
Expand Down

0 comments on commit 4a8e47d

Please sign in to comment.