Skip to content

Commit

Permalink
Closes sidebar when menu item is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
DrLer0 committed Nov 21, 2024
1 parent 14b8ad5 commit 8ec0e55
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
13 changes: 11 additions & 2 deletions client/src/components/Sidebar/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
IconSpeakerphone,
IconSquareArrowRight,
} from '@tabler/icons-react';
import PropTypes from 'prop-types';


import { SidebarNavSection, SidebarLink } from './SidebarNavSection';

Expand Down Expand Up @@ -61,10 +63,14 @@ const sections = [
},
];

const SidebarProps = {
toggleSidebar: PropTypes.func,
};

/**
* Collapsible sidebar
*/
export function Sidebar() {
export function Sidebar({toggleSidebar}) {
return (
<nav className={classes.navbar}>
<Group align="center" className={classes.title}>
Expand All @@ -84,9 +90,10 @@ export function Sidebar() {
{...item}
key={`section_${item.label}`}
initiallyOpened
toggleSidebar={toggleSidebar}
/>
) : (
<SidebarLink {...item} key={`header_${item.label}`} />
<SidebarLink toggleSidebar={toggleSidebar} {...item} key={`header_${item.label}`} />
);
})}
</div>
Expand All @@ -96,6 +103,8 @@ export function Sidebar() {
);
}

Sidebar.propTypes = SidebarProps;

/**
*
*/
Expand Down
9 changes: 6 additions & 3 deletions client/src/components/Sidebar/SidebarNavSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const SidebarNavSectionProps = {
label: PropTypes.string.isRequired,
}),
),
toggleSidebar: PropTypes.func
};
/**
* Sidebar section of sublinks with a collapsible accordion header
Expand All @@ -26,6 +27,7 @@ export function SidebarNavSection({
label,
initiallyOpened = false,
links = [],
toggleSidebar
}) {
const [opened, setOpened] = useState(initiallyOpened);

Expand All @@ -48,7 +50,7 @@ export function SidebarNavSection({
<Collapse in={opened} className={classes.section}>
{links.map((link) => (
<div className={classes.sublink} key={link.label}>
<SidebarLink {...link} />
<SidebarLink toggleSidebar={toggleSidebar} {...link} />
</div>
))}
</Collapse>
Expand All @@ -63,17 +65,18 @@ const SidebarLinkProps = {
href: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
target: PropTypes.string,
toggleSidebar: PropTypes.func
};

/**
* Navigation link for sidebar
* @param {PropTypes.InferProps<typeof SidebarLinkProps>} props
*/
export function SidebarLink({ icon, href, label, target = '_self' }) {
export function SidebarLink({ icon, href, label, target = '_self', toggleSidebar }) {
return (
<div className={classes.sublink}>
<div>{icon}</div>
<Text component={Link} className={classes.link} to={href} target={target}>
<Text onClick={toggleSidebar} component={Link} className={classes.link} to={href} target={target}>
{label}
</Text>
</div>
Expand Down
4 changes: 2 additions & 2 deletions client/src/stories/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Drawer,
rem,
} from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import { useDidUpdate, useDisclosure } from '@mantine/hooks';

import classes from './header.module.css';
import { Sidebar } from '../../components/Sidebar/Sidebar';
Expand Down Expand Up @@ -59,7 +59,7 @@ export function Header({ user = null }) {
mx="-md"
scrollbars="y"
>
<Sidebar />
<Sidebar toggleSidebar={toggleDrawer}/>
</ScrollArea>
</Drawer>
</Box>
Expand Down

0 comments on commit 8ec0e55

Please sign in to comment.