Customize (Style) Components Guide
One of the strengths of Material React Table is that it exposes a majority of all the underlying Material UI component props used to build the table.
Additionally, in one of the sections below, you will learn how to customize and use a MUI Theme to customize colors, typography, or any other default CSS that is used by Material React Table.
Relevant Props
All props labeled mui...Props
get forwarded to Material UI components. Here is a list of all the props that are exposed in both the root props and column options.
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| Material UI Toolbar Props | |||
2 |
| Material UI IconButton Props | |||
3 |
| Material UI IconButton Props | |||
4 |
| Material UI LinearProgress Props | |||
5 |
| Material UI TextField Props | |||
6 |
| Material UI Checkbox Props | |||
7 |
| Material UI Checkbox Props | |||
8 |
| Material UI Button Props | |||
9 |
| Material UI TextField Props | |||
10 |
| Material UI TableCell Props | |||
11 |
| Material UI Skeleton Props | |||
12 |
| Material UI TableBody Props | |||
13 |
| Material UI IconButton Props | |||
14 |
|
| Material UI TableRow Props | ||
15 |
| Material UI TableContainer Props | |||
16 |
| Material UI TableCell Props | |||
17 |
| Material UI TableCell Props | |||
18 |
| Material UI TableFooter Props | |||
19 |
| Material UI TableRow Props | |||
20 |
| Material UI IconButton Props | |||
21 |
| Material UI IconButton Props | |||
22 |
| Material UI TextField Props | |||
23 |
| Material UI TableCell Props | |||
24 |
| Material UI TableHead Props | |||
25 |
| Material UI TableRow Props | |||
26 |
| Material UI TablePagination Props | |||
27 |
| MUI Paper API Docs | |||
28 |
| MUI TableProps API Docs | |||
29 |
| Material UI Chip Props | |||
30 |
| Material UI Alert Props | |||
31 |
| Material UI Toolbar Props | |||
Relevant Column Options
Some of the column options expose the same props as above, but on a per column basis.
# | Column Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| Material UI Button API | |||
2 |
| Material UI TextField API | |||
3 |
| Material UI TableCell API | |||
4 |
| Material UI TableCell API | |||
5 |
| Material UI IconButton API | |||
6 |
| Material UI TextField Props | |||
7 |
| Material UI TableCell API | |||
Material UI Props and Types
Each prop can either be passed as an object or as a callback function where you get access to the underlying table
instance and any other relevant callback parameters, such as cell
, row
, column
, etc. This lets you easily run conditional logic on the props you pass. Let's take a look at a few examples.
All
mui...Props
props are strongly typed and you should get ts hints as you write them. API docs are available on the Material UI website for each component.
Static Prop Objects
<MaterialReactTablecolumns={columns}data={data}enableRowSelection//passing the static object variant if no dynamic logic is neededmuiSelectCheckboxProps={{color: 'secondary', //makes all checkboxes use the secondary color}}/>
Callback Functions to Dynamically Set Prop Values
<MaterialReactTablecolumns={columns}data={data}enableRowSelection//passing the callback function variant. (You should get type hints for all the callback parameters available)muiSelectCheckboxProps={({ row }) => ({color: 'secondary',disabled: row.original.isAccountLocked, //access the row data to determine if the checkbox should be disabled})}/>
Styling Material UI Components
Each mui...Prop
has multiple options for you to add styling to the component. You could simply pass className
or style
props to any mui...Props
prop, but there is also the sx
prop...which totally rocks!
Hint: You should just use the
sx
prop for all styling instead ofclassName
orstyle
props.
The SX Prop
The recommended way to style Material UI components in Material React Table will be the sx
prop throughout this docs site, as it is both the most simple and the most powerful way to style Material UI components as of Material UI V5. They can work and be as simple as a style
prop, but behind the scenes, they work more like emotion styled-components by using mui/system
.
Don't worry, className
and style
props will still work, but let's show off some of the more elegant syntax you can use with the sx
prop.
The
sx
prop can be used just a simply as astyle
prop by default
<MaterialReactTablecolumns={columns}data={data}muiTableHeadCellProps={{//simple styling with the `sx` prop, works just like a style prop in this examplesx: {fontWeight: 'normal',fontSize: '14px',},}}/>
The
sx
prop gets easy access to your muiTheme without you having to call the theme from auseTheme
hook.
<MaterialReactTablecolumns={columns}data={data}muiTableHeadCellProps={{//no useTheme hook needed, just use the `sx` prop with the theme callbacksx: (theme) => ({color: theme.palette.text.secondary,}),}}/>
The
sx
prop gives you a much more concise way to add media queries to your styling.
<MaterialReactTablecolumns={columns}data={data}muiTableHeadCellProps={{//easier way to create media queries, no useMediaQuery hook needed.sx: {fontSize: {xs: '10px',sm: '11px',md: '12px',lg: '13px',xl: '14px',},},}}/>
There are many more advantages to using the sx
prop, but that is all we will discuss in these docs. You can learn more about it the official Material UI Docs.
Material UI Theme
Material React Table respects your MUI Theme. If you have already set up Material UI and a global MUI Theme, you should already be set. But if you have not, you should visit the offical Material UI Theming Docs to learn how to set that up.
function App() {//Have you setup your Mui Theme globally in your app root?return (<ThemeProvider theme={createTheme({...})}>...rest of your application</ThemeProvider>);}
Customize Theme Just for your Table
Thanks to Material UI allowing you to nest multiple Theme Providers, you can change your MUI Theme just for the <MaterialReactTable />
component by wrapping a <ThemeProvider theme={createTheme(...)}>
around just your table. The values in this theme will only effect the <MaterialReactTable />
component, and not the rest of your site. It will also inherit values from your global theme, so you don't have to redefine everything again. You can just tweak the values you want to change.
import { createTheme, ThemeProvider } from '@mui/material';//in one of your normal componentsreturn (<ThemeProvider theme={createTheme({...})}><MaterialReactTable columns={columns} data={data} /></ThemeProvider>);
Important Theme Values used by Material React Table
<MaterialReactTable />
will primarily use the following values internally from your MUI Theme by default:
theme.palette.background.default
- used as the background color for most table components by defaulttheme.palette.divider
- used in dividers in menus and for the column resizing handletheme.palette.info.main
- used in the Toolbar Alert Banner and the Toolbar DropZone componenttheme.palette.primary.main
- used as the primary color for anything colorful in the table (input fields, checkboxes, dragging borders, etc.)
Notice that by default, the secondary color isn't used at all. Remember though that you can always override which color a component uses by passing in a mui...Props
prop, like how we changed checkboxes to use the secondary color in the example above.
Custom MUI Theme Example
A common use case for this could be if you want to switch your primary and secondary colors, just for this table. Let's take a look at an example that does that, along with some other styling tweaks, so that we can make an ugly table.
First Name | Last Name | Address | City | State | |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { createTheme, ThemeProvider, useTheme } from '@mui/material';45type Person = {6 firstName: string;7 lastName: string;8 address: string;9 city: string;10 state: string;11};1213//column definitions...3738//data definitions...7778const Example: FC = () => {79 const globalTheme = useTheme(); //(optional) if you already have a theme defined in your app root, you can import here8081 const tableTheme = useMemo(82 () =>83 createTheme({84 palette: {85 mode: globalTheme.palette.mode, //let's use the same dark/light mode as the global theme86 primary: globalTheme.palette.secondary, //swap in the secondary color as the primary for the table87 info: {88 main: 'rgb(255,122,0)', //add in a custom color for the toolbar alert background stuff89 },90 background: {91 default:92 globalTheme.palette.mode === 'light'93 ? 'rgb(254,255,244)' //random light yellow color for the background in light mode94 : '#000', //pure black table in dark mode for fun95 },96 },97 typography: {98 button: {99 textTransform: 'none', //customize typography styles for all buttons in table by default100 fontSize: '1.2rem',101 },102 },103 components: {104 MuiTooltip: {105 styleOverrides: {106 tooltip: {107 fontSize: '1.1rem', //override to make tooltip font size larger108 },109 },110 },111 MuiSwitch: {112 styleOverrides: {113 thumb: {114 color: 'pink', //change the color of the switch thumb in the columns show/hide menu to pink115 },116 },117 },118 },119 }),120 [globalTheme],121 );122123 return (124 <ThemeProvider theme={tableTheme}>125 <MaterialReactTable126 columns={columns}127 data={data}128 enableRowSelection129 enableColumnOrdering130 enablePinning131 />132 </ThemeProvider>133 );134};135136export default Example;137
Customize Table Paper Styling
You can customize both the props and the styles of the internal <Paper />
component that wraps the table by passing in a muiTablePaperProps
prop. This is useful if you want to change the elevation of the paper, or add a border radius, or any other styling you want to do to the paper.
<MaterialReactTablecolumns={columns}data={data}muiTablePaperProps={{elevation: 0, //change the mui box shadow//customize paper stylessx: {borderRadius: '0',border: '1px dashed #e0e0e0',},}}/>
Customize Table Body, Rows, Columns, and Cells
Stripe Rows Example
If you need to style many of the rows or columns in a consistent manner, it usually makes sense to style the <TableBody />
component itself. For example if you want to stripe the rows, you can do something like this:
<MaterialReactTablecolumns={columns}data={data}muiTableBodyProps={{sx: {//stripe the rows, make odd rows a darker color'& tr:nth-of-type(odd)': {backgroundColor: '#f5f5f5',},},}}/>
Stripe Columns Example
Similarly, if you want to stripe the columns, you can do something like this:
<MaterialReactTablecolumns={columns}data={data}muiTableBodyProps={{sx: {//stripe the rows, make odd rows a darker color'& td:nth-of-type(odd)': {backgroundColor: '#f5f5f5',},},}}muiTableBodyCellProps={{sx: {borderRight: '2px solid #e0e0e0', //add a border between columns},}}/>
ID | First Name | Middle Name | Last Name | Address | City | State |
---|---|---|---|---|---|---|
1 | Dylan | Sprouse | Murray | 261 Erdman Ford | East Daphne | Kentucky |
2 | Raquel | Hakeem | Kohler | 769 Dominic Grove | Columbus | Ohio |
3 | Ervin | Kris | Reinger | 566 Brakus Inlet | South Linda | West Virginia |
4 | Brittany | Kathryn | McCullough | 722 Emie Stream | Lincoln | Nebraska |
5 | Branson | John | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { data, Person } from './makeData';4import { darken } from '@mui/material';56const Example: FC = () => {7 const columns = useMemo<MRT_ColumnDef<Person>[]>(8 //column definitions...44 );4546 return (47 <MaterialReactTable48 columns={columns}49 data={data}50 muiTablePaperProps={{51 elevation: 0,52 sx: {53 borderRadius: '0',54 border: '1px dashed #e0e0e0',55 },56 }}57 muiTableBodyProps={{58 sx: (theme) => ({59 '& tr:nth-of-type(odd)': {60 backgroundColor: darken(theme.palette.background.default, 0.1),61 },62 }),63 }}64 />65 );66};6768export default Example;69