Skip to content

Commit b3be685

Browse files
committed
small mrt theme refactor
1 parent 35fe3cf commit b3be685

20 files changed

+86
-104
lines changed

apps/material-react-table-docs/pages/docs/guides/customize-components.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ The default background color of the table toolbars and table are controlled by t
314314
Here is the default `mrtTheme` object used internally if not overridden:
315315

316316
```jsx
317-
const themeOverrides = parseFromValuesOrFunc(table.options.mrtTheme, theme);
318317
const baseBackgroundColor =
319318
themeOverrides?.baseBackgroundColor ??
320319
(theme.palette.mode === 'dark'
@@ -330,7 +329,6 @@ return {
330329
menuBackgroundColor: lighten(baseBackgroundColor, 0.07),
331330
pinnedRowBackgroundColor: alpha(theme.palette.primary.main, 0.1),
332331
selectedRowBackgroundColor: alpha(theme.palette.primary.main, 0.2),
333-
...themeOverrides,
334332
};
335333
```
336334

packages/material-react-table/src/components/body/MRT_TableBodyCell.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../../types';
1919
import { isCellEditable, openEditingCell } from '../../utils/cell.utils';
2020
import { getIsFirstColumn, getIsLastColumn } from '../../utils/column.utils';
21-
import { getCommonMRTCellStyles, getMRTTheme } from '../../utils/style.utils';
21+
import { getCommonMRTCellStyles } from '../../utils/style.utils';
2222
import { parseFromValuesOrFunc } from '../../utils/utils';
2323
import { MRT_CopyButton } from '../buttons/MRT_CopyButton';
2424
import { MRT_EditCellTextField } from '../inputs/MRT_EditCellTextField';
@@ -56,6 +56,7 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
5656
enableColumnPinning,
5757
enableGrouping,
5858
layoutMode,
59+
mrtTheme: { draggingBorderColor },
5960
muiSkeletonProps,
6061
muiTableBodyCellProps,
6162
},
@@ -93,8 +94,6 @@ export const MRT_TableBodyCell = <TData extends MRT_RowData>({
9394
table,
9495
});
9596

96-
const { draggingBorderColor } = getMRTTheme(table, theme);
97-
9897
const [skeletonWidth, setSkeletonWidth] = useState(100);
9998
useEffect(() => {
10099
if ((!isLoading && !showSkeletons) || skeletonWidth !== 100) return;

packages/material-react-table/src/components/body/MRT_TableBodyCellValue.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
type MRT_RowData,
66
type MRT_TableInstance,
77
} from '../../types';
8-
import { getMRTTheme } from '../../utils/style.utils';
98
import highlightWords from 'highlight-words';
109

1110
const allowedTypes = ['string', 'number'];
@@ -27,7 +26,10 @@ export const MRT_TableBodyCellValue = <TData extends MRT_RowData>({
2726
}: MRT_TableBodyCellValueProps<TData>) => {
2827
const {
2928
getState,
30-
options: { enableFilterMatchHighlighting },
29+
options: {
30+
enableFilterMatchHighlighting,
31+
mrtTheme: { matchHighlightColor },
32+
},
3133
} = table;
3234
const { column, row } = cell;
3335
const { columnDef } = column;
@@ -88,8 +90,7 @@ export const MRT_TableBodyCellValue = <TData extends MRT_RowData>({
8890
sx={
8991
match
9092
? {
91-
backgroundColor: (theme) =>
92-
getMRTTheme(table, theme).matchHighlightColor,
93+
backgroundColor: matchHighlightColor,
9394
borderRadius: '2px',
9495
color: (theme) =>
9596
theme.palette.mode === 'dark'

packages/material-react-table/src/components/body/MRT_TableBodyRow.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { getIsRowSelected } from '../../utils/row.utils';
2323
import {
2424
commonCellBeforeAfterStyles,
2525
getCommonPinnedCellStyles,
26-
getMRTTheme,
2726
} from '../../utils/style.utils';
2827
import { parseFromValuesOrFunc } from '../../utils/utils';
2928

@@ -61,6 +60,11 @@ export const MRT_TableBodyRow = <TData extends MRT_RowData>({
6160
enableStickyHeader,
6261
layoutMode,
6362
memoMode,
63+
mrtTheme: {
64+
baseBackgroundColor,
65+
pinnedRowBackgroundColor,
66+
selectedRowBackgroundColor,
67+
},
6468
muiTableBodyRowProps,
6569
renderDetailPanel,
6670
rowPinningDisplayMode,
@@ -138,12 +142,6 @@ export const MRT_TableBodyRow = <TData extends MRT_RowData>({
138142

139143
const rowRef = useRef<HTMLTableRowElement | null>(null);
140144

141-
const {
142-
baseBackgroundColor,
143-
pinnedRowBackgroundColor,
144-
selectedRowBackgroundColor,
145-
} = getMRTTheme(table, theme);
146-
147145
const cellHighlightColor = isRowSelected
148146
? selectedRowBackgroundColor
149147
: isRowPinned

packages/material-react-table/src/components/body/MRT_TableDetailPanel.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
type MRT_TableInstance,
1010
type MRT_VirtualItem,
1111
} from '../../types';
12-
import { getMRTTheme } from '../../utils/style.utils';
1312
import { parseFromValuesOrFunc } from '../../utils/utils';
1413

1514
export interface MRT_TableDetailPanelProps<TData extends MRT_RowData>
@@ -37,6 +36,7 @@ export const MRT_TableDetailPanel = <TData extends MRT_RowData>({
3736
options: {
3837
enableRowVirtualization,
3938
layoutMode,
39+
mrtTheme: { baseBackgroundColor },
4040
muiDetailPanelProps,
4141
muiTableBodyRowProps,
4242
renderDetailPanel,
@@ -89,9 +89,7 @@ export const MRT_TableDetailPanel = <TData extends MRT_RowData>({
8989
colSpan={getVisibleLeafColumns().length}
9090
{...tableCellProps}
9191
sx={(theme) => ({
92-
backgroundColor: virtualRow
93-
? getMRTTheme(table, theme).baseBackgroundColor
94-
: undefined,
92+
backgroundColor: virtualRow ? baseBackgroundColor : undefined,
9593
borderBottom: !row.getIsExpanded() ? 'none' : undefined,
9694
display: layoutMode?.startsWith('grid') ? 'flex' : undefined,
9795
py: !!DetailPanel && row.getIsExpanded() ? '1rem' : 0,

packages/material-react-table/src/components/footer/MRT_TableFooterRow.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
type MRT_TableInstance,
99
type MRT_VirtualItem,
1010
} from '../../types';
11-
import { getMRTTheme } from '../../utils/style.utils';
1211
import { parseFromValuesOrFunc } from '../../utils/utils';
1312

1413
export interface MRT_TableFooterRowProps<TData extends MRT_RowData>
@@ -25,7 +24,11 @@ export const MRT_TableFooterRow = <TData extends MRT_RowData>({
2524
...rest
2625
}: MRT_TableFooterRowProps<TData>) => {
2726
const {
28-
options: { layoutMode, muiTableFooterRowProps },
27+
options: {
28+
layoutMode,
29+
mrtTheme: { baseBackgroundColor },
30+
muiTableFooterRowProps,
31+
},
2932
} = table;
3033

3134
const { virtualColumns, virtualPaddingLeft, virtualPaddingRight } =
@@ -55,7 +58,7 @@ export const MRT_TableFooterRow = <TData extends MRT_RowData>({
5558
<TableRow
5659
{...tableRowProps}
5760
sx={(theme) => ({
58-
backgroundColor: getMRTTheme(table, theme).baseBackgroundColor,
61+
backgroundColor: baseBackgroundColor,
5962
display: layoutMode?.startsWith('grid') ? 'flex' : undefined,
6063
position: 'relative',
6164
width: '100%',

packages/material-react-table/src/components/head/MRT_TableHeadCell.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type MRT_RowData,
1616
type MRT_TableInstance,
1717
} from '../../types';
18-
import { getCommonMRTCellStyles, getMRTTheme } from '../../utils/style.utils';
18+
import { getCommonMRTCellStyles } from '../../utils/style.utils';
1919
import { parseFromValuesOrFunc } from '../../utils/utils';
2020

2121
export interface MRT_TableHeadCellProps<TData extends MRT_RowData>
@@ -47,6 +47,7 @@ export const MRT_TableHeadCell = <TData extends MRT_RowData>({
4747
enableGrouping,
4848
enableMultiSort,
4949
layoutMode,
50+
mrtTheme: { draggingBorderColor },
5051
muiTableHeadCellProps,
5152
},
5253
refs: { tableHeadCellRefs },
@@ -73,8 +74,6 @@ export const MRT_TableHeadCell = <TData extends MRT_RowData>({
7374
...rest,
7475
};
7576

76-
const { draggingBorderColor } = getMRTTheme(table, theme);
77-
7877
const isColumnPinned =
7978
enableColumnPinning &&
8079
columnDef.columnDefType !== 'group' &&

packages/material-react-table/src/components/head/MRT_TableHeadRow.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
type MRT_TableInstance,
1010
type MRT_VirtualItem,
1111
} from '../../types';
12-
import { getMRTTheme } from '../../utils/style.utils';
1312
import { parseFromValuesOrFunc } from '../../utils/utils';
1413

1514
export interface MRT_TableHeadRowProps<TData extends MRT_RowData>
@@ -26,7 +25,12 @@ export const MRT_TableHeadRow = <TData extends MRT_RowData>({
2625
...rest
2726
}: MRT_TableHeadRowProps<TData>) => {
2827
const {
29-
options: { enableStickyHeader, layoutMode, muiTableHeadRowProps },
28+
options: {
29+
enableStickyHeader,
30+
layoutMode,
31+
mrtTheme: { baseBackgroundColor },
32+
muiTableHeadRowProps,
33+
},
3034
} = table;
3135

3236
const { virtualColumns, virtualPaddingLeft, virtualPaddingRight } =
@@ -44,7 +48,7 @@ export const MRT_TableHeadRow = <TData extends MRT_RowData>({
4448
<TableRow
4549
{...tableRowProps}
4650
sx={(theme) => ({
47-
backgroundColor: getMRTTheme(table, theme).baseBackgroundColor,
51+
backgroundColor: baseBackgroundColor,
4852
boxShadow: `4px 0 8px ${alpha(theme.palette.common.black, 0.1)}`,
4953
display: layoutMode?.startsWith('grid') ? 'flex' : undefined,
5054
position:

packages/material-react-table/src/components/menus/MRT_CellActionMenu.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import Menu, { type MenuProps } from '@mui/material/Menu';
2-
import { useTheme } from '@mui/material/styles';
32
import { MRT_ActionMenuItem } from './MRT_ActionMenuItem';
43
import { type MRT_RowData, type MRT_TableInstance } from '../../types';
54
import { openEditingCell } from '../../utils/cell.utils';
6-
import { getMRTTheme } from '../../utils/style.utils';
75
import { parseFromValuesOrFunc } from '../../utils/utils';
86

97
export interface MRT_CellActionMenuProps<TData extends MRT_RowData>
@@ -23,6 +21,7 @@ export const MRT_CellActionMenu = <TData extends MRT_RowData>({
2321
enableEditing,
2422
icons: { ContentCopy, EditIcon },
2523
localization,
24+
mrtTheme: { menuBackgroundColor },
2625
renderCellActionMenuItems,
2726
},
2827
refs: { actionCellRef },
@@ -33,9 +32,6 @@ export const MRT_CellActionMenu = <TData extends MRT_RowData>({
3332
const { column } = cell;
3433
const { columnDef } = column;
3534

36-
const theme = useTheme();
37-
const { menuBackgroundColor } = getMRTTheme(table, theme);
38-
3935
const handleClose = (event?: any) => {
4036
event?.stopPropagation();
4137
table.setActionCell(null);

packages/material-react-table/src/components/menus/MRT_ColumnActionMenu.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { type MouseEvent, useState } from 'react';
22
import Menu, { type MenuProps } from '@mui/material/Menu';
3-
import { useTheme } from '@mui/material/styles';
43
import { MRT_ActionMenuItem } from './MRT_ActionMenuItem';
54
import { MRT_FilterOptionMenu } from './MRT_FilterOptionMenu';
65
import {
76
type MRT_Header,
87
type MRT_RowData,
98
type MRT_TableInstance,
109
} from '../../types';
11-
import { getMRTTheme } from '../../utils/style.utils';
1210

1311
export interface MRT_ColumnActionMenuProps<TData extends MRT_RowData>
1412
extends Partial<MenuProps> {
@@ -51,6 +49,7 @@ export const MRT_ColumnActionMenu = <TData extends MRT_RowData>({
5149
VisibilityOffIcon,
5250
},
5351
localization,
52+
mrtTheme: { menuBackgroundColor },
5453
renderColumnActionsMenuItems,
5554
},
5655
refs: { filterInputRefs },
@@ -318,9 +317,6 @@ export const MRT_ColumnActionMenu = <TData extends MRT_RowData>({
318317
: []),
319318
].filter(Boolean);
320319

321-
const theme = useTheme();
322-
const { menuBackgroundColor } = getMRTTheme(table, theme);
323-
324320
return (
325321
<Menu
326322
MenuListProps={{

0 commit comments

Comments
 (0)