Alexander
Alexander

Reputation: 1378

How to overwrite specific style in material ui component

I'm using component from MUI, when I use dev tools I can see it has those styles:

.MuiList-padding {
    padding-top: 8px;
    padding-bottom: 8px;
}

I wish to remove the padding, but I can't get it to work, I tried

  <Menu
        id="simple-menu"
        anchorEl={anchorEl}
        keepMounted
        open={Boolean(anchorEl)}
        onClose={closeMenu}
        styles={{MuiList:{{padding:none}}}} 
      >
        <somecomponent/>
      </Menu>

but no luck, any ideas?

Upvotes: 3

Views: 7253

Answers (2)

Ryan Cogswell
Ryan Cogswell

Reputation: 80976

List has a disablePadding prop (https://material-ui.com/api/list/#props).

You can set this for Menu via MenuListProps as shown below:

import React from "react";
import Button from "@material-ui/core/Button";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";

export default function SimpleMenu() {
  const [anchorEl, setAnchorEl] = React.useState(null);

  const handleClick = event => {
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  return (
    <div>
      <Button
        aria-controls="simple-menu"
        aria-haspopup="true"
        onClick={handleClick}
      >
        Open Menu
      </Button>
      <Menu
        id="simple-menu"
        anchorEl={anchorEl}
        keepMounted
        open={Boolean(anchorEl)}
        onClose={handleClose}
        MenuListProps={{
          disablePadding: true
        }}
      >
        <MenuItem onClick={handleClose}>Profile</MenuItem>
        <MenuItem onClick={handleClose}>My account</MenuItem>
        <MenuItem onClick={handleClose}>Logout</MenuItem>
      </Menu>
    </div>
  );
}

Edit Menu disablePadding

Related answers:

Upvotes: 8

Ido
Ido

Reputation: 5748

Use makeStyles to define the new style for MuiList-Padding, and than use useStyles hook and MuiListProps prop to override that style:

import React from 'react';
import Button from '@material-ui/core/Button';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import { makeStyles } from "@material-ui/core/styles";


const useStyles = makeStyles(theme => ({
  padding: {
    paddingTop: "30px",
    paddingBottom: "30px"
  }
}));

export default function SimpleMenu() {
  const [anchorEl, setAnchorEl] = React.useState(null);
  const classes = useStyles();

  const handleClick = event => {
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  return (
    <div>
      <Button aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}>
        Open Menu
      </Button>
      <Menu
        id="simple-menu"
        anchorEl={anchorEl}
        keepMounted
        open={Boolean(anchorEl)}
        onClose={handleClose}
        MenuListProps={{
          classes: {padding: classes.padding},
        }}
      >
        <MenuItem onClick={handleClose}>Profile</MenuItem>
        <MenuItem onClick={handleClose}>My account</MenuItem>
        <MenuItem onClick={handleClose}>Logout</MenuItem>
      </Menu>
    </div>
  );
}

You can check that working Code Sandbox link


Note: this answer works for material-ui V4

Upvotes: 1

Related Questions