Skip to content

Commit a68f000

Browse files
committed
fix(sidebar): wrap long menu item label in sidebar
Signed-off-by: Yi Cai <[email protected]>
1 parent a3aeae0 commit a68f000

File tree

1 file changed

+102
-75
lines changed
  • packages/app/src/components/Root

1 file changed

+102
-75
lines changed

packages/app/src/components/Root/Root.tsx

+102-75
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import ChevronRightIcon from '@mui/icons-material/ChevronRight';
2222
import ExpandMore from '@mui/icons-material/ExpandMore';
2323
import MuiMenuIcon from '@mui/icons-material/Menu';
2424
import SearchIcon from '@mui/icons-material/Search';
25+
import { SxProps } from '@mui/material/styles';
2526
import Box from '@mui/material/Box';
2627
import Collapse from '@mui/material/Collapse';
2728
import List from '@mui/material/List';
@@ -117,21 +118,21 @@ const SideBarItemWrapper = (props: SidebarItemProps) => {
117118

118119
const renderIcon = (iconName: string) => () => <MenuIcon icon={iconName} />;
119120

120-
const renderExpandIcon = (expand: boolean, isSecondLevelMenuItem = false) => {
121+
const renderExpandIcon = (expand: boolean) => {
121122
return expand ? (
122123
<ExpandMore
123124
fontSize="small"
124125
style={{
125126
display: 'flex',
126-
marginLeft: isSecondLevelMenuItem ? '' : '0.5rem',
127+
marginLeft: 8,
127128
}}
128129
/>
129130
) : (
130131
<ChevronRightIcon
131132
fontSize="small"
132133
style={{
133134
display: 'flex',
134-
marginLeft: isSecondLevelMenuItem ? '' : '0.5rem',
135+
marginLeft: 8,
135136
}}
136137
/>
137138
);
@@ -161,6 +162,30 @@ const getMenuItem = (menuItem: ResolvedMenuItem, isNestedMenuItem = false) => {
161162
);
162163
};
163164

165+
interface ExpandableMenuListProps {
166+
menuItems: ResolvedMenuItem[];
167+
isOpen: boolean;
168+
renderItem: (item: ResolvedMenuItem) => JSX.Element;
169+
sx?: SxProps;
170+
}
171+
172+
const ExpandableMenuList: React.FC<ExpandableMenuListProps> = ({
173+
menuItems,
174+
isOpen,
175+
renderItem,
176+
sx = {},
177+
}) => {
178+
if (!menuItems || menuItems.length === 0) return null;
179+
180+
return (
181+
<Collapse in={isOpen} timeout="auto" unmountOnExit>
182+
<List disablePadding sx={sx}>
183+
{menuItems.map(item => renderItem(item))}
184+
</List>
185+
</Collapse>
186+
);
187+
};
188+
164189
export const Root = ({ children }: PropsWithChildren<{}>) => {
165190
const {
166191
classes: { pageWithoutFixHeight },
@@ -197,85 +222,87 @@ export const Root = ({ children }: PropsWithChildren<{}>) => {
197222
isSubMenuOpen: boolean,
198223
) => {
199224
return (
200-
<>
201-
{menuItem.children &&
202-
menuItem.children.length > 0 &&
203-
menuItem.children?.map(child => (
204-
<Collapse
205-
key={child.name}
206-
in={isSubMenuOpen}
207-
timeout="auto"
208-
unmountOnExit
209-
>
210-
<List
211-
disablePadding
212-
sx={{
213-
paddingLeft: '4rem',
214-
fontSize: 12,
215-
'& span.MuiTypography-subtitle2': { fontSize: 12 },
216-
'& div': { width: '36px', boxShadow: '-1px 0 0 0 #3c3f42' },
217-
}}
218-
>
219-
<SideBarItemWrapper
220-
icon={() => null}
221-
text={child.title}
222-
to={child.to ?? ''}
223-
/>
224-
</List>
225-
</Collapse>
226-
))}
227-
</>
225+
<ExpandableMenuList
226+
menuItems={menuItem.children ?? []}
227+
isOpen={isSubMenuOpen}
228+
sx={{
229+
paddingLeft: '4rem',
230+
fontSize: 12,
231+
'& span.MuiTypography-subtitle2': {
232+
fontSize: 12,
233+
whiteSpace: 'normal',
234+
},
235+
'& div': { width: 36, boxShadow: '-1px 0 0 0 #3c3f42' },
236+
"& div[class*='BackstageSidebarItem-secondaryAction']": { width: 18 },
237+
a: {
238+
width: 'auto',
239+
'@media (min-width: 600px)': { width: 160 },
240+
},
241+
}}
242+
renderItem={child => (
243+
<SideBarItemWrapper
244+
key={child.title}
245+
icon={() => null}
246+
text={child.title}
247+
to={child.to ?? ''}
248+
/>
249+
)}
250+
/>
228251
);
229252
};
253+
230254
const renderExpandableMenuItems = (
231255
menuItem: ResolvedMenuItem,
232256
isOpen: boolean,
233257
) => {
234258
return (
235-
<>
236-
{menuItem.children &&
237-
menuItem.children.length > 0 &&
238-
menuItem.children?.map(child => {
239-
const isNestedMenuOpen = openItems[child.name] || false;
240-
return (
241-
<Collapse
242-
key={child.name}
243-
in={isOpen}
244-
timeout="auto"
245-
unmountOnExit
246-
>
247-
<List disablePadding>
248-
{child.children && child.children.length === 0 && (
249-
<ListItem disableGutters disablePadding>
250-
{getMenuItem(child, true)}
251-
</ListItem>
252-
)}
253-
{child.children && child.children.length > 0 && (
254-
<ListItem
255-
disableGutters
256-
disablePadding
257-
sx={{
258-
display: 'block',
259-
'& .MuiButton-label': { paddingLeft: '2rem' },
260-
}}
261-
>
262-
<SideBarItemWrapper
263-
key={child.name}
264-
icon={renderIcon(child.icon ?? '')}
265-
text={child.title}
266-
onClick={() => handleClick(child.name)}
267-
>
268-
{child.children.length > 0 &&
269-
renderExpandIcon(isNestedMenuOpen, true)}
270-
</SideBarItemWrapper>
271-
{renderExpandableNestedMenuItems(child, isNestedMenuOpen)}
272-
</ListItem>
273-
)}
274-
</List>
275-
</Collapse>
276-
);
277-
})}
278-
</>
259+
<ExpandableMenuList
260+
menuItems={menuItem.children ?? []}
261+
isOpen={isOpen}
262+
renderItem={child => {
263+
const isNestedMenuOpen = openItems[child.name] || false;
264+
return (
265+
<ListItem
266+
key={child.name}
267+
disableGutters
268+
disablePadding
269+
sx={{
270+
display: 'block',
271+
'& .MuiButton-label': { paddingLeft: '2rem' },
272+
"& span[class*='-subtitle2']": {
273+
width: 78,
274+
fontSize: 12,
275+
whiteSpace: 'normal',
276+
},
277+
"& div[class*='BackstageSidebarItem-secondaryAction']": {
278+
width:
279+
child.children && child.children.length === 0 ? 18 : 48,
280+
},
281+
a: {
282+
width: 'auto',
283+
'@media (min-width: 600px)': { width: 224 },
284+
},
285+
}}
286+
>
287+
{child.children && child.children.length === 0 ? (
288+
getMenuItem(child, true)
289+
) : (
290+
<>
291+
<SideBarItemWrapper
292+
icon={renderIcon(child.icon ?? '')}
293+
text={child.title}
294+
onClick={() => handleClick(child.name)}
295+
>
296+
{child.children!.length > 0 &&
297+
renderExpandIcon(isNestedMenuOpen)}
298+
</SideBarItemWrapper>
299+
{renderExpandableNestedMenuItems(child, isNestedMenuOpen)}
300+
</>
301+
)}
302+
</ListItem>
303+
);
304+
}}
305+
/>
279306
);
280307
};
281308

0 commit comments

Comments
 (0)