Skip to content

Commit 5c79a39

Browse files
committed
style nav buttons so they show the active path
1 parent 1393274 commit 5c79a39

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

src/client/components/NavBar/index.tsx

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef, useState } from 'react'
2-
import { Link } from 'react-router-dom'
2+
import { Link, matchPath, useLocation } from 'react-router-dom'
33
import {
44
AppBar,
55
Toolbar,
@@ -91,53 +91,60 @@ const NavBar = () => {
9191
</>
9292
)
9393
}
94+
const NavItemButton = ({ children, to, path, current, icon }) => (
95+
<Button
96+
component={Link}
97+
to={to}
98+
size="small"
99+
variant="text"
100+
startIcon={icon}
101+
sx={{
102+
borderBottom: '2px solid',
103+
borderBottomLeftRadius: '0',
104+
borderBottomRightRadius: '0',
105+
borderBottomColor: matchPath({ path: path }, current) ? 'primary.main' : 'transparent',
106+
}}
107+
>
108+
{children}
109+
</Button>
110+
)
94111

95112
const NavItems = ({ user, t, languages, handleLanguageChange, language }) => {
96113
const anchorRef = useRef<HTMLButtonElement>(null)
114+
const { pathname } = useLocation()
97115
const [openLanguageSelect, setOpenLanguageSelect] = useState(false)
116+
98117
return (
99118
<>
100119
{user?.preferences?.chatVersion !== 2 && (
101-
<Link to="/v2" style={{ textDecoration: 'none' }}>
102-
<Button>
103-
<GradeOutlined sx={styles.icon} /> {t('tryNew')}
104-
</Button>
105-
</Link>
120+
<NavItemButton to="/v2" path="v2/*" current={pathname} icon={<GradeOutlined sx={styles.icon} />}>
121+
{t('tryNew')}
122+
</NavItemButton>
106123
)}
107124
{user?.preferences?.chatVersion !== 1 && (
108-
<Link to="/v1" style={{ textDecoration: 'none' }}>
109-
<Button>
110-
<GradeOutlined sx={styles.icon} /> {t('useOld')}
111-
</Button>
112-
</Link>
125+
<NavItemButton to="/v1" path="v1/*" current={pathname} icon={<GradeOutlined sx={styles.icon} />}>
126+
{t('useOld')}
127+
</NavItemButton>
113128
)}
114129
{user.enrolledCourses.length > 0 && (
115-
<Link to="/chats" style={{ textDecoration: 'none' }}>
116-
<Button>
117-
<BookmarksOutlined sx={styles.icon} /> {t('chats')}
118-
</Button>
119-
</Link>
130+
<NavItemButton to="/chats" path="chats/*" current={pathname} icon={<BookmarksOutlined sx={styles.icon} />}>
131+
{t('chats')}
132+
</NavItemButton>
120133
)}
121134
{user.ownCourses.length > 0 && (
122-
<Link to="/courses" style={{ textDecoration: 'none' }}>
123-
<Button>
124-
<BookmarksOutlined sx={styles.icon} /> {t('courses')}
125-
</Button>
126-
</Link>
135+
<NavItemButton to="/courses" path="courses/*" current={pathname} icon={<BookmarksOutlined sx={styles.icon} />}>
136+
{t('courses')}
137+
</NavItemButton>
127138
)}
128139
{user.isStatsViewer && (
129-
<Link to="/statistics" style={{ textDecoration: 'none' }}>
130-
<Button>
131-
<AdminPanelSettingsOutlined sx={styles.icon} /> {t('courseStats')}
132-
</Button>
133-
</Link>
140+
<NavItemButton to="/statistics" path="statistics/*" current={pathname} icon={<AdminPanelSettingsOutlined sx={styles.icon} />}>
141+
{t('courseStats')}
142+
</NavItemButton>
134143
)}
135144
{user.isAdmin && (
136-
<Link to="/admin" style={{ textDecoration: 'none' }}>
137-
<Button>
138-
<AdminPanelSettingsOutlined sx={styles.icon} /> {t('admin')}
139-
</Button>
140-
</Link>
145+
<NavItemButton to="/admin" path="admin/*" current={pathname} icon={<AdminPanelSettingsOutlined sx={styles.icon} />}>
146+
{t('admin')}
147+
</NavItemButton>
141148
)}
142149
<Button
143150
ref={anchorRef}

src/client/components/NavBar/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const styles: { [key: string]: SxProps<Theme> } = {
4545
backgroundColor: 'rgba(255, 255, 255, 0.22)',
4646
},
4747
},
48-
icon: { mr: 1, fontSize: '1rem' },
48+
icon: { fontSize: '1rem' },
4949
language: { mr: 1 },
5050
item: {
5151
flexGrow: 1,

0 commit comments

Comments
 (0)