Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"description": "Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports."
},
"hideNavigation": {
"description": "Whether the navigation bar and menu icon should be hidden"
"description": "Whether the navigation bar and menu icon should be hidden."
},
"navigation": {
"description": "Navigation definition for the dashboard. <a href=\"https://mui.com/toolpad/core/react-dashboard-layout/#navigation\">Find out more</a>."
Expand Down
21 changes: 19 additions & 2 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,18 @@ describe('DashboardLayout', () => {
icon: <ShoppingCartIcon />,
},
{
segment: 'dynamic',
segment: 'dynamic/override',
title: 'Dynamic Override',
icon: <BarChartIcon />,
},
{
segment: 'dynamicMoreOnly',
title: 'Dynamic',
icon: <BarChartIcon />,
pattern: 'dynamic/:dynamicId',
},
{
segment: 'optional',
segment: 'optionalMoreOnly',
title: 'Optional',
pattern: 'optional{/:optionalId}?',
},
Expand Down Expand Up @@ -268,11 +273,23 @@ describe('DashboardLayout', () => {
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).toHaveClass(
'Mui-selected',
);
expect(
within(desktopNavigation).getByRole('link', { name: 'Dynamic Override' }),
).not.toHaveClass('Mui-selected');
rerender(<AppWithPathname pathname="/dynamic/123/456" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).not.toHaveClass(
'Mui-selected',
);

// Does not show multiple selected items if a dynamic segment is overridden by a more specific segment
rerender(<AppWithPathname pathname="/dynamic/override" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic Override' })).toHaveClass(
'Mui-selected',
);
expect(within(desktopNavigation).getByRole('link', { name: 'Dynamic' })).not.toHaveClass(
'Mui-selected',
);

rerender(<AppWithPathname pathname="/optional" />);
expect(within(desktopNavigation).getByRole('link', { name: 'Optional' })).toHaveClass(
'Mui-selected',
Expand Down
46 changes: 27 additions & 19 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { DashboardSidebarSubNavigation } from './DashboardSidebarSubNavigation';
import { ToolbarActions } from './ToolbarActions';
import { AppTitle, AppTitleProps } from './AppTitle';
import { getDrawerSxTransitionMixin, getDrawerWidthTransitionMixin } from './utils';
import { MINI_DRAWER_WIDTH } from './shared';
import type { Branding, Navigation } from '../AppProvider';

const AppBar = styled(MuiAppBar)(({ theme }) => ({
Expand Down Expand Up @@ -80,17 +81,17 @@ export interface DashboardLayoutProps {
*/
navigation?: Navigation;
/**
* Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports.
* Whether the sidebar should start collapsed in desktop size screens.
* @default false
*/
disableCollapsibleSidebar?: boolean;
defaultSidebarCollapsed?: boolean;
/**
* Whether the sidebar should start collapsed in desktop size screens.
* Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports.
* @default false
*/
defaultSidebarCollapsed?: boolean;
disableCollapsibleSidebar?: boolean;
/**
* Whether the navigation bar and menu icon should be hidden
* Whether the navigation bar and menu icon should be hidden.
* @default false
*/
hideNavigation?: boolean;
Expand Down Expand Up @@ -130,8 +131,8 @@ function DashboardLayout(props: DashboardLayoutProps) {
children,
branding: brandingProp,
navigation: navigationProp,
disableCollapsibleSidebar = false,
defaultSidebarCollapsed = false,
disableCollapsibleSidebar = false,
hideNavigation = false,
sidebarExpandedWidth = 320,
slots,
Expand Down Expand Up @@ -182,6 +183,8 @@ function DashboardLayout(props: DashboardLayoutProps) {

const [isNavigationFullyExpanded, setIsNavigationFullyExpanded] =
React.useState(isNavigationExpanded);
const [isNavigationFullyCollapsed, setIsNavigationFullyCollapsed] =
React.useState(!isNavigationExpanded);

React.useEffect(() => {
if (isNavigationExpanded) {
Expand All @@ -197,7 +200,19 @@ function DashboardLayout(props: DashboardLayoutProps) {
return () => {};
}, [isNavigationExpanded, theme]);

const selectedItemIdRef = React.useRef('');
React.useEffect(() => {
if (!isNavigationExpanded) {
const drawerWidthTransitionTimeout = setTimeout(() => {
setIsNavigationFullyCollapsed(true);
}, theme.transitions.duration.leavingScreen);

return () => clearTimeout(drawerWidthTransitionTimeout);
}

setIsNavigationFullyCollapsed(false);

return () => {};
}, [isNavigationExpanded, theme]);

const handleSetNavigationExpanded = React.useCallback(
(newExpanded: boolean) => () => {
Expand All @@ -211,17 +226,9 @@ function DashboardLayout(props: DashboardLayoutProps) {
}, [isNavigationExpanded, setIsNavigationExpanded]);

const handleNavigationLinkClick = React.useCallback(() => {
selectedItemIdRef.current = '';
setIsMobileNavigationExpanded(false);
}, [setIsMobileNavigationExpanded]);

// If useEffect was used, the reset would also happen on the client render after SSR which we don't need
React.useMemo(() => {
if (navigation) {
selectedItemIdRef.current = '';
}
}, [navigation]);

const isDesktopMini = !disableCollapsibleSidebar && !isDesktopNavigationExpanded;
const isMobileMini = !disableCollapsibleSidebar && !isMobileNavigationExpanded;

Expand Down Expand Up @@ -279,8 +286,8 @@ function DashboardLayout(props: DashboardLayoutProps) {
onLinkClick={handleNavigationLinkClick}
isMini={isMini}
isFullyExpanded={isNavigationFullyExpanded}
isFullyCollapsed={isNavigationFullyCollapsed}
hasDrawerTransitions={hasDrawerTransitions}
selectedItemId={selectedItemIdRef.current}
/>
{SidebarFooterSlot ? (
<SidebarFooterSlot mini={isMini} {...slotProps?.sidebarFooter} />
Expand All @@ -292,6 +299,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
SidebarFooterSlot,
handleNavigationLinkClick,
hasDrawerTransitions,
isNavigationFullyCollapsed,
isNavigationFullyExpanded,
navigation,
slotProps?.sidebarFooter,
Expand All @@ -300,7 +308,7 @@ function DashboardLayout(props: DashboardLayoutProps) {

const getDrawerSharedSx = React.useCallback(
(isMini: boolean, isTemporary: boolean) => {
const drawerWidth = isMini ? 64 : sidebarExpandedWidth;
const drawerWidth = isMini ? MINI_DRAWER_WIDTH : sidebarExpandedWidth;

return {
displayPrint: 'none',
Expand Down Expand Up @@ -335,7 +343,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
}}
>
<AppBar color="inherit" position="absolute" sx={{ displayPrint: 'none' }}>
<Toolbar sx={{ backgroundColor: 'inherit', mx: { xs: -0.75, sm: -1.5 } }}>
<Toolbar sx={{ backgroundColor: 'inherit', mx: { xs: -0.75, sm: -1 } }}>
<Stack
direction="row"
justifyContent="space-between"
Expand Down Expand Up @@ -485,7 +493,7 @@ DashboardLayout.propTypes /* remove-proptypes */ = {
*/
disableCollapsibleSidebar: PropTypes.bool,
/**
* Whether the navigation bar and menu icon should be hidden
* Whether the navigation bar and menu icon should be hidden.
* @default false
*/
hideNavigation: PropTypes.bool,
Expand Down
Loading