Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sidebar): wrap long menu item label in sidebar #2566

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 106 additions & 75 deletions packages/app/src/components/Root/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Box from '@mui/material/Box';
import Collapse from '@mui/material/Collapse';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import { SxProps } from '@mui/material/styles';
import { makeStyles } from 'tss-react/mui';

import DynamicRootContext, {
Expand Down Expand Up @@ -117,21 +118,21 @@ const SideBarItemWrapper = (props: SidebarItemProps) => {

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

const renderExpandIcon = (expand: boolean, isSecondLevelMenuItem = false) => {
const renderExpandIcon = (expand: boolean) => {
return expand ? (
<ExpandMore
fontSize="small"
style={{
display: 'flex',
marginLeft: isSecondLevelMenuItem ? '' : '0.5rem',
marginLeft: 8,
}}
/>
) : (
<ChevronRightIcon
fontSize="small"
style={{
display: 'flex',
marginLeft: isSecondLevelMenuItem ? '' : '0.5rem',
marginLeft: 8,
}}
/>
);
Expand Down Expand Up @@ -161,6 +162,30 @@ const getMenuItem = (menuItem: ResolvedMenuItem, isNestedMenuItem = false) => {
);
};

interface ExpandableMenuListProps {
menuItems: ResolvedMenuItem[];
isOpen: boolean;
renderItem: (item: ResolvedMenuItem) => JSX.Element;
sx?: SxProps;
}

const ExpandableMenuList: React.FC<ExpandableMenuListProps> = ({
menuItems,
isOpen,
renderItem,
sx = {},
}) => {
if (!menuItems || menuItems.length === 0) return null;

return (
<Collapse in={isOpen} timeout="auto" unmountOnExit>
<List disablePadding sx={sx}>
{menuItems.map(item => renderItem(item))}
</List>
</Collapse>
);
};

export const Root = ({ children }: PropsWithChildren<{}>) => {
const {
classes: { pageWithoutFixHeight },
Expand Down Expand Up @@ -197,85 +222,91 @@ export const Root = ({ children }: PropsWithChildren<{}>) => {
isSubMenuOpen: boolean,
) => {
return (
<>
{menuItem.children &&
menuItem.children.length > 0 &&
menuItem.children?.map(child => (
<Collapse
key={child.name}
in={isSubMenuOpen}
timeout="auto"
unmountOnExit
>
<List
disablePadding
sx={{
paddingLeft: '4rem',
fontSize: 12,
'& span.MuiTypography-subtitle2': { fontSize: 12 },
'& div': { width: '36px', boxShadow: '-1px 0 0 0 #3c3f42' },
}}
>
<SideBarItemWrapper
icon={() => null}
text={child.title}
to={child.to ?? ''}
/>
</List>
</Collapse>
))}
</>
<ExpandableMenuList
menuItems={menuItem.children ?? []}
isOpen={isSubMenuOpen}
sx={{
paddingLeft: '4.25rem',
fontSize: 12,
'& span.MuiTypography-subtitle2': {
fontSize: 12,
whiteSpace: 'normal',
wordBreak: 'break-word',
overflowWrap: 'break-word',
},
'& div': { width: 36, boxShadow: '-1px 0 0 0 #3c3f42' },
"& div[class*='BackstageSidebarItem-secondaryAction']": { width: 20 },
a: {
width: 'auto',
'@media (min-width: 600px)': { width: 160 },
},
}}
renderItem={child => (
<SideBarItemWrapper
key={child.title}
icon={() => null}
text={child.title}
to={child.to ?? ''}
/>
)}
/>
);
};

const renderExpandableMenuItems = (
menuItem: ResolvedMenuItem,
isOpen: boolean,
) => {
return (
<>
{menuItem.children &&
menuItem.children.length > 0 &&
menuItem.children?.map(child => {
const isNestedMenuOpen = openItems[child.name] || false;
return (
<Collapse
key={child.name}
in={isOpen}
timeout="auto"
unmountOnExit
>
<List disablePadding>
{child.children && child.children.length === 0 && (
<ListItem disableGutters disablePadding>
{getMenuItem(child, true)}
</ListItem>
)}
{child.children && child.children.length > 0 && (
<ListItem
disableGutters
disablePadding
sx={{
display: 'block',
'& .MuiButton-label': { paddingLeft: '2rem' },
}}
>
<SideBarItemWrapper
key={child.name}
icon={renderIcon(child.icon ?? '')}
text={child.title}
onClick={() => handleClick(child.name)}
>
{child.children.length > 0 &&
renderExpandIcon(isNestedMenuOpen, true)}
</SideBarItemWrapper>
{renderExpandableNestedMenuItems(child, isNestedMenuOpen)}
</ListItem>
)}
</List>
</Collapse>
);
})}
</>
<ExpandableMenuList
menuItems={menuItem.children ?? []}
isOpen={isOpen}
renderItem={child => {
const isNestedMenuOpen = openItems[child.name] || false;
return (
<ListItem
key={child.name}
disableGutters
disablePadding
sx={{
display: 'block',
'& .MuiButton-label': { paddingLeft: '2rem' },
"& span[class*='-subtitle2']": {
width: 78,
fontSize: 12,
whiteSpace: 'normal',
wordBreak: 'break-word',
overflowWrap: 'break-word',
},
"& div[class*='BackstageSidebarItem-secondaryAction']": {
width:
child.children && child.children.length === 0 ? 18 : 48,
},
a: {
width: 'auto',
'@media (min-width: 600px)': { width: 224 },
},
}}
>
{child.children && child.children.length === 0 ? (
getMenuItem(child, true)
) : (
<>
<SideBarItemWrapper
icon={renderIcon(child.icon ?? '')}
text={child.title}
onClick={() => handleClick(child.name)}
>
{child.children!.length > 0 &&
renderExpandIcon(isNestedMenuOpen)}
</SideBarItemWrapper>
{renderExpandableNestedMenuItems(child, isNestedMenuOpen)}
</>
)}
</ListItem>
);
}}
/>
);
};

Expand Down
Loading