From 33097551fedc66cc8fd320c34b9ea6b1e47c007b Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Fri, 8 Jul 2022 09:50:24 +0200 Subject: [PATCH] Fix AppBar jiggles when scrolling down rapidly Closes #7944 --- .../ra-ui-materialui/src/layout/Layout.tsx | 18 +++----- .../ra-ui-materialui/src/layout/Sidebar.tsx | 41 +++++++++++++------ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/packages/ra-ui-materialui/src/layout/Layout.tsx b/packages/ra-ui-materialui/src/layout/Layout.tsx index fadbea171b3..22af759d2e4 100644 --- a/packages/ra-ui-materialui/src/layout/Layout.tsx +++ b/packages/ra-ui-materialui/src/layout/Layout.tsx @@ -16,7 +16,6 @@ import { Menu as DefaultMenu, MenuProps } from './Menu'; import { Error, ErrorProps } from './Error'; import { SkipNavigationButton } from '../button'; import { useSidebarState } from './useSidebarState'; -import { useScrollTrigger } from '@mui/material'; export const Layout = (props: LayoutProps) => { const { @@ -33,7 +32,6 @@ export const Layout = (props: LayoutProps) => { const [open] = useSidebarState(); const [errorInfo, setErrorInfo] = useState(null); - let trigger = useScrollTrigger(); const handleError = (error: Error, info: ErrorInfo) => { setErrorInfo(info); @@ -42,11 +40,7 @@ export const Layout = (props: LayoutProps) => { return ( -
+
@@ -94,7 +88,6 @@ const PREFIX = 'RaLayout'; export const LayoutClasses = { appFrame: `${PREFIX}-appFrame`, contentWithSidebar: `${PREFIX}-contentWithSidebar`, - appbarCollapsed: `${PREFIX}-appbarCollapsed`, content: `${PREFIX}-content`, }; @@ -117,18 +110,17 @@ const StyledLayout = styled('div', { flexDirection: 'column', flexGrow: 1, marginTop: theme.spacing(6), - transition: `margin ${theme.transitions.easing.easeOut} ${theme.transitions.duration.shortest}ms`, [theme.breakpoints.down('sm')]: { marginTop: theme.spacing(7), }, }, - [`& .${LayoutClasses.appbarCollapsed}`]: { - marginTop: 0, - transition: `margin ${theme.transitions.easing.sharp} ${theme.transitions.duration.shorter}ms`, - }, [`& .${LayoutClasses.contentWithSidebar}`]: { display: 'flex', flexGrow: 1, + transition: theme.transitions.create('margin', { + easing: theme.transitions.easing.easeOut, + duration: theme.transitions.duration.enteringScreen, + }), }, [`& .${LayoutClasses.content}`]: { backgroundColor: theme.palette.background.default, diff --git a/packages/ra-ui-materialui/src/layout/Sidebar.tsx b/packages/ra-ui-materialui/src/layout/Sidebar.tsx index a2a4a611b4f..888e5f94426 100644 --- a/packages/ra-ui-materialui/src/layout/Sidebar.tsx +++ b/packages/ra-ui-materialui/src/layout/Sidebar.tsx @@ -2,7 +2,13 @@ import * as React from 'react'; import { styled } from '@mui/material/styles'; import { ReactElement } from 'react'; import PropTypes from 'prop-types'; -import { Drawer, DrawerProps, useMediaQuery, Theme } from '@mui/material'; +import { + Drawer, + DrawerProps, + useMediaQuery, + Theme, + useScrollTrigger, +} from '@mui/material'; import lodashGet from 'lodash/get'; import { useLocale } from 'ra-core'; @@ -13,9 +19,9 @@ export const Sidebar = (props: SidebarProps) => { const isXSmall = useMediaQuery(theme => theme.breakpoints.down('sm') ); - const isSmall = useMediaQuery(theme => theme.breakpoints.down('md')); const [open, setOpen] = useSidebarState(); useLocale(); // force redraw on locale change + const trigger = useScrollTrigger(); const toggleSidebar = () => setOpen(!open); @@ -29,22 +35,13 @@ export const Sidebar = (props: SidebarProps) => { > {children} - ) : isSmall ? ( - -
{children}
-
) : (
{children}
@@ -78,6 +75,7 @@ export const SidebarClasses = { paperAnchorDockedBottom: `${PREFIX}-paperAnchorDockedBottom`, modal: `${PREFIX}-modal`, fixed: `${PREFIX}-fixed`, + appBarCollapsed: `${PREFIX}-appBarCollapsed`, }; const StyledDrawer = styled(Drawer, { @@ -87,7 +85,24 @@ const StyledDrawer = styled(Drawer, { shouldForwardProp: () => true, })(({ open, theme }) => ({ height: 'calc(100vh - 3em)', - + marginTop: 0, + transition: theme.transitions.create('margin', { + easing: theme.transitions.easing.easeOut, + duration: theme.transitions.duration.enteringScreen, + }), + [`&.${SidebarClasses.appBarCollapsed}`]: { + // compensate the margin of the Layout appFrame instead of removing it in the Layout + // because otherwise, the appFrame content without margin may revert the scrollTrigger, + // leading to a visual jiggle + marginTop: theme.spacing(-6), + [theme.breakpoints.down('sm')]: { + marginTop: theme.spacing(-7), + }, + transition: theme.transitions.create('margin', { + easing: theme.transitions.easing.sharp, + duration: theme.transitions.duration.leavingScreen, + }), + }, [`& .${SidebarClasses.docked}`]: {}, [`& .${SidebarClasses.paper}`]: {}, [`& .${SidebarClasses.paperAnchorLeft}`]: {},