-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathMenu.tsx
More file actions
212 lines (192 loc) · 6.47 KB
/
Copy pathMenu.tsx
File metadata and controls
212 lines (192 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
ListSubheader,
Tooltip,
} from '@mui/material'
import { styled } from '@mui/material/styles'
import React, { useContext, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Link, useLocation } from 'react-router'
import { LayoutContextInterface, LayoutCtx } from 'src/layout/LayoutContext'
import DonateModal from 'src/pages/DonateModal/DonateModal'
import { PAGES } from 'src/routes'
type MainMenuProps = {
collapsed?: boolean
/* Density only — both the sider and the drawer fit themselves to the space they get
(see NavList). The sider is driven by a mouse and shares its screen with the page, so
it starts tighter; the drawer owns the whole viewport and is touched, so it starts
roomy. */
compact?: boolean
}
const MENU_GROUPS = [
{
key: 'menu_group_analysis',
paths: [
'/single-line-map',
'/timeline',
'/gaps',
'/gaps_patterns',
'/operator',
'/vehicle',
'/train',
],
},
{
key: 'menu_group_maps',
paths: ['/map', '/velocity-heatmap'],
},
{
key: 'menu_group_community',
paths: ['/public-appeal', '/about', '/donate'],
},
] as const
// antd's menu blues, kept as they were so the selected row survives the port unchanged
const SELECTED_COLORS = {
light: { backgroundColor: '#e6f4ff', color: '#1677ff' },
dark: { backgroundColor: '#1668dc', color: '#fff' },
}
const ROW_HEIGHT = { compact: 36, roomy: 44 }
/* WCAG 2.5.8 puts the floor for a pointer target at 24px. The sider is driven by a mouse
so it can approach that; the drawer is touched, so it keeps a larger floor and lets the
drawer scroll rather than shrink past it. */
const MIN_ROW_HEIGHT = { compact: 28, roomy: 32 }
/**
* Auto-fit: the list is a flex column sized to whatever holds it — the sider on desktop,
* the drawer body on mobile — so the rows absorb the available height and shrink from
* ROW_HEIGHT toward MIN_ROW_HEIGHT on a short viewport instead of overflowing. Past that
* floor the container's own overflow scrolls.
*
* Every row is a flex item of this one container — the group headings are siblings rather
* than nested lists — so all rows carry the same shrink weight and stay the same height as
* each other. Spacing is `gap`, not margins: margins don't shrink, and would pin the rows
* above their flex basis.
*/
const NavList = styled(List, {
shouldForwardProp: (prop) => prop !== 'compact' && prop !== 'collapsed',
})<MainMenuProps>(({ theme, compact, collapsed }) => {
const density = compact ? 'compact' : 'roomy'
return {
padding: compact ? '4px 8px 8px' : '8px 10px 12px',
display: 'flex',
flexDirection: 'column',
gap: 2,
height: '100%',
boxSizing: 'border-box',
'& .MuiListSubheader-root': {
// Fixed overhead — only the rows shrink, so the headings stay lean: on a phone three
// roomy headings cost more than a whole row of the height they're competing for.
flex: '0 0 auto',
padding: '6px 12px 2px',
marginBlockStart: compact ? 4 : 8,
backgroundColor: 'transparent',
fontSize: 11,
fontWeight: 700,
lineHeight: 1.6,
letterSpacing: '0.08em',
textTransform: 'uppercase',
color: theme.palette.text.secondary,
},
'& .MuiListItem-root': {
flex: `0 1 ${ROW_HEIGHT[density]}px`,
minHeight: MIN_ROW_HEIGHT[density],
},
'& .MuiListItemButton-root': {
// The row's height comes from its flex item; the button only fills it.
height: '100%',
minHeight: 0,
paddingBlock: 0,
paddingInline: collapsed ? 0 : 12,
justifyContent: collapsed ? 'center' : undefined,
borderRadius: compact ? 8 : 10,
'&.Mui-selected, &.Mui-selected:hover': SELECTED_COLORS[theme.palette.mode],
},
'& .MuiListItemIcon-root': {
minWidth: 0,
marginInlineEnd: collapsed ? 0 : 10,
color: 'inherit',
'& .MuiSvgIcon-root': { fontSize: 20 },
},
'& .MuiListItemText-primary': {
fontSize: 14,
},
}
})
const MainMenu = ({ collapsed = false, compact = false }: MainMenuProps) => {
const { t, i18n } = useTranslation()
const { setDrawerOpen } = useContext<LayoutContextInterface>(LayoutCtx)
const [isDonateModalVisible, setDonateModalVisible] = useState(false)
const { pathname } = useLocation()
// src/routes imports the layout, so PAGES is still in its temporal dead zone while
// this module initializes — the lookup has to be built at render time.
const pageByPath = new Map<string, (typeof PAGES)[number]>(PAGES.map((page) => [page.path, page]))
const handleDonateClick = (event: React.MouseEvent) => {
event.preventDefault()
setDonateModalVisible(true)
setDrawerOpen(false)
}
const renderItem = (path: string) => {
const page = pageByPath.get(path)
if (!page) return null
const label = t(page.label)
const selected = pathname === path
const ariaCurrent = selected ? 'page' : undefined
const content = (
<>
<ListItemIcon>{page.icon}</ListItemIcon>
{!collapsed && <ListItemText primary={label} slotProps={{ primary: { noWrap: true } }} />}
</>
)
const button =
page.label === 'donate_title' ? (
<ListItemButton
component="a"
href="#"
onClick={handleDonateClick}
selected={selected}
aria-current={ariaCurrent}>
{content}
</ListItemButton>
) : (
<ListItemButton
component={Link}
to={path}
onClick={() => setDrawerOpen(false)}
selected={selected}
aria-current={ariaCurrent}>
{content}
</ListItemButton>
)
return (
<ListItem key={path} disablePadding>
{collapsed ? (
<Tooltip title={label} placement={i18n.dir() === 'rtl' ? 'left' : 'right'}>
{button}
</Tooltip>
) : (
button
)}
</ListItem>
)
}
return (
<>
<NavList className="sidebar-menu" compact={compact} collapsed={collapsed}>
{renderItem('/')}
{MENU_GROUPS.flatMap(({ key, paths }) => [
collapsed ? null : (
<ListSubheader key={key} disableSticky>
{t(key)}
</ListSubheader>
),
...paths.map((path) => renderItem(path)),
])}
</NavList>
<DonateModal isVisible={isDonateModalVisible} onClose={() => setDonateModalVisible(false)} />
</>
)
}
export default MainMenu