Customize Component Styles Guide
There are a few ways in which you can style the internal components of Mantine React Table. Some ways are much easier and faster than others. This guide will cover the new MRT CSS variable system, how you customize your Mantine Theme, how you can write your own CSS with CSS modules, or how to pass in custom styles to the components.
If all you care about is changing the background color of the table, you should try to use the MRT CSS variables first. They are the easiest and fastest way to change the background color of the table.
MRT CSS Variables
Before you try to write a bunch of overriding CSS (with className
or style
props), you should try to customize the MRT CSS variables first, especially if all you want to do is change the background color of some parts of the table.
MRT CSS Variables List
Here is a list of all of the MRT CSS variables that are set in the :root
of your document once you import the MRT styles.css
file from 'mantine-react-table'
:
--mrt-base-background-color
: default background color for everything--mrt-striped-row-background-color
: background color for striped rows--mrt-row-hover-background-color
: background color for row hover--mrt-striped-row-hover-background-color
: background color for striped row hover--mrt-selected-row-background-color
: background color for selected row--mrt-selected-row-hover-background-color
: background color for selected row hover--mrt-pinned-row-background-color
: background color for pinned row--mrt-pinned-row-hover-background-color
: background color for pinned row hover--mrt-pinned-column-background-color
: background color for pinned column--mrt-dragging-hovered-border-color
: border color for dragging hovered cell--mrt-dragging-drag-border-color
: border color for dragging cell--mrt-resize-column-border-color
: border color for column resize
These CSS variables are automatically calculated with intelligent defaults based on light and dark mode.
How to set MRT CSS Variables
You can override these CSS variables on a per table basis, or globally in your app from a higher up root element. MRT already defines these CSS variables in the :root
of the document, so if you try to override them there in your own CSS, you may have some issues. Instead, you can either just override them in the style
surrounding the MRT table components, or by defining them in a class and passing the classes to the className
of the most "root" MRT component that you are using. Usually, this is the mantinePaperProps
table option.
If you do start changing some CSS variables like the --mrt-base-background-color
, make sure to also redefine the other CSS variables that derive from it, like --mrt-striped-row-background-color
, --mrt-row-hover-background-color
, etc. Otherwise, those CSS variables will still be calculated based on the default --mrt-base-background-color
. Row hovers will have gray backgrounds instead of lighted or darkened versions of your new --mrt-base-background-color
.
1. Define MRT CSS Variables in a CSS Class (Recommended)
You can copy the MRT CSS variable definitions from the MRT Source Code
2. Define MRT CSS Variables in Style Props
Pin | 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 |
Mantine Theme
Mantine React Table respects your Mantine Theme. If you have already set up Mantine and a global Mantine Theme, you should already be set. But if you have not, you should visit the official Mantine Theming Docs to learn how to set that up.
Customize Theme Just for your Table
Thanks to Mantine allowing you to nest multiple Theme Providers, you can change your Mantine Theme just for the <MantineReactTable />
component by wrapping a <MantineProvider theme={{...}}>
around just your table. The values in this theme will only effect the <MantineReactTable />
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.
CSS Modules with MRT
Anytime that you want to style the components inside of Mantine React Table, you can just do it with CSS. It's (usually) just that easy.
All internal components of any significance have specific mrt-
classNames or mantine-
classNames that you can target from your CSS or CSS modules. If you are using CSS modules and you are trying to target the pre-defined MRT classNames, you will need to use the :global()
selector to target the internal MRT classNames so that the classNames do not get hashed.
Note: If overriding background colors, try to customize the MRT CSS variables first. There is somewhat complicated logic that goes into calculating the background colors of the table components that solve edge cases with multiple features enabled. It's best to let MRT handle that for you.
Target Conditional data- attributes
MRT makes use of a lot of data-
attributes to conditionally style the table components. You can target these data-
attributes in your CSS or CSS modules to style the components based on their state.
Here is a list of just some of the data-
attributes that MRT uses:
data-column-pinned
: position of the pinned column if it is pinneddata-dragging-column
: true if the column is being draggeddata-dragging-row
: true if the row is being draggeddata-first-right-pinned
: true if the column is the first right pinned columndata-hovered-column-target
: true if the column is being hovereddata-hovered-row-target
: true if the row is being hovereddata-index
: the index of the row or columndata-last-left-pinned
: true if the column is the last left pinned columndata-last-row
: true if the row is the last rowdata-resizing
: true if the column is being resizeddata-row-pinned
: position of the pinned row if it is pinneddata-selected
: true if the row is selecteddata-striped
: true if the row is striped
This list is subject to change as MRT evolves, but you can always inspect the table components in your browser to see what data-
attributes are being used.
Pass in Custom Styles
For quick and "dirty" styling, you can always just use the style
prop on any of the mantine*Props
options. It depends on your codebase and your team's preferences whether or not you want to use this method. Technically, Inline Styles are less performant than CSS or CSS modules, as discussed in the official Mantine Docs. But for some quick styling, especially conditional styling, it can be very useful.