Skip to content

Commit

Permalink
Change to useMediaQuery instead of useWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytula committed Aug 26, 2019
1 parent 5df028e commit 5f5e30c
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions packages/ra-ui-materialui/src/layout/AppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,13 @@ import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import MenuIcon from '@material-ui/icons/Menu';
import { useTheme } from '@material-ui/styles';
import useMediaQuery from '@material-ui/core/useMediaQuery';
import { toggleSidebar } from 'ra-core';

import LoadingIndicator from './LoadingIndicator';
import DefaultUserMenu from './UserMenu';
import HideOnScroll from './HideOnScroll';

/**
* Be careful using this hook. It only works because the number of
* breakpoints in theme is static. It will break once you change the number of
* breakpoints. See https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
*/
function useWidth() {
const theme = useTheme();
const keys = [...theme.breakpoints.keys].reverse();
return (
keys.reduce((output, key) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const matches = useMediaQuery(theme.breakpoints.up(key));
return !output && matches ? key : output;
}, null) || 'xs'
);
}

const useStyles = makeStyles(theme => ({
toolbar: {
paddingRight: 24,
Expand Down Expand Up @@ -77,7 +59,7 @@ const AppBar = ({
const classes = useStyles({ classes: classesOverride });
const dispatch = useDispatch();
const locale = useSelector(state => state.i18n.locale);
const width = useWidth();
const isXSmall = useMediaQuery(theme => theme.breakpoints.down('xs'));

return (
<HideOnScroll>
Expand All @@ -89,7 +71,7 @@ const AppBar = ({
>
<Toolbar
disableGutters
variant={width === 'xs' ? 'regular' : 'dense'}
variant={isXSmall ? 'regular' : 'dense'}
className={classes.toolbar}
>
<IconButton
Expand Down

0 comments on commit 5f5e30c

Please sign in to comment.