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 Material UI Theme to customize colors, typography, or any other default CSS that is used by Material React Table.
Relevant Table Options
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 Box Props | |||
2 |
| Material UI IconButton Props | |||
3 |
| Material UI IconButton Props | |||
4 |
| Material UI Button Props | |||
5 |
| Material UI TableCell Props | |||
6 |
| Material UI Dialog Props | |||
7 |
| Material UI TextField Props | |||
8 |
| Material UI IconButton Props | |||
9 |
| Material UI IconButton Props | |||
10 |
| Material UI Autocomplete Props | |||
11 |
| Material UI Checkbox Props | |||
12 |
| MUI X DatePicker Props | |||
13 |
| Material UI Slider Props | |||
14 |
| Material UI TextField Props | |||
15 |
| Material UI LinearProgress Props | |||
16 |
| Material UI TablePagination Props | |||
17 |
| Material UI IconButton Props | |||
18 |
| Material UI TextField Props | |||
19 |
| Material UI Checkbox Props | |||
20 |
| Material UI Checkbox Props | |||
21 |
| Material UI Skeleton Props | |||
22 |
| Material UI TableCell Props | |||
23 |
| Material UI TableBody Props | |||
24 |
|
| Material UI TableRow Props | ||
25 |
| Material UI TableContainer Props | |||
26 |
| Material UI TableCell Props | |||
27 |
| Material UI TableFooter Props | |||
28 |
| Material UI TableRow Props | |||
29 |
| Material UI TableCell Props | |||
30 |
| Material UI TableHead Props | |||
31 |
| Material UI TableRow Props | |||
32 |
| Material UI Paper API Docs | |||
33 |
| Material UI TableProps API Docs | |||
34 |
| Material UI Chip Props | |||
35 |
| Material UI Alert Props | |||
36 |
| Material UI Box 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 IconButton API | |||
2 |
| Material UI Button API | |||
3 |
| Material UI TextField API | |||
4 |
| MUI X DatePicker Props | |||
5 |
| Material UI Checkbox Props | |||
6 |
| MUI X DatePicker Props | |||
7 |
| Material UI Slider Props | |||
8 |
| Material UI TextField Props | |||
9 |
| Material UI TableCell API | |||
10 |
| Material UI TableCell API | |||
11 |
| 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
const table = useMaterialReactTable({columns,data,enableRowSelection: true,//passing the static object variant if no dynamic logic is neededmuiSelectCheckboxProps: {color: 'secondary', //makes all checkboxes use the secondary color},});return <MaterialReactTable table={table} />;
Callback Functions to Dynamically Set Prop Values
const table = useMaterialReactTable({columns,data,enableRowSelection: true,//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}),});return <MaterialReactTable table={table} />;
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
const table = useMaterialReactTable({columns,data,muiTableHeadCellProps: {//simple styling with the `sx` prop, works just like a style prop in this examplesx: {fontWeight: 'normal',fontSize: '14px',},},});return <MaterialReactTable table={table} />;
The
sx
prop gets easy access to your muiTheme without you having to call the theme from auseTheme
hook.
const table = useMaterialReactTable({columns,data,muiTableHeadCellProps: {//no useTheme hook needed, just use the `sx` prop with the theme callbacksx: (theme) => ({color: theme.palette.text.secondary,}),},});return <MaterialReactTable table={table} />;
The
sx
prop gives you a much more concise way to add media queries to your styling.
const table = useMaterialReactTable({columns,data,muiTableHeadCellProps: {//easier way to create media queries, no useMediaQuery hook needed.sx: {fontSize: {xs: '10px',sm: '11px',md: '12px',lg: '13px',xl: '14px',},},},});return <MaterialReactTable table={table} />;
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 Material UI Theme. If you have already set up Material UI and a global Material UI 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 Material UI 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 do not 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>);
Custom Material UI Theme Example
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 |
1-5 of 5
1import { useMemo } from 'react';2import { MaterialReactTable, type 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 = () => {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 enableColumnPinning131 />132 </ThemeProvider>133 );134};135136export default Example;137
MRT Theme Values
New in V2
Material React Table has a new mrtTheme
table option where you can change a few of the default color values used by Material React Table.
The default background color of the table toolbars and table are controlled by the mrtTheme.baseBackgroundColor
value. Dragging borders, selected or pinned rows, and filter match highlighting are also configurable in the mrtTheme
object.
Here is the default mrtTheme
object used internally if not overridden:
{baseBackgroundColor:theme.palette.mode === 'dark'? lighten(theme.palette.background.default, 0.05): theme.palette.background.default,draggingBorderColor: theme.palette.primary.main,matchHighlightColor:theme.palette.mode === 'dark'? darken(theme.palette.warning.dark, 0.25): lighten(theme.palette.warning.light, 0.5),pinnedRowBackgroundColor: alpha(theme.palette.primary.main, 0.1),selectedRowBackgroundColor: alpha(theme.palette.primary.main, 0.2),...table.options.mrtTheme,};
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.
const table = useMaterialReactTable({columns,data,muiTablePaperProps: {elevation: 0, //change the mui box shadow//customize paper stylessx: {borderRadius: '0',border: '1px dashed #e0e0e0',},},});return <MaterialReactTable table={table} />;
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:
const table = useMaterialReactTable({columns,data,muiTableBodyProps: {sx: {//stripe the rows, make odd rows a darker color'& tr:nth-of-type(odd) > td': {backgroundColor: '#f5f5f5',},},},});return <MaterialReactTable table={table} />;
Stripe Columns Example
Similarly, if you want to stripe the columns, you can do something like this:
const table = useMaterialReactTable({columns,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},},});
Pin | 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 |
1-5 of 5
1import { useMemo } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';8import { darken, lighten, useTheme } from '@mui/material';910const Example = () => {11 const theme = useTheme();1213 //light or dark green14 const baseBackgroundColor =15 theme.palette.mode === 'dark'16 ? 'rgba(3, 44, 43, 1)'17 : 'rgba(244, 255, 233, 1)';1819 const columns = useMemo<MRT_ColumnDef<Person>[]>(20 //column definitions...57 );5859 const table = useMaterialReactTable({60 columns,61 data,62 enableColumnResizing: true,63 enableRowPinning: true,64 enableRowSelection: true,65 muiTablePaperProps: {66 elevation: 0,67 sx: {68 borderRadius: '0',69 },70 },71 muiTableBodyProps: {72 sx: (theme) => ({73 '& tr:nth-of-type(odd):not([data-selected="true"]):not([data-pinned="true"]) > td':74 {75 backgroundColor: darken(baseBackgroundColor, 0.1),76 },77 '& tr:nth-of-type(odd):not([data-selected="true"]):not([data-pinned="true"]):hover > td':78 {79 backgroundColor: darken(baseBackgroundColor, 0.2),80 },81 '& tr:nth-of-type(even):not([data-selected="true"]):not([data-pinned="true"]) > td':82 {83 backgroundColor: lighten(baseBackgroundColor, 0.1),84 },85 '& tr:nth-of-type(even):not([data-selected="true"]):not([data-pinned="true"]):hover > td':86 {87 backgroundColor: darken(baseBackgroundColor, 0.2),88 },89 }),90 },91 mrtTheme: (theme) => ({92 baseBackgroundColor: baseBackgroundColor,93 draggingBorderColor: theme.palette.secondary.main,94 }),95 });9697 return <MaterialReactTable table={table} />;98};99100export default Example;101