From 1c964eb1cbfa458d3595e2e0d367e6479dd65992 Mon Sep 17 00:00:00 2001 From: Nasreddine Bac Ali Date: Mon, 9 May 2022 17:24:53 +0200 Subject: [PATCH] fix: refactor sidebar item to accept a component as wrapper --- src/docs/Root.tsx | 5 +++-- src/lib/components/Sidebar/SidebarItem.tsx | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/docs/Root.tsx b/src/docs/Root.tsx index 53fdb8081..3df3d5a8a 100644 --- a/src/docs/Root.tsx +++ b/src/docs/Root.tsx @@ -2,13 +2,14 @@ import { FC, Suspense, useState } from 'react'; import { BsGithub } from 'react-icons/bs'; import { HiMenuAlt1 } from 'react-icons/hi'; import { SiStorybook } from 'react-icons/si'; -import { Route, Routes } from 'react-router-dom'; +import { Link, Route, Routes, useLocation } from 'react-router-dom'; import { DarkThemeToggle, Navbar, Sidebar, Spinner } from '../lib'; import { routes } from './routes'; export const Root: FC = () => { const [collapsed, setCollapsed] = useState(false); + const { pathname } = useLocation(); return (
@@ -47,7 +48,7 @@ export const Root: FC = () => { {routes.map(({ href, icon, title }, key) => ( - + {title} ))} diff --git a/src/lib/components/Sidebar/SidebarItem.tsx b/src/lib/components/Sidebar/SidebarItem.tsx index ca47d6ac7..6ed8554d9 100644 --- a/src/lib/components/Sidebar/SidebarItem.tsx +++ b/src/lib/components/Sidebar/SidebarItem.tsx @@ -1,5 +1,5 @@ import classNames from 'classnames'; -import { ComponentProps, FC, PropsWithChildren } from 'react'; +import { ComponentProps, ElementType, FC, PropsWithChildren } from 'react'; import { Badge, BadgeColor } from '../Badge'; import { Tooltip } from '../Tooltip'; import { useSidebarContext } from './SidebarContext'; @@ -12,14 +12,16 @@ export interface SidebarItem { labelColor?: BadgeColor; } -export interface SidebarItemProps extends PropsWithChildren { - href: string; +export interface SidebarItemProps extends PropsWithChildren> { + as?: ElementType; + active?: boolean; } const SidebarItem: FC = ({ children, className, - href, + as: Component = 'a', + active, icon: Icon, label, labelColor = 'blue', @@ -27,7 +29,6 @@ const SidebarItem: FC = ({ }) => { const { collapsed } = useSidebarContext(); const { insideCollapse } = useSidebarItemContext(); - const isCurrentPage = typeof window !== 'undefined' && window.location.pathname === href; const Wrapper = ({ children: wrapperChildren }: PropsWithChildren>) => (
  • @@ -43,23 +44,22 @@ const SidebarItem: FC = ({ return ( - {Icon && ( )} @@ -73,7 +73,7 @@ const SidebarItem: FC = ({ {label} )} - + ); };