Last active
August 22, 2021 17:55
-
-
Save alanfoandrade/eca5febd718a195f21ebdbbd46813a6e to your computer and use it in GitHub Desktop.
SideBar Chakra component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Link as ChakraLink } from '@chakra-ui/react'; | |
import React, { cloneElement, ReactElement } from 'react'; | |
import { useLocation } from 'react-router'; | |
import { Link, LinkProps } from 'react-router-dom'; | |
interface IActiveLinkProps extends LinkProps { | |
children: ReactElement; | |
shouldMatchExactHref?: boolean; | |
} | |
export const ActiveLink: React.FC<IActiveLinkProps> = ({ | |
children, | |
shouldMatchExactHref = false, | |
to, | |
...rest | |
}) => { | |
const { pathname } = useLocation(); | |
let isActive = false; | |
if (shouldMatchExactHref && pathname === to) { | |
isActive = true; | |
} | |
if (!shouldMatchExactHref && pathname.startsWith(String(to))) { | |
isActive = true; | |
} | |
return ( | |
<ChakraLink as={Link} to={to} display="flex" alignItems="center" {...rest}> | |
{cloneElement(children, { | |
color: isActive ? 'blue.500' : 'gray.700', | |
})} | |
</ChakraLink> | |
); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Box, | |
Drawer, | |
DrawerBody, | |
DrawerCloseButton, | |
DrawerContent, | |
DrawerHeader, | |
DrawerOverlay, | |
useBreakpointValue, | |
} from '@chakra-ui/react'; | |
import { useSidebarDrawer } from '../../hooks/sidebarDrawer'; | |
import { SidebarNav } from './SidebarNav'; | |
export const Sidebar = (): JSX.Element => { | |
const { isOpen, onClose } = useSidebarDrawer(); | |
const isDrawerSidebar = useBreakpointValue({ | |
base: true, | |
lg: false, | |
}); | |
if (isDrawerSidebar) { | |
return ( | |
<Drawer isOpen={isOpen} placement="left" onClose={onClose}> | |
<DrawerOverlay> | |
<DrawerContent> | |
<DrawerCloseButton mt="6" /> | |
<DrawerHeader>Navegação</DrawerHeader> | |
<DrawerBody> | |
<SidebarNav /> | |
</DrawerBody> | |
</DrawerContent> | |
</DrawerOverlay> | |
</Drawer> | |
); | |
} | |
return ( | |
<Box as="aside" p="8" bg="white"> | |
<SidebarNav /> | |
</Box> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment