Skip to content
Advertisement

Add custom style inside DataGrid Toolbar’s popup component Material-UI

I’m creating a custom Data Grid Toolbar component by modifying existing Grid Toolbar components from Material-UI.

Here is the official example for the Grid Toolbar components:

If we click one of the Grid Toolbar components, it will show a popup/popper as seen in the screenshot below.

enter image description here

What I want to do is to change all font sizes inside every popup/popper from each Grid Toolbar component.

I try to change one text for example. As we can see from the screenshot below, if we inspect the text then change the font-size and color properties directly, it would change.

enter image description here

But if I grab and copy the selector (in this case is .MuiTypography-body1), then I paste it in my code, there nothing changes (the font-size and color properties don’t change).

Here is the simple code:

const CustomGridToolbarColumns = withStyles((theme) => ({
  root: {
    color: "dodgerblue",
    "& .MuiTypography-body1": {
      fontSize: 20,
      color: "red"
    }
  }
}))(GridToolbarColumnsButton);

I also want to change all font-size and color properties inside each popup/popper of each Grid Toolbar component. I inspect the popup/popper then change the font-size and color properties but still don’t work as seen in the screenshot below.

enter image description here

Here are the dependencies (in package.json file):

"@material-ui/core": "^4.12.3",
"@material-ui/styles": "^4.11.4",
"@mui/x-data-grid-pro": "^4.0.0",

Here is the full code: https://codesandbox.io/s/data-grid-material-ui-custom-toolbar-kd9gj

So my questions are:

  1. how can we change some properties inside the popup/popper of every Grid Toolbar component?
  2. how can we change all properties inside the popup/popper of every Grid Toolbar component?

Advertisement

Answer

You can inspect the element and see that the component you need to apply the style to is called GridPanel. This is the wrapper component of the filters and columns panel. See all the component slots here for reference.

V5

<DataGrid
  {...data}
  components={{
    Toolbar: GridToolbar,
  }}
  componentsProps={{
    panel: {
      sx: {
        '& .MuiTypography-root': {
          color: 'dodgerblue',
          fontSize: 20,
        },
        '& .MuiDataGrid-filterForm': {
          bgcolor: 'lightblue',
        },
      },
    },
  }}
/>

In order to change the styles of the other 2 GridMenus (density/export), you need to target the MuiDataGrid-menuList class name. Currently I see there is no other way around using global styles because DataGrid does not allow you to customize the GridMenu component:

<GlobalStyles
  styles={{
    '.MuiDataGrid-menuList': {
      backgroundColor: 'pink',

      '& .MuiMenuItem-root': {
        fontSize: 26,
      },
    },
  }}
/>

Codesandbox Demo

V4

import { DataGrid, GridToolbar, GridPanel } from "@mui/x-data-grid";

const useStyles = makeStyles({
  panel: {
    '& .MuiTypography-root': {
      color: 'dodgerblue',
      fontSize: 20,
    },
  },
});
<DataGrid
  {...data}
  components={{
    Toolbar: GridToolbar,
  }}
  componentsProps={{
    // GridPanel
    panel: { className: classes.panel },
  }}
/>
<GlobalStyles
  styles={{
    ".MuiDataGrid-gridMenuList": {
      backgroundColor: "pink",

      "& .MuiMenuItem-root": {
        fontSize: 26
      }
    }
  }}
/>

Codesandbox Demo

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement