Skip to content

Commit

Permalink
refactor(sidebar): navigating to first child route on parent button c…
Browse files Browse the repository at this point in the history
…lick
  • Loading branch information
rockingrohit9639 committed Nov 29, 2024
1 parent 359ae32 commit 0c95a51
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions app/components/layout/sidebar/sidebar-nav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from "react";
import { ChevronDownIcon } from "@radix-ui/react-icons";
import { NavLink, useMatches } from "@remix-run/react";
import { NavLink, useMatches, useNavigate } from "@remix-run/react";
import invariant from "tiny-invariant";
import Icon from "~/components/icons/icon";
import {
Collapsible,
Expand Down Expand Up @@ -32,6 +33,7 @@ export default function SidebarNav({
items,
}: SidebarNavProps) {
const matches = useMatches();
const navigate = useNavigate();

const isRouteActive = useCallback(
(route: string) => {
Expand All @@ -50,6 +52,12 @@ export default function SidebarNav({
(navItem: NavItem) => {
switch (navItem.type) {
case "parent": {
const firstChildRoute = navItem.children[0];
invariant(
typeof firstChildRoute !== "undefined",
"'parent' nav item should have at lease one child route"
);

const isAnyChildActive = isAnyRouteActive(
navItem.children.map((child) => child.to)
);
Expand All @@ -63,7 +71,12 @@ export default function SidebarNav({
>
<SidebarMenuItem key={navItem.title}>
<CollapsibleTrigger asChild>
<SidebarMenuButton tooltip={navItem.title}>
<SidebarMenuButton
tooltip={navItem.title}
onClick={() => {
navigate(firstChildRoute.to);
}}
>
<Icon
size="xs"
icon={navItem.icon}
Expand Down Expand Up @@ -137,7 +150,7 @@ export default function SidebarNav({
return (
<SidebarMenuItem key={navItem.title} onClick={navItem.onClick}>
<SidebarMenuButton
className="font-medium"
className="font-semibold"
tooltip={navItem.title}
>
<Icon size="xs" icon={navItem.icon} className="text-gray-600" />
Expand Down

0 comments on commit 0c95a51

Please sign in to comment.