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

[DashboardLayout] Fix sidebar CSS transitions for some breakpoints #4522

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
"chalk": "5.3.0",
"concurrently": "9.1.0",
"css-mediaquery": "^0.1.2",
"eslint": "8.57.1",
"eslint-config-airbnb": "19.0.4",
"eslint-config-airbnb-typescript": "18.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe('DashboardLayout', () => {
action: <div>Action 1</div>,
},
{
title: 'Item',
title: 'Item 2',
segment: 'item2',
icon: <DescriptionIcon />,
action: <div>Action 2</div>,
Expand Down
25 changes: 12 additions & 13 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,32 +152,32 @@ function DashboardLayout(props: DashboardLayoutProps) {
React.useState(!defaultSidebarCollapsed);
const [isMobileNavigationExpanded, setIsMobileNavigationExpanded] = React.useState(false);

const isUnderMdViewport = useMediaQuery(
theme.breakpoints.down('md'),
const isOverSmViewport = useMediaQuery(
theme.breakpoints.up('sm'),
appWindowContext && {
matchMedia: appWindowContext.matchMedia,
},
);
const isOverSmViewport = useMediaQuery(
theme.breakpoints.up('sm'),
const isOverMdViewport = useMediaQuery(
theme.breakpoints.up('md'),
appWindowContext && {
matchMedia: appWindowContext.matchMedia,
},
);

const isNavigationExpanded = isUnderMdViewport
? isMobileNavigationExpanded
: isDesktopNavigationExpanded;
const isNavigationExpanded = isOverMdViewport
? isDesktopNavigationExpanded
: isMobileNavigationExpanded;

const setIsNavigationExpanded = React.useCallback(
(newExpanded: boolean) => {
if (isUnderMdViewport) {
setIsMobileNavigationExpanded(newExpanded);
} else {
if (isOverMdViewport) {
setIsDesktopNavigationExpanded(newExpanded);
} else {
setIsMobileNavigationExpanded(newExpanded);
}
},
[isUnderMdViewport],
[isOverMdViewport],
);

const [isNavigationFullyExpanded, setIsNavigationFullyExpanded] =
Expand Down Expand Up @@ -249,8 +249,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
[toggleNavigationExpanded],
);

const hasDrawerTransitions =
isOverSmViewport && (disableCollapsibleSidebar || !isUnderMdViewport);
const hasDrawerTransitions = isOverSmViewport && (!disableCollapsibleSidebar || isOverMdViewport);

const ToolbarActionsSlot = slots?.toolbarActions ?? ToolbarActions;
const ToolbarAccountSlot = slots?.toolbarAccount ?? Account;
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions test/setupVitest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { afterEach, vi } from 'vitest';
import failOnConsole from 'vitest-fail-on-console';
import { cleanup } from '@testing-library/react';
import mediaQuery from 'css-mediaquery';

failOnConsole({
silenceMessage: (errorMessage) => {
Expand All @@ -16,18 +17,24 @@ afterEach(cleanup);

// Mocks

function createMatchMedia(width: number) {
return (query: string) => ({
matches: mediaQuery.match(query, {
width,
}),
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
});
}

if (typeof window !== 'undefined' && !window.matchMedia) {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
value: createMatchMedia(window.innerWidth),
});
}
Loading